Skip to content

Commit

Permalink
change adds validation for lima config values on start or create command
Browse files Browse the repository at this point in the history
  • Loading branch information
olamilekan000 committed Jan 20, 2025
1 parent 597ffed commit 5eff66a
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,53 @@ func Validate(y *LimaYAML, warn bool) error {
}
}

fmt.Println("----")

Check failure on line 375 in pkg/limayaml/validate.go

View workflow job for this annotation

GitHub Actions / Lints

use of `fmt.Println` forbidden because "Do not use fmt.Print statements." (forbidigo)
fmt.Println("----")

Check failure on line 376 in pkg/limayaml/validate.go

View workflow job for this annotation

GitHub Actions / Lints

use of `fmt.Println` forbidden because "Do not use fmt.Print statements." (forbidigo)
fmt.Println("----")

Check failure on line 377 in pkg/limayaml/validate.go

View workflow job for this annotation

GitHub Actions / Lints

use of `fmt.Println` forbidden because "Do not use fmt.Print statements." (forbidigo)
fmt.Println("----")

Check failure on line 378 in pkg/limayaml/validate.go

View workflow job for this annotation

GitHub Actions / Lints

use of `fmt.Println` forbidden because "Do not use fmt.Print statements." (forbidigo)
logrus.Debugf("Rosetta.Enabled: %v, VMType: %v", *y.Rosetta.Enabled, *y.VMType)
fmt.Println("----")

Check failure on line 380 in pkg/limayaml/validate.go

View workflow job for this annotation

GitHub Actions / Lints

use of `fmt.Println` forbidden because "Do not use fmt.Print statements." (forbidigo)
fmt.Println("----")

Check failure on line 381 in pkg/limayaml/validate.go

View workflow job for this annotation

GitHub Actions / Lints

use of `fmt.Println` forbidden because "Do not use fmt.Print statements." (forbidigo)
fmt.Println("----")

Check failure on line 382 in pkg/limayaml/validate.go

View workflow job for this annotation

GitHub Actions / Lints

use of `fmt.Println` forbidden because "Do not use fmt.Print statements." (forbidigo)
fmt.Println("----")

Check failure on line 383 in pkg/limayaml/validate.go

View workflow job for this annotation

GitHub Actions / Lints

use of `fmt.Println` forbidden because "Do not use fmt.Print statements." (forbidigo)
if y.Rosetta.Enabled != nil {
if *y.Rosetta.Enabled && *y.VMType != VZ {
return fmt.Errorf("field `rosetta.enabled` can only be enabled for VMType %q; got %q", VZ, *y.VMType)
}
}

if y.NestedVirtualization != nil && *y.NestedVirtualization {
if *y.VMType != VZ {
return fmt.Errorf("field `nestedVirtualization` can only be enabled for VMType %q; got %q", VZ, *y.VMType)
}
}

if y.MountType != nil {
if err := validateMountType(y); err != nil {
return err
}
}

return nil
}

func validateMountType(y *LimaYAML) error {
validMountTypes := map[string]bool{
REVSSHFS: true,
NINEP: true,
VIRTIOFS: true,
}

if !validMountTypes[*y.MountType] {
return fmt.Errorf("field `mountType` %s, valid options are: %s, %s, %s", *y.MountType, REVSSHFS, NINEP, VIRTIOFS)
}

if *y.MountType == VIRTIOFS && runtime.GOOS == "darwin" {
if y.VMType == nil || *y.VMType != VZ {
return fmt.Errorf("field `mountType` requires vmType 'vz' on macOS (darwin); got %s", *y.VMType)
}
}

return nil
}

