remote_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. "sync"
  19. "testing"
  20. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  21. "github.com/coreos/flannel/pkg/ip"
  22. "github.com/coreos/flannel/subnet"
  23. )
  24. const expectedNetwork = "10.1.0.0/16"
  25. func TestRemote(t *testing.T) {
  26. config := fmt.Sprintf(`{"Network": %q}`, expectedNetwork)
  27. sm := subnet.NewMockManager(1, config)
  28. addr := "127.0.0.1:9999"
  29. ctx, cancel := context.WithCancel(context.Background())
  30. wg := sync.WaitGroup{}
  31. wg.Add(1)
  32. go func() {
  33. RunServer(ctx, sm, addr)
  34. wg.Done()
  35. }()
  36. doTestRemote(ctx, t, addr)
  37. cancel()
  38. wg.Wait()
  39. }
  40. func mustParseIP4(s string) ip.IP4 {
  41. a, err := ip.ParseIP4(s)
  42. if err != nil {
  43. panic(err)
  44. }
  45. return a
  46. }
  47. func mustParseIP4Net(s string) ip.IP4Net {
  48. _, n, err := net.ParseCIDR(s)
  49. if err != nil {
  50. panic(err)
  51. }
  52. return ip.FromIPNet(n)
  53. }
  54. func doTestRemote(ctx context.Context, t *testing.T, remoteAddr string) {
  55. sm := NewRemoteManager(remoteAddr)
  56. cfg, err := sm.GetNetworkConfig(ctx, "_")
  57. if err != nil {
  58. t.Errorf("GetNetworkConfig failed: %v", err)
  59. }
  60. if cfg.Network.String() != expectedNetwork {
  61. t.Errorf("GetNetworkConfig returned bad network: %v vs %v", cfg.Network, expectedNetwork)
  62. }
  63. attrs := &subnet.LeaseAttrs{
  64. PublicIP: mustParseIP4("1.1.1.1"),
  65. }
  66. l, err := sm.AcquireLease(ctx, "_", attrs)
  67. if err != nil {
  68. t.Errorf("AcquireLease failed: %v", err)
  69. }
  70. if !mustParseIP4Net(expectedNetwork).Contains(l.Subnet.IP) {
  71. t.Errorf("AcquireLease returned subnet not in network: %v (in %v)", l.Subnet, expectedNetwork)
  72. }
  73. if err = sm.RenewLease(ctx, "_", l); err != nil {
  74. t.Errorf("RenewLease failed: %v", err)
  75. }
  76. doTestWatch(t, sm)
  77. }
  78. func doTestWatch(t *testing.T, sm subnet.Manager) {
  79. ctx, cancel := context.WithCancel(context.Background())
  80. defer cancel()
  81. res := make(chan error)
  82. barrier := make(chan struct{})
  83. sm.WatchLeases(ctx, "_", nil)
  84. var expectedSubnet ip.IP4Net
  85. go func() {
  86. wr, err := sm.WatchLeases(ctx, "_", nil)
  87. if err != nil {
  88. res <- fmt.Errorf("WatchLeases failed: %v", err)
  89. return
  90. }
  91. if len(wr.Events) > 0 && len(wr.Snapshot) > 0 {
  92. res <- fmt.Errorf("WatchLeases returned events and snapshots")
  93. return
  94. }
  95. res <- nil
  96. <-barrier
  97. wr, err = sm.WatchLeases(ctx, "_", wr.Cursor)
  98. if err != nil {
  99. res <- fmt.Errorf("WatchLeases failed: %v", err)
  100. return
  101. }
  102. if len(wr.Events) == 0 {
  103. res <- fmt.Errorf("WatchLeases returned empty events")
  104. return
  105. }
  106. if wr.Events[0].Type != subnet.SubnetAdded {
  107. res <- fmt.Errorf("WatchLeases returned event with wrong EventType: %v vs %v", wr.Events[0].Type, subnet.SubnetAdded)
  108. return
  109. }
  110. if !wr.Events[0].Lease.Subnet.Equal(expectedSubnet) {
  111. res <- fmt.Errorf("WatchLeases returned unexpected subnet: %v vs %v", wr.Events[0].Lease.Subnet, expectedSubnet)
  112. }
  113. res <- nil
  114. }()
  115. if err := <-res; err != nil {
  116. t.Fatal(err.Error())
  117. }
  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. expectedSubnet = l.Subnet
  130. barrier <- struct{}{}
  131. if err := <-res; err != nil {
  132. t.Fatal(err.Error())
  133. }
  134. }