Skip to content

Commit

Permalink
test: validate package (#2569)
Browse files Browse the repository at this point in the history
## Description

This PR adds tests to validate package

## Checklist before merging

- [ ] Test, docs, adr added or updated as needed
- [ ] [Contributor Guide
Steps](https://github.com/defenseunicorns/zarf/blob/main/.github/CONTRIBUTING.md#developer-workflow)
followed

Signed-off-by: Austin Abro <AustinAbro321@gmail.com>
  • Loading branch information
AustinAbro321 committed Jul 23, 2024
1 parent a3de04a commit e706a86
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/types/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ func (pkg ZarfPackage) IsSBOMAble() bool {

// ZarfMetadata lists information about the current ZarfPackage.
type ZarfMetadata struct {
// The Name regex permits lowercase letters, numbers, and hyphens not at the start
// https://regex101.com/r/FLdG9G/2
Name string `json:"name" jsonschema:"description=Name to identify this Zarf package,pattern=^[a-z0-9][a-z0-9\\-]*$"`
Description string `json:"description,omitempty" jsonschema:"description=Additional information about this package"`
Version string `json:"version,omitempty" jsonschema:"description=Generic string set by a package author to track the package version (Note: ZarfInitConfigs will always be versioned to the CLIVersion they were created with)"`
Expand Down
38 changes: 38 additions & 0 deletions src/types/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ func (pkg ZarfPackage) Validate() error {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrInitNoYOLO))
}

if !IsLowercaseNumberHyphenNoStartHyphen(pkg.Metadata.Name) {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrPkgName, pkg.Metadata.Name))
}

if len(pkg.Components) == 0 {
err = errors.Join(err, fmt.Errorf("package must have at least 1 component"))
}

for _, variable := range pkg.Variables {
if varErr := variable.Validate(); varErr != nil {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrVariable, varErr))
}
}

for _, constant := range pkg.Constants {
if varErr := constant.Validate(); varErr != nil {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrConstant, varErr))
Expand Down Expand Up @@ -80,6 +94,14 @@ func (pkg ZarfPackage) Validate() error {
}
uniqueComponentNames[component.Name] = true

if !IsLowercaseNumberHyphenNoStartHyphen(component.Name) {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrComponentName, component.Name))
}

if !slices.Contains(supportedOS, component.Only.LocalOS) {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrComponentLocalOS, component.Name, component.Only.LocalOS, supportedOS))
}

if component.IsRequired() {
if component.Default {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrComponentReqDefault, component.Name))
Expand Down Expand Up @@ -259,10 +281,18 @@ func (action ZarfComponentAction) Validate() error {
func (chart ZarfChart) Validate() error {
var err error

if chart.Name == "" {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrChartNameMissing))
}

if len(chart.Name) > ZarfMaxChartNameLength {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrChartName, chart.Name, ZarfMaxChartNameLength))
}

if chart.Namespace == "" {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrChartNamespaceMissing, chart.Name))
}

// Must have a url or localPath (and not both)
if chart.URL != "" && chart.LocalPath != "" {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrChartURLOrPath, chart.Name))
Expand All @@ -272,13 +302,21 @@ func (chart ZarfChart) Validate() error {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrChartURLOrPath, chart.Name))
}

if chart.Version == "" {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrChartVersion, chart.Name))
}

return err
}

// Validate runs all validation checks on a manifest.
func (manifest ZarfManifest) Validate() error {
var err error

if manifest.Name == "" {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrManifestNameMissing))
}

