Merge pull request #235 from crawford/routes
network: add support for CIDR addresses Debian routes
This commit is contained in:
commit
8f766e4666
@ -236,7 +236,11 @@ func parseInterfaceStanza(attributes []string, options []string) (*stanzaInterfa
|
||||
for i, field := range fields[:len(fields)-1] {
|
||||
switch field {
|
||||
case "-net":
|
||||
if _, dst, err := net.ParseCIDR(fields[i+1]); err == nil {
|
||||
route.destination = *dst
|
||||
} else {
|
||||
route.destination.IP = net.ParseIP(fields[i+1])
|
||||
}
|
||||
case "netmask":
|
||||
route.destination.Mask = net.IPMask(net.ParseIP(fields[i+1]).To4())
|
||||
case "gw":
|
||||
|
@ -285,12 +285,17 @@ func TestBadParseInterfaceStanzasStaticPostUp(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseInterfaceStanzaStaticPostUp(t *testing.T) {
|
||||
options := []string{
|
||||
for _, tt := range []struct {
|
||||
options []string
|
||||
expect []route
|
||||
}{
|
||||
{
|
||||
options: []string{
|
||||
"address 192.168.1.100",
|
||||
"netmask 255.255.255.0",
|
||||
"post-up route add gw 192.168.1.1 -net 192.168.1.0 netmask 255.255.255.0",
|
||||
}
|
||||
expect := []route{
|
||||
},
|
||||
expect: []route{
|
||||
{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv4(192, 168, 1, 0),
|
||||
@ -298,18 +303,39 @@ func TestParseInterfaceStanzaStaticPostUp(t *testing.T) {
|
||||
},
|
||||
gateway: net.IPv4(192, 168, 1, 1),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
options: []string{
|
||||
"address 192.168.1.100",
|
||||
"netmask 255.255.255.0",
|
||||
"post-up route add gw 192.168.1.1 -net 192.168.1.0/24 || true",
|
||||
},
|
||||
expect: []route{
|
||||
{
|
||||
destination: func() net.IPNet {
|
||||
if _, net, err := net.ParseCIDR("192.168.1.0/24"); err == nil {
|
||||
return *net
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
iface, err := parseInterfaceStanza([]string{"eth", "inet", "static"}, options)
|
||||
}(),
|
||||
gateway: net.IPv4(192, 168, 1, 1),
|
||||
},
|
||||
},
|
||||
},
|
||||
} {
|
||||
iface, err := parseInterfaceStanza([]string{"eth", "inet", "static"}, tt.options)
|
||||
if err != nil {
|
||||
t.FailNow()
|
||||
t.Fatalf("bad error (%+v): want nil, got %s\n", tt, err)
|
||||
}
|
||||
static, ok := iface.configMethod.(configMethodStatic)
|
||||
if !ok {
|
||||
t.FailNow()
|
||||
t.Fatalf("bad config method (%+v): want configMethodStatic, got %T\n", tt, iface.configMethod)
|
||||
}
|
||||
if !reflect.DeepEqual(static.routes, tt.expect) {
|
||||
t.Fatalf("bad routes (%+v): want %#v, got %#v\n", tt, tt.expect, static.routes)
|
||||
}
|
||||
if !reflect.DeepEqual(static.routes, expect) {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user