Skip to content

Commit ff9f097

Browse files
committed
Add test file handling in VirtioBlk device tests
1 parent 04c0723 commit ff9f097

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

pkg/config/virtio_test.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package config
22

33
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
47
"testing"
58
"time"
69

10+
log "github.com/sirupsen/logrus"
711
"github.com/stretchr/testify/assert"
812
"github.com/stretchr/testify/require"
913
)
@@ -16,23 +20,51 @@ type virtioDevTest struct {
1620
errorMsg string
1721
}
1822

23+
var testImagePath = filepath.Join(os.TempDir(), "vfkit_test_disk.img")
24+
1925
var virtioDevTests = map[string]virtioDevTest{
2026
"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+
},
2242
expectedDev: &VirtioBlk{
2343
DiskStorageConfig: DiskStorageConfig{
2444
StorageConfig: StorageConfig{
2545
DevName: "virtio-blk",
2646
},
27-
ImagePath: "/foo/bar",
47+
ImagePath: testImagePath,
2848
},
2949
DeviceIdentifier: "",
3050
},
31-
expectedCmdLine: []string{"--device", "virtio-blk,path=/foo/bar"},
51+
expectedCmdLine: []string{"--device", fmt.Sprintf("virtio-blk,path=%s", testImagePath)},
3252
},
3353
"NewVirtioBlkWithDevId": {
3454
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)
3668
if err != nil {
3769
return nil, err
3870
}
@@ -44,12 +76,12 @@ var virtioDevTests = map[string]virtioDevTest{
4476
StorageConfig: StorageConfig{
4577
DevName: "virtio-blk",
4678
},
47-
ImagePath: "/foo/bar",
79+
ImagePath: testImagePath,
4880
},
4981
DeviceIdentifier: "test",
5082
},
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)},
5385
},
5486
"NewNVMe": {
5587
newDev: func() (VirtioDevice, error) { return NVMExpressControllerNew("/foo/bar") },
@@ -296,6 +328,12 @@ func testErrorVirtioDev(t *testing.T, test *virtioDevTest) {
296328
}
297329

298330
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+
}()
299337
t.Run("virtio-devices", func(t *testing.T) {
300338
for name := range virtioDevTests {
301339
t.Run(name, func(t *testing.T) {
@@ -307,6 +345,5 @@ func TestVirtioDevices(t *testing.T) {
307345
}
308346
})
309347
}
310-
311348
})
312349
}

0 commit comments

Comments
 (0)