gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/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 "io/ioutil" 10 "net" 11 "os" 12 "reflect" 13 "testing" 14 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func testEndpointTypeSet(t *testing.T, value string, expected EndpointType) { 19 var endpointType EndpointType 20 21 err := endpointType.Set(value) 22 assert.NoError(t, err) 23 assert.Equal(t, endpointType, expected) 24 } 25 26 func TestPhysicalEndpointTypeSet(t *testing.T) { 27 testEndpointTypeSet(t, "physical", PhysicalEndpointType) 28 } 29 30 func TestVethEndpointTypeSet(t *testing.T) { 31 testEndpointTypeSet(t, "virtual", VethEndpointType) 32 } 33 34 func TestVhostUserEndpointTypeSet(t *testing.T) { 35 testEndpointTypeSet(t, "vhost-user", VhostUserEndpointType) 36 } 37 38 func TestBridgedMacvlanEndpointTypeSet(t *testing.T) { 39 testEndpointTypeSet(t, "macvlan", BridgedMacvlanEndpointType) 40 } 41 42 func TestMacvtapEndpointTypeSet(t *testing.T) { 43 testEndpointTypeSet(t, "macvtap", MacvtapEndpointType) 44 } 45 46 func TestEndpointTypeSetFailure(t *testing.T) { 47 var endpointType EndpointType 48 49 assert.Error(t, endpointType.Set("wrong-value")) 50 } 51 52 func testEndpointTypeString(t *testing.T, endpointType *EndpointType, expected string) { 53 result := endpointType.String() 54 assert.Equal(t, result, expected) 55 } 56 57 func TestPhysicalEndpointTypeString(t *testing.T) { 58 endpointType := PhysicalEndpointType 59 testEndpointTypeString(t, &endpointType, string(PhysicalEndpointType)) 60 } 61 62 func TestVethEndpointTypeString(t *testing.T) { 63 endpointType := VethEndpointType 64 testEndpointTypeString(t, &endpointType, string(VethEndpointType)) 65 } 66 67 func TestVhostUserEndpointTypeString(t *testing.T) { 68 endpointType := VhostUserEndpointType 69 testEndpointTypeString(t, &endpointType, string(VhostUserEndpointType)) 70 } 71 72 func TestBridgedMacvlanEndpointTypeString(t *testing.T) { 73 endpointType := BridgedMacvlanEndpointType 74 testEndpointTypeString(t, &endpointType, string(BridgedMacvlanEndpointType)) 75 } 76 77 func TestMacvtapEndpointTypeString(t *testing.T) { 78 endpointType := MacvtapEndpointType 79 testEndpointTypeString(t, &endpointType, string(MacvtapEndpointType)) 80 } 81 82 func TestIncorrectEndpointTypeString(t *testing.T) { 83 var endpointType EndpointType 84 testEndpointTypeString(t, &endpointType, "") 85 } 86 87 func TestSaveLoadIfPair(t *testing.T) { 88 macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x04} 89 90 tmpfile, err := ioutil.TempFile("", "vc-save-load-net-") 91 assert.Nil(t, err) 92 defer os.Remove(tmpfile.Name()) 93 94 netPair := &NetworkInterfacePair{ 95 TapInterface: TapInterface{ 96 ID: "uniqueTestID-4", 97 Name: "br4_kata", 98 TAPIface: NetworkInterface{ 99 Name: "tap4_kata", 100 HardAddr: macAddr.String(), 101 }, 102 VMFds: []*os.File{tmpfile}, // won't be saved to disk 103 VhostFds: []*os.File{tmpfile}, // won't be saved to disk 104 }, 105 VirtIface: NetworkInterface{ 106 Name: "eth4", 107 HardAddr: macAddr.String(), 108 }, 109 NetInterworkingModel: DefaultNetInterworkingModel, 110 } 111 112 // Save to disk then load it back. 113 savedIfPair := saveNetIfPair(netPair) 114 loadedIfPair := loadNetIfPair(savedIfPair) 115 116 // Since VMFds and VhostFds are't saved, netPair and loadedIfPair are not equal. 117 assert.False(t, reflect.DeepEqual(netPair, loadedIfPair)) 118 119 netPair.TapInterface.VMFds = nil 120 netPair.TapInterface.VhostFds = nil 121 // They are equal now. 122 assert.True(t, reflect.DeepEqual(netPair, loadedIfPair)) 123 }