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

Add more merging and overriding tests #308

Merged
merged 3 commits into from
Jan 21, 2021
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
33 changes: 22 additions & 11 deletions pkg/utils/overriding/merging.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,31 @@ func MergeDevWorkspaceTemplateSpec(
parentFlattenedContent *workspaces.DevWorkspaceTemplateSpecContent,
pluginFlattenedContents ...*workspaces.DevWorkspaceTemplateSpecContent) (*workspaces.DevWorkspaceTemplateSpecContent, error) {

allContents := []*workspaces.DevWorkspaceTemplateSpecContent{parentFlattenedContent}
allContents = append(allContents, pluginFlattenedContents...)
allContents = append(allContents, mainContent)

if err := ensureNoConflictWithParent(mainContent, parentFlattenedContent); err != nil {
return nil, err
allContents := []*workspaces.DevWorkspaceTemplateSpecContent{}
if parentFlattenedContent != nil {
allContents = append(allContents, parentFlattenedContent)
}
if len(pluginFlattenedContents) > 0 {
allContents = append(allContents, pluginFlattenedContents...)
}
allContents = append(allContents, mainContent)

if err := ensureNoConflictsWithPlugins(mainContent, pluginFlattenedContents...); err != nil {
return nil, err
// Check for conflicts
if parentFlattenedContent != nil {
if err := ensureNoConflictWithParent(mainContent, parentFlattenedContent); err != nil {
return nil, err
}
}
// also need to ensure no conflict between parent and plugins
if err := ensureNoConflictsWithPlugins(parentFlattenedContent, pluginFlattenedContents...); err != nil {
return nil, err
if len(pluginFlattenedContents) > 0 {
if err := ensureNoConflictsWithPlugins(mainContent, pluginFlattenedContents...); err != nil {
return nil, err
}
if parentFlattenedContent != nil {
// also need to ensure no conflict between parent and plugins
if err := ensureNoConflictsWithPlugins(parentFlattenedContent, pluginFlattenedContents...); err != nil {
return nil, err
}
}
}

result := workspaces.DevWorkspaceTemplateSpecContent{}
Expand Down
69 changes: 69 additions & 0 deletions pkg/utils/overriding/overriding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,75 @@ func TestMerging(t *testing.T) {
})
}

func TestPluginOverrides(t *testing.T) {
originalFile := "test-fixtures/patches/override-just-plugin/original.yaml"
patchFile := "test-fixtures/patches/override-just-plugin/patch.yaml"
resultFile := "test-fixtures/patches/override-just-plugin/result.yaml"

originalDWT := workspaces.DevWorkspaceTemplateSpecContent{}
patch := workspaces.PluginOverrides{}
expectedDWT := workspaces.DevWorkspaceTemplateSpecContent{}

readFileToStruct(t, originalFile, &originalDWT)
readFileToStruct(t, patchFile, &patch)
readFileToStruct(t, resultFile, &expectedDWT)

gotDWT, err := OverrideDevWorkspaceTemplateSpec(&originalDWT, patch)
if assert.NoError(t, err) {
assert.Equal(t, &expectedDWT, gotDWT)
}
}

func TestMergingOnlyPlugins(t *testing.T) {
baseFile := "test-fixtures/merges/no-parent/main.yaml"
pluginFile := "test-fixtures/merges/no-parent/plugin.yaml"
resultFile := "test-fixtures/merges/no-parent/result.yaml"

baseDWT := workspaces.DevWorkspaceTemplateSpecContent{}
pluginDWT := workspaces.DevWorkspaceTemplateSpecContent{}
expectedDWT := workspaces.DevWorkspaceTemplateSpecContent{}

readFileToStruct(t, baseFile, &baseDWT)
readFileToStruct(t, pluginFile, &pluginDWT)
readFileToStruct(t, resultFile, &expectedDWT)

gotDWT, err := MergeDevWorkspaceTemplateSpec(&baseDWT, nil, &pluginDWT)
if assert.NoError(t, err) {
assert.Equal(t, &expectedDWT, gotDWT)
}
}

func TestMergingOnlyParent(t *testing.T) {
// Reuse only plugin case since it's compatible
baseFile := "test-fixtures/merges/no-parent/main.yaml"
parentFile := "test-fixtures/merges/no-parent/plugin.yaml"
resultFile := "test-fixtures/merges/no-parent/result.yaml"

baseDWT := workspaces.DevWorkspaceTemplateSpecContent{}
parentDWT := workspaces.DevWorkspaceTemplateSpecContent{}
expectedDWT := workspaces.DevWorkspaceTemplateSpecContent{}

readFileToStruct(t, baseFile, &baseDWT)
readFileToStruct(t, parentFile, &parentDWT)
readFileToStruct(t, resultFile, &expectedDWT)

gotDWT, err := MergeDevWorkspaceTemplateSpec(&baseDWT, &parentDWT)
if assert.NoError(t, err) {
assert.Equal(t, &expectedDWT, gotDWT)
}
}

func readFileToStruct(t *testing.T, path string, into interface{}) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("Failed to read test file from %s: %s", path, err.Error())
}
err = yaml.Unmarshal(bytes, into)
if err != nil {
t.Fatalf("Failed to unmarshal file into struct: %s", err.Error())
}
}

// Since order of error message lines is not deterministic, it's necessary to compare
// in a weaker way than asserting string equality.
func compareErrorMessages(t *testing.T, expected, actual string, failReason string) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/utils/overriding/test-fixtures/merges/no-parent/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
components:
- name: test-component
container:
image: test-image
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
components:
- name: test-plugin-component
container:
image: test-plugin-image
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
components:
- name: test-plugin-component
container:
image: test-plugin-image
- name: test-component
container:
image: test-image
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
projects:
- name: test-project
git:
remotes:
origin: "https://test-data.io"

components:
- name: my-component
container:
image: testimg

commands:
- id: test-command
exec:
component: my-component
commandLine: "echo 'just a test'"

events:
preStart:
- test-command
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
components:
- name: my-component
container:
image: overridden-image

commands:
- id: test-command
exec:
component: my-component
commandLine: "echo 'this bit is updated'"
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
projects:
- name: test-project
git:
remotes:
origin: "https://test-data.io"

components:
- name: my-component
container:
image: overridden-image

commands:
- id: test-command
exec:
component: my-component
commandLine: "echo 'this bit is updated'"

events:
preStart:
- test-command