subnet_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  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 uint64) *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 TestWatchLeaseAdded(t *testing.T) {
  94. msr := newDummyRegistry(0)
  95. sm := newEtcdManager(msr)
  96. ctx, cancel := context.WithCancel(context.Background())
  97. defer cancel()
  98. events := make(chan []Event)
  99. go WatchLeases(ctx, sm, "", events)
  100. // skip over the initial snapshot
  101. <-events
  102. expected := "10.3.3.0-24"
  103. msr.createSubnet(ctx, "_", expected, `{"PublicIP": "1.1.1.1"}`, 0)
  104. evtBatch := <-events
  105. if len(evtBatch) != 1 {
  106. t.Fatalf("WatchSubnets produced wrong sized event batch")
  107. }
  108. evt := evtBatch[0]
  109. if evt.Type != SubnetAdded {
  110. t.Fatalf("WatchSubnets produced wrong event type")
  111. }
  112. actual := evt.Lease.Key()
  113. if actual != expected {
  114. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", expected, actual)
  115. }
  116. }
  117. func TestWatchLeaseRemoved(t *testing.T) {
  118. msr := newDummyRegistry(0)
  119. sm := newEtcdManager(msr)
  120. ctx, cancel := context.WithCancel(context.Background())
  121. defer cancel()
  122. events := make(chan []Event)
  123. go WatchLeases(ctx, sm, "", events)
  124. // skip over the initial snapshot
  125. <-events
  126. expected := "10.3.4.0-24"
  127. msr.expireSubnet(expected)
  128. evtBatch := <-events
  129. if len(evtBatch) != 1 {
  130. t.Fatalf("WatchSubnets produced wrong sized event batch")
  131. }
  132. evt := evtBatch[0]
  133. if evt.Type != SubnetRemoved {
  134. t.Fatalf("WatchSubnets produced wrong event type")
  135. }
  136. actual := evt.Lease.Key()
  137. if actual != expected {
  138. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", expected, actual)
  139. }
  140. }
  141. type leaseData struct {
  142. Dummy string
  143. }
  144. func TestRenewLease(t *testing.T) {
  145. msr := newDummyRegistry(1)
  146. sm := newEtcdManager(msr)
  147. // Create LeaseAttrs
  148. extIaddr, _ := ip.ParseIP4("1.2.3.4")
  149. attrs := LeaseAttrs{
  150. PublicIP: extIaddr,
  151. BackendType: "vxlan",
  152. }
  153. ld, err := json.Marshal(&leaseData{Dummy: "test string"})
  154. if err != nil {
  155. t.Fatalf("Failed to marshal leaseData: %v", err)
  156. }
  157. attrs.BackendData = json.RawMessage(ld)
  158. // Acquire lease
  159. ctx, cancel := context.WithCancel(context.Background())
  160. defer cancel()
  161. l, err := sm.AcquireLease(ctx, "", &attrs)
  162. if err != nil {
  163. t.Fatal("AcquireLease failed: ", err)
  164. }
  165. go LeaseRenewer(ctx, sm, "", l)
  166. fmt.Println("Waiting for lease to pass original expiration")
  167. time.Sleep(2 * time.Second)
  168. // check that it's still good
  169. for _, n := range msr.subnets.Nodes {
  170. if n.Key == l.Subnet.StringSep(".", "-") {
  171. if n.Expiration.Before(time.Now()) {
  172. t.Error("Failed to renew lease: expiration did not advance")
  173. }
  174. a := LeaseAttrs{}
  175. if err := json.Unmarshal([]byte(n.Value), &a); err != nil {
  176. t.Errorf("Failed to JSON-decode LeaseAttrs: %v", err)
  177. return
  178. }
  179. if !reflect.DeepEqual(a, attrs) {
  180. t.Errorf("LeaseAttrs changed: was %#v, now %#v", attrs, a)
  181. }
  182. return
  183. }
  184. }
  185. t.Fatalf("Failed to find acquired lease")
  186. }