Skip to content

Commit

Permalink
wip - TestMergeReplace
Browse files Browse the repository at this point in the history
  • Loading branch information
aabouzaid committed Sep 18, 2023
1 parent e77ca1d commit c0388c1
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 6 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions pkg/merger/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ func (rm *resourceMerge) setStrategy() {
rm.config = mergo.WithAppendSlice
case Combine:
rm.config = mergo.WithSliceDeepCopy
default:
rm.config = func(*mergo.Config) {}
}
}

Expand Down
86 changes: 83 additions & 3 deletions pkg/merger/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func TestLoadDestinationFile(t *testing.T) {
}{
{
actual: resourceInputFiles{
Root: "./",
Destination: "testdata/test_load_destination_file.yaml",
Root: "../../examples/testdata/",
Destination: "input/base.yaml",
},
expected: "foo",
expected: "metadata.name",
},
}

Expand All @@ -96,3 +96,83 @@ func TestLoadDestinationFile(t *testing.T) {
"Expected to be %v, but got %v", tt.expected, tt.actual)
}
}

func TestMergeReplace(t *testing.T) {
tests := []struct {
name string
desc string
actual mergerResource
expected map[string]string
expectedLen int
}{
{
name: "Test replace with overlay",
desc: "The number of the outputs in 'replace+overlay' should be the same as input sources",
actual: mergerResource{
Name: "test-replace-with-overlay",
Input: resourceInput{
Method: "overlay",
Files: resourceInputFiles{
Root: "../../examples/testdata/",
Sources: []string{
"input/dev.yaml",
"input/stage.yaml",
},
Destination: "input/base.yaml",
},
},
Merge: resourceMerge{Strategy: "replace"},
Output: resourceOutput{Format: "raw"},
},
expected: map[string]string{
// In the overlay the output key are "resource.input.files.sources[*]".
"dev.yaml": "image: dev:1.0.0",
"stage.yaml": "image: stage:1.0.0",
},
expectedLen: 2,
},
{
name: "Test replace with patch",
desc: "The number of the outputs in 'replace+patch' should always be one",
actual: mergerResource{
Name: "test-replace-with-patch",
Input: resourceInput{
Method: "patch",
Files: resourceInputFiles{
Root: "../../examples/testdata/",
Sources: []string{
"input/dev.yaml",
"input/stage.yaml",
},
Destination: "input/base.yaml",
},
},
Merge: resourceMerge{Strategy: "replace"},
Output: resourceOutput{Format: "raw"},
},
expected: map[string]string{
// In the patch the output key are "resource.name".
"test-replace-with-patch": "image: stage:1.0.0",
},
expectedLen: 1,
},
}

for _, tt := range tests {
// Those methods are called in the Default method.
tt.actual.Input.Files.setRoot()
tt.actual.Merge.setStrategy()
tt.actual.Output.items = make(map[string]string)

// Merge.
tt.actual.merge()

// Assert.
assert.Equal(t, len(tt.actual.Output.items), tt.expectedLen)
for k, v := range tt.expected {
output := tt.actual.Output.items[k]
assert.Contains(t, output, v,
"Expected to be %v=%v, but got %v", k, v, output)
}
}
}
1 change: 0 additions & 1 deletion pkg/merger/testdata/test_load_destination_file.yaml

This file was deleted.

0 comments on commit c0388c1

Please sign in to comment.