|
@@ -27,141 +27,20 @@ import (
|
|
"github.com/coreos/flannel/pkg/ip"
|
|
"github.com/coreos/flannel/pkg/ip"
|
|
)
|
|
)
|
|
|
|
|
|
-type mockSubnetRegistry struct {
|
|
|
|
- subnets *etcd.Node
|
|
|
|
- addCh chan string
|
|
|
|
- delCh chan string
|
|
|
|
- index uint64
|
|
|
|
- ttl uint64
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func newMockSubnetRegistry(ttlOverride uint64) *mockSubnetRegistry {
|
|
|
|
- subnodes := []*etcd.Node{
|
|
|
|
|
|
+func newDummyRegistry(ttlOverride uint64) *mockSubnetRegistry {
|
|
|
|
+ subnets := []*etcd.Node{
|
|
&etcd.Node{Key: "10.3.1.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 10},
|
|
&etcd.Node{Key: "10.3.1.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 10},
|
|
&etcd.Node{Key: "10.3.2.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 11},
|
|
&etcd.Node{Key: "10.3.2.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 11},
|
|
&etcd.Node{Key: "10.3.4.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 12},
|
|
&etcd.Node{Key: "10.3.4.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 12},
|
|
&etcd.Node{Key: "10.3.5.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 13},
|
|
&etcd.Node{Key: "10.3.5.0-24", Value: `{ "PublicIP": "1.1.1.1" }`, ModifiedIndex: 13},
|
|
}
|
|
}
|
|
|
|
|
|
- return &mockSubnetRegistry{
|
|
|
|
- subnets: &etcd.Node{
|
|
|
|
- Nodes: subnodes,
|
|
|
|
- },
|
|
|
|
- addCh: make(chan string),
|
|
|
|
- delCh: make(chan string),
|
|
|
|
- index: 14,
|
|
|
|
- ttl: ttlOverride,
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (msr *mockSubnetRegistry) getConfig(ctx context.Context, network string) (*etcd.Response, error) {
|
|
|
|
- return &etcd.Response{
|
|
|
|
- EtcdIndex: msr.index,
|
|
|
|
- Node: &etcd.Node{
|
|
|
|
- Value: `{ "Network": "10.3.0.0/16", "SubnetMin": "10.3.1.0", "SubnetMax": "10.3.5.0" }`,
|
|
|
|
- },
|
|
|
|
- }, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (msr *mockSubnetRegistry) getSubnets(ctx context.Context, network string) (*etcd.Response, error) {
|
|
|
|
- return &etcd.Response{
|
|
|
|
- Node: msr.subnets,
|
|
|
|
- EtcdIndex: msr.index,
|
|
|
|
- }, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (msr *mockSubnetRegistry) createSubnet(ctx context.Context, network, sn, data string, ttl uint64) (*etcd.Response, error) {
|
|
|
|
- msr.index += 1
|
|
|
|
-
|
|
|
|
- if msr.ttl > 0 {
|
|
|
|
- ttl = msr.ttl
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // add squared durations :)
|
|
|
|
- exp := time.Now().Add(time.Duration(ttl) * time.Second)
|
|
|
|
-
|
|
|
|
- node := &etcd.Node{
|
|
|
|
- Key: sn,
|
|
|
|
- Value: data,
|
|
|
|
- ModifiedIndex: msr.index,
|
|
|
|
- Expiration: &exp,
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- msr.subnets.Nodes = append(msr.subnets.Nodes, node)
|
|
|
|
-
|
|
|
|
- return &etcd.Response{
|
|
|
|
- Node: node,
|
|
|
|
- EtcdIndex: msr.index,
|
|
|
|
- }, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (msr *mockSubnetRegistry) updateSubnet(ctx context.Context, network, sn, data string, ttl uint64) (*etcd.Response, error) {
|
|
|
|
- msr.index += 1
|
|
|
|
-
|
|
|
|
- // add squared durations :)
|
|
|
|
- exp := time.Now().Add(time.Duration(ttl) * time.Second)
|
|
|
|
-
|
|
|
|
- for _, n := range msr.subnets.Nodes {
|
|
|
|
- if n.Key == sn {
|
|
|
|
- n.Value = data
|
|
|
|
- n.ModifiedIndex = msr.index
|
|
|
|
- n.Expiration = &exp
|
|
|
|
-
|
|
|
|
- return &etcd.Response{
|
|
|
|
- Node: n,
|
|
|
|
- EtcdIndex: msr.index,
|
|
|
|
- }, nil
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return nil, fmt.Errorf("Subnet not found")
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (msr *mockSubnetRegistry) watchSubnets(ctx context.Context, network string, since uint64) (*etcd.Response, error) {
|
|
|
|
- var sn string
|
|
|
|
-
|
|
|
|
- select {
|
|
|
|
- case <-ctx.Done():
|
|
|
|
- return nil, ctx.Err()
|
|
|
|
-
|
|
|
|
- case sn = <-msr.addCh:
|
|
|
|
- n := etcd.Node{
|
|
|
|
- Key: sn,
|
|
|
|
- Value: `{"PublicIP": "1.1.1.1"}`,
|
|
|
|
- ModifiedIndex: msr.index,
|
|
|
|
- }
|
|
|
|
- msr.subnets.Nodes = append(msr.subnets.Nodes, &n)
|
|
|
|
- return &etcd.Response{
|
|
|
|
- Action: "add",
|
|
|
|
- Node: &n,
|
|
|
|
- }, nil
|
|
|
|
-
|
|
|
|
- case sn = <-msr.delCh:
|
|
|
|
- for i, n := range msr.subnets.Nodes {
|
|
|
|
- if n.Key == sn {
|
|
|
|
- msr.subnets.Nodes[i] = msr.subnets.Nodes[len(msr.subnets.Nodes)-1]
|
|
|
|
- msr.subnets.Nodes = msr.subnets.Nodes[:len(msr.subnets.Nodes)-2]
|
|
|
|
- return &etcd.Response{
|
|
|
|
- Action: "expire",
|
|
|
|
- Node: n,
|
|
|
|
- }, nil
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return nil, fmt.Errorf("Subnet (%s) to delete was not found: ", sn)
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (msr *mockSubnetRegistry) hasSubnet(sn string) bool {
|
|
|
|
- for _, n := range msr.subnets.Nodes {
|
|
|
|
- if n.Key == sn {
|
|
|
|
- return true
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return false
|
|
|
|
|
|
+ config := `{ "Network": "10.3.0.0/16", "SubnetMin": "10.3.1.0", "SubnetMax": "10.3.5.0" }`
|
|
|
|
+ return newMockRegistry(ttlOverride, config, subnets)
|
|
}
|
|
}
|
|
|
|
|
|
func TestAcquireLease(t *testing.T) {
|
|
func TestAcquireLease(t *testing.T) {
|
|
- msr := newMockSubnetRegistry(0)
|
|
|
|
|
|
+ msr := newDummyRegistry(0)
|
|
sm := newEtcdManager(msr)
|
|
sm := newEtcdManager(msr)
|
|
|
|
|
|
extIP, _ := ip.ParseIP4("1.2.3.4")
|
|
extIP, _ := ip.ParseIP4("1.2.3.4")
|
|
@@ -189,7 +68,7 @@ func TestAcquireLease(t *testing.T) {
|
|
}
|
|
}
|
|
|
|
|
|
func TestWatchLeaseAdded(t *testing.T) {
|
|
func TestWatchLeaseAdded(t *testing.T) {
|
|
- msr := newMockSubnetRegistry(0)
|
|
|
|
|
|
+ msr := newDummyRegistry(0)
|
|
sm := newEtcdManager(msr)
|
|
sm := newEtcdManager(msr)
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
@@ -199,7 +78,7 @@ func TestWatchLeaseAdded(t *testing.T) {
|
|
go WatchLeases(ctx, sm, "", events)
|
|
go WatchLeases(ctx, sm, "", events)
|
|
|
|
|
|
expected := "10.3.3.0-24"
|
|
expected := "10.3.3.0-24"
|
|
- msr.addCh <- expected
|
|
|
|
|
|
+ msr.createSubnet(ctx, "_", expected, `{"PublicIP": "1.1.1.1"}`, 0)
|
|
|
|
|
|
evtBatch, ok := <-events
|
|
evtBatch, ok := <-events
|
|
if !ok {
|
|
if !ok {
|
|
@@ -223,7 +102,7 @@ func TestWatchLeaseAdded(t *testing.T) {
|
|
}
|
|
}
|
|
|
|
|
|
func TestWatchLeaseRemoved(t *testing.T) {
|
|
func TestWatchLeaseRemoved(t *testing.T) {
|
|
- msr := newMockSubnetRegistry(0)
|
|
|
|
|
|
+ msr := newDummyRegistry(0)
|
|
sm := newEtcdManager(msr)
|
|
sm := newEtcdManager(msr)
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
@@ -233,7 +112,7 @@ func TestWatchLeaseRemoved(t *testing.T) {
|
|
go WatchLeases(ctx, sm, "", events)
|
|
go WatchLeases(ctx, sm, "", events)
|
|
|
|
|
|
expected := "10.3.4.0-24"
|
|
expected := "10.3.4.0-24"
|
|
- msr.delCh <- expected
|
|
|
|
|
|
+ msr.expireSubnet(expected)
|
|
|
|
|
|
evtBatch, ok := <-events
|
|
evtBatch, ok := <-events
|
|
if !ok {
|
|
if !ok {
|
|
@@ -261,7 +140,7 @@ type leaseData struct {
|
|
}
|
|
}
|
|
|
|
|
|
func TestRenewLease(t *testing.T) {
|
|
func TestRenewLease(t *testing.T) {
|
|
- msr := newMockSubnetRegistry(1)
|
|
|
|
|
|
+ msr := newDummyRegistry(1)
|
|
sm := newEtcdManager(msr)
|
|
sm := newEtcdManager(msr)
|
|
|
|
|
|
// Create LeaseAttrs
|
|
// Create LeaseAttrs
|