Skip to content

Commit c5c0fd9

Browse files
committed
Add tests for storage and container libraries
Signed-off-by: Angel Misevski <[email protected]>
1 parent cdec8d5 commit c5c0fd9

18 files changed

+720
-0
lines changed

pkg/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ func WatchControllerConfig(mgr manager.Manager) error {
251251
return err
252252
}
253253

254+
func SetupConfigForTesting(cm *corev1.ConfigMap) {
255+
ControllerCfg.update(cm)
256+
}
257+
254258
func buildDefaultConfigMap(cm *corev1.ConfigMap) {
255259
cm.Name = ConfigMapReference.Name
256260
cm.Namespace = ConfigMapReference.Namespace
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//
2+
// Copyright (c) 2019-2020 Red Hat, Inc.
3+
// This program and the accompanying materials are made
4+
// available under the terms of the Eclipse Public License 2.0
5+
// which is available at https://www.eclipse.org/legal/epl-2.0/
6+
//
7+
// SPDX-License-Identifier: EPL-2.0
8+
//
9+
// Contributors:
10+
// Red Hat, Inc. - initial API and implementation
11+
//
12+
13+
package container
14+
15+
import (
16+
"fmt"
17+
"io/ioutil"
18+
"path/filepath"
19+
"testing"
20+
21+
corev1 "k8s.io/api/core/v1"
22+
23+
devworkspace "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
24+
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
25+
"github.com/devfile/devworkspace-operator/pkg/config"
26+
"github.com/stretchr/testify/assert"
27+
"sigs.k8s.io/yaml"
28+
)
29+
30+
type testCase struct {
31+
Name string `json:"name,omitempty"`
32+
Input devworkspace.DevWorkspaceTemplateSpec `json:"input,omitempty"`
33+
Output testOutput `json:"output,omitempty"`
34+
}
35+
36+
type testOutput struct {
37+
PodAdditions *v1alpha1.PodAdditions `json:"podAdditions,omitempty"`
38+
ErrRegexp *string `json:"errRegexp,omitempty"`
39+
}
40+
41+
var testControllerCfg = &corev1.ConfigMap{
42+
Data: map[string]string{
43+
"devworkspace.sidecar.image_pull_policy": "Always",
44+
},
45+
}
46+
47+
func setupControllerCfg() {
48+
config.SetupConfigForTesting(testControllerCfg)
49+
}
50+
51+
func loadTestCaseOrPanic(t *testing.T, testFilename string) testCase {
52+
testPath := filepath.Join("./testdata", testFilename)
53+
bytes, err := ioutil.ReadFile(testPath)
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
var test testCase
58+
if err := yaml.Unmarshal(bytes, &test); err != nil {
59+
t.Fatal(err)
60+
}
61+
t.Log(fmt.Sprintf("Read file:\n%+v\n\n", test))
62+
return test
63+
}
64+
65+
func TestGetKubeContainersFromDevfile(t *testing.T) {
66+
tests := []testCase{
67+
loadTestCaseOrPanic(t, "detects-init-containers.yaml"),
68+
loadTestCaseOrPanic(t, "handles-mountSources.yaml"),
69+
loadTestCaseOrPanic(t, "handles-resources.yaml"),
70+
loadTestCaseOrPanic(t, "ignores-non-container-components.yaml"),
71+
loadTestCaseOrPanic(t, "converts-all-fields.yaml"),
72+
loadTestCaseOrPanic(t, "error-has-parent.yaml"),
73+
loadTestCaseOrPanic(t, "error-has-plugins.yaml"),
74+
loadTestCaseOrPanic(t, "error-invalid-resources.yaml"),
75+
}
76+
setupControllerCfg()
77+
78+
for _, tt := range tests {
79+
t.Run(tt.Name, func(t *testing.T) {
80+
// sanity check that file is read correctly.
81+
assert.True(t, len(tt.Input.Components) > 0, "Input defines no components")
82+
gotPodAdditions, err := GetKubeContainersFromDevfile(tt.Input)
83+
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
84+
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
85+
} else {
86+
if !assert.NoError(t, err, "Should not return error") {
87+
return
88+
}
89+
assert.Equal(t, tt.Output.PodAdditions, gotPodAdditions, "PodAdditions should match expected output")
90+
}
91+
})
92+
}
93+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: "Converts all applicable fields"
2+
3+
input:
4+
components:
5+
- name: testing-container-1
6+
container:
7+
image: testing-image-1
8+
memoryLimit: 999Mi # isolate test to not include memoryLimit
9+
sourceMapping: "/source-mapping"
10+
command: ["test", "command"]
11+
args: ["test", "args"]
12+
env:
13+
- name: "TEST_ENVVAR"
14+
value: "TEST_VALUE"
15+
endpoints:
16+
- name: "test-endpoint-1"
17+
exposure: public
18+
targetPort: 3100
19+
secure: true
20+
protocol: wss
21+
attributes:
22+
type: ide
23+
cookiesAuthEnabled: "true"
24+
- name: "test-endpoint-2"
25+
exposure: public
26+
targetPort: 8080
27+
secure: true
28+
protocol: http
29+
attributes:
30+
cookiesAuthEnabled: "true"
31+
volumeMounts:
32+
- name: "test-volume1"
33+
path: "/test-volume1-path"
34+
- name: "test-volume2"
35+
# path omitted; should use name as mountpath
36+
- name: "should-be-ignored"
37+
volume: {}
38+
39+
output:
40+
podAdditions:
41+
containers:
42+
- name: testing-container-1
43+
image: testing-image-1
44+
imagePullPolicy: Always
45+
resources:
46+
limits:
47+
memory: "999Mi"
48+
env:
49+
- name: "TEST_ENVVAR"
50+
value: "TEST_VALUE"
51+
- name: "PROJECTS_ROOT"
52+
value: "/source-mapping"
53+
- name: "PROJECTS_SOURCE"
54+
value: "/source-mapping" # Temp value until projects is figured out
55+
command:
56+
- "test"
57+
- "command"
58+
args:
59+
- "test"
60+
- "args"
61+
ports:
62+
- name: "3100-wss"
63+
containerPort: 3100
64+
protocol: TCP
65+
- name: "8080-http"
66+
containerPort: 8080
67+
protocol: TCP
68+
volumeMounts:
69+
- name: "test-volume1"
70+
mountPath: "/test-volume1-path"
71+
- name: "test-volume2"
72+
mountPath: "/test-volume2"
73+
- name: "projects"
74+
mountPath: "/source-mapping"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: "Gets initContainers correctly"
2+
3+
input:
4+
components:
5+
- name: testing-container-1
6+
container:
7+
image: testing-image-1
8+
mountSources: false # isolate test to not include volumes
9+
memoryLimit: "999Mi" # isolate test to not include memoryLimit
10+
- name: testing-container-2
11+
container:
12+
image: testing-image-2
13+
mountSources: false # isolate test to not include volumes
14+
memoryLimit: "999Mi" # isolate test to not include memoryLimit
15+
commands:
16+
- id: test_preStart_command
17+
apply:
18+
component: testing-container-2
19+
events:
20+
preStart:
21+
- "test_preStart_command"
22+
output:
23+
podAdditions:
24+
containers:
25+
- name: testing-container-1
26+
image: testing-image-1
27+
imagePullPolicy: Always
28+
resources:
29+
limits:
30+
memory: "999Mi"
31+
initContainers:
32+
- name: testing-container-2
33+
image: testing-image-2
34+
imagePullPolicy: Always
35+
resources:
36+
limits:
37+
memory: "999Mi"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: "Checks for flattened devfile (parent)"
2+
3+
input:
4+
parent:
5+
registryUrl: "has-parent"
6+
components:
7+
- name: testing-container
8+
container:
9+
image: testing-image
10+
11+
output:
12+
errRegexp: "devfile is not flattened"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: "Checks for flattened devfile (parent)"
2+
3+
input:
4+
components:
5+
- name: testing-container
6+
container:
7+
image: testing-image
8+
- name: my-plugin
9+
plugin:
10+
id: "test-id"
11+
12+
output:
13+
errRegexp: "devfile is not flattened"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: "Checks for flattened devfile (parent)"
2+
3+
input:
4+
components:
5+
- name: testing-container
6+
container:
7+
image: testing-image
8+
memoryLimit: "x"
9+
10+
output:
11+
errRegexp: "failed to parse memory limit.*"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: "Handle mountSources correctly"
2+
3+
input:
4+
components:
5+
- name: testing-container-1
6+
container:
7+
image: testing-image-1
8+
memoryLimit: 999Mi # isolate test to not include memoryLimit
9+
sourceMapping: "/testdir1"
10+
# no mountSources defined -> should mount sources
11+
- name: testing-container-2
12+
container:
13+
image: testing-image-2
14+
memoryLimit: 999Mi # isolate test to not include memoryLimit
15+
# No sourceMapping -> defaults to "/projects"
16+
mountSources: true # mountSources: true -> should mount sources
17+
- name: testing-container-3
18+
container:
19+
image: testing-image-3
20+
memoryLimit: 999Mi # isolate test to not include memoryLimit
21+
sourceMapping: "/projects"
22+
mountSources: false # mountSources: false -> should not mount sources
23+
output:
24+
podAdditions:
25+
containers:
26+
- name: testing-container-1
27+
image: testing-image-1
28+
imagePullPolicy: Always
29+
resources:
30+
limits:
31+
memory: "999Mi"
32+
volumeMounts:
33+
- name: "projects"
34+
mountPath: "/testdir1"
35+
env:
36+
- name: "PROJECTS_ROOT"
37+
value: "/testdir1"
38+
- name: "PROJECTS_SOURCE"
39+
value: "/testdir1" # Temp value until projects is figured out
40+
- name: testing-container-2
41+
image: testing-image-2
42+
imagePullPolicy: Always
43+
resources:
44+
limits:
45+
memory: "999Mi"
46+
volumeMounts:
47+
- name: "projects"
48+
mountPath: "/projects"
49+
env:
50+
- name: "PROJECTS_ROOT"
51+
value: "/projects"
52+
- name: "PROJECTS_SOURCE"
53+
value: "/projects" # Temp value until projects is figured out
54+
55+
- name: testing-container-3
56+
image: testing-image-3
57+
imagePullPolicy: Always
58+
resources:
59+
limits:
60+
memory: "999Mi"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: "Processes memoryLimit correctly"
2+
3+
input:
4+
components:
5+
- name: testing-container-1
6+
container:
7+
image: testing-image-1
8+
memoryLimit: 999Mi
9+
mountSources: false
10+
- name: testing-container-2
11+
container:
12+
image: testing-image-2
13+
# Omitted memory limit - should use default
14+
mountSources: false
15+
16+
output:
17+
podAdditions:
18+
containers:
19+
- name: testing-container-1
20+
image: testing-image-1
21+
imagePullPolicy: Always
22+
resources:
23+
limits:
24+
memory: "999Mi"
25+
- name: testing-container-2
26+
image: testing-image-2
27+
imagePullPolicy: Always
28+
resources:
29+
limits:
30+
memory: "128M"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: "Ignores non-container components"
2+
3+
input:
4+
components:
5+
- name: testing-container-1
6+
container:
7+
image: testing-image-1
8+
mountSources: false # isolate test to not include volumes
9+
memoryLimit: "999Mi" # isolate test to not include memoryLimit
10+
- name: test-volume
11+
volume: {}
12+
output:
13+
podAdditions:
14+
containers:
15+
- name: testing-container-1
16+
image: testing-image-1
17+
imagePullPolicy: Always
18+
resources:
19+
limits:
20+
memory: "999Mi"

0 commit comments

Comments
 (0)