subnet_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // Copyright 2015 CoreOS, Inc.
  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. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. "time"
  21. etcd "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/etcd/client"
  22. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  23. "github.com/coreos/flannel/pkg/ip"
  24. )
  25. func newDummyRegistry(ttlOverride time.Duration) *MockSubnetRegistry {
  26. subnets := []*etcd.Node{
  27. &etcd.Node{Key: "10.3.1.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 10},
  28. &etcd.Node{Key: "10.3.2.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 11},
  29. &etcd.Node{Key: "10.3.4.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 12},
  30. &etcd.Node{Key: "10.3.5.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 13},
  31. }
  32. config := `{ "Network": "10.3.0.0/16", "SubnetMin": "10.3.1.0", "SubnetMax": "10.3.5.0" }`
  33. return NewMockRegistry(ttlOverride, "_", config, subnets)
  34. }
  35. func TestAcquireLease(t *testing.T) {
  36. msr := newDummyRegistry(1000)
  37. sm := newEtcdManager(msr)
  38. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  39. attrs := LeaseAttrs{
  40. PublicIP: extIaddr,
  41. }
  42. l, err := sm.AcquireLease(context.Background(), "_", &attrs)
  43. if err != nil {
  44. t.Fatal("AcquireLease failed: ", err)
  45. }
  46. if l.Subnet.String() != "10.3.3.0/24" {
  47. t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", l.Subnet)
  48. }
  49. // Acquire again, should reuse
  50. if l, err = sm.AcquireLease(context.Background(), "_", &attrs); err != nil {
  51. t.Fatal("AcquireLease failed: ", err)
  52. }
  53. if l.Subnet.String() != "10.3.3.0/24" {
  54. t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", l.Subnet)
  55. }
  56. }
  57. func TestConfigChanged(t *testing.T) {
  58. msr := newDummyRegistry(1000)
  59. sm := newEtcdManager(msr)
  60. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  61. attrs := LeaseAttrs{
  62. PublicIP: extIaddr,
  63. }
  64. l, err := sm.AcquireLease(context.Background(), "_", &attrs)
  65. if err != nil {
  66. t.Fatal("AcquireLease failed: ", err)
  67. }
  68. if l.Subnet.String() != "10.3.3.0/24" {
  69. t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", l.Subnet)
  70. }
  71. // Change config
  72. config := `{ "Network": "10.4.0.0/16" }`
  73. msr.setConfig("_", config)
  74. // Acquire again, should not reuse
  75. if l, err = sm.AcquireLease(context.Background(), "_", &attrs); err != nil {
  76. t.Fatal("AcquireLease failed: ", err)
  77. }
  78. newNet := newIP4Net("10.4.0.0", 16)
  79. if !newNet.Contains(l.Subnet.IP) {
  80. t.Fatalf("Subnet mismatch: expected within %v, got: %v", newNet, l.Subnet)
  81. }
  82. }
  83. func newIP4Net(ipaddr string, prefix uint) ip.IP4Net {
  84. a, err := ip.ParseIP4(ipaddr)
  85. if err != nil {
  86. panic("failed to parse ipaddr")
  87. }
  88. return ip.IP4Net{
  89. IP: a,
  90. PrefixLen: prefix,
  91. }
  92. }
  93. func acquireLease(ctx context.Context, t *testing.T, sm Manager) *Lease {
  94. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  95. attrs := LeaseAttrs{
  96. PublicIP: extIaddr,
  97. }
  98. l, err := sm.AcquireLease(ctx, "_", &attrs)
  99. if err != nil {
  100. t.Fatal("AcquireLease failed: ", err)
  101. }
  102. return l
  103. }
  104. func TestWatchLeaseAdded(t *testing.T) {
  105. msr := newDummyRegistry(0)
  106. sm := newEtcdManager(msr)
  107. ctx, cancel := context.WithCancel(context.Background())
  108. defer cancel()
  109. l := acquireLease(ctx, t, sm)
  110. events := make(chan []Event)
  111. go WatchLeases(ctx, sm, "_", l, events)
  112. evtBatch := <-events
  113. for _, evt := range evtBatch {
  114. if evt.Lease.Key() == l.Key() {
  115. t.Errorf("WatchLeases returned our own lease")
  116. }
  117. }
  118. expected := "10.3.6.0-24"
  119. msr.createSubnet(ctx, "_", expected, `{"PublicIP": "1.1.1.1"}`, 0)
  120. evtBatch = <-events
  121. if len(evtBatch) != 1 {
  122. t.Fatalf("WatchLeases produced wrong sized event batch")
  123. }
  124. evt := evtBatch[0]
  125. if evt.Type != EventAdded {
  126. t.Fatalf("WatchLeases produced wrong event type")
  127. }
  128. actual := evt.Lease.Key()
  129. if actual != expected {
  130. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", expected, actual)
  131. }
  132. }
  133. func TestWatchLeaseRemoved(t *testing.T) {
  134. msr := newDummyRegistry(0)
  135. sm := newEtcdManager(msr)
  136. ctx, cancel := context.WithCancel(context.Background())
  137. defer cancel()
  138. l := acquireLease(ctx, t, sm)
  139. events := make(chan []Event)
  140. go WatchLeases(ctx, sm, "_", l, events)
  141. evtBatch := <-events
  142. for _, evt := range evtBatch {
  143. if evt.Lease.Key() == l.Key() {
  144. t.Errorf("WatchLeases returned our own lease")
  145. }
  146. }
  147. expected := "10.3.4.0-24"
  148. msr.expireSubnet("_", expected)
  149. evtBatch = <-events
  150. if len(evtBatch) != 1 {
  151. t.Fatalf("WatchLeases produced wrong sized event batch")
  152. }
  153. evt := evtBatch[0]
  154. if evt.Type != EventRemoved {
  155. t.Fatalf("WatchLeases produced wrong event type")
  156. }
  157. actual := evt.Lease.Key()
  158. if actual != expected {
  159. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", expected, actual)
  160. }
  161. }
  162. type leaseData struct {
  163. Dummy string
  164. }
  165. func TestRenewLease(t *testing.T) {
  166. msr := newDummyRegistry(1)
  167. sm := newEtcdManager(msr)
  168. // Create LeaseAttrs
  169. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  170. attrs := LeaseAttrs{
  171. PublicIP: extIaddr,
  172. BackendType: "vxlan",
  173. }
  174. ld, err := json.Marshal(&leaseData{Dummy: "test string"})
  175. if err != nil {
  176. t.Fatalf("Failed to marshal leaseData: %v", err)
  177. }
  178. attrs.BackendData = json.RawMessage(ld)
  179. // Acquire lease
  180. ctx, cancel := context.WithCancel(context.Background())
  181. defer cancel()
  182. l, err := sm.AcquireLease(ctx, "_", &attrs)
  183. if err != nil {
  184. t.Fatal("AcquireLease failed: ", err)
  185. }
  186. go LeaseRenewer(ctx, sm, "_", l)
  187. fmt.Println("Waiting for lease to pass original expiration")
  188. time.Sleep(2 * time.Second)
  189. // check that it's still good
  190. net, err := msr.getNetwork(ctx, "_")
  191. if err != nil {
  192. t.Error("Failed to renew lease: could not get networks: %v", err)
  193. }
  194. for _, n := range net.Node.Nodes {
  195. if n.Key == l.Subnet.StringSep(".", "-") {
  196. if n.Expiration.Before(time.Now()) {
  197. t.Error("Failed to renew lease: expiration did not advance")
  198. }
  199. a := LeaseAttrs{}
  200. if err := json.Unmarshal([]byte(n.Value), &a); err != nil {
  201. t.Errorf("Failed to JSON-decode LeaseAttrs: %v", err)
  202. return
  203. }
  204. if !reflect.DeepEqual(a, attrs) {
  205. t.Errorf("LeaseAttrs changed: was %#v, now %#v", attrs, a)
  206. }
  207. return
  208. }
  209. }
  210. t.Fatalf("Failed to find acquired lease")
  211. }
  212. func TestWatchGetNetworks(t *testing.T) {
  213. msr := newDummyRegistry(0)
  214. sm := newEtcdManager(msr)
  215. ctx, cancel := context.WithCancel(context.Background())
  216. defer cancel()
  217. // Kill the previously added "_" network
  218. msr.DeleteNetwork(ctx, "_")
  219. expected := "foobar"
  220. msr.CreateNetwork(ctx, expected, `{"Network": "10.1.1.0/16", "Backend": {"Type": "bridge"}}`)
  221. resp, err := sm.WatchNetworks(ctx, nil)
  222. if err != nil {
  223. t.Errorf("WatchNetworks(nil) failed:", err)
  224. }
  225. if len(resp.Snapshot) != 1 {
  226. t.Errorf("WatchNetworks(nil) produced wrong number of networks: expected 1, got %d", len(resp.Snapshot))
  227. }
  228. if resp.Snapshot[0] != expected {
  229. t.Errorf("WatchNetworks(nil) produced wrong network: expected %s, got %s", expected, resp.Snapshot[0])
  230. }
  231. }
  232. func TestWatchNetworkAdded(t *testing.T) {
  233. msr := newDummyRegistry(0)
  234. sm := newEtcdManager(msr)
  235. ctx, cancel := context.WithCancel(context.Background())
  236. defer cancel()
  237. events := make(chan []Event)
  238. go WatchNetworks(ctx, sm, events)
  239. // skip over the initial snapshot
  240. <-events
  241. expected := "foobar"
  242. msr.CreateNetwork(ctx, expected, `{"Network": "10.1.1.0/16", "Backend": {"Type": "bridge"}}`)
  243. evtBatch := <-events
  244. if len(evtBatch) != 1 {
  245. t.Fatalf("WatchNetworks produced wrong sized event batch")
  246. }
  247. evt := evtBatch[0]
  248. if evt.Type != EventAdded {
  249. t.Fatalf("WatchNetworks produced wrong event type")
  250. }
  251. actual := evt.Network
  252. if actual != expected {
  253. t.Errorf("WatchNetworks produced wrong network: expected %s, got %s", expected, actual)
  254. }
  255. }
  256. func TestWatchNetworkRemoved(t *testing.T) {
  257. msr := newDummyRegistry(0)
  258. sm := newEtcdManager(msr)
  259. ctx, cancel := context.WithCancel(context.Background())
  260. defer cancel()
  261. events := make(chan []Event)
  262. go WatchNetworks(ctx, sm, events)
  263. // skip over the initial snapshot
  264. <-events
  265. expected := "blah"
  266. msr.CreateNetwork(ctx, expected, `{"Network": "10.1.1.0/16", "Backend": {"Type": "bridge"}}`)
  267. // skip over the create event
  268. <-events
  269. _, err := msr.DeleteNetwork(ctx, expected)
  270. if err != nil {
  271. t.Fatalf("WatchNetworks failed to delete the network")
  272. }
  273. evtBatch := <-events
  274. if len(evtBatch) != 1 {
  275. t.Fatalf("WatchNetworks produced wrong sized event batch")
  276. }
  277. evt := evtBatch[0]
  278. if evt.Type != EventRemoved {
  279. t.Fatalf("WatchNetworks produced wrong event type")
  280. }
  281. actual := evt.Network
  282. if actual != expected {
  283. t.Errorf("WatchNetwork produced wrong network: expected %s, got %s", expected, actual)
  284. }
  285. }