diff --git a/virtcontainers/network_test.go b/virtcontainers/network_test.go index e51d48638b..12333f09d3 100644 --- a/virtcontainers/network_test.go +++ b/virtcontainers/network_test.go @@ -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 @@ -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, "") @@ -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) @@ -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) +} diff --git a/virtcontainers/qemu_arch_base_test.go b/virtcontainers/qemu_arch_base_test.go index 7da056647b..02784c8bac 100644 --- a/virtcontainers/qemu_arch_base_test.go +++ b/virtcontainers/qemu_arch_base_test.go @@ -8,6 +8,7 @@ package virtcontainers import ( "fmt" "io/ioutil" + "net" "path/filepath" "testing" @@ -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) +}