Skip to content

Commit

Permalink
test: add tests for new func supporting ignition and fix existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Stocchi authored and Luca Stocchi committed Oct 18, 2024
1 parent ed2909f commit 95ea6a3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
44 changes: 44 additions & 0 deletions cmd/vfkit/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"bytes"
"io"
"net"
"net/http"
"os"
"testing"

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

func TestStartIgnitionProvisionerServer(t *testing.T) {
socketPath := "virtiovsock"
defer os.Remove(socketPath)

ignitionData := []byte("ignition configuration")
ignitionReader := bytes.NewReader(ignitionData)

// Start the server using the socket so that it can returns the ignition data
go func() {
err := startIgnitionProvisionerServer(ignitionReader, socketPath)
require.NoError(t, err)
}()

// Make a request to the server
client := http.Client{
Transport: &http.Transport{
Dial: func(_, _ string) (net.Conn, error) {
return net.Dial("unix", socketPath)
},
},
}
resp, err := client.Get("http://unix://" + socketPath)
require.NoError(t, err)
defer resp.Body.Close()

// Verify the response from the server is actually the ignition data
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, ignitionData, body)
}
23 changes: 23 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config

import (
"testing"

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

func TestAddIgnitionFile_MultipleOptions(t *testing.T) {
vm := &VirtualMachine{}
err := vm.AddIgnitionFileFromCmdLine("file1,file2")
assert.EqualError(t, err, "ignition only accept one option in command line argument")
}

func TestAddIgnitionFile_OneOption(t *testing.T) {
vm := &VirtualMachine{}
err := vm.AddIgnitionFileFromCmdLine("file1")
require.NoError(t, err)
assert.Len(t, vm.Devices, 1)
assert.Equal(t, uint32(ignitionVsockPort), vm.Devices[0].(*VirtioVsock).Port)
assert.Equal(t, "file1", vm.Ignition.ConfigPath)
}
12 changes: 11 additions & 1 deletion pkg/config/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ var jsonTests = map[string]jsonTest{
},
expectedJSON: `{"vcpus":3,"memoryBytes":4194304000,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","initrdPath":"/initrd","kernelCmdLine":"console=hvc0"},"timesync":{"vsockPort":1234}}`,
},
"TestIgnition": {
newVM: func(t *testing.T) *VirtualMachine {
vm := newLinuxVM(t)
ignition, err := IgnitionNew("config", "socket")
require.NoError(t, err)
vm.Ignition = ignition
return vm
},
expectedJSON: `{"vcpus":3,"memoryBytes":4194304000,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","initrdPath":"/initrd","kernelCmdLine":"console=hvc0"}, "ignition":{"kind":"ignition","configPath":"config","socketPath":"socket"}}`,
},
"TestVirtioRNG": {
newVM: func(t *testing.T) *VirtualMachine {
vm := newLinuxVM(t)
Expand Down Expand Up @@ -207,7 +217,7 @@ var jsonStabilityTests = map[string]jsonStabilityTest{

return vm
},
skipFields: []string{"Bootloader", "Devices", "Timesync"},
skipFields: []string{"Bootloader", "Devices", "Timesync", "Ignition"},
expectedJSON: `{"vcpus":3,"memoryBytes":3,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","kernelCmdLine":"console=hvc0","initrdPath":"/initrd"},"devices":[{"kind":"virtiorng"}],"timesync":{"vsockPort":1234}}`,
},
"RosettaShare": {
Expand Down

0 comments on commit 95ea6a3

Please sign in to comment.