2015-02-24 14:19:00 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2015-09-07 13:33:28 -04:00
|
|
|
"github.com/docker/libnetwork/testutils"
|
2015-02-24 14:19:00 -05:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
func setupTestInterface(t *testing.T) (*networkConfiguration, *bridgeInterface) {
|
|
|
|
config := &networkConfiguration{
|
2015-04-15 01:25:42 -04:00
|
|
|
BridgeName: DefaultBridgeName}
|
|
|
|
br := &bridgeInterface{}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-04-15 01:25:42 -04:00
|
|
|
config, br := setupTestInterface(t)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrsv4, err := netlink.AddrList(br.Link, netlink.FAMILY_V4)
|
|
|
|
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
|
|
|
|
|
|
|
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}
|
|
|
|
|
|
|
|
br := &bridgeInterface{bridgeIPv4: nw}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|