mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-28 20:15:51 +00:00
NewNS() api has been moved to testutils package in the cni plugin repo. Signed-off-by: Archana Shinde <archana.m.shinde@intel.com>
93 lines
1.9 KiB
Go
93 lines
1.9 KiB
Go
// Copyright (c) 2018 Intel Corporation
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package virtcontainers
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/containernetworking/plugins/pkg/ns"
|
|
"github.com/containernetworking/plugins/pkg/testutils"
|
|
ktu "github.com/kata-containers/runtime/pkg/katatestutils"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/vishvananda/netlink"
|
|
"github.com/vishvananda/netns"
|
|
)
|
|
|
|
func TestPhysicalEndpoint_HotAttach(t *testing.T) {
|
|
assert := assert.New(t)
|
|
v := &PhysicalEndpoint{
|
|
IfaceName: "eth0",
|
|
HardAddr: net.HardwareAddr{0x02, 0x00, 0xca, 0xfe, 0x00, 0x04}.String(),
|
|
}
|
|
|
|
h := &mockHypervisor{}
|
|
|
|
err := v.HotAttach(h)
|
|
assert.Error(err)
|
|
}
|
|
|
|
func TestPhysicalEndpoint_HotDetach(t *testing.T) {
|
|
assert := assert.New(t)
|
|
v := &PhysicalEndpoint{
|
|
IfaceName: "eth0",
|
|
HardAddr: net.HardwareAddr{0x02, 0x00, 0xca, 0xfe, 0x00, 0x04}.String(),
|
|
}
|
|
|
|
h := &mockHypervisor{}
|
|
|
|
err := v.HotDetach(h, true, "")
|
|
assert.Error(err)
|
|
}
|
|
|
|
func TestIsPhysicalIface(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
if tc.NotValid(ktu.NeedRoot()) {
|
|
t.Skip(testDisabledAsNonRoot)
|
|
}
|
|
|
|
testNetIface := "testIface0"
|
|
testMTU := 1500
|
|
testMACAddr := "00:00:00:00:00:01"
|
|
|
|
hwAddr, err := net.ParseMAC(testMACAddr)
|
|
assert.NoError(err)
|
|
|
|
link := &netlink.Bridge{
|
|
LinkAttrs: netlink.LinkAttrs{
|
|
Name: testNetIface,
|
|
MTU: testMTU,
|
|
HardwareAddr: hwAddr,
|
|
TxQLen: -1,
|
|
},
|
|
}
|
|
|
|
n, err := testutils.NewNS()
|
|
assert.NoError(err)
|
|
defer n.Close()
|
|
|
|
netnsHandle, err := netns.GetFromPath(n.Path())
|
|
assert.NoError(err)
|
|
defer netnsHandle.Close()
|
|
|
|
netlinkHandle, err := netlink.NewHandleAt(netnsHandle)
|
|
assert.NoError(err)
|
|
defer netlinkHandle.Delete()
|
|
|
|
err = netlinkHandle.LinkAdd(link)
|
|
assert.NoError(err)
|
|
|
|
var isPhysical bool
|
|
err = doNetNS(n.Path(), func(_ ns.NetNS) error {
|
|
var err error
|
|
isPhysical, err = isPhysicalIface(testNetIface)
|
|
return err
|
|
})
|
|
assert.NoError(err)
|
|
assert.False(isPhysical)
|
|
}
|