mock_etcd_test.go 7.3 KB

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