Skip to content

Commit

Permalink
change adds validation for AdditionalDisks
Browse files Browse the repository at this point in the history
Signed-off-by: olalekan odukoya <[email protected]>
  • Loading branch information
olamilekan000 committed Jan 14, 2025
1 parent c08b326 commit fba79bf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"unicode"

"github.com/containerd/containerd/identifiers"
"github.com/coreos/go-semver/semver"
"github.com/docker/go-units"
"github.com/lima-vm/lima/pkg/localpathutil"
Expand Down Expand Up @@ -136,6 +137,12 @@ func Validate(y *LimaYAML, warn bool) error {
return fmt.Errorf("field `memory` has an invalid value: %w", err)
}

for i, disk := range y.AdditionalDisks {
if err := identifiers.Validate(disk.Name); err != nil {
return fmt.Errorf("field `additionalDisks[%d].name is invalid`: %w", i, err)
}
}

for i, f := range y.Mounts {
if !filepath.IsAbs(f.Location) && !strings.HasPrefix(f.Location, "~") {
return fmt.Errorf("field `mounts[%d].location` must be an absolute path, got %q",
Expand Down
25 changes: 25 additions & 0 deletions pkg/limayaml/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ func TestValidateProbes(t *testing.T) {
assert.Error(t, err, "field `probe[0].script` must start with a '#!' line")
}

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

validDisks := `
additionalDisks:
- name: "disk1"
- name: "disk2"
`
y, err := Load([]byte(validDisks+"\n"+images), "lima.yaml")
assert.NilError(t, err)

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

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

err = Validate(y, false)
assert.Error(t, err, "field `additionalDisks[0].name is invalid`: identifier must not be empty: invalid argument")
}

func TestValidateParamName(t *testing.T) {
images := `images: [{"location": "/"}]`
validProvision := `provision: [{"script": "echo $PARAM_name $PARAM_NAME $PARAM_Name_123"}]`
Expand Down

0 comments on commit fba79bf

Please sign in to comment.