test: Refactor interface tests a little

This commit is contained in:
Alex Crawford 2014-08-18 15:58:26 -07:00
parent 4a2e417781
commit e9ec78ac6f
2 changed files with 93 additions and 243 deletions

View File

@ -32,6 +32,10 @@ type logicalInterface struct {
configDepth int configDepth int
} }
func (i *logicalInterface) Name() string {
return i.name
}
func (i *logicalInterface) Network() string { func (i *logicalInterface) Network() string {
config := fmt.Sprintln("[Match]") config := fmt.Sprintln("[Match]")
if i.name != "" { if i.name != "" {
@ -73,6 +77,10 @@ func (i *logicalInterface) Link() string {
return "" return ""
} }
func (i *logicalInterface) Netdev() string {
return ""
}
func (i *logicalInterface) Filename() string { func (i *logicalInterface) Filename() string {
return fmt.Sprintf("%02x-%s", i.configDepth, i.name) return fmt.Sprintf("%02x-%s", i.configDepth, i.name)
} }
@ -93,14 +101,6 @@ type physicalInterface struct {
logicalInterface logicalInterface
} }
func (p *physicalInterface) Name() string {
return p.name
}
func (p *physicalInterface) Netdev() string {
return ""
}
func (p *physicalInterface) Type() string { func (p *physicalInterface) Type() string {
return "physical" return "physical"
} }
@ -111,10 +111,6 @@ type bondInterface struct {
options map[string]string options map[string]string
} }
func (b *bondInterface) Name() string {
return b.name
}
func (b *bondInterface) Netdev() string { func (b *bondInterface) Netdev() string {
return fmt.Sprintf("[NetDev]\nKind=bond\nName=%s\n", b.name) return fmt.Sprintf("[NetDev]\nKind=bond\nName=%s\n", b.name)
} }
@ -138,10 +134,6 @@ type vlanInterface struct {
rawDevice string rawDevice string
} }
func (v *vlanInterface) Name() string {
return v.name
}
func (v *vlanInterface) Netdev() string { func (v *vlanInterface) Netdev() string {
config := fmt.Sprintf("[NetDev]\nKind=vlan\nName=%s\n", v.name) config := fmt.Sprintf("[NetDev]\nKind=vlan\nName=%s\n", v.name)
switch c := v.config.(type) { switch c := v.config.(type) {

View File

@ -6,243 +6,101 @@ import (
"testing" "testing"
) )
func TestPhysicalInterfaceName(t *testing.T) { func TestInterfaceGenerators(t *testing.T) {
p := physicalInterface{logicalInterface{name: "testname"}} for _, tt := range []struct {
if p.Name() != "testname" { name string
t.FailNow() netdev string
} link string
} network string
kind string
func TestPhysicalInterfaceNetdev(t *testing.T) { iface InterfaceGenerator
p := physicalInterface{} }{
if p.Netdev() != "" { {
t.FailNow() name: "",
} network: "[Match]\nMACAddress=00:01:02:03:04:05\n\n[Network]\n",
} kind: "physical",
iface: &physicalInterface{logicalInterface{
func TestPhysicalInterfaceLink(t *testing.T) { hwaddr: net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5}),
p := physicalInterface{} }},
if p.Link() != "" { },
t.FailNow() {
} name: "testname",
} network: "[Match]\nName=testname\n\n[Network]\nBond=testbond1\nVLAN=testvlan1\nVLAN=testvlan2\n",
kind: "physical",
func TestPhysicalInterfaceNetwork(t *testing.T) { iface: &physicalInterface{logicalInterface{
p := physicalInterface{logicalInterface{
name: "testname", name: "testname",
children: []networkInterface{ children: []networkInterface{
&bondInterface{ &bondInterface{logicalInterface: logicalInterface{name: "testbond1"}},
logicalInterface{ &vlanInterface{logicalInterface: logicalInterface{name: "testvlan1"}, id: 1},
name: "testbond1", &vlanInterface{logicalInterface: logicalInterface{name: "testvlan2"}, id: 1},
}, },
nil, }},
nil,
}, },
&vlanInterface{ {
logicalInterface{ name: "testname",
name: "testvlan1", netdev: "[NetDev]\nKind=bond\nName=testname\n",
}, network: "[Match]\nName=testname\n\n[Network]\nBond=testbond1\nVLAN=testvlan1\nVLAN=testvlan2\nDHCP=true\n",
1, kind: "bond",
"", iface: &bondInterface{logicalInterface: logicalInterface{
},
&vlanInterface{
logicalInterface{
name: "testvlan2",
},
1,
"",
},
},
}}
network := `[Match]
Name=testname
[Network]
Bond=testbond1
VLAN=testvlan1
VLAN=testvlan2
`
if p.Network() != network {
t.FailNow()
}
}
func TestBondInterfaceName(t *testing.T) {
b := bondInterface{logicalInterface{name: "testname"}, nil, nil}
if b.Name() != "testname" {
t.FailNow()
}
}
func TestBondInterfaceNetdev(t *testing.T) {
b := bondInterface{logicalInterface{name: "testname"}, nil, nil}
netdev := `[NetDev]
Kind=bond
Name=testname
`
if b.Netdev() != netdev {
t.FailNow()
}
}
func TestBondInterfaceLink(t *testing.T) {
b := bondInterface{}
if b.Link() != "" {
t.FailNow()
}
}
func TestBondInterfaceNetwork(t *testing.T) {
b := bondInterface{
logicalInterface{
name: "testname", name: "testname",
config: configMethodDHCP{}, config: configMethodDHCP{},
children: []networkInterface{ children: []networkInterface{
&bondInterface{ &bondInterface{logicalInterface: logicalInterface{name: "testbond1"}},
logicalInterface{ &vlanInterface{logicalInterface: logicalInterface{name: "testvlan1"}, id: 1},
name: "testbond1", &vlanInterface{logicalInterface: logicalInterface{name: "testvlan2"}, id: 1},
}, },
nil, }},
nil,
},
&vlanInterface{
logicalInterface{
name: "testvlan1",
},
1,
"",
},
&vlanInterface{
logicalInterface{
name: "testvlan2",
},
1,
"",
},
},
},
nil,
nil,
}
network := `[Match]
Name=testname
[Network]
Bond=testbond1
VLAN=testvlan1
VLAN=testvlan2
DHCP=true
`
if b.Network() != network {
t.FailNow()
}
}
func TestVLANInterfaceName(t *testing.T) {
v := vlanInterface{logicalInterface{name: "testname"}, 1, ""}
if v.Name() != "testname" {
t.FailNow()
}
}
func TestVLANInterfaceNetdev(t *testing.T) {
for _, tt := range []struct {
i vlanInterface
l string
}{
{
vlanInterface{logicalInterface{name: "testname"}, 1, ""},
"[NetDev]\nKind=vlan\nName=testname\n\n[VLAN]\nId=1\n",
}, },
{ {
vlanInterface{logicalInterface{name: "testname", config: configMethodStatic{hwaddress: net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5})}}, 1, ""}, name: "testname",
"[NetDev]\nKind=vlan\nName=testname\nMACAddress=00:01:02:03:04:05\n\n[VLAN]\nId=1\n", 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, ""},
}, },
{ {
vlanInterface{logicalInterface{name: "testname", config: configMethodDHCP{hwaddress: net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5})}}, 1, ""}, name: "testname",
"[NetDev]\nKind=vlan\nName=testname\nMACAddress=00:01:02:03:04:05\n\n[VLAN]\nId=1\n", 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, ""},
}, },
} { {
if tt.i.Netdev() != tt.l { name: "testname",
t.Fatalf("bad netdev config (%q): got %q, want %q", tt.i, tt.i.Netdev(), tt.l) 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, ""},
},
func TestVLANInterfaceLink(t *testing.T) { {
v := vlanInterface{} name: "testname",
if v.Link() != "" { netdev: "[NetDev]\nKind=vlan\nName=testname\n\n[VLAN]\nId=0\n",
t.FailNow() 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{
func TestVLANInterfaceNetwork(t *testing.T) {
v := vlanInterface{
logicalInterface{
name: "testname", name: "testname",
config: configMethodStatic{ config: configMethodStatic{
addresses: []net.IPNet{ addresses: []net.IPNet{{IP: []byte{192, 168, 1, 100}, Mask: []byte{255, 255, 255, 0}}},
{ nameservers: []net.IP{[]byte{8, 8, 8, 8}},
IP: []byte{192, 168, 1, 100}, routes: []route{route{destination: net.IPNet{IP: []byte{0, 0, 0, 0}, Mask: []byte{0, 0, 0, 0}}, gateway: []byte{1, 2, 3, 4}}},
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},
},
},
},
},
0,
"",
}
network := `[Match]
Name=testname
[Network]
DNS=8.8.8.8
[Address]
Address=192.168.1.100/24
[Route]
Destination=0.0.0.0/0
Gateway=1.2.3.4
`
if v.Network() != network {
t.Log(v.Network())
t.FailNow()
}
}
func TestType(t *testing.T) {
for _, tt := range []struct {
i InterfaceGenerator
t string
}{
{
i: &physicalInterface{},
t: "physical",
},
{
i: &vlanInterface{},
t: "vlan",
},
{
i: &bondInterface{},
t: "bond",
}, },
} { } {
if tp := tt.i.Type(); tp != tt.t { if name := tt.iface.Name(); name != tt.name {
t.Fatalf("bad type (%q): got %s, want %s", tt.i, tp, tt.t) 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)
} }
} }
} }