Skip to content

Commit 7d16c97

Browse files
authored
Merge pull request #411 from stuggi/testhelper_volumes
[testhelper] add test helpers to assert volume and volumeMounts
2 parents b8867c3 + ccc58aa commit 7d16c97

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2023 Red Hat
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package helpers
15+
16+
import (
17+
"github.com/onsi/gomega"
18+
19+
corev1 "k8s.io/api/core/v1"
20+
)
21+
22+
// AssertVolumeExists - asserts the existence of a named volume in a []corev1.Volume.
23+
//
24+
// Example usage:
25+
//
26+
// th.AssertVolumeExists("name", []corev1.Volume{...})
27+
func (tc *TestHelper) AssertVolumeExists(name string, volumes []corev1.Volume) {
28+
gomega.Expect(volumes).To(gomega.ContainElement(gomega.HaveField("Name", name)))
29+
}
30+
31+
// AssertVolumeMountExists - asserts the existence of a named volumeMount with a subPath (if provided) in a []corev1.VolumeMount.
32+
//
33+
// Example usage:
34+
//
35+
// th.AssertVolumeMountExists("name", "subPath", []corev1.VolumeMount{...})
36+
func (tc *TestHelper) AssertVolumeMountExists(name string, subPath string, volumeMounts []corev1.VolumeMount) bool {
37+
exist := false
38+
for _, v := range volumeMounts {
39+
if v.Name == name {
40+
if subPath != "" {
41+
if v.SubPath == subPath {
42+
exist = true
43+
}
44+
} else {
45+
exist = true
46+
}
47+
}
48+
}
49+
gomega.Expect(exist).To(gomega.BeTrue())
50+
51+
return exist
52+
}

0 commit comments

Comments
 (0)