-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This tests conversion of virtio devices to command line and then parsing again the generated command line.
- Loading branch information
Showing
4 changed files
with
194 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
type virtioDevTest struct { | ||
newDev func() (VirtioDevice, error) | ||
expectedDev VirtioDevice | ||
expectedCmdLine []string | ||
alternateCmdLine []string | ||
} | ||
|
||
var virtioDevTests = map[string]virtioDevTest{ | ||
"NewVirtioBlk": { | ||
newDev: func() (VirtioDevice, error) { return VirtioBlkNew("/foo/bar") }, | ||
expectedDev: &VirtioBlk{ | ||
StorageConfig: StorageConfig{ | ||
DevName: "virtio-blk", | ||
ImagePath: "/foo/bar", | ||
}, | ||
DeviceIdentifier: "", | ||
}, | ||
expectedCmdLine: []string{"--device", "virtio-blk,path=/foo/bar"}, | ||
}, | ||
"NewVirtioBlkWithDevId": { | ||
newDev: func() (VirtioDevice, error) { | ||
dev, err := VirtioBlkNew("/foo/bar") | ||
if err != nil { | ||
return nil, err | ||
} | ||
dev.SetDeviceIdentifier("test") | ||
return dev, nil | ||
}, | ||
expectedDev: &VirtioBlk{ | ||
StorageConfig: StorageConfig{ | ||
DevName: "virtio-blk", | ||
ImagePath: "/foo/bar", | ||
}, | ||
DeviceIdentifier: "test", | ||
}, | ||
expectedCmdLine: []string{"--device", "virtio-blk,path=/foo/bar,deviceId=test"}, | ||
alternateCmdLine: []string{"--device", "virtio-blk,deviceId=test,path=/foo/bar"}, | ||
}, | ||
"NewVirtioFs": { | ||
newDev: func() (VirtioDevice, error) { return VirtioFsNew("/foo/bar", "") }, | ||
expectedDev: &VirtioFs{ | ||
SharedDir: "/foo/bar", | ||
}, | ||
expectedCmdLine: []string{"--device", "virtio-fs,sharedDir=/foo/bar"}, | ||
}, | ||
"NewVirtioFsWithTag": { | ||
newDev: func() (VirtioDevice, error) { return VirtioFsNew("/foo/bar", "myTag") }, | ||
expectedDev: &VirtioFs{ | ||
SharedDir: "/foo/bar", | ||
MountTag: "myTag", | ||
}, | ||
expectedCmdLine: []string{"--device", "virtio-fs,sharedDir=/foo/bar,mountTag=myTag"}, | ||
alternateCmdLine: []string{"--device", "virtio-fs,mountTag=myTag,sharedDir=/foo/bar"}, | ||
}, | ||
"NewVirtioVsock": { | ||
newDev: func() (VirtioDevice, error) { return VirtioVsockNew(1234, "/foo/bar.unix", false) }, | ||
expectedDev: &VirtioVsock{ | ||
Port: 1234, | ||
SocketURL: "/foo/bar.unix", | ||
}, | ||
expectedCmdLine: []string{"--device", "virtio-vsock,port=1234,socketURL=/foo/bar.unix,connect"}, | ||
alternateCmdLine: []string{"--device", "virtio-vsock,socketURL=/foo/bar.unix,connect,port=1234"}, | ||
}, | ||
"NewVirtioVsockWithListen": { | ||
newDev: func() (VirtioDevice, error) { return VirtioVsockNew(1234, "/foo/bar.unix", true) }, | ||
expectedDev: &VirtioVsock{ | ||
Port: 1234, | ||
SocketURL: "/foo/bar.unix", | ||
Listen: true, | ||
}, | ||
expectedCmdLine: []string{"--device", "virtio-vsock,port=1234,socketURL=/foo/bar.unix,listen"}, | ||
alternateCmdLine: []string{"--device", "virtio-vsock,socketURL=/foo/bar.unix,listen,port=1234"}, | ||
}, | ||
"NewVirtioRng": { | ||
newDev: func() (VirtioDevice, error) { return VirtioRngNew() }, | ||
expectedDev: &VirtioRng{}, | ||
expectedCmdLine: []string{"--device", "virtio-rng"}, | ||
}, | ||
"NewVirtioSerial": { | ||
newDev: func() (VirtioDevice, error) { return VirtioSerialNew("/foo/bar.log") }, | ||
expectedDev: &VirtioSerial{ | ||
LogFile: "/foo/bar.log", | ||
}, | ||
expectedCmdLine: []string{"--device", "virtio-serial,logFilePath=/foo/bar.log"}, | ||
}, | ||
"NewVirtioNet": { | ||
newDev: func() (VirtioDevice, error) { return VirtioNetNew("") }, | ||
expectedDev: &VirtioNet{ | ||
Nat: true, | ||
}, | ||
expectedCmdLine: []string{"--device", "virtio-net,nat"}, | ||
}, | ||
"NewVirtioNetWithMacAddress": { | ||
newDev: func() (VirtioDevice, error) { return VirtioNetNew("00:11:22:33:44:55") }, | ||
expectedDev: &VirtioNet{ | ||
Nat: true, | ||
MacAddress: []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55}, | ||
}, | ||
expectedCmdLine: []string{"--device", "virtio-net,nat,mac=00:11:22:33:44:55"}, | ||
alternateCmdLine: []string{"--device", "virtio-net,mac=00:11:22:33:44:55,nat"}, | ||
}, | ||
"NewUSBMassStorage": { | ||
newDev: func() (VirtioDevice, error) { return USBMassStorageNew("/foo/bar") }, | ||
expectedDev: &USBMassStorage{ | ||
StorageConfig: StorageConfig{ | ||
DevName: "usb-mass-storage", | ||
ImagePath: "/foo/bar", | ||
}, | ||
}, | ||
expectedCmdLine: []string{"--device", "usb-mass-storage,path=/foo/bar"}, | ||
}, | ||
} | ||
|
||
func testVirtioDev(t *testing.T, test *virtioDevTest) { | ||
dev, err := test.newDev() | ||
require.NoError(t, err) | ||
assert.Equal(t, dev, test.expectedDev) | ||
|
||
cmdLine, err := dev.ToCmdLine() | ||
require.NoError(t, err) | ||
assert.Equal(t, cmdLine, test.expectedCmdLine) | ||
|
||
dev, err = deviceFromCmdLine(cmdLine[1]) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, dev, test.expectedDev) | ||
|
||
if test.alternateCmdLine == nil { | ||
return | ||
} | ||
|
||
dev, err = deviceFromCmdLine(test.alternateCmdLine[1]) | ||
require.NoError(t, err) | ||
assert.Equal(t, dev, test.expectedDev) | ||
cmdLine, err = dev.ToCmdLine() | ||
require.NoError(t, err) | ||
assert.Equal(t, cmdLine, test.expectedCmdLine) | ||
|
||
} | ||
|
||
func TestVirtioDevices(t *testing.T) { | ||
t.Run("virtio-devices", func(t *testing.T) { | ||
for name, test := range virtioDevTests { | ||
t.Run(name, func(t *testing.T) { | ||
testVirtioDev(t, &test) | ||
}) | ||
} | ||
|
||
}) | ||
} |