123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- // Copyright 2015 CoreOS, Inc.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package network
- import (
- "net"
- "reflect"
- "strings"
- "testing"
- )
- func TestSplitStanzasNoParent(t *testing.T) {
- in := []string{"test"}
- e := "missing stanza start"
- _, err := splitStanzas(in)
- if err == nil || !strings.HasPrefix(err.Error(), e) {
- t.Fatalf("bad error for splitStanzas(%q): got %q, want %q", in, err, e)
- }
- }
- func TestBadParseStanzas(t *testing.T) {
- for in, e := range map[string]string{
- "": "missing stanza start",
- "iface": "malformed stanza start",
- "allow-?? unknown": "unknown stanza",
- } {
- _, err := parseStanzas([]string{in})
- if err == nil || !strings.HasPrefix(err.Error(), e) {
- t.Fatalf("bad error for parseStanzas(%q): got %q, want %q", in, err, e)
- }
- }
- }
- func TestBadParseInterfaceStanza(t *testing.T) {
- for _, tt := range []struct {
- in []string
- opts []string
- e string
- }{
- {[]string{}, nil, "incorrect number of attributes"},
- {[]string{"eth", "inet", "invalid"}, nil, "invalid config method"},
- {[]string{"eth", "inet", "static"}, []string{"address 192.168.1.100"}, "malformed static network config"},
- {[]string{"eth", "inet", "static"}, []string{"netmask 255.255.255.0"}, "malformed static network config"},
- {[]string{"eth", "inet", "static"}, []string{"address invalid", "netmask 255.255.255.0"}, "malformed static network config"},
- {[]string{"eth", "inet", "static"}, []string{"address 192.168.1.100", "netmask invalid"}, "malformed static network config"},
- {[]string{"eth", "inet", "static"}, []string{"address 192.168.1.100", "netmask 255.255.255.0", "hwaddress ether NotAnAddress"}, "malformed hwaddress option"},
- {[]string{"eth", "inet", "dhcp"}, []string{"hwaddress ether NotAnAddress"}, "malformed hwaddress option"},
- } {
- _, err := parseInterfaceStanza(tt.in, tt.opts)
- if err == nil || !strings.HasPrefix(err.Error(), tt.e) {
- t.Fatalf("bad error parsing interface stanza %q: got %q, want %q", tt.in, err.Error(), tt.e)
- }
- }
- }
- func TestBadParseVLANStanzas(t *testing.T) {
- conf := configMethodManual{}
- options := map[string][]string{}
- for _, in := range []string{"myvlan", "eth.vlan"} {
- _, err := parseVLANStanza(in, conf, nil, options)
- if err == nil || !strings.HasPrefix(err.Error(), "malformed vlan name") {
- t.Fatalf("did not error on bad vlan %q", in)
- }
- }
- }
- func TestSplitStanzas(t *testing.T) {
- expect := [][]string{
- {"auto lo"},
- {"iface eth1", "option: 1"},
- {"mapping"},
- {"allow-"},
- }
- lines := make([]string, 0, 5)
- for _, stanza := range expect {
- for _, line := range stanza {
- lines = append(lines, line)
- }
- }
- stanzas, err := splitStanzas(lines)
- if err != nil {
- t.FailNow()
- }
- for i, stanza := range stanzas {
- if len(stanza) != len(expect[i]) {
- t.FailNow()
- }
- for j, line := range stanza {
- if line != expect[i][j] {
- t.FailNow()
- }
- }
- }
- }
- func TestParseStanzaNil(t *testing.T) {
- defer func() {
- if r := recover(); r == nil {
- t.Fatal("parseStanza(nil) did not panic")
- }
- }()
- parseStanza(nil)
- }
- func TestParseStanzaSuccess(t *testing.T) {
- for _, in := range []string{
- "auto a",
- "iface a inet manual",
- } {
- if _, err := parseStanza([]string{in}); err != nil {
- t.Fatalf("unexpected error parsing stanza %q: %s", in, err)
- }
- }
- }
- func TestParseAutoStanza(t *testing.T) {
- interfaces := []string{"test", "attribute"}
- stanza, err := parseAutoStanza(interfaces, nil)
- if err != nil {
- t.Fatalf("unexpected error parsing auto stanza %q: %s", interfaces, err)
- }
- if !reflect.DeepEqual(stanza.interfaces, interfaces) {
- t.FailNow()
- }
- }
- func TestParseBondStanzaNoSlaves(t *testing.T) {
- bond, err := parseBondStanza("", nil, nil, map[string][]string{})
- if err != nil {
- t.FailNow()
- }
- if bond.options["bond-slaves"] != nil {
- t.FailNow()
- }
- }
- func TestParseBondStanza(t *testing.T) {
- conf := configMethodManual{}
- options := map[string][]string{
- "bond-slaves": {"1", "2"},
- }
- bond, err := parseBondStanza("test", conf, nil, options)
- if err != nil {
- t.FailNow()
- }
- if bond.name != "test" {
- t.FailNow()
- }
- if bond.kind != interfaceBond {
- t.FailNow()
- }
- if bond.configMethod != conf {
- t.FailNow()
- }
- }
- func TestParsePhysicalStanza(t *testing.T) {
- conf := configMethodManual{}
- options := map[string][]string{
- "a": {"1", "2"},
- "b": {"1"},
- }
- physical, err := parsePhysicalStanza("test", conf, nil, options)
- if err != nil {
- t.FailNow()
- }
- if physical.name != "test" {
- t.FailNow()
- }
- if physical.kind != interfacePhysical {
- t.FailNow()
- }
- if physical.configMethod != conf {
- t.FailNow()
- }
- if !reflect.DeepEqual(physical.options, options) {
- t.FailNow()
- }
- }
- func TestParseVLANStanzas(t *testing.T) {
- conf := configMethodManual{}
- options := map[string][]string{}
- for _, in := range []string{"vlan25", "eth.25"} {
- vlan, err := parseVLANStanza(in, conf, nil, options)
- if err != nil {
- t.Fatalf("unexpected error from parseVLANStanza(%q): %s", in, err)
- }
- if !reflect.DeepEqual(vlan.options["id"], []string{"25"}) {
- t.FailNow()
- }
- }
- }
- func TestParseInterfaceStanzaStaticAddress(t *testing.T) {
- options := []string{"address 192.168.1.100", "netmask 255.255.255.0"}
- expect := []net.IPNet{
- {
- IP: net.IPv4(192, 168, 1, 100),
- Mask: net.IPv4Mask(255, 255, 255, 0),
- },
- }
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "static"}, options)
- if err != nil {
- t.FailNow()
- }
- static, ok := iface.configMethod.(configMethodStatic)
- if !ok {
- t.FailNow()
- }
- if !reflect.DeepEqual(static.addresses, expect) {
- t.FailNow()
- }
- }
- func TestParseInterfaceStanzaStaticGateway(t *testing.T) {
- options := []string{"address 192.168.1.100", "netmask 255.255.255.0", "gateway 192.168.1.1"}
- expect := []route{
- {
- destination: net.IPNet{
- IP: net.IPv4(0, 0, 0, 0),
- Mask: net.IPv4Mask(0, 0, 0, 0),
- },
- gateway: net.IPv4(192, 168, 1, 1),
- },
- }
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "static"}, options)
- if err != nil {
- t.FailNow()
- }
- static, ok := iface.configMethod.(configMethodStatic)
- if !ok {
- t.FailNow()
- }
- if !reflect.DeepEqual(static.routes, expect) {
- t.FailNow()
- }
- }
- func TestParseInterfaceStanzaStaticDNS(t *testing.T) {
- options := []string{"address 192.168.1.100", "netmask 255.255.255.0", "dns-nameservers 192.168.1.10 192.168.1.11 192.168.1.12"}
- expect := []net.IP{
- net.IPv4(192, 168, 1, 10),
- net.IPv4(192, 168, 1, 11),
- net.IPv4(192, 168, 1, 12),
- }
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "static"}, options)
- if err != nil {
- t.FailNow()
- }
- static, ok := iface.configMethod.(configMethodStatic)
- if !ok {
- t.FailNow()
- }
- if !reflect.DeepEqual(static.nameservers, expect) {
- t.FailNow()
- }
- }
- func TestBadParseInterfaceStanzasStaticPostUp(t *testing.T) {
- for _, in := range []string{
- "post-up invalid",
- "post-up route add",
- "post-up route add -net",
- "post-up route add gw",
- "post-up route add netmask",
- "gateway",
- "gateway 192.168.1.1 192.168.1.2",
- } {
- options := []string{"address 192.168.1.100", "netmask 255.255.255.0", in}
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "static"}, options)
- if err != nil {
- t.Fatalf("parseInterfaceStanza with options %s got unexpected error", options)
- }
- static, ok := iface.configMethod.(configMethodStatic)
- if !ok {
- t.Fatalf("parseInterfaceStanza with options %s did not return configMethodStatic", options)
- }
- if len(static.routes) != 0 {
- t.Fatalf("parseInterfaceStanza with options %s did not return zero-length static routes", options)
- }
- }
- }
- func TestParseInterfaceStanzaStaticPostUp(t *testing.T) {
- 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{
- {
- destination: net.IPNet{
- IP: net.IPv4(192, 168, 1, 0),
- Mask: net.IPv4Mask(255, 255, 255, 0),
- },
- 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 {
- _, net, err := net.ParseCIDR("192.168.1.0/24")
- if err != nil {
- panic(err)
- }
- return *net
- }(),
- gateway: net.IPv4(192, 168, 1, 1),
- },
- },
- },
- } {
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "static"}, tt.options)
- if err != nil {
- t.Fatalf("bad error (%+v): want nil, got %s\n", tt, err)
- }
- static, ok := iface.configMethod.(configMethodStatic)
- if !ok {
- 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)
- }
- }
- }
- func TestParseInterfaceStanzaLoopback(t *testing.T) {
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "loopback"}, nil)
- if err != nil {
- t.FailNow()
- }
- if _, ok := iface.configMethod.(configMethodLoopback); !ok {
- t.FailNow()
- }
- }
- func TestParseInterfaceStanzaManual(t *testing.T) {
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "manual"}, nil)
- if err != nil {
- t.FailNow()
- }
- if _, ok := iface.configMethod.(configMethodManual); !ok {
- t.FailNow()
- }
- }
- func TestParseInterfaceStanzaDHCP(t *testing.T) {
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "dhcp"}, nil)
- if err != nil {
- t.FailNow()
- }
- if _, ok := iface.configMethod.(configMethodDHCP); !ok {
- t.FailNow()
- }
- }
- func TestParseInterfaceStanzaPostUpOption(t *testing.T) {
- options := []string{
- "post-up",
- "post-up 1 2",
- "post-up 3 4",
- }
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "manual"}, options)
- if err != nil {
- t.FailNow()
- }
- if !reflect.DeepEqual(iface.options["post-up"], []string{"1 2", "3 4"}) {
- t.Log(iface.options["post-up"])
- t.FailNow()
- }
- }
- func TestParseInterfaceStanzaPreDownOption(t *testing.T) {
- options := []string{
- "pre-down",
- "pre-down 3",
- "pre-down 4",
- }
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "manual"}, options)
- if err != nil {
- t.FailNow()
- }
- if !reflect.DeepEqual(iface.options["pre-down"], []string{"3", "4"}) {
- t.Log(iface.options["pre-down"])
- t.FailNow()
- }
- }
- func TestParseInterfaceStanzaEmptyOption(t *testing.T) {
- options := []string{
- "test",
- }
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "manual"}, options)
- if err != nil {
- t.FailNow()
- }
- if !reflect.DeepEqual(iface.options["test"], []string{}) {
- t.FailNow()
- }
- }
- func TestParseInterfaceStanzaOptions(t *testing.T) {
- options := []string{
- "test1 1",
- "test2 2 3",
- "test1 5 6",
- }
- iface, err := parseInterfaceStanza([]string{"eth", "inet", "manual"}, options)
- if err != nil {
- t.FailNow()
- }
- if !reflect.DeepEqual(iface.options["test1"], []string{"5", "6"}) {
- t.Log(iface.options["test1"])
- t.FailNow()
- }
- if !reflect.DeepEqual(iface.options["test2"], []string{"2", "3"}) {
- t.Log(iface.options["test2"])
- t.FailNow()
- }
- }
- func TestParseInterfaceStanzaHwaddress(t *testing.T) {
- for _, tt := range []struct {
- attr []string
- opt []string
- hw net.HardwareAddr
- }{
- {
- []string{"mybond", "inet", "dhcp"},
- []string{},
- nil,
- },
- {
- []string{"mybond", "inet", "dhcp"},
- []string{"hwaddress ether 00:01:02:03:04:05"},
- net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5}),
- },
- {
- []string{"mybond", "inet", "static"},
- []string{"hwaddress ether 00:01:02:03:04:05", "address 192.168.1.100", "netmask 255.255.255.0"},
- net.HardwareAddr([]byte{0, 1, 2, 3, 4, 5}),
- },
- } {
- iface, err := parseInterfaceStanza(tt.attr, tt.opt)
- if err != nil {
- t.Fatalf("error in parseInterfaceStanza (%q, %q): %q", tt.attr, tt.opt, err)
- }
- switch c := iface.configMethod.(type) {
- case configMethodStatic:
- if !reflect.DeepEqual(c.hwaddress, tt.hw) {
- t.Fatalf("bad hwaddress (%q, %q): got %q, want %q", tt.attr, tt.opt, c.hwaddress, tt.hw)
- }
- case configMethodDHCP:
- if !reflect.DeepEqual(c.hwaddress, tt.hw) {
- t.Fatalf("bad hwaddress (%q, %q): got %q, want %q", tt.attr, tt.opt, c.hwaddress, tt.hw)
- }
- }
- }
- }
- func TestParseInterfaceStanzaBond(t *testing.T) {
- iface, err := parseInterfaceStanza([]string{"mybond", "inet", "manual"}, []string{"bond-slaves eth"})
- if err != nil {
- t.FailNow()
- }
- if iface.kind != interfaceBond {
- t.FailNow()
- }
- }
- func TestParseInterfaceStanzaVLANName(t *testing.T) {
- iface, err := parseInterfaceStanza([]string{"eth0.1", "inet", "manual"}, nil)
- if err != nil {
- t.FailNow()
- }
- if iface.kind != interfaceVLAN {
- t.FailNow()
- }
- }
- func TestParseInterfaceStanzaVLANOption(t *testing.T) {
- iface, err := parseInterfaceStanza([]string{"vlan1", "inet", "manual"}, []string{"vlan_raw_device eth"})
- if err != nil {
- t.FailNow()
- }
- if iface.kind != interfaceVLAN {
- t.FailNow()
- }
- }
- func TestParseStanzasNone(t *testing.T) {
- stanzas, err := parseStanzas(nil)
- if err != nil {
- t.FailNow()
- }
- if len(stanzas) != 0 {
- t.FailNow()
- }
- }
- func TestParseStanzas(t *testing.T) {
- lines := []string{
- "auto lo",
- "iface lo inet loopback",
- "iface eth1 inet manual",
- "iface eth2 inet manual",
- "iface eth3 inet manual",
- "auto eth1 eth3",
- }
- expect := []stanza{
- &stanzaAuto{
- interfaces: []string{"lo"},
- },
- &stanzaInterface{
- name: "lo",
- kind: interfacePhysical,
- auto: true,
- configMethod: configMethodLoopback{},
- options: map[string][]string{},
- },
- &stanzaInterface{
- name: "eth1",
- kind: interfacePhysical,
- auto: true,
- configMethod: configMethodManual{},
- options: map[string][]string{},
- },
- &stanzaInterface{
- name: "eth2",
- kind: interfacePhysical,
- auto: false,
- configMethod: configMethodManual{},
- options: map[string][]string{},
- },
- &stanzaInterface{
- name: "eth3",
- kind: interfacePhysical,
- auto: true,
- configMethod: configMethodManual{},
- options: map[string][]string{},
- },
- &stanzaAuto{
- interfaces: []string{"eth1", "eth3"},
- },
- }
- stanzas, err := parseStanzas(lines)
- if err != err {
- t.FailNow()
- }
- if !reflect.DeepEqual(stanzas, expect) {
- t.FailNow()
- }
- }
|