Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: error when building a package with zero components #3403

Merged
merged 8 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading