gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/vhostuser_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  	"fmt"
    10  	"net"
    11  	"os"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/vishvananda/netlink"
    16  )
    17  
    18  func TestVhostUserSocketPath(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	// First test case: search for existing:
    22  	addresses := []netlink.Addr{
    23  		{
    24  			IPNet: &net.IPNet{
    25  				IP:   net.IPv4(192, 168, 0, 2),
    26  				Mask: net.IPv4Mask(192, 168, 0, 2),
    27  			},
    28  		},
    29  		{
    30  			IPNet: &net.IPNet{
    31  				IP:   net.IPv4(192, 168, 0, 1),
    32  				Mask: net.IPv4Mask(192, 168, 0, 1),
    33  			},
    34  		},
    35  	}
    36  
    37  	expectedPath := "/tmp/vhostuser_192.168.0.1"
    38  	expectedFileName := "vhu.sock"
    39  	expectedResult := fmt.Sprintf("%s/%s", expectedPath, expectedFileName)
    40  
    41  	err := os.Mkdir(expectedPath, 0777)
    42  	assert.NoError(err)
    43  
    44  	_, err = os.Create(expectedResult)
    45  	assert.NoError(err)
    46  	netinfo := NetworkInfo{
    47  		Addrs: addresses,
    48  	}
    49  
    50  	path, _ := vhostUserSocketPath(netinfo)
    51  	assert.Equal(path, expectedResult)
    52  
    53  	// Second test case: search doesn't include matching vsock:
    54  	addressesFalse := []netlink.Addr{
    55  		{
    56  			IPNet: &net.IPNet{
    57  				IP:   net.IPv4(192, 168, 0, 4),
    58  				Mask: net.IPv4Mask(192, 168, 0, 4),
    59  			},
    60  		},
    61  	}
    62  	netinfoFail := NetworkInfo{
    63  		Addrs: addressesFalse,
    64  	}
    65  
    66  	path, _ = vhostUserSocketPath(netinfoFail)
    67  	assert.Empty(path)
    68  
    69  	assert.NoError(os.Remove(expectedResult))
    70  	assert.NoError(os.Remove(expectedPath))
    71  }
    72  
    73  func TestVhostUserEndpointAttach(t *testing.T) {
    74  	assert := assert.New(t)
    75  	v := &VhostUserEndpoint{
    76  		SocketPath:   "/tmp/sock",
    77  		HardAddr:     "mac-addr",
    78  		EndpointType: VhostUserEndpointType,
    79  	}
    80  
    81  	h := &mockHypervisor{}
    82  
    83  	err := v.Attach(h)
    84  	assert.NoError(err)
    85  }
    86  
    87  func TestVhostUserEndpoint_HotAttach(t *testing.T) {
    88  	assert := assert.New(t)
    89  	v := &VhostUserEndpoint{
    90  		SocketPath:   "/tmp/sock",
    91  		HardAddr:     "mac-addr",
    92  		EndpointType: VhostUserEndpointType,
    93  	}
    94  
    95  	h := &mockHypervisor{}
    96  
    97  	err := v.HotAttach(h)
    98  	assert.Error(err)
    99  }
   100  
   101  func TestVhostUserEndpoint_HotDetach(t *testing.T) {
   102  	assert := assert.New(t)
   103  	v := &VhostUserEndpoint{
   104  		SocketPath:   "/tmp/sock",
   105  		HardAddr:     "mac-addr",
   106  		EndpointType: VhostUserEndpointType,
   107  	}
   108  
   109  	h := &mockHypervisor{}
   110  
   111  	err := v.HotDetach(h, true, "")
   112  	assert.Error(err)
   113  }
   114  
   115  func TestCreateVhostUserEndpoint(t *testing.T) {
   116  	macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x48}
   117  	ifcName := "vhost-deadbeef"
   118  	socket := "/tmp/vhu_192.168.0.1"
   119  	assert := assert.New(t)
   120  
   121  	netinfo := NetworkInfo{
   122  		Iface: NetlinkIface{
   123  			LinkAttrs: netlink.LinkAttrs{
   124  				HardwareAddr: macAddr,
   125  				Name:         ifcName,
   126  			},
   127  		},
   128  	}
   129  
   130  	expected := &VhostUserEndpoint{
   131  		SocketPath:   socket,
   132  		HardAddr:     macAddr.String(),
   133  		IfaceName:    ifcName,
   134  		EndpointType: VhostUserEndpointType,
   135  	}
   136  
   137  	result, err := createVhostUserEndpoint(netinfo, socket)
   138  	assert.NoError(err)
   139  	assert.Exactly(result, expected)
   140  }