-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for new func supporting ignition and fix existing tests
- Loading branch information
Luca Stocchi
authored and
Luca Stocchi
committed
Oct 18, 2024
1 parent
ed2909f
commit 95ea6a3
Showing
3 changed files
with
78 additions
and
1 deletion.
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
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) | ||
} |
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,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) | ||
} |
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