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

Commit

Permalink
network: Refactor network tests.
Browse files Browse the repository at this point in the history
Split network_test.go into separate test files.

Signed-off-by: Archana Shinde <[email protected]>
  • Loading branch information
amshinde committed Oct 11, 2018
1 parent adcd910 commit df8f21d
Show file tree
Hide file tree
Showing 7 changed files with 580 additions and 506 deletions.
47 changes: 47 additions & 0 deletions virtcontainers/bridgedmacvlan_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

package virtcontainers

import (
"net"
"reflect"
"testing"
)

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)
}
}
89 changes: 89 additions & 0 deletions virtcontainers/endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

package virtcontainers

import "testing"

func testEndpointTypeSet(t *testing.T, value string, expected EndpointType) {
//var netModel NetworkModel
var endpointType EndpointType

err := endpointType.Set(value)
if err != nil {
t.Fatal(err)
}

if endpointType != expected {
t.Fatal()
}
}

func TestPhysicalEndpointTypeSet(t *testing.T) {
testEndpointTypeSet(t, "physical", PhysicalEndpointType)
}

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

err := endpointType.Set("wrong-value")
if err == nil {
t.Fatal(err)
}
}

func testEndpointTypeString(t *testing.T, endpointType *EndpointType, expected string) {
result := endpointType.String()

if result != expected {
t.Fatal()
}
}

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

func TestVirtualEndpointTypeString(t *testing.T) {
endpointType := VirtualEndpointType
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, "")
}
32 changes: 32 additions & 0 deletions virtcontainers/macvtap_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

package virtcontainers

import (
"reflect"
"testing"
)

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)
}
}
Loading

0 comments on commit df8f21d

Please sign in to comment.