Skip to content

Commit 29a7c9c

Browse files
committed
Add tests to cover container-overrides attribute
Signed-off-by: Angel Misevski <[email protected]>
1 parent b735b99 commit 29a7c9c

7 files changed

+276
-14
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) 2019-2022 Red Hat, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package overrides
15+
16+
import (
17+
"fmt"
18+
"os"
19+
"path/filepath"
20+
"testing"
21+
22+
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
23+
"github.com/google/go-cmp/cmp"
24+
"github.com/stretchr/testify/assert"
25+
corev1 "k8s.io/api/core/v1"
26+
"sigs.k8s.io/yaml"
27+
)
28+
29+
func TestApplyContainerOverrides(t *testing.T) {
30+
tests := loadAllContainerTestCasesOrPanic(t, "testdata/container-overrides")
31+
for _, tt := range tests {
32+
t.Run(fmt.Sprintf("%s (%s)", tt.Name, tt.originalFilename), func(t *testing.T) {
33+
outContainer, err := ApplyContainerOverrides(tt.Input.Component, tt.Input.Container)
34+
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
35+
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
36+
} else {
37+
if !assert.NoError(t, err, "Should not return error") {
38+
return
39+
}
40+
assert.Truef(t, cmp.Equal(tt.Output.Container, outContainer),
41+
"Container should match expected output:\n%s",
42+
cmp.Diff(tt.Output.Container, outContainer))
43+
}
44+
})
45+
}
46+
}
47+
48+
type containerTestCase struct {
49+
Name string `json:"name,omitempty"`
50+
Input *containerTestInput `json:"input,omitempty"`
51+
Output *containerTestOutput `json:"output,omitempty"`
52+
originalFilename string
53+
}
54+
55+
type containerTestInput struct {
56+
Component *dw.Component `json:"component,omitempty"`
57+
Container *corev1.Container `json:"container,omitempty"`
58+
}
59+
60+
type containerTestOutput struct {
61+
Container *corev1.Container `json:"container,omitempty"`
62+
ErrRegexp *string `json:"errRegexp,omitempty"`
63+
}
64+
65+
func loadAllContainerTestCasesOrPanic(t *testing.T, fromDir string) []containerTestCase {
66+
files, err := os.ReadDir(fromDir)
67+
if err != nil {
68+
t.Fatal(err)
69+
}
70+
var tests []containerTestCase
71+
for _, file := range files {
72+
if file.IsDir() {
73+
tests = append(tests, loadAllContainerTestCasesOrPanic(t, filepath.Join(fromDir, file.Name()))...)
74+
} else {
75+
tests = append(tests, loadContainerTestCaseOrPanic(t, filepath.Join(fromDir, file.Name())))
76+
}
77+
}
78+
return tests
79+
}
80+
81+
func loadContainerTestCaseOrPanic(t *testing.T, testPath string) containerTestCase {
82+
bytes, err := os.ReadFile(testPath)
83+
if err != nil {
84+
t.Fatal(err)
85+
}
86+
var test containerTestCase
87+
if err := yaml.Unmarshal(bytes, &test); err != nil {
88+
t.Fatal(err)
89+
}
90+
test.originalFilename = testPath
91+
return test
92+
}

pkg/library/overrides/pods_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
)
3333

