From fba79bffff4a98868d16382a2ab8a523a7cdbbc1 Mon Sep 17 00:00:00 2001 From: olalekan odukoya Date: Tue, 14 Jan 2025 11:40:10 +0100 Subject: [PATCH] change adds validation for AdditionalDisks Signed-off-by: olalekan odukoya --- pkg/limayaml/validate.go | 7 +++++++ pkg/limayaml/validate_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pkg/limayaml/validate.go b/pkg/limayaml/validate.go index 72f2799ee0a..2bc8ad4b648 100644 --- a/pkg/limayaml/validate.go +++ b/pkg/limayaml/validate.go @@ -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" @@ -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", diff --git a/pkg/limayaml/validate_test.go b/pkg/limayaml/validate_test.go index 51254757609..4895531ed06 100644 --- a/pkg/limayaml/validate_test.go +++ b/pkg/limayaml/validate_test.go @@ -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"}]`