Skip to content

Commit 7b2942a

Browse files
committed
Modify init container library to use DevWorkspaceTemplateSpecContent
Signed-off-by: Angel Misevski <[email protected]>
1 parent 7de2c71 commit 7b2942a

7 files changed

+77
-77
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/devfile/api v0.0.0-20200826083800-9e2280a95680
88
github.com/eclipse/che-go-jsonrpc v0.0.0-20200317130110-931966b891fe // indirect
99
github.com/eclipse/che-plugin-broker v3.4.0+incompatible
10+
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
1011
github.com/go-logr/logr v0.1.0
1112
github.com/google/go-cmp v0.4.0
1213
github.com/google/uuid v1.1.1

pkg/library/lifecycle.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import (
1616
"github.com/devfile/api/pkg/apis/workspaces/v1alpha1"
1717
)
1818

19-
func GetInitContainers(devfile v1alpha1.DevWorkspaceSpec) (initContainers, mainComponents []v1alpha1.Component, err error) {
20-
components := devfile.Template.Components
21-
commands := devfile.Template.Commands
22-
events := devfile.Template.Events
19+
func GetInitContainers(devfile v1alpha1.DevWorkspaceTemplateSpecContent) (initContainers, mainComponents []v1alpha1.Component, err error) {
20+
components := devfile.Components
21+
commands := devfile.Commands
22+
events := devfile.Events
2323
if events == nil || commands == nil {
2424
// All components are
2525
return nil, components, nil

pkg/library/lifecycle_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ import (
2020

2121
"github.com/devfile/api/pkg/apis/workspaces/v1alpha1"
2222
"github.com/stretchr/testify/assert"
23-
"gopkg.in/yaml.v2"
23+
24+
// ghodss/yaml is required instead of the default gopkg.in/yaml.v2 since the latter
25+
// only supports yaml struct tags and the DevWorkspace API only uses json.
26+
"github.com/ghodss/yaml"
2427
)
2528

2629
type testCase struct {
27-
Name string `json:"name,omitempty"`
28-
Input v1alpha1.DevWorkspaceSpec `json:"input,omitempty"`
29-
Output testOutput `json:"output,omitempty"`
30+
Name string `json:"name,omitempty"`
31+
Input v1alpha1.DevWorkspaceTemplateSpecContent `json:"input,omitempty"`
32+
Output testOutput `json:"output,omitempty"`
3033
}
3134

3235
type testOutput struct {
@@ -60,13 +63,13 @@ func TestGetInitContainers(t *testing.T) {
6063
for _, tt := range tests {
6164
t.Run(tt.Name, func(t *testing.T) {
6265
// sanity check that file reads correctly.
63-
assert.True(t, len(tt.Input.Template.Components) > 0, "Input defines no components")
66+
assert.True(t, len(tt.Input.Components) > 0, "Input defines no components")
6467
gotInitContainers, gotMainComponents, err := GetInitContainers(tt.Input)
6568
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
6669
assert.Regexp(t, tt.Output.ErrRegexp, err.Error(), "Error message should match")
6770
} else {
68-
assert.Equal(t, tt.Output.InitContainers, gotInitContainers)
69-
assert.Equal(t, tt.Output.MainContainers, gotMainComponents)
71+
assert.Equal(t, tt.Output.InitContainers, gotInitContainers, "Init containers should match expected")
72+
assert.Equal(t, tt.Output.MainContainers, gotMainComponents, "Main containers should match expected")
7073
}
7174
})
7275
}

pkg/library/testdata/lifecycle/init_and_main_container.yaml

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
name: "Should return all components when devfile contains no events"
1+
name: "Should use container as both init and main when multiple commands apply"
22

33
input:
4-
template:
5-
components:
6-
- container:
7-
name: test-container1
8-
image: my-image
9-
- container:
10-
name: test-container2
11-
image: my-image
12-
commands:
13-
- exec:
14-
id: test_preStart_command
15-
component: test-container1
16-
command: "test_command"
17-
- exec:
18-
id: test_regular_command
19-
component: test-container1
20-
command: "test_command"
21-
events:
22-
preStart:
23-
- "test_preStart_command"
4+
components:
5+
- container:
6+
name: test-container1
7+
image: my-image
8+
- container:
9+
name: test-container2
10+
image: my-image
11+
commands:
12+
- exec:
13+
id: test_preStart_command
14+
component: test-container1
15+
command: "test_command"
16+
- exec:
17+
id: test_regular_command
18+
component: test-container1
19+
command: "test_command"
20+
events:
21+
preStart:
22+
- "test_preStart_command"
2423

2524
output:
2625
initContainers:

pkg/library/testdata/lifecycle/no_events.yaml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
name: "Should return all components when devfile contains no events"
22

33
input:
4-
template:
5-
components:
6-
- container:
7-
name: test-container1
8-
image: my-image
9-
- container:
10-
name: test-container2
11-
image: my-image
12-
commands:
13-
- exec:
14-
component: test-container1
15-
command: "test_command"
4+
components:
5+
- container:
6+
name: test-container1
7+
image: my-image
8+
- container:
9+
name: test-container2
10+
image: my-image
11+
commands:
12+
- exec:
13+
component: test-container1
14+
command: "test_command"
1615

1716
output:
1817
initContainers:

pkg/library/testdata/lifecycle/prestart_apply_command.yaml

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
name: "Should return all components when devfile contains no events"
1+
name: "Should return init container with prestart apply command"
22

33
input:
4-
template:
5-
components:
6-
- container:
7-
name: test-container1
8-
image: my-image
9-
- container:
10-
name: test-container2
11-
image: my-image
12-
commands:
13-
- apply:
14-
id: test_apply_command
15-
component: test-container1
16-
events:
17-
preStart:
18-
- "test_apply_command"
4+
components:
5+
- container:
6+
name: test-container1
7+
image: my-image
8+
- container:
9+
name: test-container2
10+
image: my-image
11+
commands:
12+
- apply:
13+
id: test_apply_command
14+
component: test-container1
15+
events:
16+
preStart:
17+
- "test_apply_command"
1918

2019
output:
2120
initContainers:

pkg/library/testdata/lifecycle/prestart_exec_command.yaml

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
name: "Should return all components when devfile contains no events"
1+
name: "Should return init container with prestart exec command"
22

33
input:
4-
template:
5-
components:
6-
- container:
7-
name: test-container1
8-
image: my-image
9-
- container:
10-
name: test-container2
11-
image: my-image
12-
commands:
13-
- exec:
14-
id: test_command
15-
component: test-container1
16-
command: "test_command"
17-
events:
18-
preStart:
19-
- "test_command"
4+
components:
5+
- container:
6+
name: test-container1
7+
image: my-image
8+
- container:
9+
name: test-container2
10+
image: my-image
11+
commands:
12+
- exec:
13+
id: test_command
14+
component: test-container1
15+
command: "test_command"
16+
events:
17+
preStart:
18+
- "test_command"
2019

2120
output:
2221
initContainers:

0 commit comments

Comments
 (0)