github.com/rumpl/bof@v23.0.0-rc.2+incompatible/libnetwork/drivers/macvlan/macvlan_setup_test.go (about)

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