remote_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 := NewRemoteManager(remoteAddr)
  67. for i := 0; ; i++ {
  68. cfg, err := sm.GetNetworkConfig(ctx, "_")
  69. if err != nil {
  70. if isConnRefused(err) {
  71. if i == 100 {
  72. t.Fatalf("Out of connection retries")
  73. }
  74. fmt.Println("Connection refused, retrying...")
  75. time.Sleep(300 * time.Millisecond)
  76. continue
  77. }
  78. t.Fatalf("GetNetworkConfig failed: %v", err)
  79. }
  80. if cfg.Network.String() != expectedNetwork {
  81. t.Errorf("GetNetworkConfig returned bad network: %v vs %v", cfg.Network, expectedNetwork)
  82. }
  83. break
  84. }
  85. attrs := &subnet.LeaseAttrs{
  86. PublicIP: mustParseIP4("1.1.1.1"),
  87. }
  88. l, err := sm.AcquireLease(ctx, "_", attrs)
  89. if err != nil {
  90. t.Fatalf("AcquireLease failed: %v", err)
  91. }
  92. if !mustParseIP4Net(expectedNetwork).Contains(l.Subnet.IP) {
  93. t.Errorf("AcquireLease returned subnet not in network: %v (in %v)", l.Subnet, expectedNetwork)
  94. }
  95. if err = sm.RenewLease(ctx, "_", l); err != nil {
  96. t.Errorf("RenewLease failed: %v", err)
  97. }
  98. doTestWatch(t, sm)
  99. }
  100. func doTestWatch(t *testing.T, sm subnet.Manager) {
  101. ctx, cancel := context.WithCancel(context.Background())
  102. defer cancel()
  103. events := make(chan []subnet.Event)
  104. go subnet.WatchLeases(ctx, sm, "_", events)
  105. // skip over the initial snapshot
  106. <-events
  107. attrs := &subnet.LeaseAttrs{
  108. PublicIP: mustParseIP4("1.1.1.2"),
  109. }
  110. l, err := sm.AcquireLease(ctx, "_", attrs)
  111. if err != nil {
  112. t.Errorf("AcquireLease failed: %v", err)
  113. return
  114. }
  115. if !mustParseIP4Net(expectedNetwork).Contains(l.Subnet.IP) {
  116. t.Errorf("AcquireLease returned subnet not in network: %v (in %v)", l.Subnet, expectedNetwork)
  117. }
  118. evtBatch := <-events
  119. if len(evtBatch) != 1 {
  120. t.Fatalf("WatchSubnets produced wrong sized event batch")
  121. }
  122. evt := evtBatch[0]
  123. if evt.Type != subnet.SubnetAdded {
  124. t.Fatalf("WatchSubnets produced wrong event type")
  125. }
  126. if evt.Lease.Key() != l.Key() {
  127. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", l.Key(), evt.Lease.Key())
  128. }
  129. }