Skip to content

Commit

Permalink
Add test cases for init-persistent-home container automounts
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <[email protected]>
  • Loading branch information
dkwon17 committed Jun 21, 2024
1 parent c8d7eb4 commit 9874cd4
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/library/lifecycle/prestart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func TestGetInitContainers(t *testing.T) {
loadPreStartTestCaseOrPanic(t, "prestart_exec_command.yaml"),
loadPreStartTestCaseOrPanic(t, "prestart_apply_command.yaml"),
loadPreStartTestCaseOrPanic(t, "init_and_main_container.yaml"),
loadPreStartTestCaseOrPanic(t, "persistent_home_initcontainer_first_initcontainer.yaml"),
loadPreStartTestCaseOrPanic(t, "persistent_home_initcontainer_second_initcontainer.yaml"),
loadPreStartTestCaseOrPanic(t, "persistent_home_initcontainer_only_initcontainer.yaml"),
}

for _, tt := range tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: "Should set the init-persistent-home init container when init-persistent-home is the first preStart event"

input:
components:
- name: test-container1
container:
image: my-image
- name: test-container2
container:
image: my-image
- name: init-persistent-home
container:
image: my-image
commands:
- id: test_preStart_command
apply:
component: test-container1
- id: test_regular_command
exec:
component: test-container1
command: "test_command"
- id: init-persistent-home
apply:
component: init-persistent-home
events:
preStart:
- "init-persistent-home"
- "test_preStart_command"

output:
initContainers:
- name: init-persistent-home
container:
image: my-image
- name: test-container1
container:
image: my-image
mainContainers:
- name: test-container1
container:
image: my-image
- name: test-container2
container:
image: my-image
errRegexp:
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: "Should set the init-persistent-home init container when it is the only init container"

input:
components:
- name: test-container1
container:
image: my-image
- name: test-container2
container:
image: my-image
- name: init-persistent-home
container:
image: my-image
commands:
- id: test_regular_command
exec:
component: test-container1
command: "test_command"
- id: init-persistent-home
apply:
component: init-persistent-home
events:
preStart:
- "init-persistent-home"

output:
initContainers:
- name: init-persistent-home
container:
image: my-image
mainContainers:
- name: test-container1
container:
image: my-image
- name: test-container2
container:
image: my-image
errRegexp:
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: "Should set the init-persistent-home init container when init-persistent-home is the second preStart event"

input:
components:
- name: test-container1
container:
image: my-image
- name: test-container2
container:
image: my-image
- name: init-persistent-home
container:
image: my-image
commands:
- id: test_preStart_command
apply:
component: test-container1
- id: test_regular_command
exec:
component: test-container1
command: "test_command"
- id: init-persistent-home
apply:
component: init-persistent-home
events:
preStart:
- "test_preStart_command"
- "init-persistent-home"

output:
initContainers:
- name: init-persistent-home
container:
image: my-image
- name: test-container1
container:
image: my-image
mainContainers:
- name: test-container1
container:
image: my-image
- name: test-container2
container:
image: my-image
errRegexp:
93 changes: 93 additions & 0 deletions pkg/provision/automount/common_persistenthome_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// Copyright (c) 2019-2024 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 automount

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
)

func TestProvisionAutomountResourcesIntoPersistentHomeEnabled(t *testing.T) {
tests := []testCase{
loadTestCaseOrPanic(t, "testdata/testProvisionsConfigmaps.yaml"),
loadTestCaseOrPanic(t, "testdata/testProvisionsProjectedVolumes.yaml"),
loadTestCaseOrPanic(t, "testdata/testProvisionsSecrets.yaml"),
}

testPodAdditions := &v1alpha1.PodAdditions{
Containers: []corev1.Container{{
Name: "test-container",
Image: "test-image",
}},
InitContainers: []corev1.Container{{
Name: "init-persistent-home",
Image: "test-image",
}, {
Name: "test-container",
Image: "test-image",
}},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("%s (%s)", tt.Name, tt.TestPath), func(t *testing.T) {
podAdditions := testPodAdditions.DeepCopy()
testAPI := sync.ClusterAPI{
Client: fake.NewClientBuilder().WithObjects(tt.Input.allObjects...).Build(),
}

err := ProvisionAutoMountResourcesInto(podAdditions, testAPI, testNamespace, true)

if !assert.NoError(t, err, "Unexpected error") {
return
}
assert.Truef(t, cmp.Equal(tt.Output.Volumes, podAdditions.Volumes, testDiffOpts),
"Volumes should match expected output:\n%s",
cmp.Diff(tt.Output.Volumes, podAdditions.Volumes, testDiffOpts))

for _, container := range podAdditions.Containers {
assert.Truef(t, cmp.Equal(tt.Output.VolumeMounts, container.VolumeMounts, testDiffOpts),
"Container VolumeMounts should match expected output:\n%s",
cmp.Diff(tt.Output.VolumeMounts, container.VolumeMounts, testDiffOpts))
assert.Truef(t, cmp.Equal(tt.Output.EnvFrom, container.EnvFrom, testDiffOpts),
"Container EnvFrom should match expected output:\n%s",
cmp.Diff(tt.Output.EnvFrom, container.EnvFrom, testDiffOpts))
}

for _, container := range podAdditions.InitContainers {
if container.Name == "init-persistent-home" {
assert.Truef(t, container.VolumeMounts == nil || len(container.VolumeMounts) == 0,
"The init-persistent-home container should not have any volume mounts if persistent home is enabled")
} else {
assert.Truef(t, cmp.Equal(tt.Output.VolumeMounts, container.VolumeMounts, testDiffOpts),
"Container VolumeMounts should match expected output:\n%s",
cmp.Diff(tt.Output.VolumeMounts, container.VolumeMounts, testDiffOpts))
assert.Truef(t, cmp.Equal(tt.Output.EnvFrom, container.EnvFrom, testDiffOpts),
"Container EnvFrom should match expected output:\n%s",
cmp.Diff(tt.Output.EnvFrom, container.EnvFrom, testDiffOpts))
}
}

})
}
}

0 comments on commit 9874cd4

Please sign in to comment.