Skip to content

Commit

Permalink
test: clean up tests for composer (#2532)
Browse files Browse the repository at this point in the history
## Description

Cleans up the composer tests to the newer testing style. Also marks test
helper functions as helpers.

## Related Issue

Relates to #2512

## Checklist before merging

- [x] Test, docs, adr added or updated as needed
- [x] [Contributor Guide
Steps](https://github.com/defenseunicorns/zarf/blob/main/.github/CONTRIBUTING.md#developer-workflow)
followed

---------

Co-authored-by: schristoff <167717759+schristoff-du@users.noreply.github.com>
Co-authored-by: razzle <razzle@defenseunicorns.com>
Co-authored-by: Lucas Rodriguez <lucas.rodriguez@defenseunicorns.com>
Signed-off-by: Austin Abro <AustinAbro321@gmail.com>
  • Loading branch information
4 people authored and AustinAbro321 committed Jul 23, 2024
1 parent 1d4e8b6 commit 9d6d9f0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 86 deletions.
14 changes: 0 additions & 14 deletions src/pkg/packager/composer/extensions.go

This file was deleted.

3 changes: 2 additions & 1 deletion src/pkg/packager/composer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/zarf/src/extensions/bigbang"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/packager/deprecated"
"github.com/defenseunicorns/zarf/src/pkg/utils"
Expand Down Expand Up @@ -303,7 +304,7 @@ func (ic *ImportChain) Compose() (composed *types.ZarfComponent, err error) {
overrideResources(composed, node.ZarfComponent)
overrideActions(composed, node.ZarfComponent)

composeExtensions(composed, node.ZarfComponent, node.relativeToHead)
bigbang.Compose(composed, node.ZarfComponent, node.relativeToHead)

node = node.prev
}
Expand Down
125 changes: 54 additions & 71 deletions src/pkg/packager/composer/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ import (
func TestNewImportChain(t *testing.T) {
t.Parallel()

type testCase struct {
name string
head types.ZarfComponent
arch string
flavor string
expectedErrorMessage string
}

testCases := []testCase{
tests := []struct {
name string
head types.ZarfComponent
arch string
flavor string
expectedErr string
}{
{
name: "No Architecture",
head: types.ZarfComponent{},
expectedErrorMessage: "architecture must be provided",
name: "No Architecture",
head: types.ZarfComponent{},
expectedErr: "architecture must be provided",
},
{
name: "Circular Import",
Expand All @@ -40,34 +38,25 @@ func TestNewImportChain(t *testing.T) {
Path: ".",
},
},
arch: "amd64",
expectedErrorMessage: "detected circular import chain",
arch: "amd64",
expectedErr: "detected circular import chain",
},
}
testPackageName := "test-package"
for _, testCase := range testCases {
testCase := testCase

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

_, err := NewImportChain(testCase.head, 0, testPackageName, testCase.arch, testCase.flavor)
require.Contains(t, err.Error(), testCase.expectedErrorMessage)
_, err := NewImportChain(tt.head, 0, testPackageName, tt.arch, tt.flavor)
require.ErrorContains(t, err, tt.expectedErr)
})
}
}

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

type testCase struct {
name string
ic *ImportChain
returnError bool
expectedComposed types.ZarfComponent
expectedErrorMessage string
}

firstDirectory := "hello"
secondDirectory := "world"
finalDirectory := filepath.Join(firstDirectory, secondDirectory)
Expand All @@ -76,27 +65,29 @@ func TestCompose(t *testing.T) {
secondDirectoryActionDefault := filepath.Join(firstDirectory, "world-dc")
firstDirectoryActionDefault := "hello-dc"

testCases := []testCase{
tests := []struct {
name string
ic *ImportChain
expectedComposed types.ZarfComponent
}{
{
name: "Single Component",
ic: createChainFromSlice([]types.ZarfComponent{
ic: createChainFromSlice(t, []types.ZarfComponent{
{
Name: "no-import",
},
}),
returnError: false,
expectedComposed: types.ZarfComponent{
Name: "no-import",
},
},
{
name: "Multiple Components",
ic: createChainFromSlice([]types.ZarfComponent{
createDummyComponent("hello", firstDirectory, "hello"),
createDummyComponent("world", secondDirectory, "world"),
createDummyComponent("today", "", "hello"),
ic: createChainFromSlice(t, []types.ZarfComponent{
createDummyComponent(t, "hello", firstDirectory, "hello"),
createDummyComponent(t, "world", secondDirectory, "world"),
createDummyComponent(t, "today", "", "hello"),
}),
returnError: false,
expectedComposed: types.ZarfComponent{
Name: "import-hello",
// Files should always be appended with corrected directories
Expand Down Expand Up @@ -243,35 +234,21 @@ func TestCompose(t *testing.T) {
},
},
}

for _, testCase := range testCases {
testCase := testCase

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

composed, err := testCase.ic.Compose()
if testCase.returnError {
require.Contains(t, err.Error(), testCase.expectedErrorMessage)
} else {
require.EqualValues(t, &testCase.expectedComposed, composed)
}
composed, err := tt.ic.Compose()
require.NoError(t, err)
require.EqualValues(t, &tt.expectedComposed, composed)
})
}
}

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

type testCase struct {
name string
ic *ImportChain
existingVars []variables.InteractiveVariable
existingConsts []variables.Constant
expectedVars []variables.InteractiveVariable
expectedConsts []variables.Constant
}

head := Node{
vars: []variables.InteractiveVariable{
{
Expand Down Expand Up @@ -316,7 +293,14 @@ func TestMerging(t *testing.T) {
tail.prev = &head
testIC := &ImportChain{head: &head, tail: &tail}

testCases := []testCase{
tests := []struct {
name string
ic *ImportChain
existingVars []variables.InteractiveVariable
existingConsts []variables.Constant
expectedVars []variables.InteractiveVariable
expectedConsts []variables.Constant
}{
{
name: "empty-ic",
ic: &ImportChain{},
Expand Down Expand Up @@ -425,41 +409,40 @@ func TestMerging(t *testing.T) {
},
}

for _, testCase := range testCases {
testCase := testCase

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

mergedVars := testCase.ic.MergeVariables(testCase.existingVars)
require.EqualValues(t, testCase.expectedVars, mergedVars)
mergedVars := tt.ic.MergeVariables(tt.existingVars)
require.EqualValues(t, tt.expectedVars, mergedVars)

mergedConsts := testCase.ic.MergeConstants(testCase.existingConsts)
require.EqualValues(t, testCase.expectedConsts, mergedConsts)
mergedConsts := tt.ic.MergeConstants(tt.existingConsts)
require.EqualValues(t, tt.expectedConsts, mergedConsts)
})
}
}

func createChainFromSlice(components []types.ZarfComponent) (ic *ImportChain) {
func createChainFromSlice(t *testing.T, components []types.ZarfComponent) (ic *ImportChain) {
t.Helper()

ic = &ImportChain{}
testPackageName := "test-package"

if len(components) == 0 {
return ic
}

ic.append(components[0], 0, testPackageName, ".", nil, nil)
history := []string{}

for idx := 1; idx < len(components); idx++ {
history = append(history, components[idx-1].Import.Path)
ic.append(components[idx], idx, testPackageName, filepath.Join(history...), nil, nil)
}

return ic
}

func createDummyComponent(name, importDir, subName string) types.ZarfComponent {
func createDummyComponent(t *testing.T, name, importDir, subName string) types.ZarfComponent {
t.Helper()

return types.ZarfComponent{
Name: fmt.Sprintf("import-%s", name),
Import: types.ZarfComponentImport{
Expand Down

0 comments on commit 9d6d9f0

Please sign in to comment.