discovery_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package discovery
  2. import (
  3. "errors"
  4. "math/rand"
  5. "sort"
  6. "reflect"
  7. "testing"
  8. "time"
  9. "github.com/coreos/etcd/client"
  10. "github.com/coreos/etcd/etcdserver/etcdhttp"
  11. )
  12. func TestCheckCluster(t *testing.T) {
  13. cluster := "1000"
  14. self := "/1000/1"
  15. tests := []struct {
  16. nodes []*client.Node
  17. werr error
  18. wsize int
  19. }{
  20. {
  21. // self is in the size range
  22. client.Nodes{
  23. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  24. {Key: self, CreatedIndex: 2},
  25. {Key: "/1000/2", CreatedIndex: 3},
  26. {Key: "/1000/3", CreatedIndex: 4},
  27. {Key: "/1000/4", CreatedIndex: 5},
  28. },
  29. nil,
  30. 3,
  31. },
  32. {
  33. // self is in the size range
  34. client.Nodes{
  35. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  36. {Key: "/1000/2", CreatedIndex: 2},
  37. {Key: "/1000/3", CreatedIndex: 3},
  38. {Key: self, CreatedIndex: 4},
  39. {Key: "/1000/4", CreatedIndex: 5},
  40. },
  41. nil,
  42. 3,
  43. },
  44. {
  45. // self is out of the size range
  46. client.Nodes{
  47. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  48. {Key: "/1000/2", CreatedIndex: 2},
  49. {Key: "/1000/3", CreatedIndex: 3},
  50. {Key: "/1000/4", CreatedIndex: 4},
  51. {Key: self, CreatedIndex: 5},
  52. },
  53. ErrFullCluster,
  54. 3,
  55. },
  56. {
  57. // self is not in the cluster
  58. client.Nodes{
  59. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  60. {Key: "/1000/2", CreatedIndex: 2},
  61. {Key: "/1000/3", CreatedIndex: 3},
  62. },
  63. nil,
  64. 3,
  65. },
  66. {
  67. client.Nodes{
  68. {Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
  69. {Key: "/1000/2", CreatedIndex: 2},
  70. {Key: "/1000/3", CreatedIndex: 3},
  71. {Key: "/1000/4", CreatedIndex: 4},
  72. },
  73. ErrFullCluster,
  74. 3,
  75. },
  76. {
  77. // bad size key
  78. client.Nodes{
  79. {Key: "/1000/_config/size", Value: "bad", CreatedIndex: 1},
  80. },
  81. ErrBadSizeKey,
  82. 0,
  83. },
  84. {
  85. // no size key
  86. client.Nodes{},
  87. ErrSizeNotFound,
  88. 0,
  89. },
  90. }
  91. for i, tt := range tests {
  92. rs := make([]*client.Response, 0)
  93. if len(tt.nodes) > 0 {
  94. rs = append(rs, &client.Response{Node: tt.nodes[0]})
  95. rs = append(rs, &client.Response{
  96. Node: &client.Node{
  97. Key: cluster,
  98. Nodes: tt.nodes,
  99. },
  100. })
  101. }
  102. c := &clientWithResp{rs: rs}
  103. d := discovery{cluster: cluster, id: 1, c: c}
  104. ns, size, err := d.checkCluster()
  105. if err != tt.werr {
  106. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  107. }
  108. if reflect.DeepEqual(ns, tt.nodes) {
  109. t.Errorf("#%d: nodes = %v, want %v", i, ns, tt.nodes)
  110. }
  111. if size != tt.wsize {
  112. t.Errorf("#%d: size = %v, want %d", i, size, tt.wsize)
  113. }
  114. }
  115. }
  116. func TestWaitNodes(t *testing.T) {
  117. all := client.Nodes{
  118. {Key: "/1000/1", CreatedIndex: 2},
  119. {Key: "/1000/2", CreatedIndex: 3},
  120. {Key: "/1000/3", CreatedIndex: 4},
  121. }
  122. tests := []struct {
  123. nodes client.Nodes
  124. size int
  125. rs []*client.Response
  126. werr error
  127. wall client.Nodes
  128. }{
  129. {
  130. all,
  131. 3,
  132. []*client.Response{},
  133. nil,
  134. all,
  135. },
  136. {
  137. all[:1],
  138. 3,
  139. []*client.Response{
  140. {Node: &client.Node{Key: "/1000/2", CreatedIndex: 3}},
  141. {Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
  142. },
  143. nil,
  144. all,
  145. },
  146. {
  147. all[:2],
  148. 3,
  149. []*client.Response{
  150. {Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
  151. },
  152. nil,
  153. all,
  154. },
  155. {
  156. append(all, &client.Node{Key: "/1000/4", CreatedIndex: 5}),
  157. 3,
  158. []*client.Response{
  159. {Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
  160. },
  161. nil,
  162. all,
  163. },
  164. }
  165. for i, tt := range tests {
  166. c := &clientWithResp{nil, &watcherWithResp{tt.rs}}
  167. d := &discovery{cluster: "1000", c: c}
  168. g, err := d.waitNodes(tt.nodes, tt.size)
  169. if err != tt.werr {
  170. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  171. }
  172. if !reflect.DeepEqual(g, tt.wall) {
  173. t.Errorf("#%d: all = %v, want %v", i, g, tt.wall)
  174. }
  175. }
  176. }
  177. func TestCreateSelf(t *testing.T) {
  178. rs := []*client.Response{{Node: &client.Node{Key: "1000/1", CreatedIndex: 2}}}
  179. w := &watcherWithResp{rs}
  180. errw := &watcherWithErr{errors.New("watch err")}
  181. c := &clientWithResp{rs, w}
  182. errc := &clientWithErr{errors.New("create err"), w}
  183. errwc := &clientWithResp{rs, errw}
  184. tests := []struct {
  185. c client.Client
  186. werr error
  187. }{
  188. // no error
  189. {c, nil},
  190. // client.create returns an error
  191. {errc, errc.err},
  192. // watcher.next retuens an error
  193. {errwc, errw.err},
  194. }
  195. for i, tt := range tests {
  196. d := discovery{cluster: "1000", c: tt.c}
  197. if err := d.createSelf(); err != tt.werr {
  198. t.Errorf("#%d: err = %v, want %v", i, err, nil)
  199. }
  200. }
  201. }
  202. func TestNodesToPeers(t *testing.T) {
  203. nodes := client.Nodes{
  204. {Key: "/1000/1", Value: "1=1.1.1.1", CreatedIndex: 1},
  205. {Key: "/1000/2", Value: "2=2.2.2.2", CreatedIndex: 2},
  206. {Key: "/1000/3", Value: "3=3.3.3.3", CreatedIndex: 3},
  207. }
  208. w := &etcdhttp.Peers{}
  209. w.Set("1=1.1.1.1&2=2.2.2.2&3=3.3.3.3")
  210. badnodes := client.Nodes{{Key: "1000/1", Value: "1=1.1.1.1&???", CreatedIndex: 1}}
  211. tests := []struct {
  212. ns client.Nodes
  213. wp *etcdhttp.Peers
  214. we bool
  215. }{
  216. {nodes, w, false},
  217. {badnodes, nil, true},
  218. }
  219. for i, tt := range tests {
  220. peers, err := nodesToPeers(tt.ns)
  221. if tt.we {
  222. if err == nil {
  223. t.Fatalf("#%d: err = %v, want not nil", i, err)
  224. }
  225. } else {
  226. if err != nil {
  227. t.Fatalf("#%d: err = %v, want nil", i, err)
  228. }
  229. }
  230. if !reflect.DeepEqual(peers, tt.wp) {
  231. t.Errorf("#%d: peers = %v, want %v", i, peers, tt.wp)
  232. }
  233. }
  234. }
  235. func TestSortableNodes(t *testing.T) {
  236. ns := client.Nodes{
  237. {CreatedIndex: 5},
  238. {CreatedIndex: 1},
  239. {CreatedIndex: 3},
  240. {CreatedIndex: 4},
  241. }
  242. // add some randomness
  243. for i := 0; i < 10000; i++ {
  244. ns = append(ns, &client.Node{CreatedIndex: uint64(rand.Int31())})
  245. }
  246. sns := sortableNodes{ns}
  247. sort.Sort(sns)
  248. cis := make([]int, 0)
  249. for _, n := range sns.Nodes {
  250. cis = append(cis, int(n.CreatedIndex))
  251. }
  252. if sort.IntsAreSorted(cis) != true {
  253. t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
  254. }
  255. cis = make([]int, 0)
  256. for _, n := range ns {
  257. cis = append(cis, int(n.CreatedIndex))
  258. }
  259. if sort.IntsAreSorted(cis) != true {
  260. t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
  261. }
  262. }
  263. type clientWithResp struct {
  264. rs []*client.Response
  265. w client.Watcher
  266. }
  267. func (c *clientWithResp) Create(key string, value string, ttl time.Duration) (*client.Response, error) {
  268. if len(c.rs) == 0 {
  269. return &client.Response{}, nil
  270. }
  271. r := c.rs[0]
  272. c.rs = c.rs[1:]
  273. return r, nil
  274. }
  275. func (c *clientWithResp) Get(key string) (*client.Response, error) {
  276. if len(c.rs) == 0 {
  277. return &client.Response{}, client.ErrKeyNoExist
  278. }
  279. r := c.rs[0]
  280. c.rs = c.rs[1:]
  281. return r, nil
  282. }
  283. func (c *clientWithResp) Watch(key string, waitIndex uint64) client.Watcher {
  284. return c.w
  285. }
  286. func (c *clientWithResp) RecursiveWatch(key string, waitIndex uint64) client.Watcher {
  287. return c.w
  288. }
  289. type clientWithErr struct {
  290. err error
  291. w client.Watcher
  292. }
  293. func (c *clientWithErr) Create(key string, value string, ttl time.Duration) (*client.Response, error) {
  294. return &client.Response{}, c.err
  295. }
  296. func (c *clientWithErr) Get(key string) (*client.Response, error) {
  297. return &client.Response{}, c.err
  298. }
  299. func (c *clientWithErr) Watch(key string, waitIndex uint64) client.Watcher {
  300. return c.w
  301. }
  302. func (c *clientWithErr) RecursiveWatch(key string, waitIndex uint64) client.Watcher {
  303. return c.w
  304. }
  305. type watcherWithResp struct {
  306. rs []*client.Response
  307. }
  308. func (w *watcherWithResp) Next() (*client.Response, error) {
  309. if len(w.rs) == 0 {
  310. return &client.Response{}, nil
  311. }
  312. r := w.rs[0]
  313. w.rs = w.rs[1:]
  314. return r, nil
  315. }
  316. type watcherWithErr struct {
  317. err error
  318. }
  319. func (w *watcherWithErr) Next() (*client.Response, error) {
  320. return &client.Response{}, w.err
  321. }