Skip to content

Commit

Permalink
feat: error when building a package with zero components (#3403)
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 authored Feb 3, 2025
1 parent b489363 commit 33d8a2a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/internal/packager2/layout/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,10 @@ func TestCreateReproducibleTarballFromDir(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "c09d17f612f241cdf549e5fb97c9e063a8ad18ae7a9f3af066332ed6b38556ad", shaSum)
}

func TestLoadPackageErrorWithoutCompatibleFlavor(t *testing.T) {
t.Parallel()
lint.ZarfSchema = testutil.LoadSchema(t, "../../../../zarf.schema.json")
_, err := LoadPackage(context.Background(), filepath.Join("testdata", "package-with-flavors"), "non-existent-flavor", map[string]string{})
require.EqualError(t, err, fmt.Sprintf("package validation failed: %s", lint.PkgValidateErrNoComponents))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
kind: ZarfPackageConfig
metadata:
name: test
components:
- name: test-flavor
only:
flavor: pistachio

- name: test-flavor
only:
flavor: cashew
4 changes: 4 additions & 0 deletions src/pkg/lint/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ const (
PkgValidateErrManifestFileOrKustomize = "manifest %q must have at least one file or kustomization"
PkgValidateErrManifestNameLength = "manifest %q exceed the maximum length of %d characters"
PkgValidateErrVariable = "invalid package variable: %w"
PkgValidateErrNoComponents = "package does not contain any compatible components"
)

// ValidatePackage runs all validation checks on the package.
func ValidatePackage(pkg v1alpha1.ZarfPackage) error {
var err error
if len(pkg.Components) == 0 {
err = errors.Join(err, errors.New(PkgValidateErrNoComponents))
}
if pkg.Kind == v1alpha1.ZarfInitConfig && pkg.Metadata.YOLO {
err = errors.Join(err, errors.New(PkgValidateErrInitNoYOLO))
}
Expand Down
10 changes: 10 additions & 0 deletions src/pkg/lint/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ func TestZarfPackageValidate(t *testing.T) {
},
expectedErrs: nil,
},
{
name: "no components",
pkg: v1alpha1.ZarfPackage{
Kind: v1alpha1.ZarfPackageConfig,
Metadata: v1alpha1.ZarfMetadata{
Name: "valid-package",
},
},
expectedErrs: []string{PkgValidateErrNoComponents},
},
{
name: "invalid package",
pkg: v1alpha1.ZarfPackage{
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/creator/creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestLoadPackageDefinition(t *testing.T) {
{
name: "invalid package definition",
testDir: "invalid",
expectedErr: "linting error found 1 instance(s)",
expectedErr: "package validation failed: package does not contain any compatible components",
creator: NewPackageCreator(types.ZarfCreateOptions{}, ""),
},
{
Expand All @@ -47,7 +47,7 @@ func TestLoadPackageDefinition(t *testing.T) {
{
name: "invalid package definition",
testDir: "invalid",
expectedErr: "linting error found 1 instance(s)",
expectedErr: "package validation failed: package does not contain any compatible components",
creator: NewSkeletonCreator(types.ZarfCreateOptions{}, types.ZarfPublishOptions{}),
},
}
Expand Down

0 comments on commit 33d8a2a

Please sign in to comment.