From 4307ec283b817997bdcf989767a99d57f7361b9f Mon Sep 17 00:00:00 2001 From: Christian Simon Date: Tue, 17 Mar 2015 22:45:10 +0100 Subject: [PATCH] [WiP] Adds testing for bridge's IPv6 support * fixes #10001 * test for global subnets <= 80 * test for global subnets > 80 * test link local allocations * test duplicated addresses * test regression from bug #11427 Signed-off-by: Christian Simon --- daemon/networkdriver/bridge/driver_test.go | 118 +++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/daemon/networkdriver/bridge/driver_test.go b/daemon/networkdriver/bridge/driver_test.go index 02bea9ce13..8c20dffb85 100644 --- a/daemon/networkdriver/bridge/driver_test.go +++ b/daemon/networkdriver/bridge/driver_test.go @@ -1,6 +1,7 @@ package bridge import ( + "fmt" "net" "strconv" "testing" @@ -104,6 +105,123 @@ func TestHostnameFormatChecking(t *testing.T) { } } +func newInterfaceAllocation(t *testing.T, input engine.Env) (output engine.Env) { + eng := engine.New() + eng.Logging = false + + done := make(chan bool) + + // set IPv6 global if given + if input.Exists("globalIPv6Network") { + _, globalIPv6Network, _ = net.ParseCIDR(input.Get("globalIPv6Network")) + } + + job := eng.Job("allocate_interface", "container_id") + job.Env().Init(&input) + reader, _ := job.Stdout.AddPipe() + go func() { + output.Decode(reader) + done <- true + }() + + res := Allocate(job) + job.Stdout.Close() + <-done + + if input.Exists("expectFail") && input.GetBool("expectFail") { + if res == engine.StatusOK { + t.Fatal("Doesn't fail to allocate network interface") + } + } else { + if res != engine.StatusOK { + t.Fatal("Failed to allocate network interface") + } + } + + if input.Exists("globalIPv6Network") { + // check for bug #11427 + _, subnet, _ := net.ParseCIDR(input.Get("globalIPv6Network")) + if globalIPv6Network.IP.String() != subnet.IP.String() { + t.Fatal("globalIPv6Network was modified during allocation") + } + // clean up IPv6 global + globalIPv6Network = nil + } + + return +} + +func TestIPv6InterfaceAllocationAutoNetmaskGt80(t *testing.T) { + + input := engine.Env{} + + _, subnet, _ := net.ParseCIDR("2001:db8:1234:1234:1234::/81") + + // set global ipv6 + input.Set("globalIPv6Network", subnet.String()) + + output := newInterfaceAllocation(t, input) + + // ensure low manually assigend global ip + ip := net.ParseIP(output.Get("GlobalIPv6")) + _, subnet, _ = net.ParseCIDR(fmt.Sprintf("%s/%d", subnet.IP.String(), 120)) + if !subnet.Contains(ip) { + t.Fatalf("Error ip %s not in subnet %s", ip.String(), subnet.String()) + } +} + +func TestIPv6InterfaceAllocationAutoNetmaskLe80(t *testing.T) { + + input := engine.Env{} + + _, subnet, _ := net.ParseCIDR("2001:db8:1234:1234:1234::/80") + + // set global ipv6 + input.Set("globalIPv6Network", subnet.String()) + input.Set("RequestedMac", "ab:cd:ab:cd:ab:cd") + + output := newInterfaceAllocation(t, input) + + // ensure global ip with mac + ip := net.ParseIP(output.Get("GlobalIPv6")) + expected_ip := net.ParseIP("2001:db8:1234:1234:1234:abcd:abcd:abcd") + if ip.String() != expected_ip.String() { + t.Fatalf("Error ip %s should be %s", ip.String(), expected_ip.String()) + } + + // ensure link local format + ip = net.ParseIP(output.Get("LinkLocalIPv6")) + expected_ip = net.ParseIP("fe80::a9cd:abff:fecd:abcd") + if ip.String() != expected_ip.String() { + t.Fatalf("Error ip %s should be %s", ip.String(), expected_ip.String()) + } + +} + +func TestIPv6InterfaceAllocationRequest(t *testing.T) { + + input := engine.Env{} + + _, subnet, _ := net.ParseCIDR("2001:db8:1234:1234:1234::/80") + expected_ip := net.ParseIP("2001:db8:1234:1234:1234::1328") + + // set global ipv6 + input.Set("globalIPv6Network", subnet.String()) + input.Set("RequestedIPv6", expected_ip.String()) + + output := newInterfaceAllocation(t, input) + + // ensure global ip with mac + ip := net.ParseIP(output.Get("GlobalIPv6")) + if ip.String() != expected_ip.String() { + t.Fatalf("Error ip %s should be %s", ip.String(), expected_ip.String()) + } + + // retry -> fails for duplicated address + input.SetBool("expectFail", true) + output = newInterfaceAllocation(t, input) +} + func TestMacAddrGeneration(t *testing.T) { ip := net.ParseIP("192.168.0.1") mac := generateMacAddr(ip).String()