subnet_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package subnet
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/coreos-inc/kolach/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  9. "github.com/coreos-inc/kolach/pkg"
  10. )
  11. type mockSubnetRegistry struct {
  12. subnets *etcd.Node
  13. ch chan string
  14. index uint64
  15. }
  16. func newMockSubnetRegistry(ch chan string) *mockSubnetRegistry {
  17. subnodes := []*etcd.Node{
  18. &etcd.Node{Key: "10.3.1.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 10},
  19. &etcd.Node{Key: "10.3.2.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 11},
  20. &etcd.Node{Key: "10.3.4.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 12},
  21. &etcd.Node{Key: "10.3.5.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 13},
  22. }
  23. return &mockSubnetRegistry{
  24. subnets: &etcd.Node{
  25. Nodes: subnodes,
  26. },
  27. ch: ch,
  28. index: 14,
  29. }
  30. }
  31. func (msr *mockSubnetRegistry) getConfig() (*etcd.Response, error) {
  32. return &etcd.Response{
  33. EtcdIndex: msr.index,
  34. Node: &etcd.Node{
  35. Value: `{ "Network": "10.3.0.0/16", "FirstIP": "10.3.1.0", "LastIP": "10.3.5.0" }`,
  36. },
  37. }, nil
  38. }
  39. func (msr *mockSubnetRegistry) getSubnets() (*etcd.Response, error) {
  40. return &etcd.Response{
  41. Node: msr.subnets,
  42. EtcdIndex: msr.index,
  43. }, nil
  44. }
  45. func (msr *mockSubnetRegistry) createSubnet(sn, data string, ttl uint64) (*etcd.Response, error) {
  46. msr.index += 1
  47. // add squared durations :)
  48. exp := time.Now().Add(time.Duration(ttl) * time.Second)
  49. node := &etcd.Node{
  50. Key: sn,
  51. Value: data,
  52. ModifiedIndex: msr.index,
  53. Expiration: &exp,
  54. }
  55. msr.subnets.Nodes = append(msr.subnets.Nodes, node)
  56. return &etcd.Response{
  57. Node: node,
  58. EtcdIndex: msr.index,
  59. }, nil
  60. }
  61. func (msr *mockSubnetRegistry) updateSubnet(sn, data string, ttl uint64) (*etcd.Response, error) {
  62. msr.index += 1
  63. // add squared durations :)
  64. exp := time.Now().Add(time.Duration(ttl) * time.Second)
  65. node := &etcd.Node{
  66. Key: sn,
  67. Value: data,
  68. ModifiedIndex: msr.index,
  69. Expiration: &exp,
  70. }
  71. return &etcd.Response{
  72. Node: node,
  73. EtcdIndex: msr.index,
  74. }, nil
  75. }
  76. func (msr *mockSubnetRegistry) watchSubnets(since uint64, stop chan bool) (*etcd.Response, error) {
  77. for {
  78. var sn string
  79. select {
  80. case <-stop:
  81. return nil, nil
  82. case sn = <-msr.ch:
  83. n := etcd.Node{
  84. Key: sn,
  85. ModifiedIndex: msr.index,
  86. }
  87. msr.subnets.Nodes = append(msr.subnets.Nodes, &n)
  88. return &etcd.Response{Node: &n}, nil
  89. }
  90. }
  91. }
  92. func (msr *mockSubnetRegistry) hasSubnet(sn string) bool {
  93. for _, n := range msr.subnets.Nodes {
  94. if n.Key == sn {
  95. return true
  96. }
  97. }
  98. return false
  99. }
  100. func netIPNetToString(n *net.IPNet) string {
  101. return strings.Replace(n.String(), "/", "-", 1)
  102. }
  103. func TestAcquireLease(t *testing.T) {
  104. msr := newMockSubnetRegistry(nil)
  105. sm, err := newSubnetManager(msr)
  106. if err != nil {
  107. t.Fatalf("Failed to create subnet manager: %s", err)
  108. }
  109. ip, _ := pkg.ParseIP4("1.2.3.4")
  110. data := `{ "PublicIP": "1.2.3.4" }`
  111. sn, err := sm.AcquireLease(ip, data)
  112. if err != nil {
  113. t.Fatal("AcquireLease failed: ", err)
  114. }
  115. if sn.String() != "10.3.3.0/24" {
  116. t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", sn)
  117. }
  118. // Acquire again, should reuse
  119. if sn, err = sm.AcquireLease(ip, data); err != nil {
  120. t.Fatal("AcquireLease failed: ", err)
  121. }
  122. if sn.String() != "10.3.3.0/24" {
  123. t.Fatal("Subnet mismatch: expected 10.3.3.0/24, got: ", sn)
  124. }
  125. }
  126. func TestWatchLeases(t *testing.T) {
  127. msr := newMockSubnetRegistry(make(chan string))
  128. sm, err := newSubnetManager(msr)
  129. if err != nil {
  130. t.Fatalf("Failed to create subnet manager: %s", err)
  131. }
  132. ip, _ := pkg.ParseIP4("1.2.3.4")
  133. data := `{ "PublicIP": "1.2.3.4" }`
  134. _, err = sm.AcquireLease(ip, data)
  135. if err != nil {
  136. t.Fatalf("RegisterSubnet failed: %s", err)
  137. }
  138. events := make(chan EventBatch)
  139. sm.Start(events)
  140. <-events
  141. var expected string
  142. for i := 1; i <= 9; i++ {
  143. expected = fmt.Sprintf("10.3.%d.0-24", i)
  144. if !msr.hasSubnet(expected) {
  145. msr.ch <- expected
  146. break
  147. }
  148. }
  149. evtBatch, ok := <-events
  150. if !ok {
  151. t.Fatalf("WatchSubnets did not publish")
  152. }
  153. if len(evtBatch) != 1 {
  154. t.Fatalf("WatchSubnets produced wrong sized event batch")
  155. }
  156. evt := evtBatch[0]
  157. if evt.Type != SubnetAdded {
  158. t.Fatalf("WatchSubnets produced wrong event type")
  159. }
  160. actual := evt.Lease.Network.StringSep(".", "-")
  161. if actual != expected {
  162. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", expected, actual)
  163. }
  164. sm.Stop()
  165. }