Skip to content

Commit e78fd62

Browse files
Merge pull request #456 from posikoya/posikoya/env-tests-extraMounts
add envtests for extraMounts feature
2 parents 4643f96 + 792d6a1 commit e78fd62

5 files changed

Lines changed: 570 additions & 0 deletions

File tree

test/functional/ansibletest_controller_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,112 @@ var _ = Describe("AnsibleTest controller", func() {
116116
Expect(pod.Name).ToNot(BeEmpty())
117117
})
118118
})
119+
120+
Context("extraMounts", func() {
121+
BeforeEach(func() {
122+
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)
123+
Expect(k8sClient.Create(ctx, openstackConfigMap)).Should(Succeed())
124+
Expect(k8sClient.Create(ctx, openstackSecret)).Should(Succeed())
125+
126+
testOperatorConfigMap := CreateTestOperatorConfigMap(namespace)
127+
Expect(k8sClient.Create(ctx, testOperatorConfigMap)).Should(Succeed())
128+
})
129+
130+
When("AnsibleTest is created with extraMounts", func() {
131+
BeforeEach(func() {
132+
CreateExtraConfigMap(namespace, ExtraConfigMapName)
133+
134+
spec := GetDefaultAnsibleTestSpec()
135+
spec["extraMounts"] = BuildExtraMountsSpec("AnsibleTest",
136+
GetDefaultConfigMapExtraMount())
137+
138+
DeferCleanup(th.DeleteInstance, CreateAnsibleTest(ansibleTestName, spec))
139+
})
140+
141+
It("should add extra volume and volumeMount to the pod", func() {
142+
pod := GetTestOperatorPod(namespace, ansibleTestName.Name)
143+
ExpectPodHasConfigMapVolume(pod, ExtraConfigVolName, ExtraConfigMapName)
144+
ExpectPodHasVolumeMount(pod, ExtraConfigVolName, ExtraConfigMountPath)
145+
})
146+
})
147+
148+
When("AnsibleTest is created with Secret as the source of extraMount", func() {
149+
BeforeEach(func() {
150+
CreateExtraSecret(namespace, ExtraSecretName)
151+
152+
spec := GetDefaultAnsibleTestSpec()
153+
spec["extraMounts"] = BuildExtraMountsSpec("AnsibleTest",
154+
GetDefaultSecretExtraMount())
155+
156+
DeferCleanup(th.DeleteInstance, CreateAnsibleTest(ansibleTestName, spec))
157+
})
158+
159+
It("should add secret based extra volume and volumeMount to the pod", func() {
160+
pod := GetTestOperatorPod(namespace, ansibleTestName.Name)
161+
ExpectPodHasSecretVolume(pod, ExtraSecretVolName, ExtraSecretName)
162+
ExpectPodHasVolumeMount(pod, ExtraSecretVolName, ExtraSecretMountPath)
163+
})
164+
})
165+
166+
When("AnsibleTest is created with multiple extraMounts configmap and secret", func() {
167+
BeforeEach(func() {
168+
CreateExtraConfigMap(namespace, ExtraConfigMapName)
169+
CreateExtraSecret(namespace, ExtraSecretName)
170+
171+
spec := GetDefaultAnsibleTestSpec()
172+
spec["extraMounts"] = BuildExtraMountsSpec("AnsibleTest",
173+
GetDefaultConfigMapExtraMount(),
174+
GetDefaultSecretExtraMount(),
175+
)
176+
177+
DeferCleanup(th.DeleteInstance, CreateAnsibleTest(ansibleTestName, spec))
178+
})
179+
180+
It("should add all extra volumes and volumeMounts to the pod", func() {
181+
pod := GetTestOperatorPod(namespace, ansibleTestName.Name)
182+
ExpectPodHasConfigMapVolume(pod, ExtraConfigVolName, ExtraConfigMapName)
183+
ExpectPodHasSecretVolume(pod, ExtraSecretVolName, ExtraSecretName)
184+
ExpectPodHasVolumeMount(pod, ExtraConfigVolName, ExtraConfigMountPath)
185+
ExpectPodHasVolumeMount(pod, ExtraSecretVolName, ExtraSecretMountPath)
186+
})
187+
})
188+
189+
When("AnsibleTest is created with no propagation field", func() {
190+
BeforeEach(func() {
191+
CreateExtraConfigMap(namespace, ExtraConfigMapName)
192+
193+
spec := GetDefaultAnsibleTestSpec()
194+
spec["extraMounts"] = BuildExtraMountsSpec("",
195+
GetDefaultConfigMapExtraMount())
196+
197+
DeferCleanup(th.DeleteInstance, CreateAnsibleTest(ansibleTestName, spec))
198+
})
199+
200+
It("should add extra volume and volumeMount when propagation is omitted", func() {
201+
pod := GetTestOperatorPod(namespace, ansibleTestName.Name)
202+
ExpectPodHasConfigMapVolume(pod, ExtraConfigVolName, ExtraConfigMapName)
203+
ExpectPodHasVolumeMount(pod, ExtraConfigVolName, ExtraConfigMountPath)
204+
})
205+
})
206+
207+
When("AnsibleTest created with extraMounts is using the wrong propagation type", func() {
208+
BeforeEach(func() {
209+
CreateExtraConfigMap(namespace, ExtraConfigMapName)
210+
211+
spec := GetDefaultAnsibleTestSpec()
212+
spec["extraMounts"] = BuildExtraMountsSpec("Tempest",
213+
GetDefaultConfigMapExtraMount())
214+
215+
DeferCleanup(th.DeleteInstance, CreateAnsibleTest(ansibleTestName, spec))
216+
})
217+
218+
It("should not add extra volume and volumeMount to the pod", func() {
219+
pod := GetTestOperatorPod(namespace, ansibleTestName.Name)
220+
ExpectPodNotHasVolume(pod, ExtraConfigVolName)
221+
ExpectPodNotHasVolumeMount(pod, ExtraConfigVolName)
222+
})
223+
})
224+
225+
})
226+
119227
})

