Skip to content

Commit 7772fb5

Browse files
committed
change adds validation for lima config values on start or create command
Signed-off-by: olalekan odukoya <[email protected]>
1 parent 597ffed commit 7772fb5

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

pkg/limayaml/validate.go

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

375+
if y.Rosetta.Enabled != nil && *y.Rosetta.Enabled {
376+
if *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+
}
402+
403+
if !validMountTypes[*y.MountType] {
404+
return fmt.Errorf("field `mountType` %s, valid options are: %s, %s, %s", *y.MountType, REVSSHFS, NINEP, VIRTIOFS)
405+
}
406+
407+
if *y.MountType == VIRTIOFS && runtime.GOOS == "darwin" {
408+
if y.VMType != nil && *y.VMType != VZ {
409+
return fmt.Errorf("field `mountType` requires vmType 'vz' on macOS (darwin); got %s", *y.VMType)
410+
}
411+
}
412+
375413
return nil
376414
}
377415

@@ -432,6 +470,9 @@ func validateNetwork(y *LimaYAML) error {
432470
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))
433471
}
434472
}
473+
if nw.VZNAT != nil && *nw.VZNAT && *y.VMType != VZ {
474+
return fmt.Errorf("field `%s.vzNAT` needs vmType set to vz; got %s", field, *y.VMType)
475+
}
435476
// FillDefault() will make sure that nw.Interface is not the empty string
436477
if len(nw.Interface) >= 16 {
437478
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

+105
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,108 @@ 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+
invalidRosetta := `
194+
rosetta:
195+
enabled: true
196+
vmType: "qemu"
197+
`
198+
y, err := Load([]byte(invalidRosetta+"\n"+images), "lima.yaml")
199+
assert.NilError(t, err)
200+
201+
err = Validate(y, false)
202+
assert.Error(t, err, "field `rosetta.enabled` can only be enabled for VMType \"vz\"; got \"qemu\"")
203+
204+
validRosetta := `
205+
rosetta:
206+
enabled: true
207+
vmType: "vz"
208+
`
209+
y, err = Load([]byte(validRosetta+"\n"+images), "lima.yaml")
210+
assert.NilError(t, err)
211+
212+
err = Validate(y, false)
213+
assert.NilError(t, err)
214+
215+
rosettaDisabled := `
216+
rosetta:
217+
enabled: false
218+
vmType: "qemu"
219+
`
220+
y, err = Load([]byte(rosettaDisabled+"\n"+images), "lima.yaml")
221+
assert.NilError(t, err)
222+
223+
err = Validate(y, false)
224+
assert.NilError(t, err)
225+
}
226+
227+
func TestValidateNestedVirtualization(t *testing.T) {
228+
images := `images: [{"location": "/"}]`
229+
230+
validYAML := `
231+
nestedVirtualization: true
232+
vmType: vz
233+
` + images
234+
235+
y, err := Load([]byte(validYAML), "lima.yaml")
236+
assert.NilError(t, err)
237+
238+
err = Validate(y, false)
239+
assert.NilError(t, err)
240+
241+
invalidYAML := `
242+
nestedVirtualization: true
243+
vmType: qemu
244+
` + images
245+
246+
y, err = Load([]byte(invalidYAML), "lima.yaml")
247+
assert.NilError(t, err)
248+
249+
err = Validate(y, false)
250+
assert.Error(t, err, "field `nestedVirtualization` can only be enabled for VMType \"vz\"; got \"qemu\"")
251+
}
252+
253+
func TestValidateMountTypeOS(t *testing.T) {
254+
images := `images: [{"location": "/"}]`
255+
256+
inValidMountTypeLinux := `
257+
mountType: "rMountType"
258+
`
259+
y, err := Load([]byte(inValidMountTypeLinux+"\n"+images), "lima.yaml")
260+
assert.NilError(t, err)
261+
262+
err = Validate(y, true)
263+
assert.Error(t, err, "field `mountType` must be \"reverse-sshfs\" or \"9p\" or \"virtiofs\", or \"wsl2\", got \"rMountType\"")
264+
265+
validMountTypeLinux := `
266+
mountType: "virtiofs"
267+
`
268+
y, err = Load([]byte(validMountTypeLinux+"\n"+images), "lima.yaml")
269+
assert.NilError(t, err)
270+
271+
err = Validate(y, true)
272+
assert.Error(t, err, "field `mountType` requires vmType 'vz' on macOS (darwin); got qemu")
273+
274+
validMountTypeMac := `
275+
mountType: "virtiofs"
276+
vmType: "vz"
277+
`
278+
y, err = Load([]byte(validMountTypeMac+"\n"+images), "lima.yaml")
279+
assert.NilError(t, err)
280+
281+
err = Validate(y, false)
282+
assert.NilError(t, err)
283+
284+
invalidMountTypeMac := `
285+
mountType: "virtiofs"
286+
vmType: "qemu"
287+
`
288+
y, err = Load([]byte(invalidMountTypeMac+"\n"+images), "lima.yaml")
289+
assert.NilError(t, err)
290+
291+
err = Validate(y, false)
292+
assert.Error(t, err, "field `mountType` requires vmType 'vz' on macOS (darwin); got qemu")
293+
}

0 commit comments

Comments
 (0)