github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/physical_endpoint_test.go (about)

     1  // Copyright (c) 2018 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package virtcontainers
     7  
     8  import (
     9  	"net"
    10  	"testing"
    11  
    12  	"github.com/containernetworking/plugins/pkg/ns"
    13  	"github.com/containernetworking/plugins/pkg/testutils"
    14  	ktu "github.com/kata-containers/runtime/pkg/katatestutils"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/vishvananda/netlink"
    17  	"github.com/vishvananda/netns"
    18  )
    19  
    20  func TestPhysicalEndpoint_HotAttach(t *testing.T) {
    21  	assert := assert.New(t)
    22  	v := &PhysicalEndpoint{
    23  		IfaceName: "eth0",
    24  		HardAddr:  net.HardwareAddr{0x02, 0x00, 0xca, 0xfe, 0x00, 0x04}.String(),
    25  	}
    26  
    27  	h := &mockHypervisor{}
    28  
    29  	err := v.HotAttach(h)
    30  	assert.Error(err)
    31  }
    32  
    33  func TestPhysicalEndpoint_HotDetach(t *testing.T) {
    34  	assert := assert.New(t)
    35  	v := &PhysicalEndpoint{
    36  		IfaceName: "eth0",
    37  		HardAddr:  net.HardwareAddr{0x02, 0x00, 0xca, 0xfe, 0x00, 0x04}.String(),
    38  	}
    39  
    40  	h := &mockHypervisor{}
    41  
    42  	err := v.HotDetach(h, true, "")
    43  	assert.Error(err)
    44  }
    45  
    46  func TestIsPhysicalIface(t *testing.T) {
    47  	assert := assert.New(t)
    48  
    49  	if tc.NotValid(ktu.NeedRoot()) {
    50  		t.Skip(testDisabledAsNonRoot)
    51  	}
    52  
    53  	testNetIface := "testIface0"
    54  	testMTU := 1500
    55  	testMACAddr := "00:00:00:00:00:01"
    56  
    57  	hwAddr, err := net.ParseMAC(testMACAddr)
    58  	assert.NoError(err)
    59  
    60  	link := &netlink.Bridge{
    61  		LinkAttrs: netlink.LinkAttrs{
    62  			Name:         testNetIface,
    63  			MTU:          testMTU,
    64  			HardwareAddr: hwAddr,
    65  			TxQLen:       -1,
    66  		},
    67  	}
    68  
    69  	n, err := testutils.NewNS()
    70  	assert.NoError(err)
    71  	defer n.Close()
    72  
    73  	netnsHandle, err := netns.GetFromPath(n.Path())
    74  	assert.NoError(err)
    75  	defer netnsHandle.Close()
    76  
    77  	netlinkHandle, err := netlink.NewHandleAt(netnsHandle)
    78  	assert.NoError(err)
    79  	defer netlinkHandle.Delete()
    80  
    81  	err = netlinkHandle.LinkAdd(link)
    82  	assert.NoError(err)
    83  
    84  	var isPhysical bool
    85  	err = doNetNS(n.Path(), func(_ ns.NetNS) error {
    86  		var err error
    87  		isPhysical, err = isPhysicalIface(testNetIface)
    88  		return err
    89  	})
    90  	assert.NoError(err)
    91  	assert.False(isPhysical)
    92  }