2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-02-24 14:19:00 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/testutils"
|
2015-02-24 14:19:00 -05:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
func setupTestInterface(t *testing.T, nh *netlink.Handle) (*networkConfiguration, *bridgeInterface) {
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &networkConfiguration{
|
2015-04-15 01:25:42 -04:00
|
|
|
BridgeName: DefaultBridgeName}
|
2016-05-16 14:51:40 -04:00
|
|
|
br := &bridgeInterface{nlh: nh}
|
2015-04-15 01:25:42 -04:00
|
|
|
|
|
|
|
if err := setupDevice(config, br); err != nil {
|
2015-02-24 14:19:00 -05:00
|
|
|
t.Fatalf("Bridge creation failed: %v", err)
|
|
|
|
}
|
2015-04-15 01:25:42 -04:00
|
|
|
return config, br
|
2015-02-24 14:19:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupBridgeIPv4Fixed(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-02-24 14:19:00 -05:00
|
|
|
|
|
|
|
ip, netw, err := net.ParseCIDR("192.168.1.1/24")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse bridge IPv4: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-06-12 18:06:40 -04:00
|
|
|
defer nh.Close()
|
2016-05-16 14:51:40 -04:00
|
|
|
|
|
|
|
config, br := setupTestInterface(t, nh)
|
2015-04-15 01:25:42 -04:00
|
|
|
config.AddressIPv4 = &net.IPNet{IP: ip, Mask: netw.Mask}
|
|
|
|
if err := setupBridgeIPv4(config, br); err != nil {
|
2015-02-24 14:19:00 -05:00
|
|
|
t.Fatalf("Failed to setup bridge IPv4: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
addrsv4, err := nh.AddrList(br.Link, netlink.FAMILY_V4)
|
2015-02-24 14:19:00 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to list device IPv4 addresses: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var found bool
|
|
|
|
for _, addr := range addrsv4 {
|
2015-04-15 01:25:42 -04:00
|
|
|
if config.AddressIPv4.String() == addr.IPNet.String() {
|
2015-02-24 14:19:00 -05:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2015-04-15 01:25:42 -04:00
|
|
|
t.Fatalf("Bridge device does not have requested IPv4 address %v", config.AddressIPv4)
|
2015-02-24 14:19:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:13:44 -04:00
|
|
|
func TestSetupGatewayIPv4(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-04-24 18:13:44 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-06-12 18:06:40 -04:00
|
|
|
defer nh.Close()
|
2016-05-16 14:51:40 -04:00
|
|
|
|
2015-04-24 18:13:44 -04:00
|
|
|
ip, nw, _ := net.ParseCIDR("192.168.0.24/16")
|
|
|
|
nw.IP = ip
|
2015-07-27 19:48:30 -04:00
|
|
|
gw := net.ParseIP("192.168.2.254")
|
2015-04-24 18:13:44 -04:00
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &networkConfiguration{
|
2015-04-24 18:13:44 -04:00
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
DefaultGatewayIPv4: gw}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
br := &bridgeInterface{bridgeIPv4: nw, nlh: nh}
|
2015-04-24 18:13:44 -04:00
|
|
|
|
|
|
|
if err := setupGatewayIPv4(config, br); err != nil {
|
|
|
|
t.Fatalf("Set Default Gateway failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gw.Equal(br.gatewayIPv4) {
|
|
|
|
t.Fatalf("Set Default Gateway failed. Expected %v, Found %v", gw, br.gatewayIPv4)
|
|
|
|
}
|
|
|
|
}
|