Skip to content

Commit 5eff66a

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

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

pkg/limayaml/validate.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,53 @@ func Validate(y *LimaYAML, warn bool) error {
372372
}
373373
}
374374

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

@@ -432,6 +479,9 @@ func validateNetwork(y *LimaYAML) error {
432479
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))
433480
}
434481
}
482+
if nw.VZNAT != nil && *nw.VZNAT && *y.VMType != VZ {
483+
return fmt.Errorf("field `%s.vzNAT` needs vmType set to vz; got %s", field, *y.VMType)
484+
}
435485
// FillDefault() will make sure that nw.Interface is not the empty string
436486
if len(nw.Interface) >= 16 {
437487
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

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

0 commit comments

Comments
 (0)