github.com/vishvananda/netlink@v1.3.0/bridge_linux_test.go (about)

     1  package netlink
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"testing"
     7  )
     8  
     9  func TestBridgeVlan(t *testing.T) {
    10  	minKernelRequired(t, 3, 10)
    11  
    12  	tearDown := setUpNetlinkTest(t)
    13  	defer tearDown()
    14  	if err := remountSysfs(); err != nil {
    15  		t.Fatal(err)
    16  	}
    17  	bridgeName := "foo"
    18  	bridge := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeName}}
    19  	if err := LinkAdd(bridge); err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	if err := ioutil.WriteFile(fmt.Sprintf("/sys/devices/virtual/net/%s/bridge/vlan_filtering", bridgeName), []byte("1"), 0644); err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	if vlanMap, err := BridgeVlanList(); err != nil {
    26  		t.Fatal(err)
    27  	} else {
    28  		if len(vlanMap) != 1 {
    29  			t.Fatal()
    30  		}
    31  		if vInfo, ok := vlanMap[int32(bridge.Index)]; !ok {
    32  			t.Fatal("vlanMap should include foo port vlan info")
    33  		} else {
    34  			if len(vInfo) != 1 {
    35  				t.Fatal()
    36  			} else {
    37  				if !vInfo[0].EngressUntag() || !vInfo[0].PortVID() || vInfo[0].Vid != 1 {
    38  					t.Fatalf("bridge vlan show get wrong return %s", vInfo[0].String())
    39  				}
    40  			}
    41  		}
    42  	}
    43  	dummy := &Dummy{LinkAttrs: LinkAttrs{Name: "dum1"}}
    44  	if err := LinkAdd(dummy); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	if err := LinkSetMaster(dummy, bridge); err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	if err := BridgeVlanAdd(dummy, 2, false, false, false, false); err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	if err := BridgeVlanAdd(dummy, 3, true, true, false, false); err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	if err := BridgeVlanAddRange(dummy, 4, 6, false, false, false, false); err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	if vlanMap, err := BridgeVlanList(); err != nil {
    60  		t.Fatal(err)
    61  	} else {
    62  		if len(vlanMap) != 2 {
    63  			t.Fatal()
    64  		}
    65  		if vInfo, ok := vlanMap[int32(bridge.Index)]; !ok {
    66  			t.Fatal("vlanMap should include foo port vlan info")
    67  		} else {
    68  			if fmt.Sprintf("%v", vInfo) != "[{Flags:6 Vid:1}]" {
    69  				t.Fatalf("unexpected result %v", vInfo)
    70  			}
    71  		}
    72  		if vInfo, ok := vlanMap[int32(dummy.Index)]; !ok {
    73  			t.Fatal("vlanMap should include dum1 port vlan info")
    74  		} else {
    75  			if fmt.Sprintf("%v", vInfo) != "[{Flags:4 Vid:1} {Flags:0 Vid:2} {Flags:6 Vid:3} {Flags:0 Vid:4} {Flags:0 Vid:5} {Flags:0 Vid:6}]" {
    76  				t.Fatalf("unexpected result %v", vInfo)
    77  			}
    78  		}
    79  	}
    80  }
    81  
    82  func TestBridgeGroupFwdMask(t *testing.T) {
    83  	minKernelRequired(t, 4, 15) //minimal release for per-port group_fwd_mask
    84  	tearDown := setUpNetlinkTest(t)
    85  	defer tearDown()
    86  	if err := remountSysfs(); err != nil {
    87  		t.Fatal(err)
    88  	}
    89  	bridgeName := "foo"
    90  	var mask uint16 = 0xfff0
    91  	bridge := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeName}, GroupFwdMask: &mask}
    92  	if err := LinkAdd(bridge); err != nil {
    93  		t.Fatal(err)
    94  	}
    95  	brlink, err := LinkByName(bridgeName)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	if *(brlink.(*Bridge).GroupFwdMask) != mask {
   100  		t.Fatalf("created bridge has group_fwd_mask value %x, different from expected %x",
   101  			*(brlink.(*Bridge).GroupFwdMask), mask)
   102  	}
   103  	dummyName := "dm1"
   104  	dummy := &Dummy{LinkAttrs: LinkAttrs{Name: dummyName, MasterIndex: brlink.Attrs().Index}}
   105  	if err := LinkAdd(dummy); err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	dmLink, err := LinkByName(dummyName)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	if err = LinkSetBRSlaveGroupFwdMask(dmLink, mask); err != nil {
   113  		t.Fatal(err)
   114  	}
   115  }