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