github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libnetwork/drivers/macvlan/macvlan_setup_test.go (about)

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