-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test cases for init-persistent-home container automounts
Signed-off-by: David Kwon <[email protected]>
- Loading branch information
Showing
5 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...ibrary/lifecycle/testdata/prestart/persistent_home_initcontainer_first_initcontainer.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
38 changes: 38 additions & 0 deletions
38
...library/lifecycle/testdata/prestart/persistent_home_initcontainer_only_initcontainer.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
45 changes: 45 additions & 0 deletions
45
...brary/lifecycle/testdata/prestart/persistent_home_initcontainer_second_initcontainer.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
|
||
}) | ||
} | ||
} |