Expand Down Expand Up @@ -432,6 +479,9 @@ func validateNetwork(y *LimaYAML) error {
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))
}
}
if nw.VZNAT != nil && *nw.VZNAT && *y.VMType != VZ {
return fmt.Errorf("field `%s.vzNAT` needs vmType set to vz; got %s", field, *y.VMType)
}
// FillDefault() will make sure that nw.Interface is not the empty string
if len(nw.Interface) >= 16 {
return fmt.Errorf("field `%s.interface` must be less than 16 bytes, but is %d bytes: %q", field, len(nw.Interface), nw.Interface)
Expand Down
121 changes: 121 additions & 0 deletions pkg/limayaml/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,124 @@ func TestValidateParamIsUsed(t *testing.T) {
assert.Error(t, err, "field `param` key \"rootFul\" is not used in any provision, probe, copyToHost, or portForward")
}
}

func TestValidateRosetta(t *testing.T) {
images := `images: [{"location": "/"}]`

rosettaNilData := `
vmType: "qemu"
`
y, err := Load([]byte(rosettaNilData+"\n"+images), "lima.yaml")
assert.NilError(t, err)

err = Validate(y, false)
assert.NilError(t, err)

invalidRosetta := `
rosetta:
enabled: true
vmType: "qemu"
`
y, err = Load([]byte(invalidRosetta+"\n"+images), "lima.yaml")
assert.NilError(t, err)

err = Validate(y, false)
assert.Error(t, err, "field `rosetta.enabled` can only be enabled for VMType \"vz\"; got \"qemu\"")

validRosetta := `
rosetta:
enabled: true
vmType: "vz"
`
y, err = Load([]byte(validRosetta+"\n"+images), "lima.yaml")
assert.NilError(t, err)

err = Validate(y, false)
assert.NilError(t, err)

rosettaDisabled := `
rosetta:
enabled: false
vmType: "qemu"
`
y, err = Load([]byte(rosettaDisabled+"\n"+images), "lima.yaml")
assert.NilError(t, err)

err = Validate(y, false)
assert.NilError(t, err)
}

func TestValidateNestedVirtualization(t *testing.T) {
images := `images: [{"location": "/"}]`

validYAML := `
nestedVirtualization: true
vmType: vz
` + images

y, err := Load([]byte(validYAML), "lima.yaml")
assert.NilError(t, err)

err = Validate(y, false)
assert.NilError(t, err)

invalidYAML := `
nestedVirtualization: true
vmType: qemu
` + images

y, err = Load([]byte(invalidYAML), "lima.yaml")
assert.NilError(t, err)

err = Validate(y, false)
assert.Error(t, err, "field `nestedVirtualization` can only be enabled for VMType \"vz\"; got \"qemu\"")
}

func TestValidateMountTypeOS(t *testing.T) {
images := `images: [{"location": "/"}]`

nilMountConf := ``
y, err := Load([]byte(nilMountConf+"\n"+images), "lima.yaml")
assert.NilError(t, err)

err = Validate(y, false)
assert.NilError(t, err)

inValidMountTypeLinux := `
mountType: "rMountType"
`
y, err = Load([]byte(inValidMountTypeLinux+"\n"+images), "lima.yaml")
assert.NilError(t, err)

err = Validate(y, true)
assert.Error(t, err, "field `mountType` must be \"reverse-sshfs\" or \"9p\" or \"virtiofs\", or \"wsl2\", got \"rMountType\"")

validMountTypeLinux := `
mountType: "virtiofs"
`
y, err = Load([]byte(validMountTypeLinux+"\n"+images), "lima.yaml")
assert.NilError(t, err)

err = Validate(y, true)
assert.Error(t, err, "field `mountType` requires vmType 'vz' on macOS (darwin); got qemu")

validMountTypeMac := `
mountType: "virtiofs"
vmType: "vz"
`
y, err = Load([]byte(validMountTypeMac+"\n"+images), "lima.yaml")
assert.NilError(t, err)

err = Validate(y, false)
assert.NilError(t, err)

invalidMountTypeMac := `
mountType: "virtiofs"
vmType: "qemu"
`
y, err = Load([]byte(invalidMountTypeMac+"\n"+images), "lima.yaml")
assert.NilError(t, err)

err = Validate(y, false)
assert.Error(t, err, "field `mountType` requires vmType 'vz' on macOS (darwin); got qemu")
}

0 comments on commit 5eff66a

Please sign in to comment.