Skip to content

Commit

Permalink
Add tests for .spec.contributions field in DevWorkspace
Browse files Browse the repository at this point in the history
Add tests covering .spec.contributions field use in DevWorkspaces. This
is done by copying tests for plugin components and adapting them to use
contributions instead to make sure we cover as many use cases as
possible.

Signed-off-by: Angel Misevski <[email protected]>
  • Loading branch information
amisevsk committed Oct 18, 2022
1 parent 9f760b3 commit 2b70f7a
Show file tree
Hide file tree
Showing 26 changed files with 1,942 additions and 9 deletions.
39 changes: 30 additions & 9 deletions pkg/library/flatten/flatten_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestResolveDevWorkspaceKubernetesReference(t *testing.T) {
// sanity check: input defines components
assert.True(t, len(tt.Input.DevWorkspace.Components) > 0, "Test case defines workspace with no components")
testResolverTools := getTestingTools(tt.Input, "test-ignored")
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, testResolverTools)
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, nil, testResolverTools)
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
} else {
Expand All @@ -55,7 +55,7 @@ func TestResolveDevWorkspacePluginRegistry(t *testing.T) {
assert.True(t, len(tt.Input.DevWorkspace.Components) > 0, "Test case defines workspace with no components")
testResolverTools := getTestingTools(tt.Input, "test-ignored")

outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, testResolverTools)
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, nil, testResolverTools)
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
} else {
Expand All @@ -78,7 +78,7 @@ func TestResolveDevWorkspacePluginURI(t *testing.T) {
assert.True(t, len(tt.Input.DevWorkspace.Components) > 0, "Test case defines workspace with no components")
testResolverTools := getTestingTools(tt.Input, "test-ignored")

outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, testResolverTools)
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, nil, testResolverTools)
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
} else {
Expand All @@ -101,7 +101,7 @@ func TestResolveDevWorkspaceParents(t *testing.T) {
assert.True(t, len(tt.Input.DevWorkspace.Components) > 0, "Test case defines workspace with no components")
testResolverTools := getTestingTools(tt.Input, "test-ignored")

outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, testResolverTools)
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, nil, testResolverTools)
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
} else {
Expand All @@ -127,7 +127,7 @@ func TestResolveDevWorkspaceMissingDefaults(t *testing.T) {
assert.True(t, len(tt.Input.DevWorkspace.Components) > 0, "Test case defines workspace with no components")
testResolverTools := getTestingTools(tt.Input, "")

outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, testResolverTools)
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, nil, testResolverTools)
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
} else {
Expand All @@ -150,7 +150,7 @@ func TestResolveDevWorkspaceAnnotations(t *testing.T) {
assert.True(t, len(tt.Input.DevWorkspace.Components) > 0, "Test case defines devworkspace with no components")
testResolverTools := getTestingTools(tt.Input, "test-ignored")

outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, testResolverTools)
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, nil, testResolverTools)
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
} else {
Expand All @@ -173,7 +173,7 @@ func TestResolveDevWorkspaceTemplateNamespaceRestriction(t *testing.T) {
assert.True(t, len(tt.Input.DevWorkspace.Components) > 0, "Test case defines devworkspace with no components")
testResolverTools := getTestingTools(tt.Input, "test-namespace")

outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, testResolverTools)
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, nil, testResolverTools)
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
} else {
Expand All @@ -196,7 +196,7 @@ func TestMergesDuplicateVolumeComponents(t *testing.T) {
assert.True(t, len(tt.Input.DevWorkspace.Components) > 0, "Test case defines workspace with no components")
testResolverTools := getTestingTools(tt.Input, "test-ignored")

outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, testResolverTools)
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, nil, testResolverTools)
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
} else {
Expand All @@ -219,7 +219,28 @@ func TestMergeContainerContributions(t *testing.T) {
assert.True(t, len(tt.Input.DevWorkspace.Components) > 0, "Test case defines workspace with no components")
testResolverTools := getTestingTools(tt.Input, "test-ignored")

outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, testResolverTools)
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, nil, testResolverTools)
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
} else {
if !assert.NoError(t, err, "Should not return error") {
return
}
assert.Truef(t, cmp.Equal(tt.Output.DevWorkspace, outputWorkspace, testutil.WorkspaceTemplateDiffOpts),
"DevWorkspace should match expected output:\n%s",
cmp.Diff(tt.Output.DevWorkspace, outputWorkspace, testutil.WorkspaceTemplateDiffOpts))
}
})
}
}

