remote_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. events := make(chan []subnet.Event)
  82. go subnet.WatchLeases(ctx, sm, "_", events)
  83. // skip over the initial snapshot
  84. <-events
  85. attrs := &subnet.LeaseAttrs{
  86. PublicIP: mustParseIP4("1.1.1.2"),
  87. }
  88. l, err := sm.AcquireLease(ctx, "_", attrs)
  89. if err != nil {
  90. t.Errorf("AcquireLease failed: %v", err)
  91. return
  92. }
  93. if !mustParseIP4Net(expectedNetwork).Contains(l.Subnet.IP) {
  94. t.Errorf("AcquireLease returned subnet not in network: %v (in %v)", l.Subnet, expectedNetwork)
  95. }
  96. evtBatch := <-events
  97. if len(evtBatch) != 1 {
  98. t.Fatalf("WatchSubnets produced wrong sized event batch")
  99. }
  100. evt := evtBatch[0]
  101. if evt.Type != subnet.SubnetAdded {
  102. t.Fatalf("WatchSubnets produced wrong event type")
  103. }
  104. if evt.Lease.Key() != l.Key() {
  105. t.Errorf("WatchSubnet produced wrong subnet: expected %s, got %s", l.Key(), evt.Lease.Key())
  106. }
  107. }