Skip to content

Commit 5dc7b37

Browse files
committed
change adds validation for lima config values on start or create command
1 parent 597ffed commit 5dc7b37

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

pkg/limayaml/validate.go

+42
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,45 @@ func Validate(y *LimaYAML, warn bool) error {
372372
}
373373
}
374374

375+
if y.Rosetta.Enabled != nil {
376+
if *y.Rosetta.Enabled && *y.VMType != VZ {
377+
return fmt.Errorf("field `rosetta.enabled` can only be enabled for VMType %q; got %q", VZ, *y.VMType)
378+
}
379+
}
380+
381+
if y.NestedVirtualization != nil && *y.NestedVirtualization {
382+
if *y.VMType != VZ {
383+
return fmt.Errorf("field `nestedVirtualization` can only be enabled for VMType %q; got %q", VZ, *y.VMType)
384+
}
385+
}
386+
387+
if y.MountType != nil {
388+
if err := validateMountType(y); err != nil {
389+
return err
390+
}
391+
}
392+
393+
return nil
394+
}
395+
396+
func validateMountType(y *LimaYAML) error {
397+
validMountTypes := map[string]bool{
398+
REVSSHFS: true,
399+
NINEP: true,
400+
VIRTIOFS: true,
401+
WSLMount: true,
402+
}
403+
404+
if !validMountTypes[*y.MountType] {
405+
return fmt.Errorf("field `mountType` %s, valid options are: %s, %s, %s, %s", *y.MountType, REVSSHFS, NINEP, VIRTIOFS, WSLMount)
406+
}
407+
408+
if *y.MountType == VIRTIOFS && runtime.GOOS == "darwin" && IsNativeArch(AARCH64) {
409+
if y.VMType == nil || *y.VMType != VZ {
410+
return fmt.Errorf("field `mountType` requires vmType 'vz' on macOS (darwin); got %s", *y.VMType)
411+
}
412+
}
413+
375414
return nil
376415
}
377416

