Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
382 changes: 382 additions & 0 deletions test/functional/ansibletest_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
//revive:disable-next-line:dot-imports
. "github.com/openstack-k8s-operators/lib-common/modules/common/test/helpers"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

Expand Down Expand Up @@ -116,4 +117,385 @@ var _ = Describe("AnsibleTest controller", func() {
Expect(pod.Name).ToNot(BeEmpty())
})
})

When("AnsibleTest is created with extraMounts", func() {
BeforeEach(func() {
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)
Expect(k8sClient.Create(ctx, openstackConfigMap)).Should(Succeed())
Expect(k8sClient.Create(ctx, openstackSecret)).Should(Succeed())

testOperatorConfigMap := CreateTestOperatorConfigMap(namespace)
Expect(k8sClient.Create(ctx, testOperatorConfigMap)).Should(Succeed())

extraConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "extra-config",
Namespace: namespace,
},
Data: map[string]string{
"config.conf": "key=value",
},
}
Expect(k8sClient.Create(ctx, extraConfigMap)).Should(Succeed())

spec := GetDefaultAnsibleTestSpec()
spec["extraMounts"] = []map[string]any{
{
"extraVol": []map[string]any{
{
"propagation": []string{"AnsibleTest"},
"volumes": []map[string]any{
{
"name": "extra-config-vol",
"configMap": map[string]any{
"name": "extra-config",
},
},
},
"mounts": []map[string]any{
{
"name": "extra-config-vol",
"mountPath": "/etc/extra-config",
"readOnly": true,
},
},
},
},
},
}
DeferCleanup(th.DeleteInstance, CreateAnsibleTest(ansibleTestName, spec))
})

It("should add extra volume and volumeMount to the pod", func() {
pod := GetTestOperatorPod(namespace, ansibleTestName.Name)

foundVolume := false
for _, vol := range pod.Spec.Volumes {
if vol.Name == "extra-config-vol" {
foundVolume = true
Expect(vol.VolumeSource.ConfigMap).NotTo(BeNil())
Expect(vol.VolumeSource.ConfigMap.Name).To(Equal("extra-config"))
break
}
}
Expect(foundVolume).To(BeTrue(), "expected pod to have volume 'extra-config-vol'")

container := pod.Spec.Containers[0]
foundMount := false
for _, mount := range container.VolumeMounts {
if mount.Name == "extra-config-vol" {
foundMount = true
Expect(mount.MountPath).To(Equal("/etc/extra-config"))
Expect(mount.ReadOnly).To(BeTrue())
break
}
}
Expect(foundMount).To(BeTrue(), "expected container to have volumeMount 'extra-config-vol'")
})
})

When("AnsibleTest is created with Secret as the source of extraMount", func() {
BeforeEach(func() {
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)
Expect(k8sClient.Create(ctx, openstackConfigMap)).Should(Succeed())
Expect(k8sClient.Create(ctx, openstackSecret)).Should(Succeed())

testOperatorConfigMap := CreateTestOperatorConfigMap(namespace)
Expect(k8sClient.Create(ctx, testOperatorConfigMap)).Should(Succeed())

extraSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "extra-secret",
Namespace: namespace,
},
StringData: map[string]string{
"secret.conf": "key=value",
},
}
Expect(k8sClient.Create(ctx, extraSecret)).Should(Succeed())

spec := GetDefaultAnsibleTestSpec()
spec["extraMounts"] = []map[string]any{
{
"extraVol": []map[string]any{
{
"propagation": []string{"AnsibleTest"},
"volumes": []map[string]any{
{
"name": "extra-secret-vol",
"secret": map[string]any{
"secretName": "extra-secret",
},
},
},
"mounts": []map[string]any{
{
"name": "extra-secret-vol",
"mountPath": "/etc/extra-secret",
"readOnly": true,
},
},
},
},
},
}
DeferCleanup(th.DeleteInstance, CreateAnsibleTest(ansibleTestName, spec))
})

It("should add secret based extra volume and volumeMount to the pod", func() {
pod := GetTestOperatorPod(namespace, ansibleTestName.Name)

foundVolume := false
for _, vol := range pod.Spec.Volumes {
if vol.Name == "extra-secret-vol" {
foundVolume = true
Expect(vol.VolumeSource.Secret).NotTo(BeNil())
Expect(vol.VolumeSource.Secret.SecretName).To(Equal("extra-secret"))
break
}
}
Expect(foundVolume).To(BeTrue(), "expected pod to have volume 'extra-secret-vol'")

container := pod.Spec.Containers[0]
foundMount := false
for _, mount := range container.VolumeMounts {
if mount.Name == "extra-secret-vol" {
foundMount = true
Expect(mount.MountPath).To(Equal("/etc/extra-secret"))
Expect(mount.ReadOnly).To(BeTrue())
break
}
}
Expect(foundMount).To(BeTrue(), "expected container to have volumeMount 'extra-secret-vol'")
})
})

When("AnsibleTest is created with multiple extraMounts", func() {
BeforeEach(func() {
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)
Expect(k8sClient.Create(ctx, openstackConfigMap)).Should(Succeed())
Expect(k8sClient.Create(ctx, openstackSecret)).Should(Succeed())

testOperatorConfigMap := CreateTestOperatorConfigMap(namespace)
Expect(k8sClient.Create(ctx, testOperatorConfigMap)).Should(Succeed())

extraConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "extra-config",
Namespace: namespace,
},
Data: map[string]string{
"config.conf": "key=value",
},
}
Expect(k8sClient.Create(ctx, extraConfigMap)).Should(Succeed())

extraDataMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "extra-data",
Namespace: namespace,
},
Data: map[string]string{
"data.conf": "key=value",
},
}
Expect(k8sClient.Create(ctx, extraDataMap)).Should(Succeed())

