Skip to content

Commit

Permalink
Schema 1.1 Validation (suse-edge#540)
Browse files Browse the repository at this point in the history
* 1.1 validation

* add comment
  • Loading branch information
dbw7 authored Aug 26, 2024
1 parent 28eb542 commit a80991f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/image/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions pkg/image/validation/version.go
Original file line number Diff line number Diff line change
@@ -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
}
59 changes: 59 additions & 0 deletions pkg/image/validation/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit a80991f

Please sign in to comment.