From 868d0248cd74db3213ab96ff22b4068f4ecec67b Mon Sep 17 00:00:00 2001 From: zhanghj Date: Thu, 17 Sep 2020 05:37:17 -0400 Subject: [PATCH] devices: fix go test warning in manager_test.go Create "class" and "config" file in temporary device BDF dir, and remove dir created by ioutil.TempDir() when test finished. fixes: #746 Signed-off-by: zhanghj --- .../device/manager/manager_test.go | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/runtime/virtcontainers/device/manager/manager_test.go b/src/runtime/virtcontainers/device/manager/manager_test.go index 3798d1d2c6..2dc5328b68 100644 --- a/src/runtime/virtcontainers/device/manager/manager_test.go +++ b/src/runtime/virtcontainers/device/manager/manager_test.go @@ -105,7 +105,7 @@ func TestAttachVFIODevice(t *testing.T) { } tmpDir, err := ioutil.TempDir("", "") assert.Nil(t, err) - os.RemoveAll(tmpDir) + defer os.RemoveAll(tmpDir) testFDIOGroup := "2" testDeviceBDFPath := "0000:00:1c.0" @@ -114,15 +114,27 @@ func TestAttachVFIODevice(t *testing.T) { err = os.MkdirAll(devicesDir, dirMode) assert.Nil(t, err) - deviceFile := filepath.Join(devicesDir, testDeviceBDFPath) - _, err = os.Create(deviceFile) + deviceBDFDir := filepath.Join(devicesDir, testDeviceBDFPath) + err = os.MkdirAll(deviceBDFDir, dirMode) + assert.Nil(t, err) + + deviceClassFile := filepath.Join(deviceBDFDir, "class") + _, err = os.Create(deviceClassFile) + assert.Nil(t, err) + + deviceConfigFile := filepath.Join(deviceBDFDir, "config") + _, err = os.Create(deviceConfigFile) assert.Nil(t, err) savedIOMMUPath := config.SysIOMMUPath config.SysIOMMUPath = tmpDir + savedSysBusPciDevicesPath := config.SysBusPciDevicesPath + config.SysBusPciDevicesPath = devicesDir + defer func() { config.SysIOMMUPath = savedIOMMUPath + config.SysBusPciDevicesPath = savedSysBusPciDevicesPath }() path := filepath.Join(vfioPath, testFDIOGroup) @@ -220,7 +232,7 @@ func TestAttachVhostUserBlkDevice(t *testing.T) { vhostUserStorePath: tmpDir, } assert.Nil(t, err) - os.RemoveAll(tmpDir) + defer os.RemoveAll(tmpDir) vhostUserDevNodePath := filepath.Join(tmpDir, "/block/devices/") vhostUserSockPath := filepath.Join(tmpDir, "/block/sockets/")