Skip to content

Commit

Permalink
Add tests to cover container-overrides attribute
Browse files Browse the repository at this point in the history
Signed-off-by: Angel Misevski <amisevsk@redhat.com>
  • Loading branch information
amisevsk committed Oct 19, 2022
1 parent 8435a33 commit fde20a4
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 14 deletions.
92 changes: 92 additions & 0 deletions pkg/library/overrides/containers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) 2019-2022 Red Hat, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package overrides

import (
"fmt"
"os"
"path/filepath"
"testing"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
)

func TestApplyContainerOverrides(t *testing.T) {
tests := loadAllContainerTestCasesOrPanic(t, "testdata/container-overrides")
for _, tt := range tests {
t.Run(fmt.Sprintf("%s (%s)", tt.Name, tt.originalFilename), func(t *testing.T) {
outContainer, err := ApplyContainerOverrides(tt.Input.Component, tt.Input.Container)
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.Container, outContainer),
"Container should match expected output:\n%s",
cmp.Diff(tt.Output.Container, outContainer))
}
})
}
}

type containerTestCase struct {
Name string `json:"name,omitempty"`
Input *containerTestInput `json:"input,omitempty"`
Output *containerTestOutput `json:"output,omitempty"`
originalFilename string
}

type containerTestInput struct {
Component *dw.Component `json:"component,omitempty"`
Container *corev1.Container `json:"container,omitempty"`
}

type containerTestOutput struct {
Container *corev1.Container `json:"container,omitempty"`
ErrRegexp *string `json:"errRegexp,omitempty"`
}

func loadAllContainerTestCasesOrPanic(t *testing.T, fromDir string) []containerTestCase {
files, err := os.ReadDir(fromDir)
if err != nil {
t.Fatal(err)
}
var tests []containerTestCase
for _, file := range files {
if file.IsDir() {
tests = append(tests, loadAllContainerTestCasesOrPanic(t, filepath.Join(fromDir, file.Name()))...)
} else {
tests = append(tests, loadContainerTestCaseOrPanic(t, filepath.Join(fromDir, file.Name())))
}
}
return tests
}

func loadContainerTestCaseOrPanic(t *testing.T, testPath string) containerTestCase {
bytes, err := os.ReadFile(testPath)
if err != nil {
t.Fatal(err)
}
var test containerTestCase
if err := yaml.Unmarshal(bytes, &test); err != nil {
t.Fatal(err)
}
test.originalFilename = testPath
return test
}
28 changes: 14 additions & 14 deletions pkg/library/overrides/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

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

