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
  • Loading branch information
AustinAbro321 committed Jun 5, 2024
1 parent d832c60 commit c1d1d44
Show file tree
Hide file tree
Showing 9 changed files with 611 additions and 100 deletions.
12 changes: 6 additions & 6 deletions src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ const (
PkgValidateErrActionClusterNetwork = "a single wait action must contain only one of cluster or network"
PkgValidateErrChart = "invalid chart definition: %w"
PkgValidateErrChartName = "chart %q exceed the maximum length of %d characters"
PkgValidateErrChartNameMissing = "chart %q must include a name"
PkgValidateErrChartNameMissing = "chart must include a name"
PkgValidateErrChartNameNotUnique = "chart name %q is not unique"
PkgValidateErrChartNamespaceMissing = "chart %q must include a namespace"
PkgValidateErrChartURLOrPath = "chart %q must have either a url or localPath"
Expand All @@ -715,17 +715,17 @@ const (
PkgValidateErrManifest = "invalid manifest definition: %w"
PkgValidateErrManifestFileOrKustomize = "manifest %q must have at least one file or kustomization"
PkgValidateErrManifestNameLength = "manifest %q exceed the maximum length of %d characters"
PkgValidateErrManifestNameMissing = "manifest %q must include a name"
PkgValidateErrManifestNameMissing = "manifest must include a name"
PkgValidateErrManifestNameNotUnique = "manifest name %q is not unique"
PkgValidateErrName = "invalid package name: %w"
PkgValidateErrPkgConstantName = "constant name %q must be all uppercase and contain no special characters except _"
PkgValidateErrPkgConstantPattern = "provided value for constant %q does not match pattern %q"
PkgValidateErrPkgName = "package name %q must be all lowercase and contain no special characters except '-' and cannot start with a '-'"
PkgValidateErrVariable = "invalid package variable: %w"
PkgValidateErrYOLONoArch = "cluster architecture not allowed"
PkgValidateErrYOLONoDistro = "cluster distros not allowed"
PkgValidateErrYOLONoGit = "git repos not allowed"
PkgValidateErrYOLONoOCI = "OCI images not allowed"
PkgValidateErrYOLONoArch = "cluster architecture not allowed in YOLO"
PkgValidateErrYOLONoDistro = "cluster distros not allowed in YOLO"
PkgValidateErrYOLONoGit = "git repos not allowed in YOLO"
PkgValidateErrYOLONoOCI = "OCI images not allowed in YOLO"
)

// Collection of reusable error messages.
Expand Down
10 changes: 9 additions & 1 deletion src/pkg/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,15 @@ func Paragraph(format string, a ...any) string {

// Paragraphn formats text into an n column paragraph
func Paragraphn(n int, format string, a ...any) string {
return pterm.DefaultParagraph.WithMaxWidth(n).Sprintf(format, a...)
// Split the text to keep pterm formatting but add newlines
lines := strings.Split(fmt.Sprintf(format, a...), "\n")

formattedLines := make([]string, len(lines))
for i, line := range lines {
formattedLines[i] = pterm.DefaultParagraph.WithMaxWidth(n).Sprintf(line)
}

return strings.Join(formattedLines, "\n")
}

// PrintDiff prints the differences between a and b with a as original and b as new
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/composer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func NewImportChain(head types.ZarfComponent, index int, originalPackageName, ar
}

// TODO: stuff like this should also happen in linting
if err := node.ZarfComponent.ValidateImportDefinition(); err != nil {
if err := node.ZarfComponent.Validate(); err != nil {
return ic, err
}

Expand Down
3 changes: 1 addition & 2 deletions src/pkg/packager/sources/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ func TestPackageSource(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

// TODO once our messaging is thread safe, re-parallelize this test
opts := &types.ZarfPackageOptions{
PackageSource: tt.src,
Shasum: tt.shasum,
Expand Down
2 changes: 1 addition & 1 deletion src/types/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// ZarfComponent is the primary functional grouping of assets to deploy by Zarf.
type ZarfComponent struct {
// Name is the unique identifier for this component
Name string `json:"name" jsonschema:"description=The name of the component,pattern=^[a-z0-9\\-]*[a-z0-9]$"`
Name string `json:"name" jsonschema:"description=The name of the component,pattern=^[a-z0-9][a-z0-9\\-]*$"`

// Description is a message given to a user when deciding to enable this component or not
Description string `json:"description,omitempty" jsonschema:"description=Message to include during package deploy describing the purpose of this component"`
Expand Down
2 changes: 1 addition & 1 deletion src/types/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (pkg ZarfPackage) IsSBOMAble() bool {

// ZarfMetadata lists information about the current ZarfPackage.
type ZarfMetadata struct {
Name string `json:"name" jsonschema:"description=Name to identify this Zarf package,pattern=^[a-z0-9\\-]*[a-z0-9]$"`
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)"`
URL string `json:"url,omitempty" jsonschema:"description=Link to package information when online"`
Expand Down
Loading

0 comments on commit c1d1d44

Please sign in to comment.