Skip to content

Commit

Permalink
change make logs quiet by default and adds a verbose flag to enable v…
Browse files Browse the repository at this point in the history
…erbose log output

Signed-off-by: olalekan odukoya <[email protected]>
  • Loading branch information
olamilekan000 committed Jan 19, 2025
1 parent 597ffed commit f458f0e
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,44 @@ func Validate(y *LimaYAML, warn bool) error {
}
}

if y.Rosetta.Enabled != nil && *y.Rosetta.Enabled {
if *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 +470,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
107 changes: 107 additions & 0 deletions pkg/limayaml/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"runtime"
"testing"

"github.com/sirupsen/logrus"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -186,3 +187,109 @@ 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": "/"}]`

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": "/"}]`

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

err = Validate(y, true)
logrus.Info("inValidMountTypeLinux: from within test ", inValidMountTypeLinux)
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 f458f0e

Please sign in to comment.