| 1 | |
| 2 | |
| 3 | |
| 4 | package qemu |
| 5 | |
| 6 | import ( |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/coreos/go-semver/semver" |
| 13 | "gotest.tools/v3/assert" |
| 14 | |
| 15 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 16 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 17 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 18 | ) |
| 19 | |
| 20 | func TestArgValue(t *testing.T) { |
| 21 | type testCase struct { |
| 22 | key string |
| 23 | expectedValue string |
| 24 | expectedOK bool |
| 25 | } |
| 26 | args := []string{"-cpu", "foo", "-no-reboot", "-m", "2G", "-s"} |
| 27 | testCases := []testCase{ |
| 28 | { |
| 29 | key: "-cpu", |
| 30 | expectedValue: "foo", |
| 31 | expectedOK: true, |
| 32 | }, |
| 33 | { |
| 34 | key: "-no-reboot", |
| 35 | expectedValue: "", |
| 36 | expectedOK: true, |
| 37 | }, |
| 38 | { |
| 39 | key: "-m", |
| 40 | expectedValue: "2G", |
| 41 | expectedOK: true, |
| 42 | }, |
| 43 | { |
| 44 | key: "-machine", |
| 45 | expectedValue: "", |
| 46 | expectedOK: false, |
| 47 | }, |
| 48 | { |
| 49 | key: "-s", |
| 50 | expectedValue: "", |
| 51 | expectedOK: true, |
| 52 | }, |
| 53 | } |
| 54 | |
| 55 | for _, tc := range testCases { |
| 56 | v, ok := argValue(args, tc.key) |
| 57 | assert.Equal(t, tc.expectedValue, v) |
| 58 | assert.Equal(t, tc.expectedOK, ok) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestParseQemuVersion(t *testing.T) { |
| 63 | type testCase struct { |
| 64 | versionOutput string |
| 65 | expectedValue string |
| 66 | expectedError string |
| 67 | } |
| 68 | testCases := []testCase{ |
| 69 | { |
| 70 | |
| 71 | versionOutput: "QEMU emulator version 1.5.3 (qemu-kvm-1.5.3-175.el7_9.6), " + |
| 72 | "Copyright (c) 2003-2008 Fabrice Bellard\n", |
| 73 | expectedValue: "1.5.3", |
| 74 | expectedError: "", |
| 75 | }, |
| 76 | { |
| 77 | |
| 78 | versionOutput: "QEMU emulator version 8.0.0 (v8.0.0)\n" + |
| 79 | "Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers\n", |
| 80 | expectedValue: "8.0.0", |
| 81 | expectedError: "", |
| 82 | }, |
| 83 | { |
| 84 | versionOutput: "foobar", |
| 85 | expectedValue: "0.0.0", |
| 86 | expectedError: "failed to parse", |
| 87 | }, |
| 88 | } |
| 89 | |
| 90 | for _, tc := range testCases { |
| 91 | v, err := parseQemuVersion(tc.versionOutput) |
| 92 | if tc.expectedError == "" { |
| 93 | assert.NilError(t, err) |
| 94 | } else { |
| 95 | assert.ErrorContains(t, err, tc.expectedError) |
| 96 | } |
| 97 | assert.Equal(t, tc.expectedValue, v.String()) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestSwtpmCmdline(t *testing.T) { |
| 102 | if runtime.GOOS == "windows" { |
| 103 | t.Skip("swtpm unix socket mode is not supported on Windows host") |
| 104 | } |
| 105 | |
| 106 | tmpDir := t.TempDir() |
| 107 | |
| 108 | |
| 109 | binDir := filepath.Join(tmpDir, "bin") |
| 110 | err := os.MkdirAll(binDir, 0o755) |
| 111 | assert.NilError(t, err) |
| 112 | swtpmPath := filepath.Join(binDir, "swtpm") |
| 113 | err = os.WriteFile(swtpmPath, []byte{}, 0o755) |
| 114 | assert.NilError(t, err) |
| 115 | |
| 116 | |
| 117 | t.Setenv("PATH", binDir) |
| 118 | |
| 119 | |
| 120 | cfg := Config{ |
| 121 | Name: "tpm-test", |
| 122 | InstanceDir: tmpDir, |
| 123 | LimaYAML: &limatype.LimaYAML{}, |
| 124 | } |
| 125 | |
| 126 | stateDir := filepath.Join(tmpDir, filenames.SwtpmDir) |
| 127 | swtpmSock := filepath.Join(tmpDir, filenames.SwtpmSock) |
| 128 | |
| 129 | expectedArgs := []string{ |
| 130 | "socket", |
| 131 | "--tpmstate", "dir=" + stateDir, |
| 132 | "--ctrl", "type=unixio,path=" + swtpmSock, |
| 133 | "--tpm2", |
| 134 | "--terminate", |
| 135 | "--log", "level=1", |
| 136 | } |
| 137 | |
| 138 | exe, args, err := SwtpmCmdline(cfg) |
| 139 | assert.NilError(t, err) |
| 140 | assert.Equal(t, exe, swtpmPath) |
| 141 | assert.DeepEqual(t, args, expectedArgs) |
| 142 | |
| 143 | |
| 144 | _, err = os.Stat(stateDir) |
| 145 | assert.NilError(t, err) |
| 146 | |
| 147 | |
| 148 | err = os.WriteFile(swtpmSock, []byte("stale socket"), 0o644) |
| 149 | assert.NilError(t, err) |
| 150 | |
| 151 | _, _, err = SwtpmCmdline(cfg) |
| 152 | assert.NilError(t, err) |
| 153 | _, err = os.Stat(swtpmSock) |
| 154 | assert.ErrorIs(t, err, os.ErrNotExist) |
| 155 | } |
| 156 | |
| 157 | func TestParseKVMNestedParam(t *testing.T) { |
| 158 | type testCase struct { |
| 159 | value string |
| 160 | expected bool |
| 161 | expectedError string |
| 162 | } |
| 163 | testCases := []testCase{ |
| 164 | {value: "Y\n", expected: true}, |
| 165 | {value: "N\n", expected: false}, |
| 166 | {value: "1\n", expected: true}, |
| 167 | {value: "0\n", expected: false}, |
| 168 | {value: "", expectedError: "unexpected value"}, |
| 169 | {value: "foo", expectedError: "unexpected value"}, |
| 170 | } |
| 171 | for _, tc := range testCases { |
| 172 | enabled, err := parseKVMNestedParam(tc.value) |
| 173 | if tc.expectedError == "" { |
| 174 | assert.NilError(t, err) |
| 175 | } else { |
| 176 | assert.ErrorContains(t, err, tc.expectedError) |
| 177 | } |
| 178 | assert.Equal(t, tc.expected, enabled) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | func TestValidateConfigNestedVirtualization(t *testing.T) { |
| 183 | cfg := &limatype.LimaYAML{ |
| 184 | NestedVirtualization: new(true), |
| 185 | } |
| 186 | err := validateConfig(cfg) |
| 187 | if runtime.GOOS == "darwin" { |
| 188 | productVer, verErr := osutil.ProductVersion() |
| 189 | assert.NilError(t, verErr) |
| 190 | if productVer.LessThan(*semver.New("15.0.0")) { |
| 191 | assert.ErrorContains(t, err, "nested virtualization requires macOS 15 or newer") |
| 192 | } else { |
| 193 | assert.NilError(t, err) |
| 194 | } |
| 195 | } else { |
| 196 | assert.NilError(t, err) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func TestNestedVirtualizationEnabled(t *testing.T) { |
| 201 | if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" { |
| 202 | |
| 203 | enabled, err := nestedVirtualizationEnabled(limatype.X8664, "hvf", "host", semver.New("11.1.0")) |
| 204 | assert.NilError(t, err) |
| 205 | assert.Equal(t, false, enabled) |
| 206 | |
| 207 | |
| 208 | enabled, err = nestedVirtualizationEnabled(limatype.AARCH64, "tcg", "cortex-a72", semver.New("11.1.0")) |
| 209 | assert.NilError(t, err) |
| 210 | assert.Equal(t, false, enabled) |
| 211 | |
| 212 | |
| 213 | _, err = nestedVirtualizationEnabled(limatype.AARCH64, "hvf", "host", semver.New("11.0.0")) |
| 214 | assert.ErrorContains(t, err, "QEMU 11.0.0 is too old") |
| 215 | |
| 216 | |
| 217 | enabled, err = nestedVirtualizationEnabled(limatype.AARCH64, "hvf", "host", semver.New("11.1.0")) |
| 218 | assert.NilError(t, err) |
| 219 | assert.Equal(t, true, enabled) |
| 220 | } |
| 221 | } |