From a80991f6642f56b7eed0ee0b7ff8ca07fe652b42 Mon Sep 17 00:00:00 2001 From: dbw7 Date: Mon, 26 Aug 2024 15:29:00 -0400 Subject: [PATCH] Schema 1.1 Validation (#540) * 1.1 validation * add comment --- pkg/image/validation/validation.go | 1 + pkg/image/validation/version.go | 31 +++++++++++++++ pkg/image/validation/version_test.go | 59 ++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 pkg/image/validation/version.go create mode 100644 pkg/image/validation/version_test.go diff --git a/pkg/image/validation/validation.go b/pkg/image/validation/validation.go index b9800d73..eddf12b2 100644 --- a/pkg/image/validation/validation.go +++ b/pkg/image/validation/validation.go @@ -15,6 +15,7 @@ func ValidateDefinition(ctx *image.Context) map[string][]FailedValidation { failures := map[string][]FailedValidation{} validations := map[string]validateComponent{ + versionComponent: validateVersion, imageComponent: validateImage, osComponent: validateOperatingSystem, registryComponent: validateEmbeddedArtifactRegistry, diff --git a/pkg/image/validation/version.go b/pkg/image/validation/version.go new file mode 100644 index 00000000..eb69bb86 --- /dev/null +++ b/pkg/image/validation/version.go @@ -0,0 +1,31 @@ +package validation + +import ( + "github.com/suse-edge/edge-image-builder/pkg/image" +) + +const ( + versionComponent = "Version" +) + +// Note: This method of validating the EIB version in the image definition is only a temporary implementation +// until a more robust solution can be found. +func validateVersion(ctx *image.Context) []FailedValidation { + var failures []FailedValidation + definition := *ctx.ImageDefinition + + var apiVersionsDefined bool + for i := range definition.Kubernetes.Helm.Charts { + if len(definition.Kubernetes.Helm.Charts[i].APIVersions) != 0 { + apiVersionsDefined = true + } + } + + if definition.APIVersion == "1.0" && apiVersionsDefined { + failures = append(failures, FailedValidation{ + UserMessage: "Helm chart APIVersions field is not supported in EIB version 1.0, must use EIB version 1.1", + }) + } + + return failures +} diff --git a/pkg/image/validation/version_test.go b/pkg/image/validation/version_test.go new file mode 100644 index 00000000..4d927392 --- /dev/null +++ b/pkg/image/validation/version_test.go @@ -0,0 +1,59 @@ +package validation + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/suse-edge/edge-image-builder/pkg/image" +) + +func TestValidateVersion(t *testing.T) { + tests := map[string]struct { + ImageDefinition image.Definition + ExpectedFailedMessages []string + }{ + `valid version with Helm APIVersions`: { + ImageDefinition: image.Definition{ + APIVersion: "1.1", + Kubernetes: image.Kubernetes{Helm: image.Helm{Charts: []image.HelmChart{ + { + APIVersions: []string{"1.30.3+k3s1"}, + }, + }}}, + }, + }, + `invalid version with Helm APIVersions`: { + ImageDefinition: image.Definition{ + APIVersion: "1.0", + Kubernetes: image.Kubernetes{Helm: image.Helm{Charts: []image.HelmChart{ + { + APIVersions: []string{"1.30.3+k3s1"}, + }, + }}}, + }, + ExpectedFailedMessages: []string{ + "Helm chart APIVersions field is not supported in EIB version 1.0, must use EIB version 1.1", + }, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + imageDef := test.ImageDefinition + ctx := image.Context{ + ImageDefinition: &imageDef, + } + failedValidations := validateVersion(&ctx) + assert.Len(t, failedValidations, len(test.ExpectedFailedMessages)) + + var foundMessages []string + for _, foundValidation := range failedValidations { + foundMessages = append(foundMessages, foundValidation.UserMessage) + } + + for _, expectedMessage := range test.ExpectedFailedMessages { + assert.Contains(t, foundMessages, expectedMessage) + } + }) + } +}