Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
tests: Add tests for macvlan and macvtap endpoints
Browse files Browse the repository at this point in the history
Add unit tests in network_test.go and qemu_arch_base_test.go
for macvlan and macvtap network endpoints.

Signed-off-by: Archana Shinde <archana.m.shinde@intel.com>
  • Loading branch information
amshinde committed Oct 3, 2018
1 parent def070d commit 378191a
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
101 changes: 101 additions & 0 deletions virtcontainers/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ func TestVirtualEndpointTypeSet(t *testing.T) {
testEndpointTypeSet(t, "virtual", VirtualEndpointType)
}

func TestVhostUserEndpointTypeSet(t *testing.T) {
testEndpointTypeSet(t, "vhost-user", VhostUserEndpointType)
}

func TestBridgedMacvlanEndpointTypeSet(t *testing.T) {
testEndpointTypeSet(t, "macvlan", BridgedMacvlanEndpointType)
}

func TestMacvtapEndpointTypeSet(t *testing.T) {
testEndpointTypeSet(t, "macvtap", MacvtapEndpointType)
}

func TestEndpointTypeSetFailure(t *testing.T) {
var endpointType EndpointType

Expand Down Expand Up @@ -167,6 +179,21 @@ func TestVirtualEndpointTypeString(t *testing.T) {
testEndpointTypeString(t, &endpointType, string(VirtualEndpointType))
}

func TestVhostUserEndpointTypeString(t *testing.T) {
endpointType := VhostUserEndpointType
testEndpointTypeString(t, &endpointType, string(VhostUserEndpointType))
}

func TestBridgedMacvlanEndpointTypeString(t *testing.T) {
endpointType := BridgedMacvlanEndpointType
testEndpointTypeString(t, &endpointType, string(BridgedMacvlanEndpointType))
}

func TestMacvtapEndpointTypeString(t *testing.T) {
endpointType := MacvtapEndpointType
testEndpointTypeString(t, &endpointType, string(MacvtapEndpointType))
}

func TestIncorrectEndpointTypeString(t *testing.T) {
var endpointType EndpointType
testEndpointTypeString(t, &endpointType, "")
Expand Down Expand Up @@ -293,6 +320,62 @@ func TestCreateVirtualNetworkEndpointInvalidArgs(t *testing.T) {
}
}

func TestCreateBridgedMacvlanEndpoint(t *testing.T) {
macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x04}

expected := &BridgedMacvlanEndpoint{
NetPair: NetworkInterfacePair{
ID: "uniqueTestID-4",
Name: "br4_kata",
VirtIface: NetworkInterface{
Name: "eth4",
HardAddr: macAddr.String(),
},
TAPIface: NetworkInterface{
Name: "tap4_kata",
},
NetInterworkingModel: DefaultNetInterworkingModel,
},
EndpointType: BridgedMacvlanEndpointType,
}

result, err := createBridgedMacvlanNetworkEndpoint(4, "", DefaultNetInterworkingModel)
if err != nil {
t.Fatal(err)
}

// the resulting ID will be random - so let's overwrite to test the rest of the flow
result.NetPair.ID = "uniqueTestID-4"

// the resulting mac address will be random - so lets overwrite it
result.NetPair.VirtIface.HardAddr = macAddr.String()

if reflect.DeepEqual(result, expected) == false {
t.Fatalf("\nGot: %+v, \n\nExpected: %+v", result, expected)
}
}

func TestCreateMacvtapEndpoint(t *testing.T) {
netInfo := NetworkInfo{
Iface: NetlinkIface{
Type: "macvtap",
},
}
expected := &MacvtapEndpoint{
EndpointType: MacvtapEndpointType,
EndpointProperties: netInfo,
}

result, err := createMacvtapNetworkEndpoint(netInfo)
if err != nil {
t.Fatal(err)
}

if reflect.DeepEqual(result, expected) == false {
t.Fatalf("\nGot: %+v, \n\nExpected: %+v", result, expected)
}
}

func TestIsPhysicalIface(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip(testDisabledAsNonRoot)
Expand Down Expand Up @@ -605,3 +688,21 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) {
"Routes returned didn't match: got %+v, expecting %+v", resRoutes, expectedRoutes)

}

func TestGenerateRandomPrivateMacAdd(t *testing.T) {
assert := assert.New(t)

addr1, err := generateRandomPrivateMacAddr()
assert.NoError(err)

_, err = net.ParseMAC(addr1)
assert.NoError(err)

addr2, err := generateRandomPrivateMacAddr()
assert.NoError(err)

_, err = net.ParseMAC(addr2)
assert.NoError(err)

assert.NotEqual(addr1, addr2)
}
63 changes: 63 additions & 0 deletions virtcontainers/qemu_arch_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package virtcontainers
import (
"fmt"
"io/ioutil"
"net"
"path/filepath"
"testing"

Expand Down Expand Up @@ -432,3 +433,65 @@ func TestQemuArchBaseAppendSCSIController(t *testing.T) {
_, ioThread = qemuArchBase.appendSCSIController(devices, true)
assert.NotNil(ioThread)
}

func TestQemuArchBaseAppendNetwork(t *testing.T) {
var devices []govmmQemu.Device
assert := assert.New(t)
qemuArchBase := newQemuArchBase()

macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x04}

macvlanEp := &BridgedMacvlanEndpoint{
NetPair: NetworkInterfacePair{
ID: "uniqueTestID-4",
Name: "br4_kata",
VirtIface: NetworkInterface{
Name: "eth4",
HardAddr: macAddr.String(),
},
TAPIface: NetworkInterface{
Name: "tap4_kata",
},
NetInterworkingModel: DefaultNetInterworkingModel,
},
EndpointType: BridgedMacvlanEndpointType,
}

macvtapEp := &MacvtapEndpoint{
EndpointType: MacvtapEndpointType,
EndpointProperties: NetworkInfo{
Iface: NetlinkIface{
Type: "macvtap",
},
},
}

expectedOut := []govmmQemu.Device{
govmmQemu.NetDevice{
Type: networkModelToQemuType(macvlanEp.NetPair.NetInterworkingModel),
Driver: govmmQemu.VirtioNetPCI,
ID: fmt.Sprintf("network-%d", 0),
IFName: macvlanEp.NetPair.TAPIface.Name,
MACAddress: macvlanEp.NetPair.TAPIface.HardAddr,
DownScript: "no",
Script: "no",
FDs: macvlanEp.NetPair.VMFds,
VhostFDs: macvlanEp.NetPair.VhostFds,
},
govmmQemu.NetDevice{
Type: govmmQemu.MACVTAP,
Driver: govmmQemu.VirtioNetPCI,
ID: fmt.Sprintf("network-%d", 1),
IFName: macvtapEp.Name(),
MACAddress: macvtapEp.HardwareAddr(),
DownScript: "no",
Script: "no",
FDs: macvtapEp.VMFds,
VhostFDs: macvtapEp.VhostFds,
},
}

devices = qemuArchBase.appendNetwork(devices, macvlanEp)
devices = qemuArchBase.appendNetwork(devices, macvtapEp)
assert.Equal(expectedOut, devices)
}

0 comments on commit 378191a

Please sign in to comment.