2014-05-23 11:58:38 -07:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-08-18 15:58:26 -07:00
|
|
|
func TestInterfaceGenerators(t *testing.T) {
|
2014-06-11 23:21:35 -07:00
|
|
|
for _, tt := range []struct {
|
2014-08-18 15:58:26 -07:00
|
|
|
name string
|
|
|
|
netdev string
|
|
|
|
link string
|
|
|
|
network string
|
|
|
|
kind string
|
|
|
|
iface InterfaceGenerator
|
2014-06-11 23:21:35 -07:00
|
|
|
}{
|
|
|
|
{
|
2014-08-18 15:58:26 -07:00
|
|
|
name: "",
|
|
|
|
network: "[Match]\nMACAddress=00:01:02:03:04:05\n\n[Network]\n",
|
|
|
|
kind: "physical",
|
|
|
|
iface: &physicalInterface{logicalInterface{
|
|
|
|
hwaddr: net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5}),
|
|
|
|
}},
|
2014-06-11 23:21:35 -07:00
|
|
|
},
|
|
|
|
{
|
2014-08-18 15:58:26 -07:00
|
|
|
name: "testname",
|
|
|
|
network: "[Match]\nName=testname\n\n[Network]\nBond=testbond1\nVLAN=testvlan1\nVLAN=testvlan2\n",
|
|
|
|
kind: "physical",
|
|
|
|
iface: &physicalInterface{logicalInterface{
|
|
|
|
name: "testname",
|
|
|
|
children: []networkInterface{
|
|
|
|
&bondInterface{logicalInterface: logicalInterface{name: "testbond1"}},
|
|
|
|
&vlanInterface{logicalInterface: logicalInterface{name: "testvlan1"}, id: 1},
|
|
|
|
&vlanInterface{logicalInterface: logicalInterface{name: "testvlan2"}, id: 1},
|
|
|
|
},
|
|
|
|
}},
|
2014-06-11 23:21:35 -07:00
|
|
|
},
|
|
|
|
{
|
2014-08-18 15:58:26 -07:00
|
|
|
name: "testname",
|
|
|
|
netdev: "[NetDev]\nKind=bond\nName=testname\n",
|
|
|
|
network: "[Match]\nName=testname\n\n[Network]\nBond=testbond1\nVLAN=testvlan1\nVLAN=testvlan2\nDHCP=true\n",
|
|
|
|
kind: "bond",
|
|
|
|
iface: &bondInterface{logicalInterface: logicalInterface{
|
|
|
|
name: "testname",
|
|
|
|
config: configMethodDHCP{},
|
|
|
|
children: []networkInterface{
|
|
|
|
&bondInterface{logicalInterface: logicalInterface{name: "testbond1"}},
|
|
|
|
&vlanInterface{logicalInterface: logicalInterface{name: "testvlan1"}, id: 1},
|
|
|
|
&vlanInterface{logicalInterface: logicalInterface{name: "testvlan2"}, id: 1},
|
2014-05-23 11:58:38 -07:00
|
|
|
},
|
2014-08-18 15:58:26 -07:00
|
|
|
}},
|
2014-05-23 11:58:38 -07:00
|
|
|
},
|
2014-06-30 13:37:09 -07:00
|
|
|
{
|
2014-08-18 15:58:26 -07:00
|
|
|
name: "testname",
|
|
|
|
netdev: "[NetDev]\nKind=vlan\nName=testname\n\n[VLAN]\nId=1\n",
|
|
|
|
network: "[Match]\nName=testname\n\n[Network]\n",
|
|
|
|
kind: "vlan",
|
|
|
|
iface: &vlanInterface{logicalInterface{name: "testname"}, 1, ""},
|
2014-06-30 13:37:09 -07:00
|
|
|
},
|
|
|
|
{
|
2014-08-18 15:58:26 -07:00
|
|
|
name: "testname",
|
|
|
|
netdev: "[NetDev]\nKind=vlan\nName=testname\nMACAddress=00:01:02:03:04:05\n\n[VLAN]\nId=1\n",
|
|
|
|
network: "[Match]\nName=testname\n\n[Network]\n",
|
|
|
|
kind: "vlan",
|
|
|
|
iface: &vlanInterface{logicalInterface{name: "testname", config: configMethodStatic{hwaddress: net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5})}}, 1, ""},
|
2014-06-30 13:37:09 -07:00
|
|
|
},
|
|
|
|
{
|
2014-08-18 15:58:26 -07:00
|
|
|
name: "testname",
|
|
|
|
netdev: "[NetDev]\nKind=vlan\nName=testname\nMACAddress=00:01:02:03:04:05\n\n[VLAN]\nId=1\n",
|
|
|
|
network: "[Match]\nName=testname\n\n[Network]\nDHCP=true\n",
|
|
|
|
kind: "vlan",
|
|
|
|
iface: &vlanInterface{logicalInterface{name: "testname", config: configMethodDHCP{hwaddress: net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5})}}, 1, ""},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "testname",
|
|
|
|
netdev: "[NetDev]\nKind=vlan\nName=testname\n\n[VLAN]\nId=0\n",
|
|
|
|
network: "[Match]\nName=testname\n\n[Network]\nDNS=8.8.8.8\n\n[Address]\nAddress=192.168.1.100/24\n\n[Route]\nDestination=0.0.0.0/0\nGateway=1.2.3.4\n",
|
|
|
|
kind: "vlan",
|
|
|
|
iface: &vlanInterface{logicalInterface: logicalInterface{
|
|
|
|
name: "testname",
|
|
|
|
config: configMethodStatic{
|
|
|
|
addresses: []net.IPNet{{IP: []byte{192, 168, 1, 100}, Mask: []byte{255, 255, 255, 0}}},
|
|
|
|
nameservers: []net.IP{[]byte{8, 8, 8, 8}},
|
|
|
|
routes: []route{route{destination: net.IPNet{IP: []byte{0, 0, 0, 0}, Mask: []byte{0, 0, 0, 0}}, gateway: []byte{1, 2, 3, 4}}},
|
|
|
|
},
|
|
|
|
}},
|
2014-06-30 13:37:09 -07:00
|
|
|
},
|
|
|
|
} {
|
2014-08-18 15:58:26 -07:00
|
|
|
if name := tt.iface.Name(); name != tt.name {
|
|
|
|
t.Fatalf("bad name (%q): want %q, got %q", tt.iface, tt.name, name)
|
|
|
|
}
|
|
|
|
if netdev := tt.iface.Netdev(); netdev != tt.netdev {
|
|
|
|
t.Fatalf("bad netdev (%q): want %q, got %q", tt.iface, tt.netdev, netdev)
|
|
|
|
}
|
|
|
|
if link := tt.iface.Link(); link != tt.link {
|
|
|
|
t.Fatalf("bad link (%q): want %q, got %q", tt.iface, tt.link, link)
|
|
|
|
}
|
|
|
|
if network := tt.iface.Network(); network != tt.network {
|
|
|
|
t.Fatalf("bad network (%q): want %q, got %q", tt.iface, tt.network, network)
|
|
|
|
}
|
|
|
|
if kind := tt.iface.Type(); kind != tt.kind {
|
|
|
|
t.Fatalf("bad type (%q): want %q, got %q", tt.iface, tt.kind, kind)
|
2014-06-30 13:37:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestModprobeParams(t *testing.T) {
|
|
|
|
for _, tt := range []struct {
|
|
|
|
i InterfaceGenerator
|
|
|
|
p string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
i: &physicalInterface{},
|
|
|
|
p: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
i: &vlanInterface{},
|
|
|
|
p: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
i: &bondInterface{
|
|
|
|
logicalInterface{},
|
|
|
|
nil,
|
|
|
|
map[string]string{
|
|
|
|
"a": "1",
|
|
|
|
"b": "2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
p: "a=1 b=2",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
if p := tt.i.ModprobeParams(); p != tt.p {
|
|
|
|
t.Fatalf("bad params (%q): got %s, want %s", tt.i, p, tt.p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 11:58:38 -07:00
|
|
|
func TestBuildInterfacesLo(t *testing.T) {
|
|
|
|
stanzas := []*stanzaInterface{
|
|
|
|
&stanzaInterface{
|
|
|
|
name: "lo",
|
|
|
|
kind: interfacePhysical,
|
|
|
|
auto: false,
|
|
|
|
configMethod: configMethodLoopback{},
|
|
|
|
options: map[string][]string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
interfaces := buildInterfaces(stanzas)
|
|
|
|
if len(interfaces) != 0 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 22:07:33 -07:00
|
|
|
func TestBuildInterfacesBlindBond(t *testing.T) {
|
|
|
|
stanzas := []*stanzaInterface{
|
|
|
|
{
|
|
|
|
name: "bond0",
|
|
|
|
kind: interfaceBond,
|
|
|
|
auto: false,
|
|
|
|
configMethod: configMethodManual{},
|
|
|
|
options: map[string][]string{
|
2014-06-30 13:37:09 -07:00
|
|
|
"bond-slaves": []string{"eth0"},
|
2014-06-11 22:07:33 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
interfaces := buildInterfaces(stanzas)
|
|
|
|
bond0 := &bondInterface{
|
|
|
|
logicalInterface{
|
2014-06-12 18:13:19 -05:00
|
|
|
name: "bond0",
|
|
|
|
config: configMethodManual{},
|
|
|
|
children: []networkInterface{},
|
2014-07-17 20:47:37 -07:00
|
|
|
configDepth: 0,
|
2014-06-11 22:07:33 -07:00
|
|
|
},
|
|
|
|
[]string{"eth0"},
|
2014-06-30 13:37:09 -07:00
|
|
|
map[string]string{},
|
2014-06-11 22:07:33 -07:00
|
|
|
}
|
|
|
|
eth0 := &physicalInterface{
|
|
|
|
logicalInterface{
|
2014-06-12 18:13:19 -05:00
|
|
|
name: "eth0",
|
|
|
|
config: configMethodManual{},
|
|
|
|
children: []networkInterface{bond0},
|
2014-07-17 20:47:37 -07:00
|
|
|
configDepth: 1,
|
2014-06-11 22:07:33 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
expect := []InterfaceGenerator{bond0, eth0}
|
|
|
|
if !reflect.DeepEqual(interfaces, expect) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildInterfacesBlindVLAN(t *testing.T) {
|
|
|
|
stanzas := []*stanzaInterface{
|
|
|
|
{
|
|
|
|
name: "vlan0",
|
|
|
|
kind: interfaceVLAN,
|
|
|
|
auto: false,
|
|
|
|
configMethod: configMethodManual{},
|
|
|
|
options: map[string][]string{
|
|
|
|
"id": []string{"0"},
|
|
|
|
"raw_device": []string{"eth0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
interfaces := buildInterfaces(stanzas)
|
|
|
|
vlan0 := &vlanInterface{
|
|
|
|
logicalInterface{
|
2014-06-12 18:13:19 -05:00
|
|
|
name: "vlan0",
|
|
|
|
config: configMethodManual{},
|
|
|
|
children: []networkInterface{},
|
2014-07-17 20:47:37 -07:00
|
|
|
configDepth: 0,
|
2014-06-11 22:07:33 -07:00
|
|
|
},
|
|
|
|
0,
|
|
|
|
"eth0",
|
|
|
|
}
|
|
|
|
eth0 := &physicalInterface{
|
|
|
|
logicalInterface{
|
2014-06-12 18:13:19 -05:00
|
|
|
name: "eth0",
|
|
|
|
config: configMethodManual{},
|
|
|
|
children: []networkInterface{vlan0},
|
2014-07-17 20:47:37 -07:00
|
|
|
configDepth: 1,
|
2014-06-11 22:07:33 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
expect := []InterfaceGenerator{eth0, vlan0}
|
|
|
|
if !reflect.DeepEqual(interfaces, expect) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 11:58:38 -07:00
|
|
|
func TestBuildInterfaces(t *testing.T) {
|
|
|
|
stanzas := []*stanzaInterface{
|
|
|
|
&stanzaInterface{
|
|
|
|
name: "eth0",
|
|
|
|
kind: interfacePhysical,
|
|
|
|
auto: false,
|
|
|
|
configMethod: configMethodManual{},
|
|
|
|
options: map[string][]string{},
|
|
|
|
},
|
|
|
|
&stanzaInterface{
|
|
|
|
name: "bond0",
|
|
|
|
kind: interfaceBond,
|
|
|
|
auto: false,
|
|
|
|
configMethod: configMethodManual{},
|
|
|
|
options: map[string][]string{
|
2014-06-30 13:37:09 -07:00
|
|
|
"bond-slaves": []string{"eth0"},
|
|
|
|
"bond-mode": []string{"4"},
|
|
|
|
"bond-miimon": []string{"100"},
|
2014-05-23 11:58:38 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&stanzaInterface{
|
|
|
|
name: "bond1",
|
|
|
|
kind: interfaceBond,
|
|
|
|
auto: false,
|
|
|
|
configMethod: configMethodManual{},
|
|
|
|
options: map[string][]string{
|
2014-06-30 13:37:09 -07:00
|
|
|
"bond-slaves": []string{"bond0"},
|
2014-05-23 11:58:38 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&stanzaInterface{
|
|
|
|
name: "vlan0",
|
|
|
|
kind: interfaceVLAN,
|
|
|
|
auto: false,
|
|
|
|
configMethod: configMethodManual{},
|
|
|
|
options: map[string][]string{
|
|
|
|
"id": []string{"0"},
|
|
|
|
"raw_device": []string{"eth0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&stanzaInterface{
|
|
|
|
name: "vlan1",
|
|
|
|
kind: interfaceVLAN,
|
|
|
|
auto: false,
|
|
|
|
configMethod: configMethodManual{},
|
|
|
|
options: map[string][]string{
|
|
|
|
"id": []string{"1"},
|
|
|
|
"raw_device": []string{"bond0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
interfaces := buildInterfaces(stanzas)
|
|
|
|
vlan1 := &vlanInterface{
|
|
|
|
logicalInterface{
|
2014-06-12 18:13:19 -05:00
|
|
|
name: "vlan1",
|
|
|
|
config: configMethodManual{},
|
|
|
|
children: []networkInterface{},
|
2014-07-17 20:47:37 -07:00
|
|
|
configDepth: 0,
|
2014-05-23 11:58:38 -07:00
|
|
|
},
|
|
|
|
1,
|
|
|
|
"bond0",
|
|
|
|
}
|
|
|
|
vlan0 := &vlanInterface{
|
|
|
|
logicalInterface{
|
2014-06-12 18:13:19 -05:00
|
|
|
name: "vlan0",
|
|
|
|
config: configMethodManual{},
|
|
|
|
children: []networkInterface{},
|
2014-07-17 20:47:37 -07:00
|
|
|
configDepth: 0,
|
2014-05-23 11:58:38 -07:00
|
|
|
},
|
|
|
|
0,
|
|
|
|
"eth0",
|
|
|
|
}
|
|
|
|
bond1 := &bondInterface{
|
|
|
|
logicalInterface{
|
2014-06-12 18:13:19 -05:00
|
|
|
name: "bond1",
|
|
|
|
config: configMethodManual{},
|
|
|
|
children: []networkInterface{},
|
2014-07-17 20:47:37 -07:00
|
|
|
configDepth: 0,
|
2014-05-23 11:58:38 -07:00
|
|
|
},
|
|
|
|
[]string{"bond0"},
|
2014-06-30 13:37:09 -07:00
|
|
|
map[string]string{},
|
2014-05-23 11:58:38 -07:00
|
|
|
}
|
|
|
|
bond0 := &bondInterface{
|
|
|
|
logicalInterface{
|
2014-06-12 18:13:19 -05:00
|
|
|
name: "bond0",
|
|
|
|
config: configMethodManual{},
|
|
|
|
children: []networkInterface{bond1, vlan1},
|
|
|
|
configDepth: 1,
|
2014-05-23 11:58:38 -07:00
|
|
|
},
|
|
|
|
[]string{"eth0"},
|
2014-06-30 13:37:09 -07:00
|
|
|
map[string]string{
|
|
|
|
"mode": "4",
|
|
|
|
"miimon": "100",
|
|
|
|
},
|
2014-05-23 11:58:38 -07:00
|
|
|
}
|
|
|
|
eth0 := &physicalInterface{
|
|
|
|
logicalInterface{
|
2014-06-12 18:13:19 -05:00
|
|
|
name: "eth0",
|
|
|
|
config: configMethodManual{},
|
|
|
|
children: []networkInterface{bond0, vlan0},
|
2014-07-17 20:47:37 -07:00
|
|
|
configDepth: 2,
|
2014-05-23 11:58:38 -07:00
|
|
|
},
|
|
|
|
}
|
2014-08-25 12:01:27 -07:00
|
|
|
expect := []InterfaceGenerator{bond0, bond1, eth0, vlan0, vlan1}
|
2014-05-23 11:58:38 -07:00
|
|
|
if !reflect.DeepEqual(interfaces, expect) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 18:13:19 -05:00
|
|
|
|
|
|
|
func TestFilename(t *testing.T) {
|
|
|
|
for _, tt := range []struct {
|
|
|
|
i logicalInterface
|
|
|
|
f string
|
|
|
|
}{
|
|
|
|
{logicalInterface{name: "iface", configDepth: 0}, "00-iface"},
|
|
|
|
{logicalInterface{name: "iface", configDepth: 9}, "09-iface"},
|
|
|
|
{logicalInterface{name: "iface", configDepth: 10}, "0a-iface"},
|
|
|
|
{logicalInterface{name: "iface", configDepth: 53}, "35-iface"},
|
|
|
|
} {
|
|
|
|
if tt.i.Filename() != tt.f {
|
|
|
|
t.Fatalf("bad filename (%q): got %q, want %q", tt.i, tt.i.Filename(), tt.f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|