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

test: clean up tests for composer #2532

Merged
merged 8 commits into from
May 23, 2024
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
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
Loading