if len(manifest.Name) > ZarfMaxChartNameLength {
err = errors.Join(err, fmt.Errorf(lang.PkgValidateErrManifestNameLength, manifest.Name, ZarfMaxChartNameLength))
}
Expand Down
51 changes: 45 additions & 6 deletions src/types/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,30 @@ func TestZarfPackageValidate(t *testing.T) {
},
expectedErrs: nil,
},
{
name: "no components",
pkg: ZarfPackage{
Kind: ZarfPackageConfig,
Metadata: ZarfMetadata{
Name: "empty-components",
},
Components: []ZarfComponent{},
},
expectedErrs: []string{"package must have at least 1 component"},
},
{
name: "invalid package",
pkg: ZarfPackage{
Kind: ZarfPackageConfig,
Metadata: ZarfMetadata{
Name: "invalid-package",
Name: "-invalid-package",
},
Components: []ZarfComponent{
{
Name: "invalid",
Name: "-invalid",
Only: ZarfComponentOnlyTarget{
LocalOS: "unsupportedOS",
},
Required: helpers.BoolPtr(true),
Default: true,
Charts: []ZarfChart{
Expand Down Expand Up @@ -81,7 +95,15 @@ func TestZarfPackageValidate(t *testing.T) {
Name: "duplicate",
},
},
Variables: []variables.InteractiveVariable{
{
Variable: variables.Variable{Name: "not_uppercase"},
},
},
Constants: []variables.Constant{
{
Name: "not_uppercase",
},
{
Name: "BAD",
Pattern: "^good_val$",
Expand All @@ -90,8 +112,13 @@ func TestZarfPackageValidate(t *testing.T) {
},
},
expectedErrs: []string{
fmt.Sprintf(lang.PkgValidateErrPkgName, "-invalid-package"),
fmt.Errorf(lang.PkgValidateErrVariable, fmt.Errorf(lang.PkgValidateMustBeUppercase, "not_uppercase")).Error(),
fmt.Errorf(lang.PkgValidateErrConstant, fmt.Errorf(lang.PkgValidateErrPkgConstantName, "not_uppercase")).Error(),
fmt.Errorf(lang.PkgValidateErrConstant, fmt.Errorf(lang.PkgValidateErrPkgConstantPattern, "BAD", "^good_val$")).Error(),
fmt.Sprintf(lang.PkgValidateErrComponentReqDefault, "invalid"),
fmt.Sprintf(lang.PkgValidateErrComponentName, "-invalid"),
fmt.Sprintf(lang.PkgValidateErrComponentLocalOS, "-invalid", "unsupportedOS", supportedOS),
fmt.Sprintf(lang.PkgValidateErrComponentReqDefault, "-invalid"),
fmt.Sprintf(lang.PkgValidateErrChartNameNotUnique, "chart1"),
fmt.Sprintf(lang.PkgValidateErrManifestNameNotUnique, "manifest1"),
fmt.Sprintf(lang.PkgValidateErrComponentReqGrouped, "required-in-group"),
Expand Down Expand Up @@ -160,6 +187,11 @@ func TestValidateManifest(t *testing.T) {
manifest: ZarfManifest{Name: "valid", Files: []string{"a-file"}},
expectedErrs: nil,
},
{
name: "empty name",
manifest: ZarfManifest{Name: "", Files: []string{"a-file"}},
expectedErrs: []string{lang.PkgValidateErrManifestNameMissing},
},
{
name: "long name",
manifest: ZarfManifest{Name: longName, Files: []string{"a-file"}},
Expand Down Expand Up @@ -196,12 +228,17 @@ func TestValidateChart(t *testing.T) {
}{
{
name: "valid",
chart: ZarfChart{Name: "chart1", URL: "http://whatever"},
chart: ZarfChart{Name: "chart1", Namespace: "whatever", URL: "http://whatever", Version: "v1.0.0"},
expectedErrs: nil,
},
{
name: "empty name",
chart: ZarfChart{Name: "", Namespace: "whatever", URL: "http://whatever", Version: "v1.0.0"},
expectedErrs: []string{lang.PkgValidateErrChartNameMissing},
},
{
name: "long name",
chart: ZarfChart{Name: longName, URL: "http://whatever"},
chart: ZarfChart{Name: longName, Namespace: "whatever", URL: "http://whatever", Version: "v1.0.0"},
expectedErrs: []string{
fmt.Sprintf(lang.PkgValidateErrChartName, longName, ZarfMaxChartNameLength),
},
Expand All @@ -210,12 +247,14 @@ func TestValidateChart(t *testing.T) {
name: "no url or local path",
chart: ZarfChart{Name: "invalid"},
expectedErrs: []string{
fmt.Sprintf(lang.PkgValidateErrChartNamespaceMissing, "invalid"),
fmt.Sprintf(lang.PkgValidateErrChartURLOrPath, "invalid"),
fmt.Sprintf(lang.PkgValidateErrChartVersion, "invalid"),
},
},
{
name: "both url and local path",
chart: ZarfChart{Name: "invalid", URL: "http://whatever", LocalPath: "wherever"},
chart: ZarfChart{Name: "invalid", Namespace: "whatever", URL: "http://whatever", LocalPath: "wherever", Version: "v1.0.0"},
expectedErrs: []string{
fmt.Sprintf(lang.PkgValidateErrChartURLOrPath, "invalid"),
},
Expand Down

0 comments on commit e706a86

Please sign in to comment.