2014-07-24 01:41:10 +08:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2014-08-08 09:37:38 +08:00
|
|
|
"strings"
|
2014-07-24 01:41:10 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-08-08 19:24:18 +08:00
|
|
|
func ipAssigned(iface *net.Interface, ip net.IP) bool {
|
2014-08-08 09:37:38 +08:00
|
|
|
addrs, _ := iface.Addrs()
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
args := strings.SplitN(addr.String(), "/", 2)
|
|
|
|
if args[0] == ip.String() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddDelNetworkIp(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaceName := "lo"
|
|
|
|
ip := net.ParseIP("127.0.1.1")
|
|
|
|
mask := net.IPv4Mask(255, 255, 255, 255)
|
2014-08-13 18:38:20 +08:00
|
|
|
ipNet := &net.IPNet{IP: ip, Mask: mask}
|
2014-08-08 09:37:38 +08:00
|
|
|
|
|
|
|
iface, err := net.InterfaceByName(ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
t.Skip("No 'lo' interface; skipping tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := NetworkLinkAddIp(iface, ip, ipNet); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-08 19:24:18 +08:00
|
|
|
if !ipAssigned(iface, ip) {
|
2014-08-08 09:37:38 +08:00
|
|
|
t.Fatalf("Could not locate address '%s' in lo address list.", ip.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := NetworkLinkDelIp(iface, ip, ipNet); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-08 19:24:18 +08:00
|
|
|
if ipAssigned(iface, ip) {
|
2014-08-13 18:38:20 +08:00
|
|
|
t.Fatalf("Located address '%s' in lo address list after removal.", ip.String())
|
2014-08-08 09:37:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 01:41:10 +08:00
|
|
|
func TestCreateBridgeWithMac(t *testing.T) {
|
2014-07-24 04:45:05 +08:00
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-24 01:41:10 +08:00
|
|
|
name := "testbridge"
|
|
|
|
|
|
|
|
if err := CreateBridge(name, true); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := net.InterfaceByName(name); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup and tests
|
|
|
|
|
|
|
|
if err := DeleteBridge(name); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := net.InterfaceByName(name); err == nil {
|
2014-08-06 08:29:37 +08:00
|
|
|
t.Fatalf("expected error getting interface because %s bridge was deleted", name)
|
2014-07-24 01:41:10 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-24 02:34:07 +08:00
|
|
|
|
2014-08-05 01:29:06 +08:00
|
|
|
func TestCreateBridgeLink(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
name := "mybrlink"
|
|
|
|
|
|
|
|
if err := NetworkLinkAdd(name, "bridge"); err != nil {
|
2014-08-06 07:40:26 +08:00
|
|
|
t.Fatal(err)
|
2014-08-05 01:29:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := net.InterfaceByName(name); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := NetworkLinkDel(name); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := net.InterfaceByName(name); err == nil {
|
2014-08-06 08:29:37 +08:00
|
|
|
t.Fatalf("expected error getting interface because %s bridge was deleted", name)
|
2014-08-05 01:29:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-24 02:34:07 +08:00
|
|
|
func TestCreateVethPair(t *testing.T) {
|
2014-07-24 04:45:05 +08:00
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-24 02:34:07 +08:00
|
|
|
var (
|
|
|
|
name1 = "veth1"
|
|
|
|
name2 = "veth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := NetworkCreateVethPair(name1, name2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-09-15 08:35:29 +08:00
|
|
|
defer NetworkLinkDel(name1)
|
2014-07-24 02:34:07 +08:00
|
|
|
|
|
|
|
if _, err := net.InterfaceByName(name1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := net.InterfaceByName(name2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2014-09-18 08:16:37 +08:00
|
|
|
|
|
|
|
func TestSetMACAddress(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
name := "testmac"
|
|
|
|
mac := randMacAddr()
|
|
|
|
|
|
|
|
if err := NetworkLinkAdd(name, "bridge"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer NetworkLinkDel(name)
|
|
|
|
|
|
|
|
if err := NetworkSetMacAddress(name, mac); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
iface, err := net.InterfaceByName(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if iface.HardwareAddr.String() != mac {
|
|
|
|
t.Fatalf("mac address %q does not match %q", iface.HardwareAddr, mac)
|
|
|
|
}
|
|
|
|
}
|