2015-02-24 21:41:17 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2015-04-13 14:40:42 -04:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
2015-02-24 21:41:17 -05:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
2015-03-04 16:25:43 -05:00
|
|
|
func setupVerifyTest(t *testing.T) *bridgeInterface {
|
2015-04-15 01:25:42 -04:00
|
|
|
inf := &bridgeInterface{}
|
2015-02-24 21:41:17 -05:00
|
|
|
|
|
|
|
br := netlink.Bridge{}
|
|
|
|
br.LinkAttrs.Name = "default0"
|
|
|
|
if err := netlink.LinkAdd(&br); err == nil {
|
|
|
|
inf.Link = &br
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Failed to create bridge interface: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return inf
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupVerify(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
2015-02-24 21:41:17 -05:00
|
|
|
|
|
|
|
addrv4 := net.IPv4(192, 168, 1, 1)
|
|
|
|
inf := setupVerifyTest(t)
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &networkConfiguration{}
|
2015-04-15 01:25:42 -04:00
|
|
|
config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()}
|
2015-02-24 21:41:17 -05:00
|
|
|
|
2015-04-15 01:25:42 -04:00
|
|
|
if err := netlink.AddrAdd(inf.Link, &netlink.Addr{IPNet: config.AddressIPv4}); err != nil {
|
|
|
|
t.Fatalf("Failed to assign IPv4 %s to interface: %v", config.AddressIPv4, err)
|
2015-02-24 21:41:17 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 15:00:36 -04:00
|
|
|
if err := setupVerifyAndReconcile(config, inf); err != nil {
|
2015-02-24 21:41:17 -05:00
|
|
|
t.Fatalf("Address verification failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupVerifyBad(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
2015-02-24 21:41:17 -05:00
|
|
|
|
|
|
|
addrv4 := net.IPv4(192, 168, 1, 1)
|
|
|
|
inf := setupVerifyTest(t)
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &networkConfiguration{}
|
2015-04-15 01:25:42 -04:00
|
|
|
config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()}
|
2015-02-24 21:41:17 -05:00
|
|
|
|
|
|
|
ipnet := &net.IPNet{IP: net.IPv4(192, 168, 1, 2), Mask: addrv4.DefaultMask()}
|
|
|
|
if err := netlink.AddrAdd(inf.Link, &netlink.Addr{IPNet: ipnet}); err != nil {
|
|
|
|
t.Fatalf("Failed to assign IPv4 %s to interface: %v", ipnet, err)
|
|
|
|
}
|
|
|
|
|
2015-04-16 15:00:36 -04:00
|
|
|
if err := setupVerifyAndReconcile(config, inf); err == nil {
|
2015-02-24 21:41:17 -05:00
|
|
|
t.Fatal("Address verification was expected to fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupVerifyMissing(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
2015-02-24 21:41:17 -05:00
|
|
|
|
|
|
|
addrv4 := net.IPv4(192, 168, 1, 1)
|
|
|
|
inf := setupVerifyTest(t)
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &networkConfiguration{}
|
2015-04-15 01:25:42 -04:00
|
|
|
config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()}
|
2015-02-24 21:41:17 -05:00
|
|
|
|
2015-04-16 15:00:36 -04:00
|
|
|
if err := setupVerifyAndReconcile(config, inf); err == nil {
|
2015-02-24 21:41:17 -05:00
|
|
|
t.Fatal("Address verification was expected to fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupVerifyIPv6(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
2015-02-24 21:41:17 -05:00
|
|
|
|
|
|
|
addrv4 := net.IPv4(192, 168, 1, 1)
|
|
|
|
inf := setupVerifyTest(t)
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &networkConfiguration{}
|
2015-04-15 01:25:42 -04:00
|
|
|
config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()}
|
|
|
|
config.EnableIPv6 = true
|
2015-02-24 21:41:17 -05:00
|
|
|
|
2015-03-04 16:25:43 -05:00
|
|
|
if err := netlink.AddrAdd(inf.Link, &netlink.Addr{IPNet: bridgeIPv6}); err != nil {
|
|
|
|
t.Fatalf("Failed to assign IPv6 %s to interface: %v", bridgeIPv6, err)
|
2015-02-24 21:41:17 -05:00
|
|
|
}
|
2015-04-15 01:25:42 -04:00
|
|
|
if err := netlink.AddrAdd(inf.Link, &netlink.Addr{IPNet: config.AddressIPv4}); err != nil {
|
|
|
|
t.Fatalf("Failed to assign IPv4 %s to interface: %v", config.AddressIPv4, err)
|
2015-02-24 21:41:17 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 15:00:36 -04:00
|
|
|
if err := setupVerifyAndReconcile(config, inf); err != nil {
|
2015-02-24 21:41:17 -05:00
|
|
|
t.Fatalf("Address verification failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupVerifyIPv6Missing(t *testing.T) {
|
2015-04-13 14:40:42 -04:00
|
|
|
defer netutils.SetupTestNetNS(t)()
|
2015-02-24 21:41:17 -05:00
|
|
|
|
|
|
|
addrv4 := net.IPv4(192, 168, 1, 1)
|
|
|
|
inf := setupVerifyTest(t)
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &networkConfiguration{}
|
2015-04-15 01:25:42 -04:00
|
|
|
config.AddressIPv4 = &net.IPNet{IP: addrv4, Mask: addrv4.DefaultMask()}
|
|
|
|
config.EnableIPv6 = true
|
2015-02-24 21:41:17 -05:00
|
|
|
|
2015-04-15 01:25:42 -04:00
|
|
|
if err := netlink.AddrAdd(inf.Link, &netlink.Addr{IPNet: config.AddressIPv4}); err != nil {
|
|
|
|
t.Fatalf("Failed to assign IPv4 %s to interface: %v", config.AddressIPv4, err)
|
2015-02-24 21:41:17 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 15:00:36 -04:00
|
|
|
if err := setupVerifyAndReconcile(config, inf); err == nil {
|
2015-02-24 21:41:17 -05:00
|
|
|
t.Fatal("Address verification was expected to fail")
|
|
|
|
}
|
|
|
|
}
|