func TestMergeSpecContributions(t *testing.T) {
tests := testutil.LoadAllTestsOrPanic(t, "testdata/spec-contributions")
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
testResolverTools := getTestingTools(tt.Input, "test-namespace")

outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, tt.Input.Contributions, testResolverTools)
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
} else {
Expand Down
3 changes: 3 additions & 0 deletions pkg/library/flatten/internal/testutil/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ type TestCase struct {
}

type TestInput struct {
// DevWorkspace is the .spec.template field of a DevWorkspace
DevWorkspace *dw.DevWorkspaceTemplateSpec `json:"devworkspace,omitempty"`
// Contributions is the .spec.containerContributions field of a DevWorkspace
Contributions []dw.ComponentContribution `json:"contributions,omitempty"`
// DevWorkspaceResources is a map of string keys to devworkspace templates
DevWorkspaceResources map[string]dw.DevWorkspaceTemplate `json:"devworkspaceResources,omitempty"`
// DevfileResources is a map of string keys to devfile resources
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: "Adds attributes from contribution"

input:
devworkspace:
components:
- name: test-component
attributes:
controller.devfile.io/merge-contribution: true
container:
image: test-image
memoryLimit: 1Gi
memoryRequest: 1000Mi
cpuLimit: 1500m
cpuRequest: "1"
contributions:
- name: test-contribution
uri: test-contribution.yaml

devfileResources:
test-contribution.yaml:
schemaVersion: 2.1.0
metadata:
name: test-contribution
components:
- name: test-contribution
attributes:
controller.devfile.io/container-contribution: true
container:
image: contribution-image
memoryLimit: 512Mi
memoryRequest: 1.5G
cpuLimit: "0.5"
cpuRequest: 500m

output:
devworkspace:
components:
- name: test-component
attributes:
controller.devfile.io/merged-contributions: "test-contribution"
container:
image: test-image
memoryLimit: 1536Mi
memoryRequest: "2548576000" # 1.5G + 1000Mi = 1.5*1000^3 + 1000*1024^2
cpuLimit: "2"
cpuRequest: 1500m
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: "Adds unmerged elements"

input:
devworkspace:
components:
- name: test-component
attributes:
controller.devfile.io/merge-contribution: true
container:
image: test-image
env:
- name: TEST_ENVVAR
value: TEST_VALUE
contributions:
- name: test-contribution
uri: test-contribution.yaml

devfileResources:
test-contribution.yaml:
schemaVersion: 2.1.0
metadata:
name: test-contribution
components:
- name: test-contribution
attributes:
controller.devfile.io/container-contribution: true
container:
image: contribution-image
env:
- name: CONTRIB_ENVVAR
value: CONTRIB_VALUE
- name: unmerged-container
container:
image: unmerged-container
- name: unmerged-volume
volume: {}
commands:
- name: plugin-command
apply:
component: unmerged-container
events:
prestart:
- plugin-command

output:
devworkspace:
components:
- name: test-component
attributes:
controller.devfile.io/merged-contributions: "test-contribution"
container:
image: test-image
env:
- name: TEST_ENVVAR
value: TEST_VALUE
- name: CONTRIB_ENVVAR
value: CONTRIB_VALUE
- name: unmerged-container
attributes:
controller.devfile.io/imported-by: test-contribution
container:
image: unmerged-container
- name: unmerged-volume
attributes:
controller.devfile.io/imported-by: test-contribution
volume: {}
commands:
- name: plugin-command
attributes:
controller.devfile.io/imported-by: test-contribution
apply:
component: unmerged-container
events:
prestart:
- plugin-command
Loading

0 comments on commit 2b70f7a

Please sign in to comment.