test/functional/base_test.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,21 @@ const (
3636
DefaultStorageClass = "local-storage"
3737
DefaultComputeSSHKeySecret = "dataplane-ansible-ssh-private-key-secret" // #nosec G101
3838
DefaultWorkloadSSHKeySecret = "dataplane-ansible-ssh-private-key-secret" // #nosec G101
39+
ExtraConfigMapName = "extra-config"
40+
ExtraConfigVolName = "extra-config-vol"
41+
ExtraConfigMountPath = "/etc/extra-config"
42+
ExtraSecretName = "extra-secret" // #nosec G101
43+
ExtraSecretVolName = "extra-secret-vol" // #nosec G101
44+
ExtraSecretMountPath = "/etc/extra-secret" // #nosec G101
3945
)
4046

47+
type ExtraMount struct {
48+
VolName string
49+
SourceName string // ConfigMap or Secret name
50+
MountPath string
51+
IsSecret bool
52+
}
53+
4154
func CreateUnstructured(rawObj map[string]any) *unstructured.Unstructured {
4255
logger.Info("Creating", "raw", rawObj)
4356
unstructuredObj := &unstructured.Unstructured{Object: rawObj}
@@ -87,6 +100,136 @@ func CreateTestOperatorConfigMap(namespace string) *corev1.ConfigMap {
87100
}
88101
}
89102

