subnet_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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(0)
  37. sm := newEtcdManager(msr)
  38. extIP, _ := ip.ParseIP4("1.2.3.4")
  39. attrs := LeaseAttrs{
  40. PublicIP: extIP,
  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 TestWatchLeaseAdded(t *testing.T) {
  58. msr := newDummyRegistry(0)
  59. sm := newEtcdManager(msr)
  60. ctx, cancel := context.WithCancel(context.Background())
  61. defer cancel()
  62. events := make(chan []Event)
  63. go WatchLeases(ctx, sm, "", events)
  64. // skip over the initial snapshot
  65. <-events
  66. expected := "10.3.3.0-24"
  67. msr.createSubnet(ctx, "_", expected, `{"PublicIP": "1.1.1.1"}`, 0)
  68. evtBatch := <-events
  69. if len(evtBatch) != 1 {
  70. t.Fatalf("WatchSubnets produced wrong sized event batch")
  71. }
  72. evt := evtBatch[0]
  73. if evt.Type != SubnetAdded {
  74. t.Fatalf("WatchSubnets produced wrong event type")
  75. }
  76. actual := evt.Lease.Key()
  77. if actual != expected {
  78. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", expected, actual)
  79. }
  80. }
  81. func TestWatchLeaseRemoved(t *testing.T) {
  82. msr := newDummyRegistry(0)
  83. sm := newEtcdManager(msr)
  84. ctx, cancel := context.WithCancel(context.Background())
  85. defer cancel()
  86. events := make(chan []Event)
  87. go WatchLeases(ctx, sm, "", events)
  88. // skip over the initial snapshot
  89. <-events
  90. expected := "10.3.4.0-24"
  91. msr.expireSubnet(expected)
  92. evtBatch := <-events
  93. if len(evtBatch) != 1 {
  94. t.Fatalf("WatchSubnets produced wrong sized event batch")
  95. }
  96. evt := evtBatch[0]
  97. if evt.Type != SubnetRemoved {
  98. t.Fatalf("WatchSubnets produced wrong event type")
  99. }
  100. actual := evt.Lease.Key()
  101. if actual != expected {
  102. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", expected, actual)
  103. }
  104. }
  105. type leaseData struct {
  106. Dummy string
  107. }
  108. func TestRenewLease(t *testing.T) {
  109. msr := newDummyRegistry(1)
  110. sm := newEtcdManager(msr)
  111. // Create LeaseAttrs
  112. extIP, _ := ip.ParseIP4("1.2.3.4")
  113. attrs := LeaseAttrs{
  114. PublicIP: extIP,
  115. BackendType: "vxlan",
  116. }
  117. ld, err := json.Marshal(&leaseData{Dummy: "test string"})
  118. if err != nil {
  119. t.Fatalf("Failed to marshal leaseData: %v", err)
  120. }
  121. attrs.BackendData = json.RawMessage(ld)
  122. // Acquire lease
  123. ctx, cancel := context.WithCancel(context.Background())
  124. defer cancel()
  125. l, err := sm.AcquireLease(ctx, "", &attrs)
  126. if err != nil {
  127. t.Fatal("AcquireLease failed: ", err)
  128. }
  129. go LeaseRenewer(ctx, sm, "", l)
  130. fmt.Println("Waiting for lease to pass original expiration")
  131. time.Sleep(2 * time.Second)
  132. // check that it's still good
  133. for _, n := range msr.subnets.Nodes {
  134. if n.Key == l.Subnet.StringSep(".", "-") {
  135. if n.Expiration.Before(time.Now()) {
  136. t.Error("Failed to renew lease: expiration did not advance")
  137. }
  138. a := LeaseAttrs{}
  139. if err := json.Unmarshal([]byte(n.Value), &a); err != nil {
  140. t.Errorf("Failed to JSON-decode LeaseAttrs: %v", err)
  141. return
  142. }
  143. if !reflect.DeepEqual(a, attrs) {
  144. t.Errorf("LeaseAttrs changed: was %#v, now %#v", attrs, a)
  145. }
  146. return
  147. }
  148. }
  149. t.Fatalf("Failed to find acquired lease")
  150. }