1
1
package config
2
2
3
3
import (
4
+ "fmt"
5
+ "os"
6
+ "path/filepath"
4
7
"testing"
5
8
"time"
6
9
10
+ log "github.com/sirupsen/logrus"
7
11
"github.com/stretchr/testify/assert"
8
12
"github.com/stretchr/testify/require"
9
13
)
@@ -16,23 +20,51 @@ type virtioDevTest struct {
16
20
errorMsg string
17
21
}
18
22
23
+ var testImagePath = filepath .Join (os .TempDir (), "vfkit_test_disk.img" )
24
+
19
25
var virtioDevTests = map [string ]virtioDevTest {
20
26
"NewVirtioBlk" : {
21
- newDev : func () (VirtioDevice , error ) { return VirtioBlkNew ("/foo/bar" ) },
27
+ newDev : func () (VirtioDevice , error ) {
28
+ testImage , err := os .Create (testImagePath )
29
+ if err != nil {
30
+ return nil , fmt .Errorf ("failed to create test image %s: %v" , testImagePath , err )
31
+ }
32
+ if _ , err := testImage .Write ([]byte {'0' , '0' , '0' , '0' }); err != nil {
33
+ testImage .Close ()
34
+ return nil , fmt .Errorf ("failed to write test image %s: %v" , testImagePath , err )
35
+ }
36
+
37
+ if err := testImage .Close (); err != nil {
38
+ return nil , fmt .Errorf ("failed to close test image: %w" , err )
39
+ }
40
+ return VirtioBlkNew (testImagePath )
41
+ },
22
42
expectedDev : & VirtioBlk {
23
43
DiskStorageConfig : DiskStorageConfig {
24
44
StorageConfig : StorageConfig {
25
45
DevName : "virtio-blk" ,
26
46
},
27
- ImagePath : "/foo/bar" ,
47
+ ImagePath : testImagePath ,
28
48
},
29
49
DeviceIdentifier : "" ,
30
50
},
31
- expectedCmdLine : []string {"--device" , "virtio-blk,path=/foo/bar" },
51
+ expectedCmdLine : []string {"--device" , fmt . Sprintf ( "virtio-blk,path=%s" , testImagePath ) },
32
52
},
33
53
"NewVirtioBlkWithDevId" : {
34
54
newDev : func () (VirtioDevice , error ) {
35
- dev , err := VirtioBlkNew ("/foo/bar" )
55
+ testImage , err := os .Create (testImagePath )
56
+ if err != nil {
57
+ return nil , fmt .Errorf ("failed to create test image %s: %v" , testImagePath , err )
58
+ }
59
+ if _ , err := testImage .Write ([]byte {'0' , '0' , '0' , '0' }); err != nil {
60
+ testImage .Close ()
61
+ return nil , fmt .Errorf ("failed to write test image %s: %v" , testImagePath , err )
62
+ }
63
+
64
+ if err := testImage .Close (); err != nil {
65
+ return nil , fmt .Errorf ("failed to close test image: %w" , err )
66
+ }
67
+ dev , err := VirtioBlkNew (testImagePath )
36
68
if err != nil {
37
69
return nil , err
38
70
}
@@ -44,12 +76,12 @@ var virtioDevTests = map[string]virtioDevTest{
44
76
StorageConfig : StorageConfig {
45
77
DevName : "virtio-blk" ,
46
78
},
47
- ImagePath : "/foo/bar" ,
79
+ ImagePath : testImagePath ,
48
80
},
49
81
DeviceIdentifier : "test" ,
50
82
},
51
- expectedCmdLine : []string {"--device" , "virtio-blk,path=/foo/bar ,deviceId=test" },
52
- alternateCmdLine : []string {"--device" , "virtio-blk,deviceId=test,path=/foo/bar" },
83
+ expectedCmdLine : []string {"--device" , fmt . Sprintf ( "virtio-blk,path=%s ,deviceId=test" , testImagePath ) },
84
+ alternateCmdLine : []string {"--device" , fmt . Sprintf ( "virtio-blk,deviceId=test,path=%s" , testImagePath ) },
53
85
},
54
86
"NewNVMe" : {
55
87
newDev : func () (VirtioDevice , error ) { return NVMExpressControllerNew ("/foo/bar" ) },
@@ -296,6 +328,12 @@ func testErrorVirtioDev(t *testing.T, test *virtioDevTest) {
296
328
}
297
329
298
330
func TestVirtioDevices (t * testing.T ) {
331
+ defer func () {
332
+ err := os .Remove (testImagePath )
333
+ if err != nil {
334
+ log .Warn ("Failed to remove test image" )
335
+ }
336
+ }()
299
337
t .Run ("virtio-devices" , func (t * testing.T ) {
300
338
for name := range virtioDevTests {
301
339
t .Run (name , func (t * testing.T ) {
@@ -307,6 +345,5 @@ func TestVirtioDevices(t *testing.T) {
307
345
}
308
346
})
309
347
}
310
-
311
348
})
312
349
}
0 commit comments