mock_etcd_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright 2015 flannel authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package subnet
  15. import (
  16. "fmt"
  17. "sync"
  18. "testing"
  19. etcd "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/etcd/client"
  20. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  21. )
  22. func expectSuccess(t *testing.T, r *etcd.Response, err error, expected *etcd.Response, expectedValue string) {
  23. if err != nil {
  24. t.Fatalf("Failed to get etcd keys: %v", err)
  25. }
  26. if r == nil {
  27. t.Fatal("etcd response was nil")
  28. }
  29. if r.Action != expected.Action {
  30. t.Fatalf("Unexpected action %s (expected %s)", r.Action, expected.Action)
  31. }
  32. if r.Index < expected.Index {
  33. t.Fatalf("Unexpected response index %v (expected >= %v)", r.Index, expected.Index)
  34. }
  35. if expected.Node != nil {
  36. if expected.Node.Key != r.Node.Key {
  37. t.Fatalf("Unexpected response node %s key %s (expected %s)", r.Node.Key, r.Node.Key, expected.Node.Key)
  38. }
  39. if expected.Node.Value != r.Node.Value {
  40. t.Fatalf("Unexpected response node %s value %s (expected %s)", r.Node.Key, r.Node.Value, expected.Node.Value)
  41. }
  42. if expected.Node.Dir != r.Node.Dir {
  43. t.Fatalf("Unexpected response node %s dir %v (expected %v)", r.Node.Key, r.Node.Dir, expected.Node.Dir)
  44. }
  45. if expected.Node.CreatedIndex != r.Node.CreatedIndex {
  46. t.Fatalf("Unexpected response node %s CreatedIndex %v (expected %v)", r.Node.Key, r.Node.CreatedIndex, expected.Node.CreatedIndex)
  47. }
  48. if expected.Node.ModifiedIndex > r.Node.ModifiedIndex {
  49. t.Fatalf("Unexpected response node %s ModifiedIndex %v (expected %v)", r.Node.Key, r.Node.ModifiedIndex, expected.Node.ModifiedIndex)
  50. }
  51. }
  52. if expectedValue != "" {
  53. if r.Node == nil {
  54. t.Fatalf("Unexpected empty response node")
  55. }
  56. if r.Node.Value != expectedValue {
  57. t.Fatalf("Unexpected response node %s value %s (expected %s)", r.Node.Key, r.Node.Value, expectedValue)
  58. }
  59. }
  60. }
  61. func watchMockEtcd(ctx context.Context, watcher etcd.Watcher, result chan error) {
  62. type evt struct {
  63. key string
  64. event string
  65. received bool
  66. }
  67. expected := []evt{
  68. {"/coreos.com/network/foobar/config", "create", false},
  69. {"/coreos.com/network/blah/config", "create", false},
  70. {"/coreos.com/network/blah/config", "update", false},
  71. {"/coreos.com/network/foobar/config", "delete", false},
  72. {"/coreos.com/network/foobar", "delete", false},
  73. }
  74. // Wait for delete events on /coreos.com/network/foobar and its
  75. // 'config' child, and for the update event on
  76. // /coreos.com/network/foobar (for 'config' delete) and on
  77. // /coreos.com/network (for 'foobar' delete)
  78. numEvents := 0
  79. for {
  80. resp, err := watcher.Next(ctx)
  81. if err != nil {
  82. if err == context.Canceled {
  83. break
  84. }
  85. result <- fmt.Errorf("Unexpected error watching for event: %v", err)
  86. break
  87. }
  88. if resp.Node == nil {
  89. result <- fmt.Errorf("Unexpected empty node watching for event")
  90. break
  91. }
  92. found := false
  93. for i, e := range expected {
  94. if e.key == resp.Node.Key && e.event == resp.Action {
  95. if expected[i].received != true {
  96. expected[i].received = true
  97. found = true
  98. numEvents += 1
  99. }
  100. break
  101. }
  102. }
  103. if found == false {
  104. result <- fmt.Errorf("Received unexpected or already received event %v", resp)
  105. break
  106. }
  107. if numEvents == len(expected) {
  108. result <- nil
  109. break
  110. }
  111. }
  112. }
  113. func TestMockEtcd(t *testing.T) {
  114. m := newMockEtcd()
  115. ctx, _ := context.WithCancel(context.Background())
  116. // Sanity tests for our mock etcd
  117. // Ensure no entries yet exist
  118. opts := &etcd.GetOptions{Recursive: true}
  119. r, err := m.Get(ctx, "/", opts)
  120. e := &etcd.Response{Action: "get", Index: 1000, Node: m.nodes["/"]}
  121. expectSuccess(t, r, err, e, "")
  122. // Create base test keys
  123. sopts := &etcd.SetOptions{Dir: true}
  124. r, err = m.Set(ctx, "/coreos.com/network", "", sopts)
  125. e = &etcd.Response{Action: "create", Index: 1002}
  126. expectSuccess(t, r, err, e, "")
  127. wg := sync.WaitGroup{}
  128. wg.Add(1)
  129. startWg := sync.WaitGroup{}
  130. startWg.Add(1)
  131. result := make(chan error, 1)
  132. go func() {
  133. wopts := &etcd.WatcherOptions{AfterIndex: m.index, Recursive: true}
  134. watcher := m.Watcher("/coreos.com/network", wopts)
  135. startWg.Done()
  136. watchMockEtcd(ctx, watcher, result)
  137. wg.Done()
  138. }()
  139. startWg.Wait()
  140. // Populate etcd with some keys
  141. netKey1 := "/coreos.com/network/foobar/config"
  142. netValue := "{ \"Network\": \"10.1.0.0/16\", \"Backend\": { \"Type\": \"host-gw\" } }"
  143. r, err = m.Create(ctx, netKey1, netValue)
  144. e = &etcd.Response{Action: "create", Index: 1004}
  145. expectSuccess(t, r, err, e, netValue)
  146. netKey2 := "/coreos.com/network/blah/config"
  147. netValue = "{ \"Network\": \"10.1.1.0/16\", \"Backend\": { \"Type\": \"host-gw\" } }"
  148. r, err = m.Create(ctx, netKey2, netValue)
  149. e = &etcd.Response{Action: "create", Index: 1006}
  150. expectSuccess(t, r, err, e, netValue)
  151. // Get it again
  152. expectedNode := r.Node
  153. opts = &etcd.GetOptions{Recursive: false}
  154. r, err = m.Get(ctx, netKey2, opts)
  155. e = &etcd.Response{Action: "get", Index: m.index, Node: expectedNode}
  156. expectSuccess(t, r, err, e, netValue)
  157. // Update it
  158. netValue = "ReallyCoolValue"
  159. r, err = m.Update(ctx, netKey2, netValue)
  160. e = &etcd.Response{Action: "update", Index: m.index}
  161. expectSuccess(t, r, err, e, netValue)
  162. // Get it again
  163. opts = &etcd.GetOptions{Recursive: false}
  164. r, err = m.Get(ctx, netKey2, opts)
  165. e = &etcd.Response{Action: "get", Index: m.index}
  166. expectSuccess(t, r, err, e, netValue)
  167. // test directory listing
  168. opts = &etcd.GetOptions{Recursive: true}
  169. r, err = m.Get(ctx, "/coreos.com/network/", opts)
  170. e = &etcd.Response{Action: "get", Index: 1007}
  171. expectSuccess(t, r, err, e, "")
  172. if len(r.Node.Nodes) != 2 {
  173. t.Fatalf("Unexpected %d children in response (expected 2)", len(r.Node.Nodes))
  174. }
  175. node1Found := false
  176. node2Found := false
  177. for _, child := range r.Node.Nodes {
  178. if child.Dir != true {
  179. t.Fatalf("Unexpected non-directory child %s", child.Key)
  180. }
  181. if child.Key == "/coreos.com/network/foobar" {
  182. node1Found = true
  183. } else if child.Key == "/coreos.com/network/blah" {
  184. node2Found = true
  185. } else {
  186. t.Fatalf("Unexpected child %s found", child.Key)
  187. }
  188. if len(child.Nodes) != 1 {
  189. t.Fatalf("Unexpected %d children in response (expected 2)", len(r.Node.Nodes))
  190. }
  191. }
  192. if node1Found == false || node2Found == false {
  193. t.Fatalf("Failed to find expected children")
  194. }
  195. // Delete a key
  196. dopts := &etcd.DeleteOptions{Recursive: true, Dir: false}
  197. r, err = m.Delete(ctx, "/coreos.com/network/foobar", dopts)
  198. if err == nil {
  199. t.Fatalf("Unexpected success deleting a directory")
  200. }
  201. // Delete a key
  202. dopts = &etcd.DeleteOptions{Recursive: true, Dir: true}
  203. r, err = m.Delete(ctx, "/coreos.com/network/foobar", dopts)
  204. e = &etcd.Response{Action: "delete", Index: 1010}
  205. expectSuccess(t, r, err, e, "")
  206. // Get it again; should fail
  207. opts = &etcd.GetOptions{Recursive: false}
  208. r, err = m.Get(ctx, netKey1, opts)
  209. if err == nil {
  210. t.Fatalf("Get of %s after delete unexpectedly succeeded", netKey1)
  211. }
  212. if r != nil {
  213. t.Fatalf("Unexpected non-nil response to get after delete %v", r)
  214. }
  215. wg.Wait()
  216. // Check errors from watch goroutine
  217. watchResult := <-result
  218. if watchResult != nil {
  219. t.Fatalf("Error watching keys: %v", watchResult)
  220. }
  221. }