| 1 | |
| 2 | |
| 3 | |
| 4 | package limayaml |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | "testing" |
| 12 | |
| 13 | "gotest.tools/v3/assert" |
| 14 | |
| 15 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 16 | "github.com/lima-vm/lima/v2/pkg/version" |
| 17 | ) |
| 18 | |
| 19 | func TestValidateEmpty(t *testing.T) { |
| 20 | y, err := Load(t.Context(), []byte{}, "empty.yaml") |
| 21 | assert.NilError(t, err) |
| 22 | err = Validate(y, false) |
| 23 | assert.Error(t, err, "field `images` must be set") |
| 24 | } |
| 25 | |
| 26 | func TestOffscreenDisplayRequiresMacOSAndVZ(t *testing.T) { |
| 27 | for _, tc := range []struct{ os, driver string }{ |
| 28 | {limatype.DARWIN, limatype.VZ}, |
| 29 | {limatype.LINUX, limatype.VZ}, |
| 30 | {limatype.DARWIN, limatype.QEMU}, |
| 31 | } { |
| 32 | t.Run(tc.os+"-"+tc.driver, func(t *testing.T) { |
| 33 | body := fmt.Sprintf("os: %s\nvmType: %s\nvideo: {display: offscreen}\nimages: [{location: /}]\n", tc.os, tc.driver) |
| 34 | y, err := Load(t.Context(), []byte(body), "lima.yaml") |
| 35 | assert.NilError(t, err) |
| 36 | err = Validate(y, false) |
| 37 | if tc.os == limatype.DARWIN && tc.driver == limatype.VZ { |
| 38 | assert.NilError(t, err) |
| 39 | } else { |
| 40 | assert.ErrorContains(t, err, "offscreen requires a macOS guest") |
| 41 | } |
| 42 | }) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestValidateMinimumLimaVersion(t *testing.T) { |
| 47 | images := `images: [{"location": "/"}]` |
| 48 | |
| 49 | tests := []struct { |
| 50 | name string |
| 51 | currentVersion string |
| 52 | minimumLimaVersion string |
| 53 | wantErr string |
| 54 | }{ |
| 55 | { |
| 56 | name: "minimumLimaVersion less than current version", |
| 57 | currentVersion: "1.1.1-114-g5bf5e513", |
| 58 | minimumLimaVersion: "1.1.0", |
| 59 | wantErr: "", |
| 60 | }, |
| 61 | { |
| 62 | name: "minimumLimaVersion greater than current version", |
| 63 | currentVersion: "1.1.1-114-g5bf5e513", |
| 64 | minimumLimaVersion: "1.1.2", |
| 65 | wantErr: "template requires Lima version `1.1.2`; this is only `1.1.1-114-g5bf5e513`", |
| 66 | }, |
| 67 | { |
| 68 | name: "invalid current version", |
| 69 | currentVersion: "<unknown>", |
| 70 | minimumLimaVersion: "0.8.0", |
| 71 | wantErr: "", |
| 72 | }, |
| 73 | { |
| 74 | name: "invalid minimumLimaVersion", |
| 75 | currentVersion: "1.1.1-114-g5bf5e513", |
| 76 | minimumLimaVersion: "invalid", |
| 77 | wantErr: "field `minimumLimaVersion` must be a semvar value, got `invalid`: invalid is not in dotted-tri format", |
| 78 | }, |
| 79 | } |
| 80 | |
| 81 | for _, tt := range tests { |
| 82 | t.Run(tt.name, func(t *testing.T) { |
| 83 | oldVersion := version.Version |
| 84 | version.Version = tt.currentVersion |
| 85 | t.Cleanup(func() { version.Version = oldVersion }) |
| 86 | |
| 87 | y, err := Load(t.Context(), []byte("minimumLimaVersion: "+tt.minimumLimaVersion+"\n"+images), "lima.yaml") |
| 88 | assert.NilError(t, err) |
| 89 | |
| 90 | err = Validate(y, false) |
| 91 | if tt.wantErr == "" { |
| 92 | assert.NilError(t, err) |
| 93 | } else { |
| 94 | assert.Error(t, err, tt.wantErr) |
| 95 | } |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestValidateDigest(t *testing.T) { |
| 101 | images := `images: [{"location": "https://cloud-images.ubuntu.com/releases/oracular/release-20250701/ubuntu-24.10-server-cloudimg-amd64.img",digest: "69f31d3208895e5f646e345fbc95190e5e311ecd1359a4d6ee2c0b6483ceca03"}]` |
| 102 | validProbe := `probes: [{"script": "#!foo"}]` |
| 103 | y, err := Load(t.Context(), []byte(validProbe+"\n"+images), "lima.yaml") |
| 104 | assert.NilError(t, err) |
| 105 | err = Validate(y, false) |
| 106 | assert.Error(t, err, "field `images[0].digest` is invalid: 69f31d3208895e5f646e345fbc95190e5e311ecd1359a4d6ee2c0b6483ceca03: invalid checksum digest format") |
| 107 | |
| 108 | images2 := `images: [{"location": "https://cloud-images.ubuntu.com/releases/oracular/release-20250701/ubuntu-24.10-server-cloudimg-amd64.img",digest: "sha001:69f31d3208895e5f646e345fbc95190e5e311ecd1359a4d6ee2c0b6483ceca03"}]` |
| 109 | y2, err := Load(t.Context(), []byte(validProbe+"\n"+images2), "lima.yaml") |
| 110 | assert.NilError(t, err) |
| 111 | err = Validate(y2, false) |
| 112 | assert.Error(t, err, "field `images[0].digest` is invalid: sha001:69f31d3208895e5f646e345fbc95190e5e311ecd1359a4d6ee2c0b6483ceca03: unsupported digest algorithm") |
| 113 | } |
| 114 | |
| 115 | func TestValidateProbes(t *testing.T) { |
| 116 | images := `images: [{"location": "/"}]` |
| 117 | validProbe := `probes: [{"script": "#!foo"}]` |
| 118 | y, err := Load(t.Context(), []byte(validProbe+"\n"+images), "lima.yaml") |
| 119 | assert.NilError(t, err) |
| 120 | |
| 121 | err = Validate(y, false) |
| 122 | assert.NilError(t, err) |
| 123 | |
| 124 | invalidProbe := `probes: [{"script": "foo"}]` |
| 125 | y, err = Load(t.Context(), []byte(invalidProbe+"\n"+images), "lima.yaml") |
| 126 | assert.NilError(t, err) |
| 127 | |
| 128 | err = Validate(y, false) |
| 129 | assert.Error(t, err, "field `probe[0].script` must start with a '#!' line") |
| 130 | |
| 131 | invalidProbe = `probes: [{file: {digest: decafbad}}]` |
| 132 | y, err = Load(t.Context(), []byte(invalidProbe+"\n"+images), "lima.yaml") |
| 133 | assert.NilError(t, err) |
| 134 | |
| 135 | err = Validate(y, false) |
| 136 | assert.Error(t, err, "field `probe[0].file.digest` support is not yet implemented\n"+ |
| 137 | "field `probe[0].script` must start with a '#!' line") |
| 138 | } |
| 139 | |
| 140 | func TestValidateProvisionMode(t *testing.T) { |
| 141 | images := `images: [{location: /}]` |
| 142 | provisionBoot := `provision: [{mode: boot, script: "touch /tmp/param-$PARAM_BOOT"}]` |
| 143 | y, err := Load(t.Context(), []byte(provisionBoot+"\n"+images), "lima.yaml") |
| 144 | assert.NilError(t, err) |
| 145 | |
| 146 | err = Validate(y, false) |
| 147 | assert.NilError(t, err) |
| 148 | |
| 149 | provisionUser := `provision: [{mode: user, script: "touch /tmp/param-$PARAM_USER"}]` |
| 150 | y, err = Load(t.Context(), []byte(provisionUser+"\n"+images), "lima.yaml") |
| 151 | assert.NilError(t, err) |
| 152 | |
| 153 | err = Validate(y, false) |
| 154 | assert.NilError(t, err) |
| 155 | |
| 156 | provisionDependency := `provision: [{mode: ansible, script: "touch /tmp/param-$PARAM_DEPENDENCY"}]` |
| 157 | y, err = Load(t.Context(), []byte(provisionDependency+"\n"+images), "lima.yaml") |
| 158 | assert.NilError(t, err) |
| 159 | |
| 160 | err = Validate(y, false) |
| 161 | assert.NilError(t, err) |
| 162 | |
| 163 | provisionInvalid := `provision: [{mode: invalid}]` |
| 164 | y, err = Load(t.Context(), []byte(provisionInvalid+"\n"+images), "lima.yaml") |
| 165 | assert.NilError(t, err) |
| 166 | |
| 167 | err = Validate(y, false) |
| 168 | assert.Error(t, err, "field `provision[0].mode` must one of `system`, `user`, `boot`, `data`, `dependency`, `ansible`, or `yq`\n"+ |
| 169 | "field `provision[0].script` must not be empty") |
| 170 | } |
| 171 | |
| 172 | func TestValidateProvisionModeOnWindows(t *testing.T) { |
| 173 | windowsBase := ` |
| 174 | os: Windows |
| 175 | arch: x86_64 |
| 176 | vmType: qemu |
| 177 | images: |
| 178 | - location: / |
| 179 | osOpts: |
| 180 | Windows: |
| 181 | virtioWin: |
| 182 | - location: / |
| 183 | arch: "x86_64" |
| 184 | ` |
| 185 | |
| 186 | tests := []struct { |
| 187 | name string |
| 188 | provision string |
| 189 | wantErrMsg string |
| 190 | }{ |
| 191 | { |
| 192 | name: "ansible not supported on windows", |
| 193 | provision: `provision: [{mode: ansible}]`, |
| 194 | wantErrMsg: "provision mode `ansible` is not supported on Windows VM", |
| 195 | }, |
| 196 | { |
| 197 | name: "boot not supported on windows", |
| 198 | provision: `provision: [{mode: boot, script: "echo hi"}]`, |
| 199 | wantErrMsg: "provision mode `boot` is not supported on Windows VM", |
| 200 | }, |
| 201 | { |
| 202 | name: "yq not supported on windows", |
| 203 | provision: `provision: [{mode: yq, expression: ".x=1", path: /tmp}]`, |
| 204 | wantErrMsg: "provision mode `yq` is not supported on Windows VM", |
| 205 | }, |
| 206 | { |
| 207 | name: "system is supported on windows", |
| 208 | provision: `provision: [{mode: system, script: "echo hi"}]`, |
| 209 | }, |
| 210 | { |
| 211 | name: "user is supported on windows", |
| 212 | provision: `provision: [{mode: user, script: "echo hi"}]`, |
| 213 | }, |
| 214 | { |
| 215 | name: "dependency is supported on windows", |
| 216 | provision: `provision: [{mode: dependency, script: "echo hi"}]`, |
| 217 | }, |
| 218 | { |
| 219 | name: "data is supported on windows", |
| 220 | provision: `provision: [{mode: data, path: /tmp, content: hello}]`, |
| 221 | }, |
| 222 | } |
| 223 | |
| 224 | for _, tt := range tests { |
| 225 | t.Run(tt.name, func(t *testing.T) { |
| 226 | y, err := Load(t.Context(), []byte(windowsBase+"\n"+tt.provision), "template.yaml") |
| 227 | assert.NilError(t, err) |
| 228 | |
| 229 | err = Validate(y, false) |
| 230 | if tt.wantErrMsg == "" { |
| 231 | assert.NilError(t, err) |
| 232 | } else { |
| 233 | assert.ErrorContains(t, err, tt.wantErrMsg) |
| 234 | } |
| 235 | }) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | func TestValidateProvisionData(t *testing.T) { |
| 240 | images := `images: [{location: /}]` |
| 241 | validData := `provision: [{mode: data, path: /tmp, content: hello}]` |
| 242 | y, err := Load(t.Context(), []byte(validData+"\n"+images), "lima.yaml") |
| 243 | assert.NilError(t, err) |
| 244 | |
| 245 | err = Validate(y, false) |
| 246 | assert.NilError(t, err) |
| 247 | |
| 248 | invalidData := `provision: [{mode: data, content: hello}]` |
| 249 | y, err = Load(t.Context(), []byte(invalidData+"\n"+images), "lima.yaml") |
| 250 | assert.NilError(t, err) |
| 251 | |
| 252 | err = Validate(y, false) |
| 253 | assert.Error(t, err, "field `provision[0].path` must not be empty when mode is `data`") |
| 254 | |
| 255 | invalidData = `provision: [{mode: data, path: /tmp, content: hello, permissions: 9}]` |
| 256 | y, err = Load(t.Context(), []byte(invalidData+"\n"+images), "lima.yaml") |
| 257 | assert.NilError(t, err) |
| 258 | |
| 259 | err = Validate(y, false) |
| 260 | assert.ErrorContains(t, err, "provision[0].permissions` must be an octal number") |
| 261 | } |
| 262 | |
| 263 | func TestValidateProvisionYQ(t *testing.T) { |
| 264 | images := `images: [{location: /}]` |
| 265 | param := `param: {"cdi": "true"}` |
| 266 | |
| 267 | validYQProvision := `provision: [{mode: yq, expression: ".features.cdi={{.Param.cdi}}", path: /tmp}]` |
| 268 | y, err := Load(t.Context(), []byte(param+"\n"+validYQProvision+"\n"+images), "lima.yaml") |
| 269 | assert.NilError(t, err) |
| 270 | err = Validate(y, false) |
| 271 | assert.NilError(t, err) |
| 272 | |
| 273 | |
| 274 | invalidYQProvision := `provision: [{mode: yq, expression: ".features.cdi={{.Param.cdi}}"}]` |
| 275 | y, err = Load(t.Context(), []byte(param+"\n"+invalidYQProvision+"\n"+images), "lima.yaml") |
| 276 | assert.NilError(t, err) |
| 277 | err = Validate(y, false) |
| 278 | assert.ErrorContains(t, err, "field `provision[0].path` must not be empty when mode is `yq`") |
| 279 | |
| 280 | |
| 281 | invalidYQProvision = `provision: [{mode: yq, expression: ".features.cdi={{.Param.cdi}}", path: tmp}]` |
| 282 | y, err = Load(t.Context(), []byte(param+"\n"+invalidYQProvision+"\n"+images), "lima.yaml") |
| 283 | assert.NilError(t, err) |
| 284 | err = Validate(y, false) |
| 285 | assert.ErrorContains(t, err, "field `provision[0].path` must be an absolute path") |
| 286 | |
| 287 | |
| 288 | invalidYQProvision = `provision: [{mode: yq, path: "/{{.Param.cdi}}"}]` |
| 289 | y, err = Load(t.Context(), []byte(param+"\n"+invalidYQProvision+"\n"+images), "lima.yaml") |
| 290 | assert.NilError(t, err) |
| 291 | err = Validate(y, false) |
| 292 | assert.ErrorContains(t, err, "field `provision[0].expression` must not be empty when mode is `yq`") |
| 293 | |
| 294 | |
| 295 | invalidYQProvision = `provision: [{mode: yq, expression: ".features.cdi={{.Param.cdi}}", path: /tmp, permissions: 9}]` |
| 296 | y, err = Load(t.Context(), []byte(param+"\n"+invalidYQProvision+"\n"+images), "lima.yaml") |
| 297 | assert.NilError(t, err) |
| 298 | err = Validate(y, false) |
| 299 | assert.ErrorContains(t, err, "provision[0].permissions` must be an octal number") |
| 300 | } |
| 301 | |
| 302 | func TestValidateAdditionalDisks(t *testing.T) { |
| 303 | images := `images: [{"location": "/"}]` |
| 304 | |
| 305 | validDisks := ` |
| 306 | additionalDisks: |
| 307 | - name: "disk1" |
| 308 | - name: "disk2" |
| 309 | ` |
| 310 | y, err := Load(t.Context(), []byte(validDisks+"\n"+images), "lima.yaml") |
| 311 | assert.NilError(t, err) |
| 312 | |
| 313 | err = Validate(y, false) |
| 314 | assert.NilError(t, err) |
| 315 | |
| 316 | invalidDisks := ` |
| 317 | additionalDisks: |
| 318 | - name: "" |
| 319 | ` |
| 320 | y, err = Load(t.Context(), []byte(invalidDisks+"\n"+images), "lima.yaml") |
| 321 | assert.NilError(t, err) |
| 322 | |
| 323 | err = Validate(y, false) |
| 324 | assert.Error(t, err, "field `additionalDisks[0].name is invalid`: identifier must not be empty") |
| 325 | } |
| 326 | |
| 327 | func TestValidateReadonlyNames(t *testing.T) { |
| 328 | images := `images: [{"location": "/"}]` |
| 329 | dir1, dir2 := t.TempDir(), t.TempDir() |
| 330 | mounts := func(sshfs1, sshfs2 string) string { |
| 331 | return fmt.Sprintf("mounts: [{location: %q, writable: true, sshfs: %s}, {location: %q, sshfs: %s}]", dir1, sshfs1, dir2, sshfs2) |
| 332 | } |
| 333 | builtin := "{sftpDriver: builtin}" |
| 334 | protected := "{sftpDriver: builtin, readonlyNames: [.git]}" |
| 335 | tests := []struct { |
| 336 | name string |
| 337 | config string |
| 338 | wantErr string |
| 339 | }{ |
| 340 | { |
| 341 | name: "valid", |
| 342 | config: "mountType: reverse-sshfs\n" + mounts(protected, builtin), |
| 343 | }, |
| 344 | { |
| 345 | name: "virtiofs", |
| 346 | config: "mountType: virtiofs\n" + mounts(protected, builtin), |
| 347 | wantErr: "field `mounts[*].sshfs.readonlyNames` requires `mountType` to be `reverse-sshfs`", |
| 348 | }, |
| 349 | { |
| 350 | name: "default driver", |
| 351 | config: "mountType: reverse-sshfs\n" + mounts("{readonlyNames: [.git]}", builtin), |
| 352 | wantErr: "field `mounts[0].sshfs.sftpDriver` must be `builtin` when any mount sets `sshfs.readonlyNames`", |
| 353 | }, |
| 354 | { |
| 355 | name: "other mount with openssh-sftp-server", |
| 356 | config: "mountType: reverse-sshfs\n" + mounts(protected, "{sftpDriver: openssh-sftp-server}"), |
| 357 | wantErr: "field `mounts[1].sshfs.sftpDriver` must be `builtin` when any mount sets `sshfs.readonlyNames`", |
| 358 | }, |
| 359 | { |
| 360 | name: "path instead of name", |
| 361 | config: "mountType: reverse-sshfs\n" + mounts("{sftpDriver: builtin, readonlyNames: [.git/hooks]}", builtin), |
| 362 | wantErr: "field `mounts[0].sshfs.readonlyNames` must contain file names, got `.git/hooks`", |
| 363 | }, |
| 364 | } |
| 365 | for _, tt := range tests { |
| 366 | t.Run(tt.name, func(t *testing.T) { |
| 367 | y, err := Load(t.Context(), []byte(tt.config+"\n"+images), "lima.yaml") |
| 368 | assert.NilError(t, err) |
| 369 | err = Validate(y, false) |
| 370 | if tt.wantErr == "" { |
| 371 | assert.NilError(t, err) |
| 372 | } else { |
| 373 | assert.Error(t, err, tt.wantErr) |
| 374 | } |
| 375 | }) |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | |
| 380 | |
| 381 | func TestValidateReadonlyNamesNested(t *testing.T) { |
| 382 | images := `images: [{"location": "/"}]` |
| 383 | parent := t.TempDir() |
| 384 | child := filepath.Join(parent, "child") |
| 385 | assert.NilError(t, os.Mkdir(child, 0o755)) |
| 386 | protected := "{sftpDriver: builtin, readonlyNames: [.git]}" |
| 387 | builtin := "{sftpDriver: builtin}" |
| 388 | config := func(parentWritable bool, parentSSHFS string) string { |
| 389 | return fmt.Sprintf("mountType: reverse-sshfs\nmounts: [{location: %q, writable: true, sshfs: %s}, {location: %q, writable: %v, sshfs: %s}]", |
| 390 | child, protected, parent, parentWritable, parentSSHFS) |
| 391 | } |
| 392 | for _, tt := range []struct { |
| 393 | name string |
| 394 | config string |
| 395 | wantErr string |
| 396 | }{ |
| 397 | {"parent protects the same names", config(true, protected), ""}, |
| 398 | {"parent is read-only", config(false, builtin), ""}, |
| 399 | {"parent writes .git", config(true, builtin), "field `mounts[1].sshfs.readonlyNames` must contain `.git`, as the writable mount `mounts[0]` inside it does"}, |
| 400 | } { |
| 401 | t.Run(tt.name, func(t *testing.T) { |
| 402 | y, err := Load(t.Context(), []byte(tt.config+"\n"+images), "lima.yaml") |
| 403 | assert.NilError(t, err) |
| 404 | err = Validate(y, false) |
| 405 | if tt.wantErr == "" { |
| 406 | assert.NilError(t, err) |
| 407 | } else { |
| 408 | assert.Error(t, err, tt.wantErr) |
| 409 | } |
| 410 | }) |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | func TestValidateParamName(t *testing.T) { |
| 415 | images := `images: [{"location": "/"}]` |
| 416 | validProvision := `provision: [{"script": "echo $PARAM_name $PARAM_NAME $PARAM_Name_123"}]` |
| 417 | validParam := []string{ |
| 418 | `param: {"name": "value"}`, |
| 419 | `param: {"NAME": "value"}`, |
| 420 | `param: {"Name_123": "value"}`, |
| 421 | } |
| 422 | for _, param := range validParam { |
| 423 | y, err := Load(t.Context(), []byte(param+"\n"+validProvision+"\n"+images), "lima.yaml") |
| 424 | assert.NilError(t, err) |
| 425 | |
| 426 | err = Validate(y, false) |
| 427 | assert.NilError(t, err) |
| 428 | } |
| 429 | |
| 430 | invalidProvision := `provision: [{"script": "echo $PARAM__Name $PARAM_3Name $PARAM_Last.Name"}]` |
| 431 | invalidParam := []string{ |
| 432 | `param: {"_Name": "value"}`, |
| 433 | `param: {"3Name": "value"}`, |
| 434 | `param: {"Last.Name": "value"}`, |
| 435 | } |
| 436 | for _, param := range invalidParam { |
| 437 | y, err := Load(t.Context(), []byte(param+"\n"+invalidProvision+"\n"+images), "lima.yaml") |
| 438 | assert.NilError(t, err) |
| 439 | |
| 440 | err = Validate(y, false) |
| 441 | assert.ErrorContains(t, err, "name does not match regex") |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | func TestValidateParamValue(t *testing.T) { |
| 446 | images := `images: [{"location": "/"}]` |
| 447 | provision := `provision: [{"script": "echo $PARAM_name"}]` |
| 448 | validParam := []string{ |
| 449 | `param: {"name": ""}`, |
| 450 | `param: {"name": "foo bar"}`, |
| 451 | `param: {"name": "foo\tbar"}`, |
| 452 | `param: {"name": "Symbols ½ and emoji → 👀"}`, |
| 453 | } |
| 454 | for _, param := range validParam { |
| 455 | y, err := Load(t.Context(), []byte(param+"\n"+provision+"\n"+images), "lima.yaml") |
| 456 | assert.NilError(t, err) |
| 457 | |
| 458 | err = Validate(y, false) |
| 459 | assert.NilError(t, err) |
| 460 | } |
| 461 | |
| 462 | invalidParam := []string{ |
| 463 | `param: {"name": "The end.\n"}`, |
| 464 | `param: {"name": "\r"}`, |
| 465 | } |
| 466 | for _, param := range invalidParam { |
| 467 | y, err := Load(t.Context(), []byte(param+"\n"+provision+"\n"+images), "lima.yaml") |
| 468 | assert.NilError(t, err) |
| 469 | |
| 470 | err = Validate(y, false) |
| 471 | assert.ErrorContains(t, err, "value contains unprintable character") |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | func TestValidateParamIsUsed(t *testing.T) { |
| 476 | paramYaml := `param: |
| 477 | name: value` |
| 478 | _, err := Load(t.Context(), []byte(paramYaml), "paramIsNotUsed.yaml") |
| 479 | assert.Error(t, err, "field `param` key `name` is not used in any provision, probe, copyToHost, or portForward") |
| 480 | |
| 481 | fieldsUsingParam := []string{ |
| 482 | `mounts: [{"location": "/tmp/{{ .Param.name }}"}]`, |
| 483 | `mounts: [{"location": "/tmp", mountPoint: "/tmp/{{ .Param.name }}"}]`, |
| 484 | `provision: [{"script": "echo {{ .Param.name }}"}]`, |
| 485 | `provision: [{"script": "echo $PARAM_name"}]`, |
| 486 | `probes: [{"script": "echo {{ .Param.name }}"}]`, |
| 487 | `probes: [{"script": "echo $PARAM_name"}]`, |
| 488 | `copyToHost: [{"guest": "/tmp/{{ .Param.name }}", "host": "/tmp"}]`, |
| 489 | `copyToHost: [{"guest": "/tmp", "host": "/tmp/{{ .Param.name }}"}]`, |
| 490 | `portForwards: [{"guestSocket": "/tmp/{{ .Param.name }}", "hostSocket": "/tmp"}]`, |
| 491 | `portForwards: [{"guestSocket": "/tmp", "hostSocket": "/tmp/{{ .Param.name }}"}]`, |
| 492 | } |
| 493 | for _, fieldUsingParam := range fieldsUsingParam { |
| 494 | _, err = Load(t.Context(), []byte(fieldUsingParam+"\n"+paramYaml), "paramIsUsed.yaml") |
| 495 | |
| 496 | assert.NilError(t, err) |
| 497 | } |
| 498 | |
| 499 | |
| 500 | rootfulYaml := `param: |
| 501 | rootful: true` |
| 502 | fieldsUsingIfParamRootfulTrue := []string{ |
| 503 | `mounts: [{"location": "/tmp/{{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}", "mountPoint": "/tmp"}]`, |
| 504 | `mounts: [{"location": "/tmp", "mountPoint": "/tmp/{{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}"}]`, |
| 505 | `provision: [{"script": "echo {{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}"}]`, |
| 506 | `probes: [{"script": "echo {{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}"}]`, |
| 507 | `copyToHost: [{"guest": "/tmp/{{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}", "host": "/tmp"}]`, |
| 508 | `copyToHost: [{"guest": "/tmp", "host": "/tmp/{{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}"}]`, |
| 509 | `portForwards: [{"guestSocket": "{{if eq .Param.rootful \"true\"}}/var/run{{else}}/run/user/{{.UID}}{{end}}/docker.sock", "hostSocket": "{{.Dir}}/sock/docker.sock"}]`, |
| 510 | `portForwards: [{"guestSocket": "/var/run/docker.sock", "hostSocket": "{{.Dir}}/sock/docker-{{if eq .Param.rootful \"true\"}}rootful{{else}}rootless{{end}}.sock"}]`, |
| 511 | } |
| 512 | for _, fieldUsingIfParamRootfulTrue := range fieldsUsingIfParamRootfulTrue { |
| 513 | _, err = Load(t.Context(), []byte(fieldUsingIfParamRootfulTrue+"\n"+rootfulYaml), "paramIsUsed.yaml") |
| 514 | |
| 515 | assert.NilError(t, err) |
| 516 | } |
| 517 | |
| 518 | |
| 519 | rootFulYaml := `param: |
| 520 | rootFul: true` |
| 521 | for _, fieldUsingIfParamRootfulTrue := range fieldsUsingIfParamRootfulTrue { |
| 522 | _, err = Load(t.Context(), []byte(fieldUsingIfParamRootfulTrue+"\n"+rootFulYaml), "paramIsUsed.yaml") |
| 523 | |
| 524 | assert.Error(t, err, "field `param` key `rootFul` is not used in any provision, probe, copyToHost, or portForward") |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | func TestValidateMultipleErrors(t *testing.T) { |
| 529 | yamlWithMultipleErrors := ` |
| 530 | os: windows |
| 531 | arch: unsupported_arch |
| 532 | portForwards: |
| 533 | - guestPort: 22 |
| 534 | hostPort: 2222 |
| 535 | - guestPort: 8080 |
| 536 | hostPort: 65536 |
| 537 | provision: |
| 538 | - mode: invalid_mode |
| 539 | script: echo test |
| 540 | - mode: data |
| 541 | content: test |
| 542 | ` |
| 543 | |
| 544 | y, err := Load(t.Context(), []byte(yamlWithMultipleErrors), "multiple-errors.yaml") |
| 545 | assert.NilError(t, err) |
| 546 | err = Validate(y, false) |
| 547 | t.Logf("Validation errors: %v", err) |
| 548 | |
| 549 | assert.Error(t, err, "field `os` must be one of [`Linux` `Darwin` `FreeBSD` `Windows`]; got `windows`\n"+ |
| 550 | "field `arch` must be one of [x86_64 aarch64 armv7l ppc64le riscv64 s390x]; got `unsupported_arch`\n"+ |
| 551 | "field `images` must be set\n"+ |
| 552 | "field `provision[0].mode` must one of `system`, `user`, `boot`, `data`, `dependency`, `ansible`, or `yq`\n"+ |
| 553 | "field `provision[1].path` must not be empty when mode is `data`") |
| 554 | } |
| 555 | |
| 556 | func TestValidateAgainstLatestConfig(t *testing.T) { |
| 557 | tests := []struct { |
| 558 | name string |
| 559 | yNew string |
| 560 | yLatest string |
| 561 | wantErr string |
| 562 | }{ |
| 563 | { |
| 564 | name: "Valid disk size unchanged", |
| 565 | yNew: `disk: 100GiB`, |
| 566 | yLatest: `disk: 100GiB`, |
| 567 | }, |
| 568 | { |
| 569 | name: "Valid disk size increased", |
| 570 | yNew: `disk: 200GiB`, |
| 571 | yLatest: `disk: 100GiB`, |
| 572 | }, |
| 573 | { |
| 574 | name: "No disk field in both YAMLs", |
| 575 | yNew: ``, |
| 576 | yLatest: ``, |
| 577 | }, |
| 578 | { |
| 579 | name: "No disk field in new YAMLs", |
| 580 | yNew: ``, |
| 581 | yLatest: `disk: 100GiB`, |
| 582 | }, |
| 583 | { |
| 584 | name: "No disk field in latest YAMLs", |
| 585 | yNew: `disk: 100GiB`, |
| 586 | yLatest: ``, |
| 587 | }, |
| 588 | { |
| 589 | name: "Disk size shrunk", |
| 590 | yNew: `disk: 50GiB`, |
| 591 | yLatest: `disk: 100GiB`, |
| 592 | wantErr: "field `disk`: shrinking the disk (100GiB --> 50GiB) is not supported", |
| 593 | }, |
| 594 | } |
| 595 | |
| 596 | for _, tt := range tests { |
| 597 | t.Run(tt.name, func(t *testing.T) { |
| 598 | err := ValidateAgainstLatestConfig(t.Context(), []byte(tt.yNew), []byte(tt.yLatest)) |
| 599 | if tt.wantErr == "" { |
| 600 | assert.NilError(t, err) |
| 601 | } else { |
| 602 | assert.Error(t, err, tt.wantErr) |
| 603 | } |
| 604 | }) |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | func TestWindowsGuest(t *testing.T) { |
| 609 | if runtime.GOOS != "linux" && runtime.GOOS != "darwin" { |
| 610 | t.Skip("this test only works on linux or darwin") |
| 611 | } |
| 612 | |
| 613 | baseYaml := ` |
| 614 | os: Windows |
| 615 | arch: %s |
| 616 | vmType: %s |
| 617 | images: |
| 618 | - location: / |
| 619 | osOpts: |
| 620 | Windows: |
| 621 | virtioWin: |
| 622 | - location: / |
| 623 | arch: "x86_64" |
| 624 | ` |
| 625 | |
| 626 | testCases := []struct { |
| 627 | name string |
| 628 | yaml string |
| 629 | wantErrMsg string |
| 630 | }{ |
| 631 | { |
| 632 | name: "ok", |
| 633 | yaml: fmt.Sprintf(baseYaml, limatype.X8664, limatype.QEMU), |
| 634 | }, |
| 635 | { |
| 636 | name: "qemu only supports Windows guest", |
| 637 | yaml: fmt.Sprintf(baseYaml, limatype.X8664, limatype.VZ), |
| 638 | wantErrMsg: "currently Windows guest is only supported on `qemu`; got `vz`", |
| 639 | }, |
| 640 | { |
| 641 | name: "x86_64 and aarch64 only support Windows guest", |
| 642 | yaml: fmt.Sprintf(baseYaml, limatype.ARMV7L, limatype.QEMU), |
| 643 | wantErrMsg: "currently Windows guest is only supported on [`x86_64`, `aarch64`]; got `armv7l`", |
| 644 | }, |
| 645 | } |
| 646 | |
| 647 | for _, tt := range testCases { |
| 648 | t.Run(tt.name, func(t *testing.T) { |
| 649 | y, err := Load(t.Context(), []byte(tt.yaml), "template.yaml") |
| 650 | assert.NilError(t, err) |
| 651 | err = Validate(y, false) |
| 652 | |
| 653 | if tt.wantErrMsg == "" { |
| 654 | assert.NilError(t, err) |
| 655 | } else { |
| 656 | assert.Equal(t, tt.wantErrMsg, err.Error()) |
| 657 | } |
| 658 | }) |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | func TestIsSupportedWindowsShell(t *testing.T) { |
| 663 | tests := []struct { |
| 664 | shell string |
| 665 | want bool |
| 666 | }{ |
| 667 | {"cmd.exe", true}, |
| 668 | {"CMD.EXE", true}, |
| 669 | {"powershell.exe", true}, |
| 670 | {"pwsh.exe", true}, |
| 671 | {`C:\Windows\System32\cmd.exe`, true}, |
| 672 | {`C:/Windows/System32/cmd.exe`, true}, |
| 673 | {`C:\Program Files\PowerShell\7\pwsh.exe`, true}, |
| 674 | {"bash.exe", false}, |
| 675 | {"cmd", false}, |
| 676 | {"/bin/bash", false}, |
| 677 | {"", false}, |
| 678 | } |
| 679 | for _, tt := range tests { |
| 680 | t.Run(tt.shell, func(t *testing.T) { |
| 681 | assert.Equal(t, tt.want, IsSupportedWindowsShell(tt.shell)) |
| 682 | }) |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | func TestValidateUserShell(t *testing.T) { |
| 687 | posixImages := `images: [{"location": "/"}]` |
| 688 | windowsBase := ` |
| 689 | os: Windows |
| 690 | arch: x86_64 |
| 691 | vmType: qemu |
| 692 | images: |
| 693 | - location: / |
| 694 | osOpts: |
| 695 | Windows: |
| 696 | virtioWin: |
| 697 | - location: / |
| 698 | arch: "x86_64" |
| 699 | ` |
| 700 | |
| 701 | tests := []struct { |
| 702 | name string |
| 703 | yaml string |
| 704 | wantErrMsg string |
| 705 | }{ |
| 706 | { |
| 707 | name: "posix absolute path ok", |
| 708 | yaml: posixImages + "\nuser:\n shell: /bin/bash\n", |
| 709 | }, |
| 710 | { |
| 711 | name: "posix relative path rejected", |
| 712 | yaml: posixImages + "\nuser:\n shell: bash\n", |
| 713 | wantErrMsg: "field `user.shell` must be an absolute path, got `bash`", |
| 714 | }, |
| 715 | { |
| 716 | name: "windows cmd.exe ok", |
| 717 | yaml: windowsBase + "\nuser:\n shell: cmd.exe\n", |
| 718 | }, |
| 719 | { |
| 720 | name: "windows pwsh.exe ok", |
| 721 | yaml: windowsBase + "\nuser:\n shell: pwsh.exe\n", |
| 722 | }, |
| 723 | { |
| 724 | name: "windows absolute cmd.exe ok", |
| 725 | yaml: windowsBase + "\nuser:\n shell: 'C:\\Windows\\System32\\cmd.exe'\n", |
| 726 | }, |
| 727 | { |
| 728 | name: "windows posix shell rejected", |
| 729 | yaml: windowsBase + "\nuser:\n shell: /bin/bash\n", |
| 730 | wantErrMsg: "field `user.shell` must be one of [cmd.exe powershell.exe pwsh.exe] for Windows guest, got `/bin/bash`", |
| 731 | }, |
| 732 | } |
| 733 | for _, tt := range tests { |
| 734 | t.Run(tt.name, func(t *testing.T) { |
| 735 | y, err := Load(t.Context(), []byte(tt.yaml), "template.yaml") |
| 736 | assert.NilError(t, err) |
| 737 | err = Validate(y, false) |
| 738 | if tt.wantErrMsg == "" { |
| 739 | assert.NilError(t, err) |
| 740 | } else { |
| 741 | assert.ErrorContains(t, err, tt.wantErrMsg) |
| 742 | } |
| 743 | }) |
| 744 | } |
| 745 | } |