Skip to content

Commit ce7b2be

Browse files
committed
Add test cases for implicit container contribution feature
Signed-off-by: Andrew Obuchowicz <[email protected]>
1 parent d03718d commit ce7b2be

21 files changed

+2053
-8
lines changed

pkg/library/flatten/flatten_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,29 @@ func TestMergeContainerContributions(t *testing.T) {
234234
}
235235
}
236236

237+
func TestMergeImplicitContainerContributions(t *testing.T) {
238+
tests := testutil.LoadAllTestsOrPanic(t, "testdata/implicit-container-contributions")
239+
for _, tt := range tests {
240+
t.Run(tt.Name, func(t *testing.T) {
241+
// sanity check: input defines components
242+
assert.True(t, len(tt.Input.DevWorkspace.Components) > 0, "Test case defines workspace with no components")
243+
testResolverTools := getTestingTools(tt.Input, "test-ignored")
244+
245+
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, nil, testResolverTools)
246+
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
247+
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
248+
} else {
249+
if !assert.NoError(t, err, "Should not return error") {
250+
return
251+
}
252+
assert.Truef(t, cmp.Equal(tt.Output.DevWorkspace, outputWorkspace, testutil.WorkspaceTemplateDiffOpts),
253+
"DevWorkspace should match expected output:\n%s",
254+
cmp.Diff(tt.Output.DevWorkspace, outputWorkspace, testutil.WorkspaceTemplateDiffOpts))
255+
}
256+
})
257+
}
258+
}
259+
237260
func TestMergeSpecContributions(t *testing.T) {
238261
tests := testutil.LoadAllTestsOrPanic(t, "testdata/spec-contributions")
239262
for _, tt := range tests {
@@ -255,6 +278,27 @@ func TestMergeSpecContributions(t *testing.T) {
255278
}
256279
}
257280

281+
func TestMergeImplicitSpecContributions(t *testing.T) {
282+
tests := testutil.LoadAllTestsOrPanic(t, "testdata/spec-contributions")
283+
for _, tt := range tests {
284+
t.Run(tt.Name, func(t *testing.T) {
285+
testResolverTools := getTestingTools(tt.Input, "test-namespace")
286+
287+
outputWorkspace, _, err := ResolveDevWorkspace(tt.Input.DevWorkspace, tt.Input.Contributions, testResolverTools)
288+
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
289+
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
290+
} else {
291+
if !assert.NoError(t, err, "Should not return error") {
292+
return
293+
}
294+
assert.Truef(t, cmp.Equal(tt.Output.DevWorkspace, outputWorkspace, testutil.WorkspaceTemplateDiffOpts),
295+
"DevWorkspace should match expected output:\n%s",
296+
cmp.Diff(tt.Output.DevWorkspace, outputWorkspace, testutil.WorkspaceTemplateDiffOpts))
297+
}
298+
})
299+
}
300+
}
301+
258302
func getTestingTools(input testutil.TestInput, testNamespace string) ResolverTools {
259303
testHttpGetter := &testutil.FakeHTTPGetter{
260304
DevfileResources: input.DevfileResources,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: "Return error if multiple components have the merge-contribution attribute set to true"
2+
3+
input:
4+
devworkspace:
5+
components:
6+
- name: test-component
7+
attributes:
8+
controller.devfile.io/merge-contribution: true
9+
container:
10+
image: test-image
11+
env:
12+
- name: TEST_ENVVAR
13+
value: TEST_VALUE
14+
- name: test-component-2
15+
attributes:
16+
controller.devfile.io/merge-contribution: true
17+
container:
18+
image: test-image
19+
env:
20+
- name: TEST_ENVVAR
21+
value: TEST_VALUE
22+
23+
- name: test-contribution
24+
plugin:
25+
uri: test-contribution.yaml
26+
27+
devfileResources:
28+
test-contribution.yaml:
29+
schemaVersion: 2.1.0
30+
metadata:
31+
name: test-contribution
32+
components:
33+
- name: test-contribution
34+
attributes:
35+
controller.devfile.io/container-contribution: true
36+
container:
37+
image: contribution-image
38+
env:
39+
- name: CONTRIB_ENVVAR
40+
value: CONTRIB_VALUE
41+
- name: unmerged-container
42+
container:
43+
image: unmerged-container
44+
- name: unmerged-volume
45+
volume: {}
46+
commands:
47+
- name: plugin-command
48+
apply:
49+
component: unmerged-container
50+
events:
51+
prestart:
52+
- plugin-command
53+
54+
output:
55+
errRegexp: "multiple components have the controller.devfile.io/merge-contribution attribute set to true. Only a single component may have the controller.devfile.io/merge-contribution attribute set to true"

pkg/library/flatten/testdata/container-contributions/no-op-if-no-target.yaml renamed to pkg/library/flatten/testdata/container-contributions/no-op-if-explicit-opt-out.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
name: "Adds unmerged elements"
1+
name: "Opt out of container contribution"
22

33
input:
44
devworkspace:
55
components:
66
- name: test-component
7-
# attributes:
8-
# controller.devfile.io/merge-contribution: true
7+
attributes:
8+
controller.devfile.io/merge-contribution: false
99
container:
1010
image: test-image
1111
env:
@@ -47,6 +47,8 @@ output:
4747
devworkspace:
4848
components:
4949
- name: test-component
50+
attributes:
51+
controller.devfile.io/merge-contribution: false
5052
container:
5153
image: test-image
5254
env:

pkg/library/flatten/testdata/container-contributions/no-op-if-no-contribution.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: "Adds unmerged elements"
1+
name: "No op if no contribution"
22

33
input:
44
devworkspace:
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: "Adds attributes from contribution"
2+
3+
input:
4+
devworkspace:
5+
components:
6+
- name: test-component
7+
container:
8+
image: test-image
9+
memoryLimit: 1Gi
10+
memoryRequest: 1000Mi
11+
cpuLimit: 1500m
12+
cpuRequest: "1"
13+
- name: test-contribution
14+
plugin:
15+
uri: test-contribution.yaml
16+
17+
devfileResources:
18+
test-contribution.yaml:
19+
schemaVersion: 2.1.0
20+
metadata:
21+
name: test-contribution
22+
components:
23+
- name: test-contribution
24+
attributes:
25+
controller.devfile.io/container-contribution: true
26+
container:
27+
image: contribution-image
28+
memoryLimit: 512Mi
29+
memoryRequest: 1.5G
30+
cpuLimit: "0.5"
31+
cpuRequest: 500m
32+
33+
output:
34+
devworkspace:
35+
components:
36+
- name: test-component
37+
attributes:
38+
controller.devfile.io/merged-contributions: "test-contribution"
39+
container:
40+
image: test-image
41+
memoryLimit: 1536Mi
42+
memoryRequest: "2548576000" # 1.5G + 1000Mi = 1.5*1000^3 + 1000*1024^2
43+
cpuLimit: "2"
44+
cpuRequest: 1500m
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: "Adds unmerged elements"
2+
3+
input:
4+
devworkspace:
5+
components:
6+
- name: test-component
7+
container:
8+
image: test-image
9+
env:
10+
- name: TEST_ENVVAR
11+
value: TEST_VALUE
12+
13+
- name: test-contribution
14+
plugin:
15+
uri: test-contribution.yaml
16+
17+
devfileResources:
18+
test-contribution.yaml:
19+
schemaVersion: 2.1.0
20+
metadata:
21+
name: test-contribution
22+
components:
23+
- name: test-contribution
24+
attributes:
25+
controller.devfile.io/container-contribution: true
26+
container:
27+
image: contribution-image
28+
env:
29+
- name: CONTRIB_ENVVAR
30+
value: CONTRIB_VALUE
31+
- name: unmerged-container
32+
container:
33+
image: unmerged-container
34+
- name: unmerged-volume
35+
volume: {}
36+
commands:
37+
- name: plugin-command
38+
apply:
39+
component: unmerged-container
40+
events:
41+
prestart:
42+
- plugin-command
43+
44+
output:
45+
devworkspace:
46+
components:
47+
- name: test-component
48+
attributes:
49+
controller.devfile.io/merged-contributions: "test-contribution"
50+
container:
51+
image: test-image
52+
env:
53+
- name: TEST_ENVVAR
54+
value: TEST_VALUE
55+
- name: CONTRIB_ENVVAR
56+
value: CONTRIB_VALUE
57+
- name: unmerged-container
58+
attributes:
59+
controller.devfile.io/imported-by: test-contribution
60+
container:
61+
image: unmerged-container
62+
- name: unmerged-volume
63+
attributes:
64+
controller.devfile.io/imported-by: test-contribution
65+
volume: {}
66+
commands:
67+
- name: plugin-command
68+
attributes:
69+
controller.devfile.io/imported-by: test-contribution
70+
apply:
71+
component: unmerged-container
72+
events:
73+
prestart:
74+
- plugin-command

0 commit comments

Comments
 (0)