@@ -432,6 +471,9 @@ func validateNetwork(y *LimaYAML) error {
432471
return fmt.Errorf("field `%s.macAddress` must be a 48 bit (6 bytes) MAC address; actual length of %q is %d bytes", field, nw.MACAddress, len(hw))
433472
}
434473
}
474+
if nw.VZNAT != nil && *nw.VZNAT && *y.VMType != VZ {
475+
return fmt.Errorf("field `%s.vzNAT` needs vmType set to vz; got %s", field, *y.VMType)
476+
}
435477
// FillDefault() will make sure that nw.Interface is not the empty string
436478
if len(nw.Interface) >= 16 {
437479
return fmt.Errorf("field `%s.interface` must be less than 16 bytes, but is %d bytes: %q", field, len(nw.Interface), nw.Interface)

pkg/limayaml/validate_test.go

+131
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,134 @@ func TestValidateParamIsUsed(t *testing.T) {
186186
assert.Error(t, err, "field `param` key \"rootFul\" is not used in any provision, probe, copyToHost, or portForward")
187187
}
188188
}
189+
190+
func TestValidateRosetta(t *testing.T) {
191+
images := `images: [{"location": "/"}]`
192+
193+
nilData := ``
194+
y, err := Load([]byte(nilData+"\n"+images), "lima.yaml")
195+
assert.NilError(t, err)
196+
197+
err = Validate(y, false)
198+
assert.NilError(t, err)
199+
200+
invalidRosetta := `
201+
rosetta:
202+
enabled: true
203+
vmType: "qemu"
204+
`
205+
y, err = Load([]byte(invalidRosetta+"\n"+images), "lima.yaml")
206+
assert.NilError(t, err)
207+
208+
err = Validate(y, false)
209+
if runtime.GOOS == "darwin" && IsNativeArch(AARCH64) {
210+
assert.Error(t, err, "field `rosetta.enabled` can only be enabled for VMType \"vz\"; got \"qemu\"")
211+
} else {
212+
assert.NilError(t, err)
213+
}
214+
215+
validRosetta := `
216+
rosetta:
217+
enabled: true
218+
vmType: "vz"
219+
`
220+
y, err = Load([]byte(validRosetta+"\n"+images), "lima.yaml")
221+
assert.NilError(t, err)
222+
223+
err = Validate(y, false)
224+
assert.NilError(t, err)
225+
226+
rosettaDisabled := `
227+
rosetta:
228+
enabled: false
229+
vmType: "qemu"
230+
`
231+
y, err = Load([]byte(rosettaDisabled+"\n"+images), "lima.yaml")
232+
assert.NilError(t, err)
233+
234+
err = Validate(y, false)
235+
assert.NilError(t, err)
236+
}
237+
238+
func TestValidateNestedVirtualization(t *testing.T) {
239+
images := `images: [{"location": "/"}]`
240+
241+
validYAML := `
242+
nestedVirtualization: true
243+
vmType: vz
244+
` + images
245+
246+
y, err := Load([]byte(validYAML), "lima.yaml")
247+
assert.NilError(t, err)
248+
249+
err = Validate(y, false)
250+
assert.NilError(t, err)
251+
252+
invalidYAML := `
253+
nestedVirtualization: true
254+
vmType: qemu
255+
` + images
256+
257+
y, err = Load([]byte(invalidYAML), "lima.yaml")
258+
assert.NilError(t, err)
259+
260+
err = Validate(y, false)
261+
assert.Error(t, err, "field `nestedVirtualization` can only be enabled for VMType \"vz\"; got \"qemu\"")
262+
}
263+
264+
func TestValidateMountTypeOS(t *testing.T) {
265+
images := `images: [{"location": "/"}]`
266+
267+
nilMountConf := ``
268+
y, err := Load([]byte(nilMountConf+"\n"+images), "lima.yaml")
269+
assert.NilError(t, err)
270+
271+
err = Validate(y, false)
272+
assert.NilError(t, err)
273+
274+
inValidMountTypeLinux := `
275+
mountType: "rMountType"
276+
`
277+
y, err = Load([]byte(inValidMountTypeLinux+"\n"+images), "lima.yaml")
278+
assert.NilError(t, err)
279+
280+
err = Validate(y, true)
281+
assert.Error(t, err, "field `mountType` must be \"reverse-sshfs\" or \"9p\" or \"virtiofs\", or \"wsl2\", got \"rMountType\"")
282+
283+
validMountTypeLinux := `
284+
mountType: "virtiofs"
285+
`
286+
y, err = Load([]byte(validMountTypeLinux+"\n"+images), "lima.yaml")
287+
assert.NilError(t, err)
288+
289+
err = Validate(y, true)
290+
if runtime.GOOS == "darwin" && IsNativeArch(AARCH64) {
291+
assert.Error(t, err, "field `mountType` requires vmType 'vz' on macOS (darwin); got qemu")
292+
} else {
293+
assert.NilError(t, err)
294+
}
295+
296+
validMountTypeMac := `
297+
mountType: "virtiofs"
298+
vmType: "vz"
299+
`
300+
y, err = Load([]byte(validMountTypeMac+"\n"+images), "lima.yaml")
301+
assert.NilError(t, err)
302+
303+
err = Validate(y, false)
304+
assert.NilError(t, err)
305+
306+
invalidMountTypeMac := `
307+
mountType: "virtiofs"
308+
vmType: "qemu"
309+
`
310+
y, err = Load([]byte(invalidMountTypeMac+"\n"+images), "lima.yaml")
311+
assert.NilError(t, err)
312+
313+
err = Validate(y, false)
314+
if runtime.GOOS == "darwin" && IsNativeArch(AARCH64) {
315+
assert.Error(t, err, "field `mountType` requires vmType 'vz' on macOS (darwin); got qemu")
316+
} else {
317+
assert.NilError(t, err)
318+
}
319+
}

0 commit comments

Comments
 (0)