cluster_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package etcdserver
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestClusterAddSlice(t *testing.T) {
  7. tests := []struct {
  8. mems []Member
  9. want *Cluster
  10. }{
  11. {
  12. []Member{},
  13. &Cluster{},
  14. },
  15. {
  16. []Member{
  17. {ID: 1, PeerURLs: []string{"foo", "bar"}},
  18. {ID: 2, PeerURLs: []string{"baz"}},
  19. },
  20. &Cluster{
  21. 1: &Member{
  22. ID: 1,
  23. PeerURLs: []string{"foo", "bar"},
  24. },
  25. 2: &Member{
  26. ID: 2,
  27. PeerURLs: []string{"baz"},
  28. },
  29. },
  30. },
  31. }
  32. for i, tt := range tests {
  33. c := &Cluster{}
  34. if err := c.AddSlice(tt.mems); err != nil {
  35. t.Errorf("#%d: err=%#v, want nil", i, err)
  36. continue
  37. }
  38. if !reflect.DeepEqual(c, tt.want) {
  39. t.Errorf("#%d: c=%#v, want %#v", i, c, tt.want)
  40. }
  41. }
  42. }
  43. func TestClusterAddSliceBad(t *testing.T) {
  44. c := Cluster{
  45. 1: &Member{ID: 1},
  46. }
  47. if err := c.AddSlice([]Member{{ID: 1}}); err == nil {
  48. t.Error("want err, but got nil")
  49. }
  50. }
  51. func TestClusterPick(t *testing.T) {
  52. cs := Cluster{
  53. 1: &Member{ID: 1, PeerURLs: []string{"abc", "def", "ghi", "jkl", "mno", "pqr", "stu"}},
  54. 2: &Member{ID: 2, PeerURLs: []string{"xyz"}},
  55. 3: &Member{ID: 3, PeerURLs: []string{}},
  56. }
  57. ids := map[string]bool{
  58. "abc": true,
  59. "def": true,
  60. "ghi": true,
  61. "jkl": true,
  62. "mno": true,
  63. "pqr": true,
  64. "stu": true,
  65. }
  66. for i := 0; i < 1000; i++ {
  67. a := cs.Pick(1)
  68. if !ids[a] {
  69. t.Errorf("returned ID %q not in expected range!", a)
  70. break
  71. }
  72. }
  73. if b := cs.Pick(2); b != "xyz" {
  74. t.Errorf("id=%q, want %q", b, "xyz")
  75. }
  76. if c := cs.Pick(3); c != "" {
  77. t.Errorf("id=%q, want %q", c, "")
  78. }
  79. if d := cs.Pick(4); d != "" {
  80. t.Errorf("id=%q, want %q", d, "")
  81. }
  82. }
  83. func TestClusterFind(t *testing.T) {
  84. tests := []struct {
  85. id uint64
  86. name string
  87. mems []Member
  88. match bool
  89. }{
  90. {
  91. 1,
  92. "node1",
  93. []Member{{Name: "node1", ID: 1}},
  94. true,
  95. },
  96. {
  97. 2,
  98. "foobar",
  99. []Member{},
  100. false,
  101. },
  102. {
  103. 2,
  104. "node2",
  105. []Member{{Name: "node1", ID: 1}, {Name: "node2", ID: 2}},
  106. true,
  107. },
  108. {
  109. 3,
  110. "node3",
  111. []Member{{Name: "node1", ID: 1}, {Name: "node2", ID: 2}},
  112. false,
  113. },
  114. }
  115. for i, tt := range tests {
  116. c := Cluster{}
  117. c.AddSlice(tt.mems)
  118. m := c.FindName(tt.name)
  119. if m == nil && !tt.match {
  120. continue
  121. }
  122. if m == nil && tt.match {
  123. t.Errorf("#%d: expected match got empty", i)
  124. }
  125. if m.Name != tt.name && tt.match {
  126. t.Errorf("#%d: got = %v, want %v", i, m.Name, tt.name)
  127. }
  128. }
  129. for i, tt := range tests {
  130. c := Cluster{}
  131. c.AddSlice(tt.mems)
  132. m := c.FindID(tt.id)
  133. if m == nil && !tt.match {
  134. continue
  135. }
  136. if m == nil && tt.match {
  137. t.Errorf("#%d: expected match got empty", i)
  138. }
  139. if m.ID != tt.id && tt.match {
  140. t.Errorf("#%d: got = %v, want %v", i, m.Name, tt.id)
  141. }
  142. }
  143. }
  144. func TestClusterSet(t *testing.T) {
  145. tests := []struct {
  146. f string
  147. mems []Member
  148. }{
  149. {
  150. "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",
  151. []Member{
  152. {ID: 3736794188555456841, Name: "mem1", PeerURLs: []string{"http://10.0.0.1:2379", "http://128.193.4.20:2379"}},
  153. {ID: 5674507346857578431, Name: "mem2", PeerURLs: []string{"http://10.0.0.2:2379"}},
  154. {ID: 2676999861503984872, Name: "default", PeerURLs: []string{"http://127.0.0.1:2379"}},
  155. },
  156. },
  157. }
  158. for i, tt := range tests {
  159. c := Cluster{}
  160. if err := c.AddSlice(tt.mems); err != nil {
  161. t.Error(err)
  162. }
  163. g := Cluster{}
  164. g.Set(tt.f)
  165. if g.String() != c.String() {
  166. t.Errorf("#%d: set = %v, want %v", i, g, c)
  167. }
  168. }
  169. }
  170. func TestClusterSetBad(t *testing.T) {
  171. tests := []string{
  172. // invalid URL
  173. "%^",
  174. // no URL defined for member
  175. "mem1=,mem2=http://128.193.4.20:2379,mem3=http://10.0.0.2:2379",
  176. "mem1,mem2=http://128.193.4.20:2379,mem3=http://10.0.0.2:2379",
  177. // TODO(philips): anyone know of a 64 bit sha1 hash collision
  178. // "06b2f82fd81b2c20=http://128.193.4.20:2379,02c60cb75083ceef=http://128.193.4.20:2379",
  179. }
  180. for i, tt := range tests {
  181. g := Cluster{}
  182. if err := g.Set(tt); err == nil {
  183. t.Errorf("#%d: set = %v, want err", i, tt)
  184. }
  185. }
  186. }
  187. func TestClusterIDs(t *testing.T) {
  188. cs := Cluster{}
  189. cs.AddSlice([]Member{
  190. {ID: 1},
  191. {ID: 4},
  192. {ID: 100},
  193. })
  194. w := []uint64{1, 4, 100}
  195. g := cs.IDs()
  196. if !reflect.DeepEqual(w, g) {
  197. t.Errorf("IDs=%+v, want %+v", g, w)
  198. }
  199. }
  200. func TestClusterAddBad(t *testing.T) {
  201. // Should not be possible to add the same ID multiple times
  202. mems := []Member{
  203. {ID: 1, Name: "mem1"},
  204. {ID: 1, Name: "mem2"},
  205. }
  206. c := &Cluster{}
  207. c.Add(Member{ID: 1, Name: "mem1"})
  208. for i, m := range mems {
  209. if err := c.Add(m); err == nil {
  210. t.Errorf("#%d: set = %v, want err", i, m)
  211. }
  212. }
  213. }
  214. func TestClusterPeerURLs(t *testing.T) {
  215. tests := []struct {
  216. mems []Member
  217. wurls []string
  218. }{
  219. // single peer with a single address
  220. {
  221. mems: []Member{
  222. {ID: 1, PeerURLs: []string{"http://192.0.2.1"}},
  223. },
  224. wurls: []string{"http://192.0.2.1"},
  225. },
  226. // single peer with a single address with a port
  227. {
  228. mems: []Member{
  229. {ID: 1, PeerURLs: []string{"http://192.0.2.1:8001"}},
  230. },
  231. wurls: []string{"http://192.0.2.1:8001"},
  232. },
  233. // several members explicitly unsorted
  234. {
  235. mems: []Member{
  236. {ID: 2, PeerURLs: []string{"http://192.0.2.3", "http://192.0.2.4"}},
  237. {ID: 3, PeerURLs: []string{"http://192.0.2.5", "http://192.0.2.6"}},
  238. {ID: 1, PeerURLs: []string{"http://192.0.2.1", "http://192.0.2.2"}},
  239. },
  240. 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"},
  241. },
  242. // no members
  243. {
  244. mems: []Member{},
  245. wurls: []string{},
  246. },
  247. // peer with no peer urls
  248. {
  249. mems: []Member{
  250. {ID: 3, PeerURLs: []string{}},
  251. },
  252. wurls: []string{},
  253. },
  254. }
  255. for i, tt := range tests {
  256. c := Cluster{}
  257. if err := c.AddSlice(tt.mems); err != nil {
  258. t.Errorf("AddSlice error: %v", err)
  259. continue
  260. }
  261. urls := c.PeerURLs()
  262. if !reflect.DeepEqual(urls, tt.wurls) {
  263. t.Errorf("#%d: PeerURLs = %v, want %v", i, urls, tt.wurls)
  264. }
  265. }
  266. }
  267. func TestClusterClientURLs(t *testing.T) {
  268. tests := []struct {
  269. mems []Member
  270. wurls []string
  271. }{
  272. // single peer with a single address
  273. {
  274. mems: []Member{
  275. {ID: 1, ClientURLs: []string{"http://192.0.2.1"}},
  276. },
  277. wurls: []string{"http://192.0.2.1"},
  278. },
  279. // single peer with a single address with a port
  280. {
  281. mems: []Member{
  282. {ID: 1, ClientURLs: []string{"http://192.0.2.1:8001"}},
  283. },
  284. wurls: []string{"http://192.0.2.1:8001"},
  285. },
  286. // several members explicitly unsorted
  287. {
  288. mems: []Member{
  289. {ID: 2, ClientURLs: []string{"http://192.0.2.3", "http://192.0.2.4"}},
  290. {ID: 3, ClientURLs: []string{"http://192.0.2.5", "http://192.0.2.6"}},
  291. {ID: 1, ClientURLs: []string{"http://192.0.2.1", "http://192.0.2.2"}},
  292. },
  293. 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"},
  294. },
  295. // no members
  296. {
  297. mems: []Member{},
  298. wurls: []string{},
  299. },
  300. // peer with no client urls
  301. {
  302. mems: []Member{
  303. {ID: 3, ClientURLs: []string{}},
  304. },
  305. wurls: []string{},
  306. },
  307. }
  308. for i, tt := range tests {
  309. c := Cluster{}
  310. if err := c.AddSlice(tt.mems); err != nil {
  311. t.Errorf("AddSlice error: %v", err)
  312. continue
  313. }
  314. urls := c.ClientURLs()
  315. if !reflect.DeepEqual(urls, tt.wurls) {
  316. t.Errorf("#%d: ClientURLs = %v, want %v", i, urls, tt.wurls)
  317. }
  318. }
  319. }