func TestNeedsOverride(t *testing.T) {
func TestNeedsPodOverride(t *testing.T) {
jsonPodOverrides := apiext.JSON{
Raw: []byte(`{"spec":{"runtimeClassName":"kata"}}`),
}
Expand Down Expand Up @@ -173,45 +173,45 @@ func TestNeedsOverride(t *testing.T) {
}
}

type testCase struct {
Name string `json:"name,omitempty"`
Input *testInput `json:"input,omitempty"`
Output *testOutput `json:"output,omitempty"`
type podTestCase struct {
Name string `json:"name,omitempty"`
Input *podTestInput `json:"input,omitempty"`
Output *podTestOutput `json:"output,omitempty"`
originalFilename string
}

type testInput struct {
type podTestInput struct {
Workspace *dw.DevWorkspaceTemplateSpec `json:"workspace,omitempty"`
PodTemplateSpec *corev1.PodTemplateSpec `json:"podTemplateSpec,omitempty"`
}

type testOutput struct {
type podTestOutput struct {
PodTemplateSpec *corev1.PodTemplateSpec `json:"podTemplateSpec,omitempty"`
ErrRegexp *string `json:"errRegexp,omitempty"`
}

func loadAllTestCasesOrPanic(t *testing.T, fromDir string) []testCase {
func loadAllPodTestCasesOrPanic(t *testing.T, fromDir string) []podTestCase {
files, err := os.ReadDir(fromDir)
if err != nil {
t.Fatal(err)
}
var tests []testCase
var tests []podTestCase
for _, file := range files {
if file.IsDir() {
tests = append(tests, loadAllTestCasesOrPanic(t, filepath.Join(fromDir, file.Name()))...)
tests = append(tests, loadAllPodTestCasesOrPanic(t, filepath.Join(fromDir, file.Name()))...)
} else {
tests = append(tests, loadTestCaseOrPanic(t, filepath.Join(fromDir, file.Name())))
tests = append(tests, loadPodTestCaseOrPanic(t, filepath.Join(fromDir, file.Name())))
}
}
return tests
}

func loadTestCaseOrPanic(t *testing.T, testPath string) testCase {
func loadPodTestCaseOrPanic(t *testing.T, testPath string) podTestCase {
bytes, err := os.ReadFile(testPath)
if err != nil {
t.Fatal(err)
}
var test testCase
var test podTestCase
if err := yaml.Unmarshal(bytes, &test); err != nil {
t.Fatal(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: "Container overrides cannot override container component fields"

input:
component:
name: test-component
attributes:
container-overrides:
image: override-image
command: ["test"]
args: ["test"]
ports:
- name: test-port
containerPort: 9999
volumeMounts:
- name: test-volume
mountPath: test-mountPath
env:
- name: test_env
value: test_val
container:
image: test-image
container:
name: test-component
image: test-image
command: ["original"]
args: ["original"]
ports:
- name: original-port
containerPort: 8080
volumeMounts:
- name: original-volume
mountPath: original-mountPath
env:
- name: original_env
value: original_val


output:
container:
name: test-component
image: test-image
command: ["original"]
args: ["original"]
ports:
- name: original-port
containerPort: 8080
volumeMounts:
- name: original-volume
mountPath: original-mountPath
env:
- name: original_env
value: original_val
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Applies overrides from container-overrides attribute as json"

input:
component:
name: test-component
attributes:
container-overrides: {"resources":{"limits":{"nvidia.com/gpu":"1"}}}
container:
image: test-image
container:
name: test-component
image: test-image

output:
container:
name: test-component
image: test-image
resources:
limits:
nvidia.com/gpu: "1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: "Applies overrides from container-overrides attribute"

input:
component:
name: test-component
attributes:
container-overrides:
resources:
limits:
nvidia.com/gpu: "1"
requests:
nvidia.com/gpu: "1"
readinessProbe:
exec:
command: ["echo", "hello"]
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
container:
image: test-image
container:
name: test-component
image: test-image

output:
container:
name: test-component
image: test-image
resources:
limits:
nvidia.com/gpu: "1"
requests:
nvidia.com/gpu: "1"
readinessProbe:
exec:
command: ["echo", "hello"]
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "Resources from overrides are merged with container-defined resources"

input:
component:
name: test-component
attributes:
container-overrides:
resources:
limits:
nvidia.com/gpu: "1"
requests:
nvidia.com/gpu: "1"
container:
image: test-image
memoryLimit: 1Gi
memoryRequest: 256Mi
cpuLimit: 1000m
cpuRequest: 500m
container:
name: test-component
image: test-image
resources:
limits:
memory: 1Gi
cpu: 1000m
requests:
memory: 256Mi
cpu: 500m

output:
container:
name: test-component
image: test-image
resources:
limits:
nvidia.com/gpu: "1"
memory: 1Gi
cpu: 1000m
requests:
nvidia.com/gpu: "1"
memory: 256Mi
cpu: 500m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: "Returns an error when container-override attribute cannot be parsed"

input:
component:
name: test-component
attributes:
container-overrides: 123
container:
image: test-image
container:
name: test-component
image: test-image

output:
errRegexp: "failed to parse .* attribute on component test-component.*"

0 comments on commit fde20a4

Please sign in to comment.