3434
func TestApplyPodOverrides(t *testing.T) {
35-
tests := loadAllTestCasesOrPanic(t, "testdata")
35+
tests := loadAllPodTestCasesOrPanic(t, "testdata/pod-overrides")
3636
for _, tt := range tests {
3737
t.Run(fmt.Sprintf("%s (%s)", tt.Name, tt.originalFilename), func(t *testing.T) {
3838
workspace := &common.DevWorkspaceWithConfig{}
@@ -55,7 +55,7 @@ func TestApplyPodOverrides(t *testing.T) {
5555
}
5656
}
5757

58-
func TestNeedsOverride(t *testing.T) {
58+
func TestNeedsPodOverride(t *testing.T) {
5959
jsonPodOverrides := apiext.JSON{
6060
Raw: []byte(`{"spec":{"runtimeClassName":"kata"}}`),
6161
}
@@ -173,45 +173,45 @@ func TestNeedsOverride(t *testing.T) {
173173
}
174174
}
175175

176-
type testCase struct {
177-
Name string `json:"name,omitempty"`
178-
Input *testInput `json:"input,omitempty"`
179-
Output *testOutput `json:"output,omitempty"`
176+
type podTestCase struct {
177+
Name string `json:"name,omitempty"`
178+
Input *podTestInput `json:"input,omitempty"`
179+
Output *podTestOutput `json:"output,omitempty"`
180180
originalFilename string
181181
}
182182

183-
type testInput struct {
183+
type podTestInput struct {
184184
Workspace *dw.DevWorkspaceTemplateSpec `json:"workspace,omitempty"`
185185
PodTemplateSpec *corev1.PodTemplateSpec `json:"podTemplateSpec,omitempty"`
186186
}
187187

188-
type testOutput struct {
188+
type podTestOutput struct {
189189
PodTemplateSpec *corev1.PodTemplateSpec `json:"podTemplateSpec,omitempty"`
190190
ErrRegexp *string `json:"errRegexp,omitempty"`
191191
}
192192

193-
func loadAllTestCasesOrPanic(t *testing.T, fromDir string) []testCase {
193+
func loadAllPodTestCasesOrPanic(t *testing.T, fromDir string) []podTestCase {
194194
files, err := os.ReadDir(fromDir)
195195
if err != nil {
196196
t.Fatal(err)
197197
}
198-
var tests []testCase
198+
var tests []podTestCase
199199
for _, file := range files {
200200
if file.IsDir() {
201-
tests = append(tests, loadAllTestCasesOrPanic(t, filepath.Join(fromDir, file.Name()))...)
201+
tests = append(tests, loadAllPodTestCasesOrPanic(t, filepath.Join(fromDir, file.Name()))...)
202202
} else {
203-
tests = append(tests, loadTestCaseOrPanic(t, filepath.Join(fromDir, file.Name())))
203+
tests = append(tests, loadPodTestCaseOrPanic(t, filepath.Join(fromDir, file.Name())))
204204
}
205205
}
206206
return tests
207207
}
208208

209-
func loadTestCaseOrPanic(t *testing.T, testPath string) testCase {
209+
func loadPodTestCaseOrPanic(t *testing.T, testPath string) podTestCase {
210210
bytes, err := os.ReadFile(testPath)
211211
if err != nil {
212212
t.Fatal(err)
213213
}
214-
var test testCase
214+
var test podTestCase
215215
if err := yaml.Unmarshal(bytes, &test); err != nil {
216216
t.Fatal(err)
217217
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: "Container overrides cannot override container component fields"
2+
3+
input:
4+
component:
5+
name: test-component
6+
attributes:
7+
container-overrides:
8+
image: override-image
9+
command: ["test"]
10+
args: ["test"]
11+
ports:
12+
- name: test-port
13+
containerPort: 9999
14+
volumeMounts:
15+
- name: test-volume
16+
mountPath: test-mountPath
17+
env:
18+
- name: test_env
19+
value: test_val
20+
container:
21+
image: test-image
22+
container:
23+
name: test-component
24+
image: test-image
25+
command: ["original"]
26+
args: ["original"]
27+
ports:
28+
- name: original-port
29+
containerPort: 8080
30+
volumeMounts:
31+
- name: original-volume
32+
mountPath: original-mountPath
33+
env:
34+
- name: original_env
35+
value: original_val
36+
37+
38+
output:
39+
container:
40+
name: test-component
41+
image: test-image
42+
command: ["original"]
43+
args: ["original"]
44+
ports:
45+
- name: original-port
46+
containerPort: 8080
47+
volumeMounts:
48+
- name: original-volume
49+
mountPath: original-mountPath
50+
env:
51+
- name: original_env
52+
value: original_val
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: "Applies overrides from container-overrides attribute as json"
2+
3+
input:
4+
component:
5+
name: test-component
6+
attributes:
7+
container-overrides: {"resources":{"limits":{"nvidia.com/gpu":"1"}}}
8+
container:
9+
image: test-image
10+
container:
11+
name: test-component
12+
image: test-image
13+
14+
output:
15+
container:
16+
name: test-component
17+
image: test-image
18+
resources:
19+
limits:
20+
nvidia.com/gpu: "1"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: "Applies overrides from container-overrides attribute"
2+
3+
input:
4+
component:
5+
name: test-component
6+
attributes:
7+
container-overrides:
8+
resources:
9+
limits:
10+
nvidia.com/gpu: "1"
11+
requests:
12+
nvidia.com/gpu: "1"
13+
readinessProbe:
14+
exec:
15+
command: ["echo", "hello"]
16+
securityContext:
17+
runAsUser: 1000
18+
runAsGroup: 3000
19+
fsGroup: 2000
20+
container:
21+
image: test-image
22+
container:
23+
name: test-component
24+
image: test-image
25+
26+
output:
27+
container:
28+
name: test-component
29+
image: test-image
30+
resources:
31+
limits:
32+
nvidia.com/gpu: "1"
33+
requests:
34+
nvidia.com/gpu: "1"
35+
readinessProbe:
36+
exec:
37+
command: ["echo", "hello"]
38+
securityContext:
39+
runAsUser: 1000
40+
runAsGroup: 3000
41+
fsGroup: 2000
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: "Resources from overrides are merged with container-defined resources"
2+
3+
input:
4+
component:
5+
name: test-component
6+
attributes:
7+
container-overrides:
8+
resources:
9+
limits:
10+
nvidia.com/gpu: "1"
11+
requests:
12+
nvidia.com/gpu: "1"
13+
container:
14+
image: test-image
15+
memoryLimit: 1Gi
16+
memoryRequest: 256Mi
17+
cpuLimit: 1000m
18+
cpuRequest: 500m
19+
container:
20+
name: test-component
21+
image: test-image
22+
resources:
23+
limits:
24+
memory: 1Gi
25+
cpu: 1000m
26+
requests:
27+
memory: 256Mi
28+
cpu: 500m
29+
30+
output:
31+
container:
32+
name: test-component
33+
image: test-image
34+
resources:
35+
limits:
36+
nvidia.com/gpu: "1"
37+
memory: 1Gi
38+
cpu: 1000m
39+
requests:
40+
nvidia.com/gpu: "1"
41+
memory: 256Mi
42+
cpu: 500m
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: "Returns an error when container-override attribute cannot be parsed"
2+
3+
input:
4+
component:
5+
name: test-component
6+
attributes:
7+
container-overrides: 123
8+
container:
9+
image: test-image
10+
container:
11+
name: test-component
12+
image: test-image
13+
14+
output:
15+
errRegexp: "failed to parse .* attribute on component test-component.*"

0 commit comments

Comments
 (0)