From 33d8a2a2dc84b89913cc74d45157a04f6230c5ec Mon Sep 17 00:00:00 2001 From: Austin Abro <37223396+AustinAbro321@users.noreply.github.com> Date: Mon, 3 Feb 2025 16:18:10 -0500 Subject: [PATCH] feat: error when building a package with zero components (#3403) Signed-off-by: Austin Abro --- src/internal/packager2/layout/create_test.go | 7 +++++++ .../layout/testdata/package-with-flavors/zarf.yaml | 11 +++++++++++ src/pkg/lint/validate.go | 4 ++++ src/pkg/lint/validate_test.go | 10 ++++++++++ src/pkg/packager/creator/creator_test.go | 4 ++-- 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/internal/packager2/layout/testdata/package-with-flavors/zarf.yaml diff --git a/src/internal/packager2/layout/create_test.go b/src/internal/packager2/layout/create_test.go index 58205b9344..7a1b9a7d3c 100644 --- a/src/internal/packager2/layout/create_test.go +++ b/src/internal/packager2/layout/create_test.go @@ -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)) +} diff --git a/src/internal/packager2/layout/testdata/package-with-flavors/zarf.yaml b/src/internal/packager2/layout/testdata/package-with-flavors/zarf.yaml new file mode 100644 index 0000000000..b8add2de34 --- /dev/null +++ b/src/internal/packager2/layout/testdata/package-with-flavors/zarf.yaml @@ -0,0 +1,11 @@ +kind: ZarfPackageConfig +metadata: + name: test +components: + - name: test-flavor + only: + flavor: pistachio + + - name: test-flavor + only: + flavor: cashew diff --git a/src/pkg/lint/validate.go b/src/pkg/lint/validate.go index 0083c8a6b7..80b35b3ad7 100644 --- a/src/pkg/lint/validate.go +++ b/src/pkg/lint/validate.go @@ -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)) } diff --git a/src/pkg/lint/validate_test.go b/src/pkg/lint/validate_test.go index 4e0abeda96..4b5612f189 100644 --- a/src/pkg/lint/validate_test.go +++ b/src/pkg/lint/validate_test.go @@ -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{ diff --git a/src/pkg/packager/creator/creator_test.go b/src/pkg/packager/creator/creator_test.go index 6d87d09aac..8f23a80eaa 100644 --- a/src/pkg/packager/creator/creator_test.go +++ b/src/pkg/packager/creator/creator_test.go @@ -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{}, ""), }, { @@ -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{}), }, }