github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/utils/utils_test.go (about)

     1  // Copyright (c) 2017 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package utils
     7  
     8  import (
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  	"reflect"
    14  	"strings"
    15  	"testing"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestFileCopySuccessful(t *testing.T) {
    21  	assert := assert.New(t)
    22  	fileContent := "testContent"
    23  
    24  	srcFile, err := ioutil.TempFile("", "test_src_copy")
    25  	assert.NoError(err)
    26  	defer os.Remove(srcFile.Name())
    27  	defer srcFile.Close()
    28  
    29  	dstFile, err := ioutil.TempFile("", "test_dst_copy")
    30  	assert.NoError(err)
    31  	defer os.Remove(dstFile.Name())
    32  
    33  	dstPath := dstFile.Name()
    34  
    35  	assert.NoError(dstFile.Close())
    36  
    37  	_, err = srcFile.WriteString(fileContent)
    38  	assert.NoError(err)
    39  
    40  	err = FileCopy(srcFile.Name(), dstPath)
    41  	assert.NoError(err)
    42  
    43  	dstContent, err := ioutil.ReadFile(dstPath)
    44  	assert.NoError(err)
    45  	assert.Equal(string(dstContent), fileContent)
    46  
    47  	srcInfo, err := srcFile.Stat()
    48  	assert.NoError(err)
    49  
    50  	dstInfo, err := os.Stat(dstPath)
    51  	assert.NoError(err)
    52  
    53  	assert.Equal(dstInfo.Mode(), srcInfo.Mode())
    54  	assert.Equal(dstInfo.IsDir(), srcInfo.IsDir())
    55  	assert.Equal(dstInfo.Size(), srcInfo.Size())
    56  }
    57  
    58  func TestFileCopySourceEmptyFailure(t *testing.T) {
    59  	assert := assert.New(t)
    60  	err := FileCopy("", "testDst")
    61  	assert.Error(err)
    62  }
    63  
    64  func TestFileCopyDestinationEmptyFailure(t *testing.T) {
    65  	assert := assert.New(t)
    66  	err := FileCopy("testSrc", "")
    67  	assert.Error(err)
    68  }
    69  
    70  func TestFileCopySourceNotExistFailure(t *testing.T) {
    71  	assert := assert.New(t)
    72  	srcFile, err := ioutil.TempFile("", "test_src_copy")
    73  	assert.NoError(err)
    74  
    75  	srcPath := srcFile.Name()
    76  	assert.NoError(srcFile.Close())
    77  	assert.NoError(os.Remove(srcPath))
    78  
    79  	err = FileCopy(srcPath, "testDest")
    80  	assert.Error(err)
    81  }
    82  
    83  func TestGenerateRandomBytes(t *testing.T) {
    84  	assert := assert.New(t)
    85  	bytesNeeded := 8
    86  	randBytes, err := GenerateRandomBytes(bytesNeeded)
    87  	assert.NoError(err)
    88  	assert.Equal(len(randBytes), bytesNeeded)
    89  }
    90  
    91  func TestRevereString(t *testing.T) {
    92  	assert := assert.New(t)
    93  	str := "Teststr"
    94  	reversed := ReverseString(str)
    95  	assert.Equal(reversed, "rtstseT")
    96  }
    97  
    98  func TestCleanupFds(t *testing.T) {
    99  	assert := assert.New(t)
   100  
   101  	tmpFile, err := ioutil.TempFile("", "testFds1")
   102  	assert.NoError(err)
   103  	filename := tmpFile.Name()
   104  	defer os.Remove(filename)
   105  
   106  	numFds := 1
   107  	fds := make([]*os.File, numFds)
   108  	assert.NotNil(fds)
   109  	assert.Equal(len(fds), 1)
   110  
   111  	fds[0] = tmpFile
   112  
   113  	CleanupFds(fds, 0)
   114  	CleanupFds(fds, 1)
   115  
   116  	err = tmpFile.Close()
   117  	assert.Error(err)
   118  }
   119  
   120  func TestWriteToFile(t *testing.T) {
   121  	assert := assert.New(t)
   122  
   123  	err := WriteToFile("/file-does-not-exist", []byte("test-data"))
   124  	assert.NotNil(err)
   125  
   126  	tmpFile, err := ioutil.TempFile("", "test_append_file")
   127  	assert.NoError(err)
   128  
   129  	filename := tmpFile.Name()
   130  	defer os.Remove(filename)
   131  
   132  	tmpFile.Close()
   133  
   134  	testData := []byte("test-data")
   135  	err = WriteToFile(filename, testData)
   136  	assert.NoError(err)
   137  
   138  	data, err := ioutil.ReadFile(filename)
   139  	assert.NoError(err)
   140  
   141  	assert.True(reflect.DeepEqual(testData, data))
   142  }
   143  
   144  func TestCalculateMilliCPUs(t *testing.T) {
   145  	assert := assert.New(t)
   146  
   147  	n := CalculateMilliCPUs(1, 1)
   148  	expected := uint32(1000)
   149  	assert.Equal(n, expected)
   150  
   151  	n = CalculateMilliCPUs(1, 0)
   152  	expected = uint32(0)
   153  	assert.Equal(n, expected)
   154  
   155  	n = CalculateMilliCPUs(-1, 1)
   156  	assert.Equal(n, expected)
   157  }
   158  
   159  func TestCaluclateVCpusFromMilliCpus(t *testing.T) {
   160  	assert := assert.New(t)
   161  
   162  	n := CalculateVCpusFromMilliCpus(1)
   163  	expected := uint32(1)
   164  	assert.Equal(n, expected)
   165  }
   166  
   167  func TestConstraintsToVCPUs(t *testing.T) {
   168  	assert := assert.New(t)
   169  
   170  	vcpus := ConstraintsToVCPUs(0, 100)
   171  	assert.Zero(vcpus)
   172  
   173  	vcpus = ConstraintsToVCPUs(100, 0)
   174  	assert.Zero(vcpus)
   175  
   176  	expectedVCPUs := uint(4)
   177  	vcpus = ConstraintsToVCPUs(4000, 1000)
   178  	assert.Equal(expectedVCPUs, vcpus)
   179  
   180  	vcpus = ConstraintsToVCPUs(4000, 1200)
   181  	assert.Equal(expectedVCPUs, vcpus)
   182  }
   183  
   184  func TestGetVirtDriveNameInvalidIndex(t *testing.T) {
   185  	assert := assert.New(t)
   186  	_, err := GetVirtDriveName(-1)
   187  	assert.Error(err)
   188  }
   189  
   190  func TestGetVirtDriveName(t *testing.T) {
   191  	assert := assert.New(t)
   192  	tests := []struct {
   193  		index         int
   194  		expectedDrive string
   195  	}{
   196  		{0, "vda"},
   197  		{25, "vdz"},
   198  		{27, "vdab"},
   199  		{704, "vdaac"},
   200  		{18277, "vdzzz"},
   201  	}
   202  
   203  	for i, test := range tests {
   204  		msg := fmt.Sprintf("test[%d]: %+v", i, test)
   205  		driveName, err := GetVirtDriveName(test.index)
   206  		assert.NoError(err, msg)
   207  		assert.Equal(driveName, test.expectedDrive, msg)
   208  	}
   209  }
   210  
   211  func TestGetSCSIIdLun(t *testing.T) {
   212  	assert := assert.New(t)
   213  
   214  	tests := []struct {
   215  		index          int
   216  		expectedScsiID int
   217  		expectedLun    int
   218  	}{
   219  		{0, 0, 0},
   220  		{1, 0, 1},
   221  		{2, 0, 2},
   222  		{255, 0, 255},
   223  		{256, 1, 0},
   224  		{257, 1, 1},
   225  		{258, 1, 2},
   226  		{512, 2, 0},
   227  		{513, 2, 1},
   228  	}
   229  
   230  	for i, test := range tests {
   231  		msg := fmt.Sprintf("test[%d]: %+v", i, test)
   232  		scsiID, lun, err := GetSCSIIdLun(test.index)
   233  		assert.NoError(err, msg)
   234  		assert.Equal(scsiID, test.expectedScsiID, msg)
   235  		assert.Equal(lun, test.expectedLun, msg)
   236  	}
   237  
   238  	_, _, err := GetSCSIIdLun(-1)
   239  	assert.Error(err)
   240  	_, _, err = GetSCSIIdLun(maxSCSIDevices + 1)
   241  	assert.Error(err)
   242  }
   243  
   244  func TestGetSCSIAddress(t *testing.T) {
   245  	assert := assert.New(t)
   246  	tests := []struct {
   247  		index               int
   248  		expectedSCSIAddress string
   249  	}{
   250  		{0, "0:0"},
   251  		{200, "0:200"},
   252  		{255, "0:255"},
   253  		{258, "1:2"},
   254  		{512, "2:0"},
   255  	}
   256  
   257  	for i, test := range tests {
   258  		msg := fmt.Sprintf("test[%d]: %+v", i, test)
   259  		scsiAddr, err := GetSCSIAddress(test.index)
   260  		assert.NoError(err, msg)
   261  		assert.Equal(scsiAddr, test.expectedSCSIAddress, msg)
   262  	}
   263  
   264  	_, err := GetSCSIAddress(-1)
   265  	assert.Error(err)
   266  }
   267  
   268  func TestMakeNameID(t *testing.T) {
   269  	assert := assert.New(t)
   270  
   271  	nameID := MakeNameID("testType", "testID", 14)
   272  	expected := "testType-testI"
   273  	assert.Equal(expected, nameID)
   274  }
   275  
   276  func TestBuildSocketPath(t *testing.T) {
   277  	assert := assert.New(t)
   278  
   279  	type testData struct {
   280  		elems    []string
   281  		valid    bool
   282  		expected string
   283  	}
   284  
   285  	longPath := strings.Repeat("/a", 106/2)
   286  	longestPath := longPath + "a"
   287  	pathTooLong := filepath.Join(longestPath, "x")
   288  
   289  	data := []testData{
   290  		{[]string{""}, false, ""},
   291  
   292  		{[]string{"a"}, true, "a"},
   293  		{[]string{"/a"}, true, "/a"},
   294  		{[]string{"a", "b", "c"}, true, "a/b/c"},
   295  		{[]string{"a", "/b", "c"}, true, "a/b/c"},
   296  		{[]string{"/a", "b", "c"}, true, "/a/b/c"},
   297  		{[]string{"/a", "/b", "/c"}, true, "/a/b/c"},
   298  
   299  		{[]string{longPath}, true, longPath},
   300  		{[]string{longestPath}, true, longestPath},
   301  		{[]string{pathTooLong}, false, ""},
   302  	}
   303  
   304  	for i, d := range data {
   305  		result, err := BuildSocketPath(d.elems...)
   306  		msg := fmt.Sprintf("test[%d]: %+v", i, d)
   307  
   308  		if d.valid {
   309  			assert.NoErrorf(err, "test %d, data %+v", i, d, msg)
   310  		} else {
   311  			assert.Errorf(err, "test %d, data %+v", i, d, msg)
   312  		}
   313  
   314  		assert.NotNil(result, msg)
   315  		assert.Equal(d.expected, result, msg)
   316  	}
   317  }
   318  
   319  func TestSupportsVsocks(t *testing.T) {
   320  	assert := assert.New(t)
   321  
   322  	orgVHostVSockDevicePath := VHostVSockDevicePath
   323  	defer func() {
   324  		VHostVSockDevicePath = orgVHostVSockDevicePath
   325  	}()
   326  
   327  	VHostVSockDevicePath = "/abc/xyz/123"
   328  	assert.False(SupportsVsocks())
   329  
   330  	vHostVSockFile, err := ioutil.TempFile("", "vhost-vsock")
   331  	assert.NoError(err)
   332  	defer os.Remove(vHostVSockFile.Name())
   333  	defer vHostVSockFile.Close()
   334  	VHostVSockDevicePath = vHostVSockFile.Name()
   335  
   336  	assert.True(SupportsVsocks())
   337  }
   338  
   339  func TestAlignMem(t *testing.T) {
   340  	assert := assert.New(t)
   341  
   342  	memSize := MemUnit(1024) * MiB
   343  	blockSize := MemUnit(512) * MiB
   344  	resultMem := memSize.AlignMem(blockSize)
   345  	expected := memSize
   346  	assert.Equal(expected, resultMem)
   347  
   348  	memSize = MemUnit(512) * MiB
   349  	blockSize = MemUnit(1024) * MiB
   350  	resultMem = memSize.AlignMem(blockSize)
   351  	expected = blockSize
   352  	assert.Equal(expected, resultMem)
   353  
   354  	memSize = MemUnit(1024) * MiB
   355  	blockSize = MemUnit(50) * MiB
   356  	resultMem = memSize.AlignMem(blockSize)
   357  	expected = memSize + (blockSize - (memSize % blockSize))
   358  	assert.Equal(expected, resultMem)
   359  }
   360  
   361  func TestToMiB(t *testing.T) {
   362  	assert := assert.New(t)
   363  
   364  	memSize := MemUnit(1) * GiB
   365  	result := memSize.ToMiB()
   366  	expected := uint64(1024)
   367  	assert.Equal(expected, result)
   368  }
   369  
   370  func TestToBytes(t *testing.T) {
   371  	assert := assert.New(t)
   372  
   373  	memSize := MemUnit(1) * GiB
   374  	result := memSize.ToBytes()
   375  	expected := uint64(1073741824)
   376  	assert.Equal(expected, result)
   377  }