forked from suse-edge/edge-image-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Schema 1.1 Validation (suse-edge#540)
* 1.1 validation * add comment
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |