remote_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 remote
  15. import (
  16. "fmt"
  17. "net"
  18. "net/url"
  19. "sync"
  20. "syscall"
  21. "testing"
  22. "time"
  23. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  24. "github.com/coreos/flannel/pkg/ip"
  25. "github.com/coreos/flannel/subnet"
  26. )
  27. const expectedNetwork = "10.1.0.0/16"
  28. func TestRemote(t *testing.T) {
  29. config := fmt.Sprintf(`{"Network": %q}`, expectedNetwork)
  30. sm := subnet.NewMockManager(1, config)
  31. addr := "127.0.0.1:9999"
  32. ctx, cancel := context.WithCancel(context.Background())
  33. wg := sync.WaitGroup{}
  34. wg.Add(1)
  35. go func() {
  36. RunServer(ctx, sm, addr, "", "", "")
  37. wg.Done()
  38. }()
  39. doTestRemote(ctx, t, addr)
  40. cancel()
  41. wg.Wait()
  42. }
  43. func mustParseIP4(s string) ip.IP4 {
  44. a, err := ip.ParseIP4(s)
  45. if err != nil {
  46. panic(err)
  47. }
  48. return a
  49. }
  50. func mustParseIP4Net(s string) ip.IP4Net {
  51. _, n, err := net.ParseCIDR(s)
  52. if err != nil {
  53. panic(err)
  54. }
  55. return ip.FromIPNet(n)
  56. }
  57. func isConnRefused(err error) bool {
  58. if uerr, ok := err.(*url.Error); ok {
  59. if operr, ok := uerr.Err.(*net.OpError); ok {
  60. return operr.Err == syscall.ECONNREFUSED
  61. }
  62. }
  63. return false
  64. }
  65. func doTestRemote(ctx context.Context, t *testing.T, remoteAddr string) {
  66. sm, err := NewRemoteManager(remoteAddr, "", "", "")
  67. if err != nil {
  68. t.Fatalf("Failed to create remote mananager: %v", err)
  69. }
  70. for i := 0; ; i++ {
  71. cfg, err := sm.GetNetworkConfig(ctx, "_")
  72. if err != nil {
  73. if isConnRefused(err) {
  74. if i == 100 {
  75. t.Fatalf("Out of connection retries")
  76. }
  77. fmt.Println("Connection refused, retrying...")
  78. time.Sleep(300 * time.Millisecond)
  79. continue
  80. }
  81. t.Fatalf("GetNetworkConfig failed: %v", err)
  82. }
  83. if cfg.Network.String() != expectedNetwork {
  84. t.Errorf("GetNetworkConfig returned bad network: %v vs %v", cfg.Network, expectedNetwork)
  85. }
  86. break
  87. }
  88. attrs := &subnet.LeaseAttrs{
  89. PublicIP: mustParseIP4("1.1.1.1"),
  90. }
  91. l, err := sm.AcquireLease(ctx, "_", attrs)
  92. if err != nil {
  93. t.Fatalf("AcquireLease failed: %v", err)
  94. }
  95. if !mustParseIP4Net(expectedNetwork).Contains(l.Subnet.IP) {
  96. t.Errorf("AcquireLease returned subnet not in network: %v (in %v)", l.Subnet, expectedNetwork)
  97. }
  98. if err = sm.RenewLease(ctx, "_", l); err != nil {
  99. t.Errorf("RenewLease failed: %v", err)
  100. }
  101. doTestWatch(t, sm)
  102. }
  103. func doTestWatch(t *testing.T, sm subnet.Manager) {
  104. ctx, cancel := context.WithCancel(context.Background())
  105. wg := sync.WaitGroup{}
  106. wg.Add(1)
  107. defer func() {
  108. cancel()
  109. wg.Wait()
  110. }()
  111. events := make(chan []subnet.Event)
  112. go func() {
  113. subnet.WatchLeases(ctx, sm, "_", events)
  114. wg.Done()
  115. }()
  116. // skip over the initial snapshot
  117. <-events
  118. attrs := &subnet.LeaseAttrs{
  119. PublicIP: mustParseIP4("1.1.1.2"),
  120. }
  121. l, err := sm.AcquireLease(ctx, "_", attrs)
  122. if err != nil {
  123. t.Errorf("AcquireLease failed: %v", err)
  124. return
  125. }
  126. if !mustParseIP4Net(expectedNetwork).Contains(l.Subnet.IP) {
  127. t.Errorf("AcquireLease returned subnet not in network: %v (in %v)", l.Subnet, expectedNetwork)
  128. }
  129. evtBatch := <-events
  130. if len(evtBatch) != 1 {
  131. t.Fatalf("WatchSubnets produced wrong sized event batch")
  132. }
  133. evt := evtBatch[0]
  134. if evt.Type != subnet.SubnetAdded {
  135. t.Fatalf("WatchSubnets produced wrong event type")
  136. }
  137. if evt.Lease.Key() != l.Key() {
  138. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", l.Key(), evt.Lease.Key())
  139. }
  140. }