spec := GetDefaultAnsibleTestSpec()
spec["extraMounts"] = []map[string]any{
{
"extraVol": []map[string]any{
{
"propagation": []string{"AnsibleTest"},
"volumes": []map[string]any{
{
"name": "extra-config-vol",
"configMap": map[string]any{
"name": "extra-config",
},
},
{
"name": "extra-data-vol",
"configMap": map[string]any{
"name": "extra-data",
},
},
},
"mounts": []map[string]any{
{
"name": "extra-config-vol",
"mountPath": "/etc/extra-config",
"readOnly": true,
},
{
"name": "extra-data-vol",
"mountPath": "/etc/extra-data",
"readOnly": true,
},
},
},
},
},
}
DeferCleanup(th.DeleteInstance, CreateAnsibleTest(ansibleTestName, spec))
})

It("should add all extra volumes and volumeMounts to the pod", func() {
pod := GetTestOperatorPod(namespace, ansibleTestName.Name)

volumeNames := []string{}
for _, vol := range pod.Spec.Volumes {
volumeNames = append(volumeNames, vol.Name)
}
Expect(volumeNames).To(ContainElement("extra-config-vol"))
Expect(volumeNames).To(ContainElement("extra-data-vol"))

mountNames := []string{}
container := pod.Spec.Containers[0]
for _, mount := range container.VolumeMounts {
mountNames = append(mountNames, mount.Name)
}
Expect(mountNames).To(ContainElement("extra-config-vol"))
Expect(mountNames).To(ContainElement("extra-data-vol"))
})
})

When("AnsibleTest is created with no propagation field", func() {
BeforeEach(func() {
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)
Expect(k8sClient.Create(ctx, openstackConfigMap)).Should(Succeed())
Expect(k8sClient.Create(ctx, openstackSecret)).Should(Succeed())

testOperatorConfigMap := CreateTestOperatorConfigMap(namespace)
Expect(k8sClient.Create(ctx, testOperatorConfigMap)).Should(Succeed())

extraConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "extra-config",
Namespace: namespace,
},
Data: map[string]string{
"config.conf": "key=value",
},
}
Expect(k8sClient.Create(ctx, extraConfigMap)).Should(Succeed())

spec := GetDefaultAnsibleTestSpec()
spec["extraMounts"] = []map[string]any{
{
"extraVol": []map[string]any{
{
"volumes": []map[string]any{
{
"name": "extra-config-vol",
"configMap": map[string]any{
"name": "extra-config",
},
},
},
"mounts": []map[string]any{
{
"name": "extra-config-vol",
"mountPath": "/etc/extra-config",
"readOnly": true,
},
},
},
},
},
}
DeferCleanup(th.DeleteInstance, CreateAnsibleTest(ansibleTestName, spec))
})

It("should add extra volume and volumeMount when propagation is omitted", func() {
pod := GetTestOperatorPod(namespace, ansibleTestName.Name)

foundVolume := false
for _, vol := range pod.Spec.Volumes {
if vol.Name == "extra-config-vol" {
foundVolume = true
Expect(vol.VolumeSource.ConfigMap).NotTo(BeNil())
Expect(vol.VolumeSource.ConfigMap.Name).To(Equal("extra-config"))
break
}
}
Expect(foundVolume).To(BeTrue(), "expected pod to have volume 'extra-config-vol'")

container := pod.Spec.Containers[0]
foundMount := false
for _, mount := range container.VolumeMounts {
if mount.Name == "extra-config-vol" {
foundMount = true
Expect(mount.MountPath).To(Equal("/etc/extra-config"))
Expect(mount.ReadOnly).To(BeTrue())
break
}
}
Expect(foundMount).To(BeTrue(), "expected container to have volumeMount 'extra-config-vol'")
})
})

When("AnsibleTest created with extraMounts using the wrong propagation type", func() {
BeforeEach(func() {
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)
Expect(k8sClient.Create(ctx, openstackConfigMap)).Should(Succeed())
Expect(k8sClient.Create(ctx, openstackSecret)).Should(Succeed())

testOperatorConfigMap := CreateTestOperatorConfigMap(namespace)
Expect(k8sClient.Create(ctx, testOperatorConfigMap)).Should(Succeed())

extraConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "extra-config",
Namespace: namespace,
},
Data: map[string]string{
"config.conf": "key=value",
},
}
Expect(k8sClient.Create(ctx, extraConfigMap)).Should(Succeed())

spec := GetDefaultAnsibleTestSpec()
spec["extraMounts"] = []map[string]any{
{
"extraVol": []map[string]any{
{
"propagation": []string{"Tempest"},
"volumes": []map[string]any{
{
"name": "extra-config-vol",
"configMap": map[string]any{
"name": "extra-config",
},
},
},
"mounts": []map[string]any{
{
"name": "extra-config-vol",
"mountPath": "/etc/extra-config",
"readOnly": true,
},
},
},
},
},
}
DeferCleanup(th.DeleteInstance, CreateAnsibleTest(ansibleTestName, spec))
})

It("should not add extra volume and volumeMount to the pod", func() {
pod := GetTestOperatorPod(namespace, ansibleTestName.Name)
for _, vol := range pod.Spec.Volumes {
Expect(vol.Name).NotTo(Equal("extra-config-vol"),
"volume should not be propagated with wrong propagation type")
}

container := pod.Spec.Containers[0]
for _, mount := range container.VolumeMounts {
Expect(mount.Name).NotTo(Equal("extra-config-vol"),
"volumeMount should not be propagated with wrong propagation type")
}
})
})

})
Loading
Loading