|
@@ -46,7 +46,7 @@ func newDummyRegistry() *MockSubnetRegistry {
|
|
}
|
|
}
|
|
|
|
|
|
config := `{ "Network": "10.3.0.0/16", "SubnetMin": "10.3.1.0", "SubnetMax": "10.3.25.0" }`
|
|
config := `{ "Network": "10.3.0.0/16", "SubnetMin": "10.3.1.0", "SubnetMax": "10.3.25.0" }`
|
|
- return NewMockRegistry("_", config, subnets)
|
|
|
|
|
|
+ return NewMockRegistry(config, subnets)
|
|
}
|
|
}
|
|
|
|
|
|
func TestAcquireLease(t *testing.T) {
|
|
func TestAcquireLease(t *testing.T) {
|
|
@@ -58,7 +58,7 @@ func TestAcquireLease(t *testing.T) {
|
|
PublicIP: extIaddr,
|
|
PublicIP: extIaddr,
|
|
}
|
|
}
|
|
|
|
|
|
- l, err := sm.AcquireLease(context.Background(), "_", &attrs)
|
|
|
|
|
|
+ l, err := sm.AcquireLease(context.Background(), &attrs)
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
}
|
|
}
|
|
@@ -68,7 +68,7 @@ func TestAcquireLease(t *testing.T) {
|
|
}
|
|
}
|
|
|
|
|
|
// Acquire again, should reuse
|
|
// Acquire again, should reuse
|
|
- l2, err := sm.AcquireLease(context.Background(), "_", &attrs)
|
|
|
|
|
|
+ l2, err := sm.AcquireLease(context.Background(), &attrs)
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
}
|
|
}
|
|
@@ -87,7 +87,7 @@ func TestConfigChanged(t *testing.T) {
|
|
PublicIP: extIaddr,
|
|
PublicIP: extIaddr,
|
|
}
|
|
}
|
|
|
|
|
|
- l, err := sm.AcquireLease(context.Background(), "_", &attrs)
|
|
|
|
|
|
+ l, err := sm.AcquireLease(context.Background(), &attrs)
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
}
|
|
}
|
|
@@ -98,10 +98,10 @@ func TestConfigChanged(t *testing.T) {
|
|
|
|
|
|
// Change config
|
|
// Change config
|
|
config := `{ "Network": "10.4.0.0/16" }`
|
|
config := `{ "Network": "10.4.0.0/16" }`
|
|
- msr.setConfig("_", config)
|
|
|
|
|
|
+ msr.setConfig(config)
|
|
|
|
|
|
// Acquire again, should not reuse
|
|
// Acquire again, should not reuse
|
|
- if l, err = sm.AcquireLease(context.Background(), "_", &attrs); err != nil {
|
|
|
|
|
|
+ if l, err = sm.AcquireLease(context.Background(), &attrs); err != nil {
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -127,7 +127,7 @@ func acquireLease(ctx context.Context, t *testing.T, sm Manager) *Lease {
|
|
PublicIP: extIaddr,
|
|
PublicIP: extIaddr,
|
|
}
|
|
}
|
|
|
|
|
|
- l, err := sm.AcquireLease(ctx, "_", &attrs)
|
|
|
|
|
|
+ l, err := sm.AcquireLease(ctx, &attrs)
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
}
|
|
}
|
|
@@ -145,7 +145,7 @@ func TestWatchLeaseAdded(t *testing.T) {
|
|
l := acquireLease(ctx, t, sm)
|
|
l := acquireLease(ctx, t, sm)
|
|
|
|
|
|
events := make(chan []Event)
|
|
events := make(chan []Event)
|
|
- go WatchLeases(ctx, sm, "_", l, events)
|
|
|
|
|
|
+ go WatchLeases(ctx, sm, l, events)
|
|
|
|
|
|
evtBatch := <-events
|
|
evtBatch := <-events
|
|
for _, evt := range evtBatch {
|
|
for _, evt := range evtBatch {
|
|
@@ -167,7 +167,7 @@ func TestWatchLeaseAdded(t *testing.T) {
|
|
attrs := &LeaseAttrs{
|
|
attrs := &LeaseAttrs{
|
|
PublicIP: ip.MustParseIP4("1.1.1.1"),
|
|
PublicIP: ip.MustParseIP4("1.1.1.1"),
|
|
}
|
|
}
|
|
- _, err := msr.createSubnet(ctx, "_", expected, attrs, 0)
|
|
|
|
|
|
+ _, err := msr.createSubnet(ctx, expected, attrs, 0)
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatalf("createSubnet filed: %v", err)
|
|
t.Fatalf("createSubnet filed: %v", err)
|
|
}
|
|
}
|
|
@@ -200,7 +200,7 @@ func TestWatchLeaseRemoved(t *testing.T) {
|
|
l := acquireLease(ctx, t, sm)
|
|
l := acquireLease(ctx, t, sm)
|
|
|
|
|
|
events := make(chan []Event)
|
|
events := make(chan []Event)
|
|
- go WatchLeases(ctx, sm, "_", l, events)
|
|
|
|
|
|
+ go WatchLeases(ctx, sm, l, events)
|
|
|
|
|
|
evtBatch := <-events
|
|
evtBatch := <-events
|
|
|
|
|
|
@@ -264,7 +264,7 @@ func TestRenewLease(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
defer cancel()
|
|
|
|
|
|
- l, err := sm.AcquireLease(ctx, "_", &attrs)
|
|
|
|
|
|
+ l, err := sm.AcquireLease(ctx, &attrs)
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
t.Fatal("AcquireLease failed: ", err)
|
|
}
|
|
}
|
|
@@ -273,12 +273,12 @@ func TestRenewLease(t *testing.T) {
|
|
|
|
|
|
fakeClock.Advance(24 * time.Hour)
|
|
fakeClock.Advance(24 * time.Hour)
|
|
|
|
|
|
- if err := sm.RenewLease(ctx, "_", l); err != nil {
|
|
|
|
|
|
+ if err := sm.RenewLease(ctx, l); err != nil {
|
|
t.Fatal("RenewLease failed: ", err)
|
|
t.Fatal("RenewLease failed: ", err)
|
|
}
|
|
}
|
|
|
|
|
|
// check that it's still good
|
|
// check that it's still good
|
|
- n, err := msr.getNetwork(ctx, "_")
|
|
|
|
|
|
+ n, err := msr.getNetwork(ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Errorf("Failed to renew lease: could not get networks: %v", err)
|
|
t.Errorf("Failed to renew lease: could not get networks: %v", err)
|
|
}
|
|
}
|
|
@@ -308,11 +308,11 @@ func TestLeaseRevoked(t *testing.T) {
|
|
|
|
|
|
l := acquireLease(ctx, t, sm)
|
|
l := acquireLease(ctx, t, sm)
|
|
|
|
|
|
- if err := sm.RevokeLease(ctx, "_", l.Subnet); err != nil {
|
|
|
|
|
|
+ if err := sm.RevokeLease(ctx, l.Subnet); err != nil {
|
|
t.Fatalf("RevokeLease failed: %v", err)
|
|
t.Fatalf("RevokeLease failed: %v", err)
|
|
}
|
|
}
|
|
|
|
|
|
- _, _, err := msr.getSubnet(ctx, "_", l.Subnet)
|
|
|
|
|
|
+ _, _, err := msr.getSubnet(ctx, l.Subnet)
|
|
if err == nil {
|
|
if err == nil {
|
|
t.Fatalf("Revoked lease still exists")
|
|
t.Fatalf("Revoked lease still exists")
|
|
}
|
|
}
|
|
@@ -321,109 +321,6 @@ func TestLeaseRevoked(t *testing.T) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func TestWatchGetNetworks(t *testing.T) {
|
|
|
|
- msr := newDummyRegistry()
|
|
|
|
- sm := NewMockManager(msr)
|
|
|
|
-
|
|
|
|
- ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
- defer cancel()
|
|
|
|
-
|
|
|
|
- // Kill the previously added "_" network
|
|
|
|
- msr.DeleteNetwork(ctx, "_")
|
|
|
|
-
|
|
|
|
- expected := "foobar"
|
|
|
|
- msr.CreateNetwork(ctx, expected, `{"Network": "10.1.1.0/16", "Backend": {"Type": "bridge"}}`)
|
|
|
|
-
|
|
|
|
- resp, err := sm.WatchNetworks(ctx, nil)
|
|
|
|
- if err != nil {
|
|
|
|
- t.Errorf("WatchNetworks(nil) failed: %v", err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if len(resp.Snapshot) != 1 {
|
|
|
|
- t.Errorf("WatchNetworks(nil) produced wrong number of networks: expected 1, got %d", len(resp.Snapshot))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if resp.Snapshot[0] != expected {
|
|
|
|
- t.Errorf("WatchNetworks(nil) produced wrong network: expected %s, got %s", expected, resp.Snapshot[0])
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func TestWatchNetworkAdded(t *testing.T) {
|
|
|
|
- msr := newDummyRegistry()
|
|
|
|
- sm := NewMockManager(msr)
|
|
|
|
-
|
|
|
|
- ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
- defer cancel()
|
|
|
|
-
|
|
|
|
- events := make(chan []Event)
|
|
|
|
- go WatchNetworks(ctx, sm, events)
|
|
|
|
-
|
|
|
|
- // skip over the initial snapshot
|
|
|
|
- <-events
|
|
|
|
-
|
|
|
|
- expected := "foobar"
|
|
|
|
- msr.CreateNetwork(ctx, expected, `{"Network": "10.1.1.0/16", "Backend": {"Type": "bridge"}}`)
|
|
|
|
-
|
|
|
|
- evtBatch := <-events
|
|
|
|
-
|
|
|
|
- if len(evtBatch) != 1 {
|
|
|
|
- t.Fatalf("WatchNetworks produced wrong sized event batch")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- evt := evtBatch[0]
|
|
|
|
-
|
|
|
|
- if evt.Type != EventAdded {
|
|
|
|
- t.Fatalf("WatchNetworks produced wrong event type")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- actual := evt.Network
|
|
|
|
- if actual != expected {
|
|
|
|
- t.Errorf("WatchNetworks produced wrong network: expected %s, got %s", expected, actual)
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func TestWatchNetworkRemoved(t *testing.T) {
|
|
|
|
- msr := newDummyRegistry()
|
|
|
|
- sm := NewMockManager(msr)
|
|
|
|
-
|
|
|
|
- ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
- defer cancel()
|
|
|
|
-
|
|
|
|
- events := make(chan []Event)
|
|
|
|
- go WatchNetworks(ctx, sm, events)
|
|
|
|
-
|
|
|
|
- // skip over the initial snapshot
|
|
|
|
- <-events
|
|
|
|
-
|
|
|
|
- expected := "blah"
|
|
|
|
- msr.CreateNetwork(ctx, expected, `{"Network": "10.1.1.0/16", "Backend": {"Type": "bridge"}}`)
|
|
|
|
-
|
|
|
|
- // skip over the create event
|
|
|
|
- <-events
|
|
|
|
-
|
|
|
|
- err := msr.DeleteNetwork(ctx, expected)
|
|
|
|
- if err != nil {
|
|
|
|
- t.Fatalf("WatchNetworks failed to delete the network")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- evtBatch := <-events
|
|
|
|
-
|
|
|
|
- if len(evtBatch) != 1 {
|
|
|
|
- t.Fatalf("WatchNetworks produced wrong sized event batch")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- evt := evtBatch[0]
|
|
|
|
-
|
|
|
|
- if evt.Type != EventRemoved {
|
|
|
|
- t.Fatalf("WatchNetworks produced wrong event type")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- actual := evt.Network
|
|
|
|
- if actual != expected {
|
|
|
|
- t.Errorf("WatchNetwork produced wrong network: expected %s, got %s", expected, actual)
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func TestAddReservation(t *testing.T) {
|
|
func TestAddReservation(t *testing.T) {
|
|
msr := newDummyRegistry()
|
|
msr := newDummyRegistry()
|
|
sm := NewMockManager(msr)
|
|
sm := NewMockManager(msr)
|
|
@@ -435,31 +332,31 @@ func TestAddReservation(t *testing.T) {
|
|
Subnet: newIP4Net("10.4.3.0", 24),
|
|
Subnet: newIP4Net("10.4.3.0", 24),
|
|
PublicIP: ip.MustParseIP4("52.195.12.13"),
|
|
PublicIP: ip.MustParseIP4("52.195.12.13"),
|
|
}
|
|
}
|
|
- if err := sm.AddReservation(ctx, "_", &r); err == nil {
|
|
|
|
|
|
+ if err := sm.AddReservation(ctx, &r); err == nil {
|
|
t.Fatalf("unexpectedly added a reservation outside of configured network")
|
|
t.Fatalf("unexpectedly added a reservation outside of configured network")
|
|
}
|
|
}
|
|
|
|
|
|
r.Subnet = newIP4Net("10.3.10.0", 24)
|
|
r.Subnet = newIP4Net("10.3.10.0", 24)
|
|
- if err := sm.AddReservation(ctx, "_", &r); err != nil {
|
|
|
|
|
|
+ if err := sm.AddReservation(ctx, &r); err != nil {
|
|
t.Fatalf("failed to add reservation: %v", err)
|
|
t.Fatalf("failed to add reservation: %v", err)
|
|
}
|
|
}
|
|
|
|
|
|
// Add the same reservation -- should succeed
|
|
// Add the same reservation -- should succeed
|
|
- if err := sm.AddReservation(ctx, "_", &r); err != nil {
|
|
|
|
|
|
+ if err := sm.AddReservation(ctx, &r); err != nil {
|
|
t.Fatalf("failed to add reservation: %v", err)
|
|
t.Fatalf("failed to add reservation: %v", err)
|
|
}
|
|
}
|
|
|
|
|
|
// Add a reservation with a different public IP -- should fail
|
|
// Add a reservation with a different public IP -- should fail
|
|
r2 := r
|
|
r2 := r
|
|
r2.PublicIP = ip.MustParseIP4("52.195.12.17")
|
|
r2.PublicIP = ip.MustParseIP4("52.195.12.17")
|
|
- if err := sm.AddReservation(ctx, "_", &r2); err != ErrLeaseTaken {
|
|
|
|
|
|
+ if err := sm.AddReservation(ctx, &r2); err != ErrLeaseTaken {
|
|
t.Fatalf("taken add reservation returned: %v", err)
|
|
t.Fatalf("taken add reservation returned: %v", err)
|
|
}
|
|
}
|
|
|
|
|
|
attrs := &LeaseAttrs{
|
|
attrs := &LeaseAttrs{
|
|
PublicIP: r.PublicIP,
|
|
PublicIP: r.PublicIP,
|
|
}
|
|
}
|
|
- l, err := sm.AcquireLease(ctx, "_", attrs)
|
|
|
|
|
|
+ l, err := sm.AcquireLease(ctx, attrs)
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatalf("failed to acquire subnet: %v", err)
|
|
t.Fatalf("failed to acquire subnet: %v", err)
|
|
}
|
|
}
|
|
@@ -482,16 +379,16 @@ func TestRemoveReservation(t *testing.T) {
|
|
Subnet: newIP4Net("10.3.10.0", 24),
|
|
Subnet: newIP4Net("10.3.10.0", 24),
|
|
PublicIP: ip.MustParseIP4("52.195.12.13"),
|
|
PublicIP: ip.MustParseIP4("52.195.12.13"),
|
|
}
|
|
}
|
|
- if err := sm.AddReservation(ctx, "_", &r); err != nil {
|
|
|
|
|
|
+ if err := sm.AddReservation(ctx, &r); err != nil {
|
|
t.Fatalf("failed to add reservation: %v", err)
|
|
t.Fatalf("failed to add reservation: %v", err)
|
|
}
|
|
}
|
|
|
|
|
|
- if err := sm.RemoveReservation(ctx, "_", r.Subnet); err != nil {
|
|
|
|
|
|
+ if err := sm.RemoveReservation(ctx, r.Subnet); err != nil {
|
|
t.Fatalf("failed to remove reservation: %v", err)
|
|
t.Fatalf("failed to remove reservation: %v", err)
|
|
}
|
|
}
|
|
|
|
|
|
// The node should have a TTL
|
|
// The node should have a TTL
|
|
- sub, _, err := msr.getSubnet(ctx, "_", r.Subnet)
|
|
|
|
|
|
+ sub, _, err := msr.getSubnet(ctx, r.Subnet)
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatalf("getSubnet failed: %v", err)
|
|
t.Fatalf("getSubnet failed: %v", err)
|
|
}
|
|
}
|
|
@@ -512,7 +409,7 @@ func TestListReservations(t *testing.T) {
|
|
Subnet: newIP4Net("10.3.10.0", 24),
|
|
Subnet: newIP4Net("10.3.10.0", 24),
|
|
PublicIP: ip.MustParseIP4("52.195.12.13"),
|
|
PublicIP: ip.MustParseIP4("52.195.12.13"),
|
|
}
|
|
}
|
|
- if err := sm.AddReservation(ctx, "_", &r1); err != nil {
|
|
|
|
|
|
+ if err := sm.AddReservation(ctx, &r1); err != nil {
|
|
t.Fatalf("failed to add reservation: %v", err)
|
|
t.Fatalf("failed to add reservation: %v", err)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -520,11 +417,11 @@ func TestListReservations(t *testing.T) {
|
|
Subnet: newIP4Net("10.3.20.0", 24),
|
|
Subnet: newIP4Net("10.3.20.0", 24),
|
|
PublicIP: ip.MustParseIP4("52.195.12.14"),
|
|
PublicIP: ip.MustParseIP4("52.195.12.14"),
|
|
}
|
|
}
|
|
- if err := sm.AddReservation(ctx, "_", &r2); err != nil {
|
|
|
|
|
|
+ if err := sm.AddReservation(ctx, &r2); err != nil {
|
|
t.Fatalf("failed to add reservation: %v", err)
|
|
t.Fatalf("failed to add reservation: %v", err)
|
|
}
|
|
}
|
|
|
|
|
|
- rs, err := sm.ListReservations(ctx, "_")
|
|
|
|
|
|
+ rs, err := sm.ListReservations(ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
if len(rs) != 2 {
|
|
if len(rs) != 2 {
|
|
t.Fatalf("unexpected number of reservations, expected 2, got %v", len(rs))
|
|
t.Fatalf("unexpected number of reservations, expected 2, got %v", len(rs))
|
|
@@ -539,7 +436,7 @@ func TestListReservations(t *testing.T) {
|
|
}
|
|
}
|
|
|
|
|
|
func inAllocatableRange(ctx context.Context, sm Manager, ipn ip.IP4Net) bool {
|
|
func inAllocatableRange(ctx context.Context, sm Manager, ipn ip.IP4Net) bool {
|
|
- cfg, err := sm.GetNetworkConfig(ctx, "_")
|
|
|
|
|
|
+ cfg, err := sm.GetNetworkConfig(ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
panic(err)
|
|
panic(err)
|
|
}
|
|
}
|