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

chore: add unit tests for creator.LoadPackageDefinition #2531

Merged
merged 9 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 0 additions & 5 deletions src/pkg/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ func (p *Packager) Create() (err error) {
return err
}

// Perform early package validation.
if err := p.cfg.Pkg.Validate(); err != nil {
return fmt.Errorf("unable to validate package: %w", err)
}

if !p.confirmAction(config.ZarfCreateStage) {
return fmt.Errorf("package creation canceled")
}
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/creator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Creator is an interface for creating Zarf packages.
type Creator interface {
LoadPackageDefinition(dst *layout.PackagePaths) (pkg types.ZarfPackage, warnings []string, err error)
LoadPackageDefinition(src *layout.PackagePaths) (pkg types.ZarfPackage, warnings []string, err error)
Assemble(dst *layout.PackagePaths, components []types.ZarfComponent, arch string) error
Output(dst *layout.PackagePaths, pkg *types.ZarfPackage) error
}
11 changes: 8 additions & 3 deletions src/pkg/packager/creator/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func NewPackageCreator(createOpts types.ZarfCreateOptions, cwd string) *PackageC
}

// LoadPackageDefinition loads and configures a zarf.yaml file during package create.
func (pc *PackageCreator) LoadPackageDefinition(dst *layout.PackagePaths) (pkg types.ZarfPackage, warnings []string, err error) {
pkg, warnings, err = dst.ReadZarfYAML()
func (pc *PackageCreator) LoadPackageDefinition(src *layout.PackagePaths) (pkg types.ZarfPackage, warnings []string, err error) {
pkg, warnings, err = src.ReadZarfYAML()
if err != nil {
return types.ZarfPackage{}, nil, err
}
Expand All @@ -86,7 +86,7 @@ func (pc *PackageCreator) LoadPackageDefinition(dst *layout.PackagePaths) (pkg t
warnings = append(warnings, templateWarnings...)

// After templates are filled process any create extensions
pkg.Components, err = pc.processExtensions(pkg.Components, dst, pkg.Metadata.YOLO)
pkg.Components, err = pc.processExtensions(pkg.Components, src, pkg.Metadata.YOLO)
if err != nil {
return types.ZarfPackage{}, nil, err
}
Expand Down Expand Up @@ -119,6 +119,11 @@ func (pc *PackageCreator) LoadPackageDefinition(dst *layout.PackagePaths) (pkg t
}
}

// Perform early package validation.
if err := pkg.Validate(); err != nil {
return types.ZarfPackage{}, nil, fmt.Errorf("unable to validate package: %w", err)
}

return pkg, warnings, nil
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
43 changes: 43 additions & 0 deletions src/pkg/packager/creator/normal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"testing"

"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/types"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -56,3 +58,44 @@ func TestDifferentialPackagePathSetCorrectly(t *testing.T) {
})
}
}

func TestLoadPackageDefinition(t *testing.T) {
t.Parallel()

tests := []struct {
name string
testDir string
expectErr bool
}{
{
name: "valid package definition",
testDir: "valid",
expectErr: false,
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved
},
{
name: "invalid package definition",
testDir: "invalid",
expectErr: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

src := layout.New(filepath.Join("testdata", tt.testDir))
pc := NewPackageCreator(types.ZarfCreateOptions{}, "")
pkg, _, err := pc.LoadPackageDefinition(src)

switch {
case tt.expectErr:
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved
require.Error(t, err)
require.Empty(t, pkg)
default:
require.NoError(t, err)
require.NotEmpty(t, pkg)
}
})
}
}
11 changes: 8 additions & 3 deletions src/pkg/packager/creator/skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func NewSkeletonCreator(createOpts types.ZarfCreateOptions, publishOpts types.Za
}

// LoadPackageDefinition loads and configure a zarf.yaml file when creating and publishing a skeleton package.
func (sc *SkeletonCreator) LoadPackageDefinition(dst *layout.PackagePaths) (pkg types.ZarfPackage, warnings []string, err error) {
pkg, warnings, err = dst.ReadZarfYAML()
func (sc *SkeletonCreator) LoadPackageDefinition(src *layout.PackagePaths) (pkg types.ZarfPackage, warnings []string, err error) {
pkg, warnings, err = src.ReadZarfYAML()
if err != nil {
return types.ZarfPackage{}, nil, err
}
Expand All @@ -60,11 +60,16 @@ func (sc *SkeletonCreator) LoadPackageDefinition(dst *layout.PackagePaths) (pkg

warnings = append(warnings, composeWarnings...)

pkg.Components, err = sc.processExtensions(pkg.Components, dst)
pkg.Components, err = sc.processExtensions(pkg.Components, src)
if err != nil {
return types.ZarfPackage{}, nil, err
}

// Perform early package validation.
if err := pkg.Validate(); err != nil {
return types.ZarfPackage{}, nil, fmt.Errorf("unable to validate package: %w", err)
}

for _, warning := range warnings {
message.Warn(warning)
}
Expand Down
55 changes: 55 additions & 0 deletions src/pkg/packager/creator/skeleton_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package creator contains functions for creating Zarf packages.
package creator

import (
"path/filepath"
"testing"

"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/types"
"github.com/stretchr/testify/require"
)

func TestSkeletonLoadPackageDefinition(t *testing.T) {
t.Parallel()

tests := []struct {
name string
testDir string
expectErr bool
}{
{
name: "valid package definition",
testDir: "valid",
expectErr: false,
},
{
name: "invalid package definition",
testDir: "invalid",
expectErr: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

src := layout.New(filepath.Join("testdata", tt.testDir))
sc := NewSkeletonCreator(types.ZarfCreateOptions{}, types.ZarfPublishOptions{})
pkg, _, err := sc.LoadPackageDefinition(src)

switch {
case tt.expectErr:
require.Error(t, err)
require.Empty(t, pkg)
default:
require.NoError(t, err)
require.NotEmpty(t, pkg)
}
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved
})
}
}
5 changes: 5 additions & 0 deletions src/pkg/packager/creator/testdata/invalid/zarf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: ZarfPackageConfig
metadata:
name: minimal-invalid
# must have at least 1 component
description: Minimal invalid package
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions src/pkg/packager/creator/testdata/valid/zarf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ZarfPackageConfig
metadata:
name: minimal-valid
description: Minimal valid package
components:
- name: component1
5 changes: 5 additions & 0 deletions src/types/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package types

import (
"errors"
"fmt"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -47,6 +48,10 @@ func (pkg ZarfPackage) Validate() error {
return fmt.Errorf(lang.PkgValidateErrPkgName, pkg.Metadata.Name)
}

if pkg.Components == nil {
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved
return errors.New("package must have at least 1 component")
}

for _, variable := range pkg.Variables {
if err := variable.Validate(); err != nil {
return fmt.Errorf(lang.PkgValidateErrVariable, err)
Expand Down
Loading