cluster_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdserver
  14. import (
  15. "path"
  16. "reflect"
  17. "testing"
  18. "github.com/coreos/etcd/store"
  19. )
  20. func TestClusterFromString(t *testing.T) {
  21. tests := []struct {
  22. f string
  23. mems []Member
  24. }{
  25. {
  26. "mem1=http://10.0.0.1:2379,mem1=http://128.193.4.20:2379,mem2=http://10.0.0.2:2379,default=http://127.0.0.1:2379",
  27. []Member{
  28. newTestMember(4322322643958477905, []string{"http://10.0.0.1:2379", "http://128.193.4.20:2379"}, "mem1", nil),
  29. newTestMember(3141198903430435750, []string{"http://10.0.0.2:2379"}, "mem2", nil),
  30. newTestMember(12762790032478827328, []string{"http://127.0.0.1:2379"}, "default", nil),
  31. },
  32. },
  33. }
  34. for i, tt := range tests {
  35. c, err := NewClusterFromString("abc", tt.f)
  36. if err != nil {
  37. t.Fatalf("#%d: unexpected new error: %v", i, err)
  38. }
  39. if c.name != "abc" {
  40. t.Errorf("#%d: name = %v, want abc", i, c.name)
  41. }
  42. wc := newTestCluster(tt.mems)
  43. if !reflect.DeepEqual(c.members, wc.members) {
  44. t.Errorf("#%d: members = %+v, want %+v", i, c.members, wc.members)
  45. }
  46. }
  47. }
  48. func TestClusterFromStringBad(t *testing.T) {
  49. tests := []string{
  50. // invalid URL
  51. "%^",
  52. // no URL defined for member
  53. "mem1=,mem2=http://128.193.4.20:2379,mem3=http://10.0.0.2:2379",
  54. "mem1,mem2=http://128.193.4.20:2379,mem3=http://10.0.0.2:2379",
  55. // TODO(philips): anyone know of a 64 bit sha1 hash collision
  56. // "06b2f82fd81b2c20=http://128.193.4.20:2379,02c60cb75083ceef=http://128.193.4.20:2379",
  57. // the same url for two members
  58. "mem1=http://128.193.4.20:2379,mem2=http://128.193.4.20:2379",
  59. }
  60. for i, tt := range tests {
  61. if _, err := NewClusterFromString("abc", tt); err == nil {
  62. t.Errorf("#%d: unexpected successful new, want err", i)
  63. }
  64. }
  65. }
  66. func TestClusterFromStore(t *testing.T) {
  67. tests := []struct {
  68. mems []Member
  69. }{
  70. {
  71. []Member{newTestMember(1, nil, "node1", nil)},
  72. },
  73. {
  74. []Member{},
  75. },
  76. {
  77. []Member{
  78. newTestMember(1, nil, "node1", nil),
  79. newTestMember(2, nil, "node2", nil),
  80. },
  81. },
  82. }
  83. for i, tt := range tests {
  84. st := store.New()
  85. hc := newTestCluster(nil)
  86. hc.SetStore(st)
  87. for _, m := range tt.mems {
  88. hc.AddMember(&m)
  89. }
  90. c := NewClusterFromStore("abc", st)
  91. if c.name != "abc" {
  92. t.Errorf("#%d: name = %v, want %v", i, c.name, "abc")
  93. }
  94. wc := newTestCluster(tt.mems)
  95. if !reflect.DeepEqual(c.members, wc.members) {
  96. t.Errorf("#%d: members = %v, want %v", i, c.members, wc.members)
  97. }
  98. }
  99. }
  100. func TestClusterMember(t *testing.T) {
  101. membs := []Member{
  102. newTestMember(1, nil, "node1", nil),
  103. newTestMember(2, nil, "node2", nil),
  104. }
  105. tests := []struct {
  106. id uint64
  107. match bool
  108. }{
  109. {1, true},
  110. {2, true},
  111. {3, false},
  112. }
  113. for i, tt := range tests {
  114. c := newTestCluster(membs)
  115. m := c.Member(tt.id)
  116. if g := m != nil; g != tt.match {
  117. t.Errorf("#%d: find member = %v, want %v", i, g, tt.match)
  118. }
  119. if m != nil && m.ID != tt.id {
  120. t.Errorf("#%d: id = %x, want %x", i, m.ID, tt.id)
  121. }
  122. }
  123. }
  124. func TestClusterMemberByName(t *testing.T) {
  125. membs := []Member{
  126. newTestMember(1, nil, "node1", nil),
  127. newTestMember(2, nil, "node2", nil),
  128. }
  129. tests := []struct {
  130. name string
  131. match bool
  132. }{
  133. {"node1", true},
  134. {"node2", true},
  135. {"node3", false},
  136. }
  137. for i, tt := range tests {
  138. c := newTestCluster(membs)
  139. m := c.MemberByName(tt.name)
  140. if g := m != nil; g != tt.match {
  141. t.Errorf("#%d: find member = %v, want %v", i, g, tt.match)
  142. }
  143. if m != nil && m.Name != tt.name {
  144. t.Errorf("#%d: name = %v, want %v", i, m.Name, tt.name)
  145. }
  146. }
  147. }
  148. func TestClusterMemberIDs(t *testing.T) {
  149. c := newTestCluster([]Member{
  150. newTestMember(1, nil, "", nil),
  151. newTestMember(4, nil, "", nil),
  152. newTestMember(100, nil, "", nil),
  153. })
  154. w := []uint64{1, 4, 100}
  155. g := c.MemberIDs()
  156. if !reflect.DeepEqual(w, g) {
  157. t.Errorf("IDs = %+v, want %+v", g, w)
  158. }
  159. }
  160. func TestClusterPeerURLs(t *testing.T) {
  161. tests := []struct {
  162. mems []Member
  163. wurls []string
  164. }{
  165. // single peer with a single address
  166. {
  167. mems: []Member{
  168. newTestMember(1, []string{"http://192.0.2.1"}, "", nil),
  169. },
  170. wurls: []string{"http://192.0.2.1"},
  171. },
  172. // single peer with a single address with a port
  173. {
  174. mems: []Member{
  175. newTestMember(1, []string{"http://192.0.2.1:8001"}, "", nil),
  176. },
  177. wurls: []string{"http://192.0.2.1:8001"},
  178. },
  179. // several members explicitly unsorted
  180. {
  181. mems: []Member{
  182. newTestMember(2, []string{"http://192.0.2.3", "http://192.0.2.4"}, "", nil),
  183. newTestMember(3, []string{"http://192.0.2.5", "http://192.0.2.6"}, "", nil),
  184. newTestMember(1, []string{"http://192.0.2.1", "http://192.0.2.2"}, "", nil),
  185. },
  186. wurls: []string{"http://192.0.2.1", "http://192.0.2.2", "http://192.0.2.3", "http://192.0.2.4", "http://192.0.2.5", "http://192.0.2.6"},
  187. },
  188. // no members
  189. {
  190. mems: []Member{},
  191. wurls: []string{},
  192. },
  193. // peer with no peer urls
  194. {
  195. mems: []Member{
  196. newTestMember(3, []string{}, "", nil),
  197. },
  198. wurls: []string{},
  199. },
  200. }
  201. for i, tt := range tests {
  202. c := newTestCluster(tt.mems)
  203. urls := c.PeerURLs()
  204. if !reflect.DeepEqual(urls, tt.wurls) {
  205. t.Errorf("#%d: PeerURLs = %v, want %v", i, urls, tt.wurls)
  206. }
  207. }
  208. }
  209. func TestClusterClientURLs(t *testing.T) {
  210. tests := []struct {
  211. mems []Member
  212. wurls []string
  213. }{
  214. // single peer with a single address
  215. {
  216. mems: []Member{
  217. newTestMember(1, nil, "", []string{"http://192.0.2.1"}),
  218. },
  219. wurls: []string{"http://192.0.2.1"},
  220. },
  221. // single peer with a single address with a port
  222. {
  223. mems: []Member{
  224. newTestMember(1, nil, "", []string{"http://192.0.2.1:8001"}),
  225. },
  226. wurls: []string{"http://192.0.2.1:8001"},
  227. },
  228. // several members explicitly unsorted
  229. {
  230. mems: []Member{
  231. newTestMember(2, nil, "", []string{"http://192.0.2.3", "http://192.0.2.4"}),
  232. newTestMember(3, nil, "", []string{"http://192.0.2.5", "http://192.0.2.6"}),
  233. newTestMember(1, nil, "", []string{"http://192.0.2.1", "http://192.0.2.2"}),
  234. },
  235. wurls: []string{"http://192.0.2.1", "http://192.0.2.2", "http://192.0.2.3", "http://192.0.2.4", "http://192.0.2.5", "http://192.0.2.6"},
  236. },
  237. // no members
  238. {
  239. mems: []Member{},
  240. wurls: []string{},
  241. },
  242. // peer with no client urls
  243. {
  244. mems: []Member{
  245. newTestMember(3, nil, "", []string{}),
  246. },
  247. wurls: []string{},
  248. },
  249. }
  250. for i, tt := range tests {
  251. c := newTestCluster(tt.mems)
  252. urls := c.ClientURLs()
  253. if !reflect.DeepEqual(urls, tt.wurls) {
  254. t.Errorf("#%d: ClientURLs = %v, want %v", i, urls, tt.wurls)
  255. }
  256. }
  257. }
  258. func TestClusterGenID(t *testing.T) {
  259. cs := newTestCluster([]Member{
  260. newTestMember(1, nil, "", nil),
  261. newTestMember(2, nil, "", nil),
  262. })
  263. cs.genID()
  264. if cs.ID() == 0 {
  265. t.Fatalf("cluster.ID = %v, want not 0", cs.ID())
  266. }
  267. previd := cs.ID()
  268. cs.SetStore(&storeRecorder{})
  269. cs.AddMember(newTestMemberp(3, nil, "", nil))
  270. cs.genID()
  271. if cs.ID() == previd {
  272. t.Fatalf("cluster.ID = %v, want not %v", cs.ID(), previd)
  273. }
  274. }
  275. func TestNodeToMemberBad(t *testing.T) {
  276. tests := []*store.NodeExtern{
  277. {Key: "/1234", Nodes: []*store.NodeExtern{
  278. {Key: "/1234/strange"},
  279. }},
  280. {Key: "/1234", Nodes: []*store.NodeExtern{
  281. {Key: "/1234/dynamic", Value: stringp("garbage")},
  282. }},
  283. {Key: "/1234", Nodes: []*store.NodeExtern{
  284. {Key: "/1234/dynamic", Value: stringp(`{"PeerURLs":null}`)},
  285. }},
  286. {Key: "/1234", Nodes: []*store.NodeExtern{
  287. {Key: "/1234/dynamic", Value: stringp(`{"PeerURLs":null}`)},
  288. {Key: "/1234/strange"},
  289. }},
  290. {Key: "/1234", Nodes: []*store.NodeExtern{
  291. {Key: "/1234/dynamic", Value: stringp(`{"PeerURLs":null}`)},
  292. {Key: "/1234/static", Value: stringp("garbage")},
  293. }},
  294. {Key: "/1234", Nodes: []*store.NodeExtern{
  295. {Key: "/1234/dynamic", Value: stringp(`{"PeerURLs":null}`)},
  296. {Key: "/1234/static", Value: stringp(`{"Name":"node1","ClientURLs":null}`)},
  297. {Key: "/1234/strange"},
  298. }},
  299. }
  300. for i, tt := range tests {
  301. if _, err := nodeToMember(tt); err == nil {
  302. t.Errorf("#%d: unexpected nil error", i)
  303. }
  304. }
  305. }
  306. func TestClusterAddMember(t *testing.T) {
  307. st := &storeRecorder{}
  308. c := newTestCluster(nil)
  309. c.SetStore(st)
  310. c.AddMember(newTestMemberp(1, nil, "node1", nil))
  311. wactions := []action{
  312. {
  313. name: "Create",
  314. params: []interface{}{
  315. path.Join(storeMembersPrefix, "1", "raftAttributes"),
  316. false,
  317. `{"PeerURLs":null}`,
  318. false,
  319. store.Permanent,
  320. },
  321. },
  322. {
  323. name: "Create",
  324. params: []interface{}{
  325. path.Join(storeMembersPrefix, "1", "attributes"),
  326. false,
  327. `{"Name":"node1"}`,
  328. false,
  329. store.Permanent,
  330. },
  331. },
  332. }
  333. if g := st.Action(); !reflect.DeepEqual(g, wactions) {
  334. t.Errorf("actions = %v, want %v", g, wactions)
  335. }
  336. }
  337. func TestClusterMembers(t *testing.T) {
  338. cls := &Cluster{
  339. members: map[uint64]*Member{
  340. 1: &Member{ID: 1},
  341. 20: &Member{ID: 20},
  342. 100: &Member{ID: 100},
  343. 5: &Member{ID: 5},
  344. 50: &Member{ID: 50},
  345. },
  346. }
  347. w := []*Member{
  348. &Member{ID: 1},
  349. &Member{ID: 5},
  350. &Member{ID: 20},
  351. &Member{ID: 50},
  352. &Member{ID: 100},
  353. }
  354. if g := cls.Members(); !reflect.DeepEqual(g, w) {
  355. t.Fatalf("Members()=%#v, want %#v", g, w)
  356. }
  357. }
  358. func TestClusterRemoveMember(t *testing.T) {
  359. st := &storeRecorder{}
  360. c := newTestCluster(nil)
  361. c.SetStore(st)
  362. c.RemoveMember(1)
  363. wactions := []action{
  364. {name: "Delete", params: []interface{}{memberStoreKey(1), true, true}},
  365. {name: "Create", params: []interface{}{removedMemberStoreKey(1), false, "", false, store.Permanent}},
  366. }
  367. if !reflect.DeepEqual(st.Action(), wactions) {
  368. t.Errorf("actions = %v, want %v", st.Action(), wactions)
  369. }
  370. }
  371. func TestNodeToMember(t *testing.T) {
  372. n := &store.NodeExtern{Key: "/1234", Nodes: []*store.NodeExtern{
  373. {Key: "/1234/attributes", Value: stringp(`{"Name":"node1","ClientURLs":null}`)},
  374. {Key: "/1234/raftAttributes", Value: stringp(`{"PeerURLs":null}`)},
  375. }}
  376. wm := &Member{ID: 0x1234, RaftAttributes: RaftAttributes{}, Attributes: Attributes{Name: "node1"}}
  377. m, err := nodeToMember(n)
  378. if err != nil {
  379. t.Fatalf("unexpected nodeToMember error: %v", err)
  380. }
  381. if !reflect.DeepEqual(m, wm) {
  382. t.Errorf("member = %+v, want %+v", m, wm)
  383. }
  384. }
  385. func newTestCluster(membs []Member) *Cluster {
  386. c := &Cluster{members: make(map[uint64]*Member), removed: make(map[uint64]bool)}
  387. for i, m := range membs {
  388. c.members[m.ID] = &membs[i]
  389. }
  390. return c
  391. }
  392. func newTestMember(id uint64, peerURLs []string, name string, clientURLs []string) Member {
  393. return Member{
  394. ID: id,
  395. RaftAttributes: RaftAttributes{PeerURLs: peerURLs},
  396. Attributes: Attributes{Name: name, ClientURLs: clientURLs},
  397. }
  398. }
  399. func newTestMemberp(id uint64, peerURLs []string, name string, clientURLs []string) *Member {
  400. m := newTestMember(id, peerURLs, name, clientURLs)
  401. return &m
  402. }