github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/libnetwork/drivers/macvlan/macvlan_setup_test.go (about) 1 package macvlan 2 3 import ( 4 "testing" 5 6 "github.com/vishvananda/netlink" 7 ) 8 9 // TestValidateLink tests the parentExists function 10 func TestValidateLink(t *testing.T) { 11 validIface := "lo" 12 invalidIface := "foo12345" 13 14 // test a valid parent interface validation 15 if ok := parentExists(validIface); !ok { 16 t.Fatalf("failed validating loopback %s", validIface) 17 } 18 // test an invalid parent interface validation 19 if ok := parentExists(invalidIface); ok { 20 t.Fatalf("failed to invalidate interface %s", invalidIface) 21 } 22 } 23 24 // TestValidateSubLink tests valid 802.1q naming convention 25 func TestValidateSubLink(t *testing.T) { 26 validSubIface := "lo.10" 27 invalidSubIface1 := "lo" 28 invalidSubIface2 := "lo:10" 29 invalidSubIface3 := "foo123.456" 30 31 // test a valid parent_iface.vlan_id 32 _, _, err := parseVlan(validSubIface) 33 if err != nil { 34 t.Fatalf("failed subinterface validation: %v", err) 35 } 36 // test an invalid vid with a valid parent link 37 _, _, err = parseVlan(invalidSubIface1) 38 if err == nil { 39 t.Fatalf("failed subinterface validation test: %s", invalidSubIface1) 40 } 41 // test a valid vid with a valid parent link with an invalid delimiter 42 _, _, err = parseVlan(invalidSubIface2) 43 if err == nil { 44 t.Fatalf("failed subinterface validation test: %v", invalidSubIface2) 45 } 46 // test an invalid parent link with a valid vid 47 _, _, err = parseVlan(invalidSubIface3) 48 if err == nil { 49 t.Fatalf("failed subinterface validation test: %v", invalidSubIface3) 50 } 51 } 52 53 // TestSetMacVlanMode tests the macvlan mode setter 54 func TestSetMacVlanMode(t *testing.T) { 55 // test macvlan bridge mode 56 mode, err := setMacVlanMode(modeBridge) 57 if err != nil { 58 t.Fatalf("error parsing %v vlan mode: %v", mode, err) 59 } 60 if mode != netlink.MACVLAN_MODE_BRIDGE { 61 t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_BRIDGE, mode) 62 } 63 // test macvlan passthrough mode 64 mode, err = setMacVlanMode(modePassthru) 65 if err != nil { 66 t.Fatalf("error parsing %v vlan mode: %v", mode, err) 67 } 68 if mode != netlink.MACVLAN_MODE_PASSTHRU { 69 t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_PASSTHRU, mode) 70 } 71 // test macvlan private mode 72 mode, err = setMacVlanMode(modePrivate) 73 if err != nil { 74 t.Fatalf("error parsing %v vlan mode: %v", mode, err) 75 } 76 if mode != netlink.MACVLAN_MODE_PRIVATE { 77 t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_PRIVATE, mode) 78 } 79 // test macvlan vepa mode 80 mode, err = setMacVlanMode(modeVepa) 81 if err != nil { 82 t.Fatalf("error parsing %v vlan mode: %v", mode, err) 83 } 84 if mode != netlink.MACVLAN_MODE_VEPA { 85 t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_VEPA, mode) 86 } 87 // test invalid mode 88 mode, err = setMacVlanMode("foo") 89 if err == nil { 90 t.Fatal("invalid macvlan mode should have returned an error") 91 } 92 if mode != 0 { 93 t.Fatalf("expected 0 got %d", mode) 94 } 95 // test null mode 96 mode, err = setMacVlanMode("") 97 if err == nil { 98 t.Fatal("invalid macvlan mode should have returned an error") 99 } 100 if mode != 0 { 101 t.Fatalf("expected 0 got %d", mode) 102 } 103 }