test: Fix fd leak causing test error

Update the `TestQemuAddDeviceKataVSOCK` test so that it:

- Doesn't hard-code the file descriptor number.
- Cleans up after itself.

The latter issue was causing an odd error similar to the following in
the test output:

```
Unable to launch /tmp/vc-tmp-526112270/hypervisor: fork/exec /tmp/vc-tmp-526112270/hypervisor: permission denied
```

Partially fixes: #1835.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
James O. D. Hunt 2019-07-04 15:50:41 +01:00
parent cd4cc02568
commit fcf9f9f6dd

View File

@ -280,22 +280,33 @@ func TestQemuAddDeviceSerialPortDev(t *testing.T) {
} }
func TestQemuAddDeviceKataVSOCK(t *testing.T) { func TestQemuAddDeviceKataVSOCK(t *testing.T) {
assert := assert.New(t)
dir, err := ioutil.TempDir("", "")
assert.NoError(err)
defer os.RemoveAll(dir)
vsockFilename := filepath.Join(dir, "vsock")
contextID := uint64(3) contextID := uint64(3)
port := uint32(1024) port := uint32(1024)
vHostFD := os.NewFile(1, "vsock")
vsockFile, err := os.Create(vsockFilename)
assert.NoError(err)
defer vsockFile.Close()
expectedOut := []govmmQemu.Device{ expectedOut := []govmmQemu.Device{
govmmQemu.VSOCKDevice{ govmmQemu.VSOCKDevice{
ID: fmt.Sprintf("vsock-%d", contextID), ID: fmt.Sprintf("vsock-%d", contextID),
ContextID: contextID, ContextID: contextID,
VHostFD: vHostFD, VHostFD: vsockFile,
}, },
} }
vsock := kataVSOCK{ vsock := kataVSOCK{
contextID: contextID, contextID: contextID,
port: port, port: port,
vhostFd: vHostFD, vhostFd: vsockFile,
} }
testQemuAddDevice(t, vsock, vSockPCIDev, expectedOut) testQemuAddDevice(t, vsock, vSockPCIDev, expectedOut)