github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/libnetwork/drivers/ipvlan/ipvlan_setup_test.go (about)

     1  // +build linux
     2  
     3  package ipvlan
     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  // TestSetIPVlanMode tests the ipvlan mode setter
    56  func TestSetIPVlanMode(t *testing.T) {
    57  	// test ipvlan l2 mode
    58  	mode, err := setIPVlanMode(modeL2)
    59  	if err != nil {
    60  		t.Fatalf("error parsing %v vlan mode: %v", mode, err)
    61  	}
    62  	if mode != netlink.IPVLAN_MODE_L2 {
    63  		t.Fatalf("expected %d got %d", netlink.IPVLAN_MODE_L2, mode)
    64  	}
    65  	// test ipvlan l3 mode
    66  	mode, err = setIPVlanMode(modeL3)
    67  	if err != nil {
    68  		t.Fatalf("error parsing %v vlan mode: %v", mode, err)
    69  	}
    70  	if mode != netlink.IPVLAN_MODE_L3 {
    71  		t.Fatalf("expected %d got %d", netlink.IPVLAN_MODE_L3, mode)
    72  	}
    73  	// test invalid mode
    74  	mode, err = setIPVlanMode("foo")
    75  	if err == nil {
    76  		t.Fatal("invalid ipvlan mode should have returned an error")
    77  	}
    78  	if mode != 0 {
    79  		t.Fatalf("expected 0 got %d", mode)
    80  	}
    81  	// test null mode
    82  	mode, err = setIPVlanMode("")
    83  	if err == nil {
    84  		t.Fatal("invalid ipvlan mode should have returned an error")
    85  	}
    86  	if mode != 0 {
    87  		t.Fatalf("expected 0 got %d", mode)
    88  	}
    89  }