Skip to content

Commit 4ec0da9

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 4ec0da9

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

pkg/limayaml/validate.go

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

375+
if y.Rosetta.Enabled != nil && *y.Rosetta.Enabled && *y.VMType != VZ {
376+
return fmt.Errorf("field `rosetta.enabled` can only be enabled for VMType %q; got %q", VZ, *y.VMType)
377+
}
378+
379+
if y.NestedVirtualization != nil && *y.NestedVirtualization && *y.VMType != VZ {
380+
return fmt.Errorf("field `nestedVirtualization` can only be enabled for VMType %q; got %q", VZ, *y.VMType)
381+
}
382+
383+
if err := validateMountType(y); err != nil {
384+
return err
385+
}
386+
387+
return nil
388+
}
389+
390+
func validateMountType(y *LimaYAML) error {
391+
validMountTypes := map[string]bool{
392+
REVSSHFS: true,
393+
NINEP: true,
394+
VIRTIOFS: true,
395+
WSLMount: true,
396+
}
397+
398+
if !validMountTypes[*y.MountType] {
399+
return fmt.Errorf("field `mountType` must be: %q, %q, %q, %q; got %q", REVSSHFS, NINEP, VIRTIOFS, WSLMount, *y.MountType)
400+
}
401+
402+
if *y.MountType == VIRTIOFS && runtime.GOOS == "darwin" && *y.VMType != VZ {
403+
return fmt.Errorf("field `mountType` %q on macOS requires vmType %q; got %q", *y.MountType, VZ, *y.VMType)
404+
}
405+
375406
return nil
376407
}
377408

@@ -432,6 +463,9 @@ func validateNetwork(y *LimaYAML) error {
432463
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))
433464
}
434465
}
466+
if nw.VZNAT != nil && *nw.VZNAT && *y.VMType != VZ {
467+
return fmt.Errorf("field `%s.vzNAT` needs vmType set to %q; got %q", field, VZ, *y.VMType)
468+
}
435469
// FillDefault() will make sure that nw.Interface is not the empty string
436470
if len(nw.Interface) >= 16 {
437471
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` \"virtiofs\" on macOS requires vmType \"vz\"; 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" {
315+
assert.Error(t, err, "field `mountType` \"virtiofs\" on macOS requires vmType \"vz\"; got \"qemu\"")
316+
} else {
317+
assert.NilError(t, err)
318+
}
319+
}

0 commit comments

Comments
 (0)