package qemu
import (
"testing"
"gotest.tools/v3/assert"
)
func TestArgValue(t *testing.T) {
type testCase struct {
key string
expectedValue string
expectedOK bool
}
args := []string{"-cpu", "foo", "-no-reboot", "-m", "2G", "-s"}
testCases := []testCase{
{
key: "-cpu",
expectedValue: "foo",
expectedOK: true,
},
{
key: "-no-reboot",
expectedValue: "",
expectedOK: true,
},
{
key: "-m",
expectedValue: "2G",
expectedOK: true,
},
{
key: "-machine",
expectedValue: "",
expectedOK: false,
},
{
key: "-s",
expectedValue: "",
expectedOK: true,
},
}
for _, tc := range testCases {
v, ok := argValue(args, tc.key)
assert.Equal(t, tc.expectedValue, v)
assert.Equal(t, tc.expectedOK, ok)
}
}
func TestParseQemuVersion(t *testing.T) {
type testCase struct {
versionOutput string
expectedValue string
expectedError string
}
testCases := []testCase{
{
versionOutput: "QEMU emulator version 1.5.3 (qemu-kvm-1.5.3-175.el7_9.6), " +
"Copyright (c) 2003-2008 Fabrice Bellard\n",
expectedValue: "1.5.3",
expectedError: "",
},
{
versionOutput: "QEMU emulator version 8.0.0 (v8.0.0)\n" +
"Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers\n",
expectedValue: "8.0.0",
expectedError: "",
},
{
versionOutput: "foobar",
expectedValue: "0.0.0",
expectedError: "failed to parse",
},
}
for _, tc := range testCases {
v, err := parseQemuVersion(tc.versionOutput)
if tc.expectedError == "" {
assert.NilError(t, err)
} else {
assert.ErrorContains(t, err, tc.expectedError)
}
assert.Equal(t, tc.expectedValue, v.String())
}
}