discovery_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. for i, tt := range tests {
  157. c := &clientWithResp{nil, &watcherWithResp{tt.rs}}
  158. d := &discovery{cluster: "1000", c: c}
  159. g, err := d.waitNodes(tt.nodes, tt.size)
  160. if err != tt.werr {
  161. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  162. }
  163. if !reflect.DeepEqual(g, tt.wall) {
  164. t.Errorf("#%d: all = %v, want %v", i, g, tt.wall)
  165. }
  166. }
  167. }
  168. func TestCreateSelf(t *testing.T) {
  169. rs := []*client.Response{{Node: &client.Node{Key: "1000/1", CreatedIndex: 2}}}
  170. w := &watcherWithResp{rs}
  171. errw := &watcherWithErr{errors.New("watch err")}
  172. c := &clientWithResp{rs, w}
  173. errc := &clientWithErr{errors.New("create err"), w}
  174. errwc := &clientWithResp{rs, errw}
  175. tests := []struct {
  176. c client.Client
  177. werr error
  178. }{
  179. // no error
  180. {c, nil},
  181. // client.create returns an error
  182. {errc, errc.err},
  183. // watcher.next retuens an error
  184. {errwc, errw.err},
  185. }
  186. for i, tt := range tests {
  187. d := discovery{cluster: "1000", c: tt.c}
  188. if err := d.createSelf(); err != tt.werr {
  189. t.Errorf("#%d: err = %v, want %v", i, err, nil)
  190. }
  191. }
  192. }
  193. func TestNodesToPeers(t *testing.T) {
  194. nodes := client.Nodes{
  195. {Key: "/1000/1", Value: "1=1.1.1.1", CreatedIndex: 1},
  196. {Key: "/1000/2", Value: "2=2.2.2.2", CreatedIndex: 2},
  197. {Key: "/1000/3", Value: "3=3.3.3.3", CreatedIndex: 3},
  198. }
  199. w := &etcdhttp.Peers{}
  200. w.Set("1=1.1.1.1&2=2.2.2.2&3=3.3.3.3")
  201. badnodes := client.Nodes{{Key: "1000/1", Value: "1=1.1.1.1&???", CreatedIndex: 1}}
  202. tests := []struct {
  203. ns client.Nodes
  204. wp *etcdhttp.Peers
  205. we bool
  206. }{
  207. {nodes, w, false},
  208. {badnodes, nil, true},
  209. }
  210. for i, tt := range tests {
  211. peers, err := nodesToPeers(tt.ns)
  212. if tt.we {
  213. if err == nil {
  214. t.Fatalf("#%d: err = %v, want not nil", i, err)
  215. }
  216. } else {
  217. if err != nil {
  218. t.Fatalf("#%d: err = %v, want nil", i, err)
  219. }
  220. }
  221. if !reflect.DeepEqual(peers, tt.wp) {
  222. t.Errorf("#%d: peers = %v, want %v", i, peers, tt.wp)
  223. }
  224. }
  225. }
  226. func TestSortableNodes(t *testing.T) {
  227. ns := client.Nodes{
  228. {CreatedIndex: 5},
  229. {CreatedIndex: 1},
  230. {CreatedIndex: 3},
  231. {CreatedIndex: 4},
  232. }
  233. // add some randomness
  234. for i := 0; i < 10000; i++ {
  235. ns = append(ns, &client.Node{CreatedIndex: uint64(rand.Int31())})
  236. }
  237. sns := sortableNodes{ns}
  238. sort.Sort(sns)
  239. cis := make([]int, 0)
  240. for _, n := range sns.Nodes {
  241. cis = append(cis, int(n.CreatedIndex))
  242. }
  243. if sort.IntsAreSorted(cis) != true {
  244. t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
  245. }
  246. cis = make([]int, 0)
  247. for _, n := range ns {
  248. cis = append(cis, int(n.CreatedIndex))
  249. }
  250. if sort.IntsAreSorted(cis) != true {
  251. t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
  252. }
  253. }
  254. type clientWithResp struct {
  255. rs []*client.Response
  256. w client.Watcher
  257. }
  258. func (c *clientWithResp) Create(key string, value string, ttl time.Duration) (*client.Response, error) {
  259. if len(c.rs) == 0 {
  260. return &client.Response{}, nil
  261. }
  262. r := c.rs[0]
  263. c.rs = c.rs[1:]
  264. return r, nil
  265. }
  266. func (c *clientWithResp) Get(key string) (*client.Response, error) {
  267. if len(c.rs) == 0 {
  268. return &client.Response{}, client.ErrKeyNoExist
  269. }
  270. r := c.rs[0]
  271. c.rs = c.rs[1:]
  272. return r, nil
  273. }
  274. func (c *clientWithResp) Watch(key string, waitIndex uint64) client.Watcher {
  275. return c.w
  276. }
  277. func (c *clientWithResp) RecursiveWatch(key string, waitIndex uint64) client.Watcher {
  278. return c.w
  279. }
  280. type clientWithErr struct {
  281. err error
  282. w client.Watcher
  283. }
  284. func (c *clientWithErr) Create(key string, value string, ttl time.Duration) (*client.Response, error) {
  285. return &client.Response{}, c.err
  286. }
  287. func (c *clientWithErr) Get(key string) (*client.Response, error) {
  288. return &client.Response{}, c.err
  289. }
  290. func (c *clientWithErr) Watch(key string, waitIndex uint64) client.Watcher {
  291. return c.w
  292. }
  293. func (c *clientWithErr) RecursiveWatch(key string, waitIndex uint64) client.Watcher {
  294. return c.w
  295. }
  296. type watcherWithResp struct {
  297. rs []*client.Response
  298. }
  299. func (w *watcherWithResp) Next() (*client.Response, error) {
  300. if len(w.rs) == 0 {
  301. return &client.Response{}, nil
  302. }
  303. r := w.rs[0]
  304. w.rs = w.rs[1:]
  305. return r, nil
  306. }
  307. type watcherWithErr struct {
  308. err error
  309. }
  310. func (w *watcherWithErr) Next() (*client.Response, error) {
  311. return &client.Response{}, w.err
  312. }