Skip to content

Commit 3cb2ae8

Browse files
authored
Merge pull request #621 from fmount/envtest-em
[envTest] Add MountPath check in AssertVolumeMountExists
2 parents 0acec6a + 11c997e commit 3cb2ae8

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

modules/common/test/helpers/volumes.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,69 @@ func (tc *TestHelper) AssertVolumeMountExists(name string, subPath string, volum
5050

5151
return exist
5252
}
53+
54+
// AssertVolumeMountPathExists - Returns true if mountPath and subPath exist in
55+
// the volumeMounts array.
56+
// Assumptions:
57+
// - an empty []corev1.VolumeMount results in a failure
58+
// - an empty "name" results in a failure
59+
// - when no mountPath and no subPath are passed (empty string) it results in a
60+
// failure
61+
// - if no mountPath is provided it is skipped during the evaluation and
62+
// - if no subPath is provided it is skipped during the evaluation
63+
// - when both mountPath and subPath are passed, it returns true only when
64+
// both are found in the volumeMount array
65+
func (tc *TestHelper) AssertVolumeMountPathExists(
66+
name string,
67+
mountPath string,
68+
subPath string,
69+
volumeMounts []corev1.VolumeMount,
70+
) bool {
71+
// Early return for an empty volumeMounts list
72+
if len(volumeMounts) == 0 {
73+
gomega.Expect(false).To(gomega.BeTrue(), "Volume mounts list is empty")
74+
return false
75+
}
76+
77+
// Early return for an empty VolumeMount name
78+
if name == "" {
79+
gomega.Expect(false).To(gomega.BeTrue(), "Volume name cannot be empty")
80+
return false
81+
}
82+
83+
// Early return if both mountPath and subPath are empty strings
84+
if mountPath == "" && subPath == "" {
85+
gomega.Expect(false).To(gomega.BeTrue(), "Both mountPath and subPath are empty")
86+
return false
87+
}
88+
89+
// init two bool variables to represent the status of mountPath and
90+
// subPath check: when the function reaches this point we have at least an
91+
// input that should be checked
92+
mountPathExists := mountPath == ""
93+
subPathExists := subPath == ""
94+
95+
for _, v := range volumeMounts {
96+
// only process the current volumeMount if the name
97+
// mathes
98+
if v.Name == name {
99+
// mountPath is passed as input and it has not
100+
// been evaluated
101+
if v.MountPath == mountPath && !mountPathExists {
102+
mountPathExists = true
103+
}
104+
// subPath is passed as input and it has not
105+
// been evaluated
106+
if v.SubPath == subPath && !subPathExists {
107+
subPathExists = true
108+
}
109+
}
110+
// check if we can break early and optimize the
111+
// iterations
112+
if mountPathExists && subPathExists {
113+
break
114+
}
115+
}
116+
gomega.Expect(mountPathExists && subPathExists).To(gomega.BeTrue())
117+
return (mountPathExists && subPathExists)
118+
}

0 commit comments

Comments
 (0)