cluster_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. // bad URL for member
  56. "default=http://localhost/",
  57. // TODO(philips): anyone know of a 64 bit sha1 hash collision
  58. // "06b2f82fd81b2c20=http://128.193.4.20:2379,02c60cb75083ceef=http://128.193.4.20:2379",
  59. // the same url for two members
  60. "mem1=http://128.193.4.20:2379,mem2=http://128.193.4.20:2379",
  61. }
  62. for i, tt := range tests {
  63. if _, err := NewClusterFromString("abc", tt); err == nil {
  64. t.Errorf("#%d: unexpected successful new, want err", i)
  65. }
  66. }
  67. }
  68. func TestClusterFromStore(t *testing.T) {
  69. tests := []struct {
  70. mems []Member
  71. }{
  72. {
  73. []Member{newTestMember(1, nil, "node1", nil)},
  74. },
  75. {
  76. []Member{},
  77. },
  78. {
  79. []Member{
  80. newTestMember(1, nil, "node1", nil),
  81. newTestMember(2, nil, "node2", nil),
  82. },
  83. },
  84. }
  85. for i, tt := range tests {
  86. st := store.New()
  87. hc := newTestCluster(nil)
  88. hc.SetStore(st)
  89. for _, m := range tt.mems {
  90. hc.AddMember(&m)
  91. }
  92. c := NewClusterFromStore("abc", st)
  93. if c.name != "abc" {
  94. t.Errorf("#%d: name = %v, want %v", i, c.name, "abc")
  95. }
  96. wc := newTestCluster(tt.mems)
  97. if !reflect.DeepEqual(c.members, wc.members) {
  98. t.Errorf("#%d: members = %v, want %v", i, c.members, wc.members)
  99. }
  100. }
  101. }
  102. func TestClusterMember(t *testing.T) {
  103. membs := []Member{
  104. newTestMember(1, nil, "node1", nil),
  105. newTestMember(2, nil, "node2", nil),
  106. }
  107. tests := []struct {
  108. id uint64
  109. match bool
  110. }{
  111. {1, true},
  112. {2, true},
  113. {3, false},
  114. }
  115. for i, tt := range tests {
  116. c := newTestCluster(membs)
  117. m := c.Member(tt.id)
  118. if g := m != nil; g != tt.match {
  119. t.Errorf("#%d: find member = %v, want %v", i, g, tt.match)
  120. }
  121. if m != nil && m.ID != tt.id {
  122. t.Errorf("#%d: id = %x, want %x", i, m.ID, tt.id)
  123. }
  124. }
  125. }
  126. func TestClusterMemberByName(t *testing.T) {
  127. membs := []Member{
  128. newTestMember(1, nil, "node1", nil),
  129. newTestMember(2, nil, "node2", nil),
  130. }
  131. tests := []struct {
  132. name string
  133. match bool
  134. }{
  135. {"node1", true},
  136. {"node2", true},
  137. {"node3", false},
  138. }
  139. for i, tt := range tests {
  140. c := newTestCluster(membs)
  141. m := c.MemberByName(tt.name)
  142. if g := m != nil; g != tt.match {
  143. t.Errorf("#%d: find member = %v, want %v", i, g, tt.match)
  144. }
  145. if m != nil && m.Name != tt.name {
  146. t.Errorf("#%d: name = %v, want %v", i, m.Name, tt.name)
  147. }
  148. }
  149. }
  150. func TestClusterMemberIDs(t *testing.T) {
  151. c := newTestCluster([]Member{
  152. newTestMember(1, nil, "", nil),
  153. newTestMember(4, nil, "", nil),
  154. newTestMember(100, nil, "", nil),
  155. })
  156. w := []uint64{1, 4, 100}
  157. g := c.MemberIDs()
  158. if !reflect.DeepEqual(w, g) {
  159. t.Errorf("IDs = %+v, want %+v", g, w)
  160. }
  161. }
  162. func TestClusterPeerURLs(t *testing.T) {
  163. tests := []struct {
  164. mems []Member
  165. wurls []string
  166. }{
  167. // single peer with a single address
  168. {
  169. mems: []Member{
  170. newTestMember(1, []string{"http://192.0.2.1"}, "", nil),
  171. },
  172. wurls: []string{"http://192.0.2.1"},
  173. },
  174. // single peer with a single address with a port
  175. {
  176. mems: []Member{
  177. newTestMember(1, []string{"http://192.0.2.1:8001"}, "", nil),
  178. },
  179. wurls: []string{"http://192.0.2.1:8001"},
  180. },
  181. // several members explicitly unsorted
  182. {
  183. mems: []Member{
  184. newTestMember(2, []string{"http://192.0.2.3", "http://192.0.2.4"}, "", nil),
  185. newTestMember(3, []string{"http://192.0.2.5", "http://192.0.2.6"}, "", nil),
  186. newTestMember(1, []string{"http://192.0.2.1", "http://192.0.2.2"}, "", nil),
  187. },
  188. 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"},
  189. },
  190. // no members
  191. {
  192. mems: []Member{},
  193. wurls: []string{},
  194. },
  195. // peer with no peer urls
  196. {
  197. mems: []Member{
  198. newTestMember(3, []string{}, "", nil),
  199. },
  200. wurls: []string{},
  201. },
  202. }
  203. for i, tt := range tests {
  204. c := newTestCluster(tt.mems)
  205. urls := c.PeerURLs()
  206. if !reflect.DeepEqual(urls, tt.wurls) {
  207. t.Errorf("#%d: PeerURLs = %v, want %v", i, urls, tt.wurls)
  208. }
  209. }
  210. }
  211. func TestClusterClientURLs(t *testing.T) {
  212. tests := []struct {
  213. mems []Member
  214. wurls []string
  215. }{
  216. // single peer with a single address
  217. {
  218. mems: []Member{
  219. newTestMember(1, nil, "", []string{"http://192.0.2.1"}),
  220. },
  221. wurls: []string{"http://192.0.2.1"},
  222. },
  223. // single peer with a single address with a port
  224. {
  225. mems: []Member{
  226. newTestMember(1, nil, "", []string{"http://192.0.2.1:8001"}),
  227. },
  228. wurls: []string{"http://192.0.2.1:8001"},
  229. },
  230. // several members explicitly unsorted
  231. {
  232. mems: []Member{
  233. newTestMember(2, nil, "", []string{"http://192.0.2.3", "http://192.0.2.4"}),
  234. newTestMember(3, nil, "", []string{"http://192.0.2.5", "http://192.0.2.6"}),
  235. newTestMember(1, nil, "", []string{"http://192.0.2.1", "http://192.0.2.2"}),
  236. },
  237. 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"},
  238. },
  239. // no members
  240. {
  241. mems: []Member{},
  242. wurls: []string{},
  243. },
  244. // peer with no client urls
  245. {
  246. mems: []Member{
  247. newTestMember(3, nil, "", []string{}),
  248. },
  249. wurls: []string{},
  250. },
  251. }
  252. for i, tt := range tests {
  253. c := newTestCluster(tt.mems)
  254. urls := c.ClientURLs()
  255. if !reflect.DeepEqual(urls, tt.wurls) {
  256. t.Errorf("#%d: ClientURLs = %v, want %v", i, urls, tt.wurls)
  257. }
  258. }
  259. }
  260. func TestClusterGenID(t *testing.T) {
  261. cs := newTestCluster([]Member{
  262. newTestMember(1, nil, "", nil),
  263. newTestMember(2, nil, "", nil),
  264. })
  265. cs.genID()
  266. if cs.ID() == 0 {
  267. t.Fatalf("cluster.ID = %v, want not 0", cs.ID())
  268. }
  269. previd := cs.ID()
  270. cs.SetStore(&storeRecorder{})
  271. cs.AddMember(newTestMemberp(3, nil, "", nil))
  272. cs.genID()
  273. if cs.ID() == previd {
  274. t.Fatalf("cluster.ID = %v, want not %v", cs.ID(), previd)
  275. }
  276. }
  277. func TestNodeToMemberBad(t *testing.T) {
  278. tests := []*store.NodeExtern{
  279. {Key: "/1234", Nodes: []*store.NodeExtern{
  280. {Key: "/1234/strange"},
  281. }},
  282. {Key: "/1234", Nodes: []*store.NodeExtern{
  283. {Key: "/1234/dynamic", Value: stringp("garbage")},
  284. }},
  285. {Key: "/1234", Nodes: []*store.NodeExtern{
  286. {Key: "/1234/dynamic", Value: stringp(`{"PeerURLs":null}`)},
  287. }},
  288. {Key: "/1234", Nodes: []*store.NodeExtern{
  289. {Key: "/1234/dynamic", Value: stringp(`{"PeerURLs":null}`)},
  290. {Key: "/1234/strange"},
  291. }},
  292. {Key: "/1234", Nodes: []*store.NodeExtern{
  293. {Key: "/1234/dynamic", Value: stringp(`{"PeerURLs":null}`)},
  294. {Key: "/1234/static", Value: stringp("garbage")},
  295. }},
  296. {Key: "/1234", Nodes: []*store.NodeExtern{
  297. {Key: "/1234/dynamic", Value: stringp(`{"PeerURLs":null}`)},
  298. {Key: "/1234/static", Value: stringp(`{"Name":"node1","ClientURLs":null}`)},
  299. {Key: "/1234/strange"},
  300. }},
  301. }
  302. for i, tt := range tests {
  303. if _, err := nodeToMember(tt); err == nil {
  304. t.Errorf("#%d: unexpected nil error", i)
  305. }
  306. }
  307. }
  308. func TestClusterAddMember(t *testing.T) {
  309. st := &storeRecorder{}
  310. c := newTestCluster(nil)
  311. c.SetStore(st)
  312. c.AddMember(newTestMemberp(1, nil, "node1", nil))
  313. wactions := []action{
  314. {
  315. name: "Create",
  316. params: []interface{}{
  317. path.Join(storeMembersPrefix, "1", "raftAttributes"),
  318. false,
  319. `{"PeerURLs":null}`,
  320. false,
  321. store.Permanent,
  322. },
  323. },
  324. {
  325. name: "Create",
  326. params: []interface{}{
  327. path.Join(storeMembersPrefix, "1", "attributes"),
  328. false,
  329. `{"Name":"node1"}`,
  330. false,
  331. store.Permanent,
  332. },
  333. },
  334. }
  335. if g := st.Action(); !reflect.DeepEqual(g, wactions) {
  336. t.Errorf("actions = %v, want %v", g, wactions)
  337. }
  338. }
  339. func TestClusterMembers(t *testing.T) {
  340. cls := &Cluster{
  341. members: map[uint64]*Member{
  342. 1: &Member{ID: 1},
  343. 20: &Member{ID: 20},
  344. 100: &Member{ID: 100},
  345. 5: &Member{ID: 5},
  346. 50: &Member{ID: 50},
  347. },
  348. }
  349. w := []*Member{
  350. &Member{ID: 1},
  351. &Member{ID: 5},
  352. &Member{ID: 20},
  353. &Member{ID: 50},
  354. &Member{ID: 100},
  355. }
  356. if g := cls.Members(); !reflect.DeepEqual(g, w) {
  357. t.Fatalf("Members()=%#v, want %#v", g, w)
  358. }
  359. }
  360. func TestClusterRemoveMember(t *testing.T) {
  361. st := &storeRecorder{}
  362. c := newTestCluster(nil)
  363. c.SetStore(st)
  364. c.RemoveMember(1)
  365. wactions := []action{
  366. {name: "Delete", params: []interface{}{memberStoreKey(1), true, true}},
  367. {name: "Create", params: []interface{}{removedMemberStoreKey(1), false, "", false, store.Permanent}},
  368. }
  369. if !reflect.DeepEqual(st.Action(), wactions) {
  370. t.Errorf("actions = %v, want %v", st.Action(), wactions)
  371. }
  372. }
  373. func TestNodeToMember(t *testing.T) {
  374. n := &store.NodeExtern{Key: "/1234", Nodes: []*store.NodeExtern{
  375. {Key: "/1234/attributes", Value: stringp(`{"Name":"node1","ClientURLs":null}`)},
  376. {Key: "/1234/raftAttributes", Value: stringp(`{"PeerURLs":null}`)},
  377. }}
  378. wm := &Member{ID: 0x1234, RaftAttributes: RaftAttributes{}, Attributes: Attributes{Name: "node1"}}
  379. m, err := nodeToMember(n)
  380. if err != nil {
  381. t.Fatalf("unexpected nodeToMember error: %v", err)
  382. }
  383. if !reflect.DeepEqual(m, wm) {
  384. t.Errorf("member = %+v, want %+v", m, wm)
  385. }
  386. }
  387. func newTestCluster(membs []Member) *Cluster {
  388. c := &Cluster{members: make(map[uint64]*Member), removed: make(map[uint64]bool)}
  389. for i, m := range membs {
  390. c.members[m.ID] = &membs[i]
  391. }
  392. return c
  393. }
  394. func newTestMember(id uint64, peerURLs []string, name string, clientURLs []string) Member {
  395. return Member{
  396. ID: id,
  397. RaftAttributes: RaftAttributes{PeerURLs: peerURLs},
  398. Attributes: Attributes{Name: name, ClientURLs: clientURLs},
  399. }
  400. }
  401. func newTestMemberp(id uint64, peerURLs []string, name string, clientURLs []string) *Member {
  402. m := newTestMember(id, peerURLs, name, clientURLs)
  403. return &m
  404. }