Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/driver/qemu/qemu_test.go
2639 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package qemu
5
6
import (
7
"testing"
8
9
"gotest.tools/v3/assert"
10
)
11
12
func TestArgValue(t *testing.T) {
13
type testCase struct {
14
key string
15
expectedValue string
16
expectedOK bool
17
}
18
args := []string{"-cpu", "foo", "-no-reboot", "-m", "2G", "-s"}
19
testCases := []testCase{
20
{
21
key: "-cpu",
22
expectedValue: "foo",
23
expectedOK: true,
24
},
25
{
26
key: "-no-reboot",
27
expectedValue: "",
28
expectedOK: true,
29
},
30
{
31
key: "-m",
32
expectedValue: "2G",
33
expectedOK: true,
34
},
35
{
36
key: "-machine",
37
expectedValue: "",
38
expectedOK: false,
39
},
40
{
41
key: "-s",
42
expectedValue: "",
43
expectedOK: true,
44
},
45
}
46
47
for _, tc := range testCases {
48
v, ok := argValue(args, tc.key)
49
assert.Equal(t, tc.expectedValue, v)
50
assert.Equal(t, tc.expectedOK, ok)
51
}
52
}
53
54
func TestParseQemuVersion(t *testing.T) {
55
type testCase struct {
56
versionOutput string
57
expectedValue string
58
expectedError string
59
}
60
testCases := []testCase{
61
{
62
// old one line version
63
versionOutput: "QEMU emulator version 1.5.3 (qemu-kvm-1.5.3-175.el7_9.6), " +
64
"Copyright (c) 2003-2008 Fabrice Bellard\n",
65
expectedValue: "1.5.3",
66
expectedError: "",
67
},
68
{
69
// new two line version
70
versionOutput: "QEMU emulator version 8.0.0 (v8.0.0)\n" +
71
"Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers\n",
72
expectedValue: "8.0.0",
73
expectedError: "",
74
},
75
{
76
versionOutput: "foobar",
77
expectedValue: "0.0.0",
78
expectedError: "failed to parse",
79
},
80
}
81
82
for _, tc := range testCases {
83
v, err := parseQemuVersion(tc.versionOutput)
84
if tc.expectedError == "" {
85
assert.NilError(t, err)
86
} else {
87
assert.ErrorContains(t, err, tc.expectedError)
88
}
89
assert.Equal(t, tc.expectedValue, v.String())
90
}
91
}
92
93