2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-02-24 21:41:17 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/testutils"
|
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 {
|
2016-05-16 14:51:40 -04:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
inf := &bridgeInterface{nlh: nh}
|
2015-02-24 21:41:17 -05:00
|
|
|
|
|
|
|
br := netlink.Bridge{}
|
|
|
|
br.LinkAttrs.Name = "default0"
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := nh.LinkAdd(&br); err == nil {
|
2015-02-24 21:41:17 -05:00
|
|
|
inf.Link = &br
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Failed to create bridge interface: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return inf
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupVerify(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(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-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(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-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(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-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(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-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(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")
|
|
|
|
}
|
|
|
|
}
|