diff --git a/Makefile b/Makefile index 4e33f623..83823e3d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all build clean +.PHONY: all build clean test CGO_CFLAGS=-mmacosx-version-min=11.0 @@ -6,6 +6,9 @@ all: build build: out/vfkit +test: + go test ./pkg/... + clean: rm -rf out diff --git a/go.mod b/go.mod index 8e4d30d6..bab370c2 100644 --- a/go.mod +++ b/go.mod @@ -7,14 +7,20 @@ require ( github.com/docker/go-units v0.4.0 github.com/h2non/filetype v1.1.3 github.com/prashantgupta24/mac-sleep-notifier v1.0.1 - github.com/sirupsen/logrus v1.8.1 + github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 - inet.af/tcpproxy v0.0.0-20210824174053-2e577fef49e2 + github.com/stretchr/testify v1.8.1 + inet.af/tcpproxy v0.0.0-20220326234310-be3ee21c9fa0 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect + github.com/kr/pretty v0.2.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/mod v0.7.0 // indirect - golang.org/x/sys v0.1.0 // indirect + golang.org/x/sys v0.3.0 // indirect + gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 57f2212b..eabae40c 100644 --- a/go.sum +++ b/go.sum @@ -11,21 +11,31 @@ github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prashantgupta24/mac-sleep-notifier v1.0.1 h1:xd1lPtnn1gxGNjD2tCoVDoOtiQcQ8B9KNFhcWgGqreQ= github.com/prashantgupta24/mac-sleep-notifier v1.0.1/go.mod h1:bcfTio1xW+rjjZzdF0kbMEs9mcCEmrOBOSK+Jeml7zM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= -github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -42,13 +52,14 @@ golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -61,6 +72,10 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -inet.af/tcpproxy v0.0.0-20210824174053-2e577fef49e2 h1:5dsmluHaw3te6yeluBq4oe2VcZq3tljF8l661Chwzwc= -inet.af/tcpproxy v0.0.0-20210824174053-2e577fef49e2/go.mod h1:Tojt5kmHpDIR2jMojxzZK2w2ZR7OILODmUo2gaSwjrk= +inet.af/tcpproxy v0.0.0-20220326234310-be3ee21c9fa0 h1:PqdHrvQRVK1zapJkd0qf6+tevvSIcWdfenVqJd3PHWU= +inet.af/tcpproxy v0.0.0-20220326234310-be3ee21c9fa0/go.mod h1:Tojt5kmHpDIR2jMojxzZK2w2ZR7OILODmUo2gaSwjrk= diff --git a/pkg/config/virtio_test.go b/pkg/config/virtio_test.go new file mode 100644 index 00000000..e2efd3c3 --- /dev/null +++ b/pkg/config/virtio_test.go @@ -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) + }) + } + + }) +}