From 6c2157ac35440bf78f8ab78691bb171cde21fa61 Mon Sep 17 00:00:00 2001 From: Wayne Starr Date: Sat, 4 Nov 2023 16:00:54 -0600 Subject: [PATCH] fix: properly handle variable and constant merging (#2129) ## Description This fixes variable and constant merging within component composability. ## Related Issue Fixes #2125 ## Type of change - [X] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Other (security config, docs update, etc) ## Checklist before merging - [X] Test, docs, adr added or updated as needed - [X] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow) followed --- src/pkg/packager/composer/list.go | 14 +- src/pkg/packager/composer/list_test.go | 180 +++++++++++++++++++++++++ 2 files changed, 188 insertions(+), 6 deletions(-) diff --git a/src/pkg/packager/composer/list.go b/src/pkg/packager/composer/list.go index 8354bb958f..89da2380c0 100644 --- a/src/pkg/packager/composer/list.go +++ b/src/pkg/packager/composer/list.go @@ -256,13 +256,14 @@ func (ic *ImportChain) MergeVariables(existing []types.ZarfPackageVariable) (mer return v1.Name == v2.Name } - merged = helpers.MergeSlices(existing, merged, exists) - node := ic.head + node := ic.tail for node != nil { // merge the vars merged = helpers.MergeSlices(node.vars, merged, exists) - node = node.next + node = node.prev } + merged = helpers.MergeSlices(existing, merged, exists) + return merged } @@ -272,13 +273,14 @@ func (ic *ImportChain) MergeConstants(existing []types.ZarfPackageConstant) (mer return c1.Name == c2.Name } - merged = helpers.MergeSlices(existing, merged, exists) - node := ic.head + node := ic.tail for node != nil { // merge the consts merged = helpers.MergeSlices(node.consts, merged, exists) - node = node.next + node = node.prev } + merged = helpers.MergeSlices(existing, merged, exists) + return merged } diff --git a/src/pkg/packager/composer/list_test.go b/src/pkg/packager/composer/list_test.go index abcce2b8df..bca4cd2fd5 100644 --- a/src/pkg/packager/composer/list_test.go +++ b/src/pkg/packager/composer/list_test.go @@ -259,6 +259,186 @@ func TestCompose(t *testing.T) { } } +func TestMerging(t *testing.T) { + t.Parallel() + + type testCase struct { + name string + ic *ImportChain + existingVars []types.ZarfPackageVariable + existingConsts []types.ZarfPackageConstant + expectedVars []types.ZarfPackageVariable + expectedConsts []types.ZarfPackageConstant + } + + head := Node{ + vars: []types.ZarfPackageVariable{ + { + Name: "TEST", + Default: "head", + }, + { + Name: "HEAD", + }, + }, + consts: []types.ZarfPackageConstant{ + { + Name: "TEST", + Value: "head", + }, + { + Name: "HEAD", + }, + }, + } + tail := Node{ + vars: []types.ZarfPackageVariable{ + { + Name: "TEST", + Default: "tail", + }, + { + Name: "TAIL", + }, + }, + consts: []types.ZarfPackageConstant{ + { + Name: "TEST", + Value: "tail", + }, + { + Name: "TAIL", + }, + }, + } + head.next = &tail + tail.prev = &head + testIC := &ImportChain{head: &head, tail: &tail} + + testCases := []testCase{ + { + name: "empty-ic", + ic: &ImportChain{}, + existingVars: []types.ZarfPackageVariable{ + { + Name: "TEST", + }, + }, + existingConsts: []types.ZarfPackageConstant{ + { + Name: "TEST", + }, + }, + expectedVars: []types.ZarfPackageVariable{ + { + Name: "TEST", + }, + }, + expectedConsts: []types.ZarfPackageConstant{ + { + Name: "TEST", + }, + }, + }, + { + name: "no-existing", + ic: testIC, + existingVars: []types.ZarfPackageVariable{}, + existingConsts: []types.ZarfPackageConstant{}, + expectedVars: []types.ZarfPackageVariable{ + { + Name: "TEST", + Default: "head", + }, + { + Name: "HEAD", + }, + { + Name: "TAIL", + }, + }, + expectedConsts: []types.ZarfPackageConstant{ + { + Name: "TEST", + Value: "head", + }, + { + Name: "HEAD", + }, + { + Name: "TAIL", + }, + }, + }, + { + name: "with-existing", + ic: testIC, + existingVars: []types.ZarfPackageVariable{ + { + Name: "TEST", + Default: "existing", + }, + { + Name: "EXISTING", + }, + }, + existingConsts: []types.ZarfPackageConstant{ + { + Name: "TEST", + Value: "existing", + }, + { + Name: "EXISTING", + }, + }, + expectedVars: []types.ZarfPackageVariable{ + { + Name: "TEST", + Default: "existing", + }, + { + Name: "EXISTING", + }, + { + Name: "HEAD", + }, + { + Name: "TAIL", + }, + }, + expectedConsts: []types.ZarfPackageConstant{ + { + Name: "TEST", + Value: "existing", + }, + { + Name: "EXISTING", + }, + { + Name: "HEAD", + }, + { + Name: "TAIL", + }, + }, + }, + } + + for _, testCase := range testCases { + testCase := testCase + + t.Run(testCase.name, func(t *testing.T) { + t.Parallel() + + mergedVars := testCase.ic.MergeVariables(testCase.existingVars) + require.EqualValues(t, testCase.expectedVars, mergedVars) + + mergedConsts := testCase.ic.MergeConstants(testCase.existingConsts) + require.EqualValues(t, testCase.expectedConsts, mergedConsts) + }) + } +} + func createChainFromSlice(components []types.ZarfComponent) (ic *ImportChain) { ic = &ImportChain{}