gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/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 "io/ioutil" 10 "os" 11 "path/filepath" 12 "reflect" 13 "strings" 14 "testing" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestFileCopySuccessful(t *testing.T) { 20 assert := assert.New(t) 21 fileContent := "testContent" 22 23 srcFile, err := ioutil.TempFile("", "test_src_copy") 24 assert.NoError(err) 25 defer os.Remove(srcFile.Name()) 26 defer srcFile.Close() 27 28 dstFile, err := ioutil.TempFile("", "test_dst_copy") 29 assert.NoError(err) 30 defer os.Remove(dstFile.Name()) 31 32 dstPath := dstFile.Name() 33 34 assert.NoError(dstFile.Close()) 35 36 _, err = srcFile.WriteString(fileContent) 37 assert.NoError(err) 38 39 err = FileCopy(srcFile.Name(), dstPath) 40 assert.NoError(err) 41 42 dstContent, err := ioutil.ReadFile(dstPath) 43 assert.NoError(err) 44 assert.Equal(string(dstContent), fileContent) 45 46 srcInfo, err := srcFile.Stat() 47 assert.NoError(err) 48 49 dstInfo, err := os.Stat(dstPath) 50 assert.NoError(err) 51 52 assert.Equal(dstInfo.Mode(), srcInfo.Mode()) 53 assert.Equal(dstInfo.IsDir(), srcInfo.IsDir()) 54 assert.Equal(dstInfo.Size(), srcInfo.Size()) 55 } 56 57 func TestFileCopySourceEmptyFailure(t *testing.T) { 58 assert := assert.New(t) 59 err := FileCopy("", "testDst") 60 assert.Error(err) 61 } 62 63 func TestFileCopyDestinationEmptyFailure(t *testing.T) { 64 assert := assert.New(t) 65 err := FileCopy("testSrc", "") 66 assert.Error(err) 67 } 68 69 func TestFileCopySourceNotExistFailure(t *testing.T) { 70 assert := assert.New(t) 71 srcFile, err := ioutil.TempFile("", "test_src_copy") 72 assert.NoError(err) 73 74 srcPath := srcFile.Name() 75 assert.NoError(srcFile.Close()) 76 assert.NoError(os.Remove(srcPath)) 77 78 err = FileCopy(srcPath, "testDest") 79 assert.Error(err) 80 } 81 82 func TestGenerateRandomBytes(t *testing.T) { 83 assert := assert.New(t) 84 bytesNeeded := 8 85 randBytes, err := GenerateRandomBytes(bytesNeeded) 86 assert.NoError(err) 87 assert.Equal(len(randBytes), bytesNeeded) 88 } 89 90 func TestRevereString(t *testing.T) { 91 assert := assert.New(t) 92 str := "Teststr" 93 reversed := ReverseString(str) 94 assert.Equal(reversed, "rtstseT") 95 } 96 97 func TestWriteToFile(t *testing.T) { 98 assert := assert.New(t) 99 100 err := WriteToFile("/file-does-not-exist", []byte("test-data")) 101 assert.NotNil(err) 102 103 tmpFile, err := ioutil.TempFile("", "test_append_file") 104 assert.NoError(err) 105 106 filename := tmpFile.Name() 107 defer os.Remove(filename) 108 109 tmpFile.Close() 110 111 testData := []byte("test-data") 112 err = WriteToFile(filename, testData) 113 assert.NoError(err) 114 115 data, err := ioutil.ReadFile(filename) 116 assert.NoError(err) 117 118 assert.True(reflect.DeepEqual(testData, data)) 119 } 120 121 func TestConstraintsToVCPUs(t *testing.T) { 122 assert := assert.New(t) 123 124 vcpus := ConstraintsToVCPUs(0, 100) 125 assert.Zero(vcpus) 126 127 vcpus = ConstraintsToVCPUs(100, 0) 128 assert.Zero(vcpus) 129 130 expectedVCPUs := uint(4) 131 vcpus = ConstraintsToVCPUs(4000, 1000) 132 assert.Equal(expectedVCPUs, vcpus) 133 134 vcpus = ConstraintsToVCPUs(4000, 1200) 135 assert.Equal(expectedVCPUs, vcpus) 136 } 137 138 func TestGetVirtDriveNameInvalidIndex(t *testing.T) { 139 assert := assert.New(t) 140 _, err := GetVirtDriveName(-1) 141 assert.Error(err) 142 } 143 144 func TestGetVirtDriveName(t *testing.T) { 145 assert := assert.New(t) 146 tests := []struct { 147 index int 148 expectedDrive string 149 }{ 150 {0, "vda"}, 151 {25, "vdz"}, 152 {27, "vdab"}, 153 {704, "vdaac"}, 154 {18277, "vdzzz"}, 155 } 156 157 for _, test := range tests { 158 driveName, err := GetVirtDriveName(test.index) 159 assert.NoError(err) 160 assert.Equal(driveName, test.expectedDrive) 161 } 162 } 163 164 func TestGetSCSIIdLun(t *testing.T) { 165 assert := assert.New(t) 166 167 tests := []struct { 168 index int 169 expectedScsiID int 170 expectedLun int 171 }{ 172 {0, 0, 0}, 173 {1, 0, 1}, 174 {2, 0, 2}, 175 {255, 0, 255}, 176 {256, 1, 0}, 177 {257, 1, 1}, 178 {258, 1, 2}, 179 {512, 2, 0}, 180 {513, 2, 1}, 181 } 182 183 for _, test := range tests { 184 scsiID, lun, err := GetSCSIIdLun(test.index) 185 assert.NoError(err) 186 assert.Equal(scsiID, test.expectedScsiID) 187 assert.Equal(lun, test.expectedLun) 188 } 189 190 _, _, err := GetSCSIIdLun(maxSCSIDevices + 1) 191 assert.NotNil(err) 192 } 193 194 func TestGetSCSIAddress(t *testing.T) { 195 assert := assert.New(t) 196 tests := []struct { 197 index int 198 expectedSCSIAddress string 199 }{ 200 {0, "0:0"}, 201 {200, "0:200"}, 202 {255, "0:255"}, 203 {258, "1:2"}, 204 {512, "2:0"}, 205 } 206 207 for _, test := range tests { 208 scsiAddr, err := GetSCSIAddress(test.index) 209 assert.NoError(err) 210 assert.Equal(scsiAddr, test.expectedSCSIAddress) 211 } 212 } 213 214 func TestBuildSocketPath(t *testing.T) { 215 assert := assert.New(t) 216 217 type testData struct { 218 elems []string 219 valid bool 220 expected string 221 } 222 223 longPath := strings.Repeat("/a", 106/2) 224 longestPath := longPath + "a" 225 pathTooLong := filepath.Join(longestPath, "x") 226 227 data := []testData{ 228 {[]string{""}, false, ""}, 229 230 {[]string{"a"}, true, "a"}, 231 {[]string{"/a"}, true, "/a"}, 232 {[]string{"a", "b", "c"}, true, "a/b/c"}, 233 {[]string{"a", "/b", "c"}, true, "a/b/c"}, 234 {[]string{"/a", "b", "c"}, true, "/a/b/c"}, 235 {[]string{"/a", "/b", "/c"}, true, "/a/b/c"}, 236 237 {[]string{longPath}, true, longPath}, 238 {[]string{longestPath}, true, longestPath}, 239 {[]string{pathTooLong}, false, ""}, 240 } 241 242 for i, d := range data { 243 result, err := BuildSocketPath(d.elems...) 244 245 if d.valid { 246 assert.NoErrorf(err, "test %d, data %+v", i, d) 247 } else { 248 assert.Errorf(err, "test %d, data %+v", i, d) 249 } 250 251 assert.NotNil(result) 252 assert.Equal(d.expected, result) 253 } 254 } 255 256 func TestSupportsVsocks(t *testing.T) { 257 assert := assert.New(t) 258 259 orgVHostVSockDevicePath := VHostVSockDevicePath 260 defer func() { 261 VHostVSockDevicePath = orgVHostVSockDevicePath 262 }() 263 264 VHostVSockDevicePath = "/abc/xyz/123" 265 assert.False(SupportsVsocks()) 266 267 vHostVSockFile, err := ioutil.TempFile("", "vhost-vsock") 268 assert.NoError(err) 269 defer os.Remove(vHostVSockFile.Name()) 270 defer vHostVSockFile.Close() 271 VHostVSockDevicePath = vHostVSockFile.Name() 272 273 assert.True(SupportsVsocks()) 274 }