Skip to content

Commit

Permalink
Start adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cfergeau committed Sep 20, 2022
1 parent 1b887db commit 36da76c
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.PHONY: fmt
.PHONY: fmt test
fmt:
@ls | grep -E '\.(h|m)$$' | xargs clang-format -i
@ls | grep -E '\.(h|m)$$' | xargs clang-format -i

test:
@go test -c . -o vz.test
@codesign --entitlements ./example/linux/vz.entitlements -s - ./vz.test || true
@./vz.test
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/Code-Hex/vz/v2

go 1.17

require golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664
require (
github.com/stretchr/testify v1.8.0
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 h1:v1W7bwXHsnLLloWYTVEdvGvA7BHMeBYsPcF0GLDxIRs=
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/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=
84 changes: 84 additions & 0 deletions virtualization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package vz

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

type testVM struct {
*VirtualMachine
tempKernelFile *os.File
stateHandlerError func(err error)
}

func (vm *testVM) Close() error {
return vm.tempKernelFile.Close()
}

func newTestVM(t *testing.T) *testVM {
// use empty file as dummy kernel as we don't expect the VM to successfully start in our tests
tempKernelFile, err := os.CreateTemp(".", "vz_vmlinuz_test")
require.NoError(t, err)
bootloader := NewLinuxBootLoader(tempKernelFile.Name())
config := NewVirtualMachineConfiguration(bootloader, 1, 64*1024*1024)
//passing the config below to NewVirtualMachine reproduces https://github.com/Code-Hex/vz/issues/43
//config := NewVirtualMachineConfiguration(&LinuxBootLoader{}, 1, 64*1024*1024)

stateHandlerError := func(err error) {
require.Error(t, err)
}

return &testVM{
VirtualMachine: NewVirtualMachine(config),
tempKernelFile: tempKernelFile,
stateHandlerError: stateHandlerError,
}
}

func TestStart(t *testing.T) {
vm := newTestVM(t)
require.NotEqual(t, vm, nil)
defer vm.Close()

require.True(t, vm.CanStart())
vm.Start(vm.stateHandlerError)
}

func TestPause(t *testing.T) {
vm := newTestVM(t)
require.NotEqual(t, vm, nil)
defer vm.Close()

require.False(t, vm.CanPause())
vm.Pause(vm.stateHandlerError)
}

func TestResume(t *testing.T) {
vm := newTestVM(t)
require.NotEqual(t, vm, nil)
defer vm.Close()

require.False(t, vm.CanResume())
vm.Resume(vm.stateHandlerError)
}

func TestRequestStop(t *testing.T) {
vm := newTestVM(t)
require.NotEqual(t, vm, nil)
defer vm.Close()

require.False(t, vm.CanRequestStop())
_, err := vm.RequestStop()
require.Error(t, err)
}

func TestStop(t *testing.T) {
vm := newTestVM(t)
require.NotEqual(t, vm, nil)
defer vm.Close()

require.False(t, vm.CanStop())
vm.Stop(vm.stateHandlerError)
}

0 comments on commit 36da76c

Please sign in to comment.