103+
func CreateExtraConfigMap(namespace string, name string) {
104+
cm := &corev1.ConfigMap{
105+
ObjectMeta: metav1.ObjectMeta{
106+
Name: name,
107+
Namespace: namespace,
108+
},
109+
Data: map[string]string{
110+
"config.conf": "key=value",
111+
},
112+
}
113+
Expect(k8sClient.Create(ctx, cm)).Should(Succeed())
114+
}
115+
116+
func CreateExtraSecret(namespace string, name string) {
117+
secret := &corev1.Secret{
118+
ObjectMeta: metav1.ObjectMeta{
119+
Name: name,
120+
Namespace: namespace,
121+
},
122+
StringData: map[string]string{
123+
"secret.conf": "key=value",
124+
},
125+
}
126+
Expect(k8sClient.Create(ctx, secret)).Should(Succeed())
127+
}
128+
129+
func BuildExtraMountsSpec(propagation string, mounts ...ExtraMount) []map[string]any {
130+
volumes := []map[string]any{}
131+
volumeMounts := []map[string]any{}
132+
133+
for _, m := range mounts {
134+
vol := map[string]any{"name": m.VolName}
135+
if m.IsSecret {
136+
vol["secret"] = map[string]any{"secretName": m.SourceName}
137+
} else {
138+
vol["configMap"] = map[string]any{"name": m.SourceName}
139+
}
140+
volumes = append(volumes, vol)
141+
142+
volumeMounts = append(volumeMounts, map[string]any{
143+
"name": m.VolName,
144+
"mountPath": m.MountPath,
145+
"readOnly": true,
146+
})
147+
}
148+
149+
extraVol := map[string]any{
150+
"volumes": volumes,
151+
"mounts": volumeMounts,
152+
}
153+
if propagation != "" {
154+
extraVol["propagation"] = []string{propagation}
155+
}
156+
return []map[string]any{
157+
{"extraVol": []map[string]any{extraVol}},
158+
}
159+
}
160+
161+
func ExpectPodHasConfigMapVolume(pod *corev1.Pod, volName string, cmName string) {
162+
foundVolume := false
163+
for _, vol := range pod.Spec.Volumes {
164+
if vol.Name == volName {
165+
foundVolume = true
166+
Expect(vol.VolumeSource.ConfigMap).NotTo(BeNil())
167+
Expect(vol.VolumeSource.ConfigMap.Name).To(Equal(cmName))
168+
break
169+
}
170+
}
171+
Expect(foundVolume).To(BeTrue(), "expected pod to have volume '%s'", volName)
172+
}
173+
174+
func ExpectPodHasSecretVolume(pod *corev1.Pod, volName string, secretName string) {
175+
foundVolume := false
176+
for _, vol := range pod.Spec.Volumes {
177+
if vol.Name == volName {
178+
foundVolume = true
179+
Expect(vol.VolumeSource.Secret).NotTo(BeNil())
180+
Expect(vol.VolumeSource.Secret.SecretName).To(Equal(secretName))
181+
break
182+
}
183+
}
184+
Expect(foundVolume).To(BeTrue(), "expected pod to have volume '%s'", volName)
185+
}
186+
187+
func ExpectPodHasVolumeMount(pod *corev1.Pod, volName string, mountPath string) {
188+
container := pod.Spec.Containers[0]
189+
foundMount := false
190+
for _, mount := range container.VolumeMounts {
191+
if mount.Name == volName {
192+
foundMount = true
193+
Expect(mount.MountPath).To(Equal(mountPath))
194+
Expect(mount.ReadOnly).To(BeTrue())
195+
break
196+
}
197+
}
198+
Expect(foundMount).To(BeTrue(), "expected container to have volumeMount '%s'", volName)
199+
}
200+
201+
func ExpectPodNotHasVolume(pod *corev1.Pod, volName string) {
202+
for _, vol := range pod.Spec.Volumes {
203+
Expect(vol.Name).NotTo(Equal(volName),
204+
"volume should not be propagated with wrong propagation type")
205+
}
206+
}
207+
208+
func ExpectPodNotHasVolumeMount(pod *corev1.Pod, volName string) {
209+
container := pod.Spec.Containers[0]
210+
for _, mount := range container.VolumeMounts {
211+
Expect(mount.Name).NotTo(Equal(volName),
212+
"volumeMount should not be propagated with wrong propagation type")
213+
}
214+
}
215+
216+
func GetDefaultConfigMapExtraMount() ExtraMount {
217+
return ExtraMount{
218+
VolName: ExtraConfigVolName,
219+
SourceName: ExtraConfigMapName,
220+
MountPath: ExtraConfigMountPath,
221+
}
222+
}
223+
224+
func GetDefaultSecretExtraMount() ExtraMount {
225+
return ExtraMount{
226+
VolName: ExtraSecretVolName,
227+
SourceName: ExtraSecretName,
228+
MountPath: ExtraSecretMountPath,
229+
IsSecret: true,
230+
}
231+
}
232+
90233
func GetTestOperatorPVC(namespace string, instanceName string) *corev1.PersistentVolumeClaim {
91234
var pvc corev1.PersistentVolumeClaim
92235
Eventually(func(g Gomega) {

test/functional/horizontest_controller_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,111 @@ var _ = Describe("HorizonTest controller", func() {
118118
Expect(pod.Name).ToNot(BeEmpty())
119119
})
120120
})
121+
122+
Context("extraMounts", func() {
123+
BeforeEach(func() {
124+
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)
125+
Expect(k8sClient.Create(ctx, openstackConfigMap)).Should(Succeed())
126+
Expect(k8sClient.Create(ctx, openstackSecret)).Should(Succeed())
127+
128+
testOperatorConfigMap := CreateTestOperatorConfigMap(namespace)
129+
Expect(k8sClient.Create(ctx, testOperatorConfigMap)).Should(Succeed())
130+
})
131+
132+
When("HorizonTest is created with extraMounts", func() {
133+
BeforeEach(func() {
134+
CreateExtraConfigMap(namespace, ExtraConfigMapName)
135+
136+
spec := GetDefaultHorizonTestSpec()
137+
spec["extraMounts"] = BuildExtraMountsSpec("HorizonTest",
138+
GetDefaultConfigMapExtraMount())
139+
140+
DeferCleanup(th.DeleteInstance, CreateHorizonTest(horizonTestName, spec))
141+
})
142+
143+
It("should add extra volume and volumeMount to the pod", func() {
144+
pod := GetTestOperatorPod(namespace, horizonTestName.Name)
145+
ExpectPodHasConfigMapVolume(pod, ExtraConfigVolName, ExtraConfigMapName)
146+
ExpectPodHasVolumeMount(pod, ExtraConfigVolName, ExtraConfigMountPath)
147+
})
148+
})
149+
150+
When("HorizonTest is created with Secret as the source of extraMount", func() {
151+
BeforeEach(func() {
152+
CreateExtraSecret(namespace, ExtraSecretName)
153+
154+
spec := GetDefaultHorizonTestSpec()
155+
spec["extraMounts"] = BuildExtraMountsSpec("HorizonTest",
156+
GetDefaultSecretExtraMount())
157+
158+
DeferCleanup(th.DeleteInstance, CreateHorizonTest(horizonTestName, spec))
159+
})
160+
161+
It("should add secret based extra volume and volumeMount to the pod", func() {
162+
pod := GetTestOperatorPod(namespace, horizonTestName.Name)
163+
ExpectPodHasSecretVolume(pod, ExtraSecretVolName, ExtraSecretName)
164+
ExpectPodHasVolumeMount(pod, ExtraSecretVolName, ExtraSecretMountPath)
165+
})
166+
})
167+
168+
When("HorizonTest is created with multiple extraMounts configmap and secret", func() {
169+
BeforeEach(func() {
170+
CreateExtraConfigMap(namespace, ExtraConfigMapName)
171+
CreateExtraSecret(namespace, ExtraSecretName)
172+
173+
spec := GetDefaultHorizonTestSpec()
174+
spec["extraMounts"] = BuildExtraMountsSpec("HorizonTest",
175+
GetDefaultConfigMapExtraMount(),
176+
GetDefaultSecretExtraMount(),
177+
)
178+
179+
DeferCleanup(th.DeleteInstance, CreateHorizonTest(horizonTestName, spec))
180+
})
181+
182+
It("should add all extra volumes and volumeMounts to the pod", func() {
183+
pod := GetTestOperatorPod(namespace, horizonTestName.Name)
184+
ExpectPodHasConfigMapVolume(pod, ExtraConfigVolName, ExtraConfigMapName)
185+
ExpectPodHasSecretVolume(pod, ExtraSecretVolName, ExtraSecretName)
186+
ExpectPodHasVolumeMount(pod, ExtraConfigVolName, ExtraConfigMountPath)
187+
ExpectPodHasVolumeMount(pod, ExtraSecretVolName, ExtraSecretMountPath)
188+
})
189+
})
190+
191+
When("HorizonTest is created with no propagation field", func() {
192+
BeforeEach(func() {
193+
CreateExtraConfigMap(namespace, ExtraConfigMapName)
194+
195+
spec := GetDefaultHorizonTestSpec()
196+
spec["extraMounts"] = BuildExtraMountsSpec("",
197+
GetDefaultConfigMapExtraMount())
198+
199+
DeferCleanup(th.DeleteInstance, CreateHorizonTest(horizonTestName, spec))
200+
})
201+
202+
It("should add extra volume and volumeMount when propagation is omitted", func() {
203+
pod := GetTestOperatorPod(namespace, horizonTestName.Name)
204+
ExpectPodHasConfigMapVolume(pod, ExtraConfigVolName, ExtraConfigMapName)
205+
ExpectPodHasVolumeMount(pod, ExtraConfigVolName, ExtraConfigMountPath)
206+
})
207+
})
208+
209+
When("HorizonTest created with extraMounts is using the wrong propagation type", func() {
210+
BeforeEach(func() {
211+
CreateExtraConfigMap(namespace, ExtraConfigMapName)
212+
213+
spec := GetDefaultHorizonTestSpec()
214+
spec["extraMounts"] = BuildExtraMountsSpec("Tempest",
215+
GetDefaultConfigMapExtraMount())
216+
217+
DeferCleanup(th.DeleteInstance, CreateHorizonTest(horizonTestName, spec))
218+
})
219+
220+
It("should not add extra volume and volumeMount to the pod", func() {
221+
pod := GetTestOperatorPod(namespace, horizonTestName.Name)
222+
ExpectPodNotHasVolume(pod, ExtraConfigVolName)
223+
ExpectPodNotHasVolumeMount(pod, ExtraConfigVolName)
224+
})
225+
})
226+
})
227+
121228
})

0 commit comments

Comments
 (0)