Skip to content

Commit 0c5f4ee

Browse files
committed
Add more envTests to increase coverage
The last PR regarding adding envTests added simple envTests for all controllers. This PR adds more specific tests and aims to increase test coverage and inlude more scenarios.
1 parent 9cc6562 commit 0c5f4ee

5 files changed

Lines changed: 226 additions & 16 deletions

File tree

test/functional/ansibletest_controller_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ var _ = Describe("AnsibleTest controller", func() {
3737
}
3838
})
3939

40+
DescribeTable("Missing Openstack resources should set InputReady to false",
41+
func(createResource func()) {
42+
createResource()
43+
DeferCleanup(th.DeleteInstance, CreateAnsibleTest(ansibleTestName, GetDefaultAnsibleTestSpec()))
44+
45+
th.ExpectCondition(
46+
ansibleTestName,
47+
ConditionGetterFunc(AnsibleTestConditionGetter),
48+
condition.InputReadyCondition,
49+
corev1.ConditionFalse,
50+
)
51+
},
52+
Entry("when config map is missing", func() {
53+
_, secret := CreateCommonOpenstackResources(namespace)
54+
Expect(k8sClient.Create(ctx, secret)).Should(Succeed())
55+
}),
56+
Entry("when secret is missing", func() {
57+
cm, _ := CreateCommonOpenstackResources(namespace)
58+
Expect(k8sClient.Create(ctx, cm)).Should(Succeed())
59+
}),
60+
)
61+
4062
When("An AnsibleTest instance is created", func() {
4163
BeforeEach(func() {
4264
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)

test/functional/base_test.go

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,50 @@ func CreateCommonOpenstackResources(namespace string) (*corev1.ConfigMap, *corev
7171
return cm, secret
7272
}
7373

74+
func GetTestOperatorPVC(namespace string, instanceName string) *corev1.PersistentVolumeClaim {
75+
var pvc corev1.PersistentVolumeClaim
76+
Eventually(func(g Gomega) {
77+
pvcList := &corev1.PersistentVolumeClaimList{}
78+
listOpts := []client.ListOption{
79+
client.InNamespace(namespace),
80+
client.MatchingLabels{
81+
"instanceName": instanceName,
82+
"operator": "test-operator",
83+
},
84+
}
85+
g.Expect(k8sClient.List(ctx, pvcList, listOpts...)).Should(Succeed())
86+
g.Expect(pvcList.Items).ToNot(BeEmpty())
87+
pvc = pvcList.Items[0]
88+
}, timeout*2, interval).Should(Succeed())
89+
return &pvc
90+
}
91+
92+
func GetTestOperatorPod(namespace string, instanceName string) *corev1.Pod {
93+
var pod corev1.Pod
94+
Eventually(func(g Gomega) {
95+
podList := &corev1.PodList{}
96+
listOpts := []client.ListOption{
97+
client.InNamespace(namespace),
98+
client.MatchingLabels{
99+
"instanceName": instanceName,
100+
"operator": "test-operator",
101+
},
102+
}
103+
g.Expect(k8sClient.List(ctx, podList, listOpts...)).Should(Succeed())
104+
g.Expect(podList.Items).To(HaveLen(1))
105+
pod = podList.Items[0]
106+
}, timeout*2, interval).Should(Succeed())
107+
return &pod
108+
}
109+
110+
func SimulatePodSucceeded(podName types.NamespacedName) {
111+
Eventually(func(g Gomega) {
112+
pod := th.GetPod(podName)
113+
pod.Status.Phase = corev1.PodSucceeded
114+
g.Expect(k8sClient.Status().Update(ctx, pod)).Should(Succeed())
115+
}, timeout*3, interval).Should(Succeed())
116+
}
117+
74118
// AnsibleTest helpers
75119
func CreateAnsibleTest(name types.NamespacedName, spec map[string]any) client.Object {
76120
raw := map[string]any{
@@ -95,9 +139,9 @@ func GetAnsibleTest(name types.NamespacedName) *testv1.AnsibleTest {
95139

96140
func GetDefaultAnsibleTestSpec() map[string]any {
97141
return map[string]any{
98-
"storageClass": "local-storage",
99-
"openStackConfigMap": "openstack-config",
100-
"openStackConfigSecret": "openstack-config-secret",
142+
"storageClass": DefaultStorageClass,
143+
"openStackConfigMap": OpenStackConfigMapName,
144+
"openStackConfigSecret": OpenStackConfigSecretName,
101145
"ansibleGitRepo": "https://github.com/example/test-repo",
102146
"ansiblePlaybookPath": "tests/playbook.yaml",
103147
}
@@ -132,7 +176,7 @@ func GetHorizonTest(name types.NamespacedName) *testv1.HorizonTest {
132176

133177
func GetDefaultHorizonTestSpec() map[string]any {
134178
return map[string]any{
135-
"storageClass": "local-storage",
179+
"storageClass": DefaultStorageClass,
136180
"adminUsername": "admin",
137181
"adminPassword": "password",
138182
"dashboardUrl": "http://horizon.example.com",
@@ -169,9 +213,9 @@ func GetTempest(name types.NamespacedName) *testv1.Tempest {
169213

170214
func GetDefaultTempestSpec() map[string]any {
171215
return map[string]any{
172-
"storageClass": "local-storage",
173-
"openStackConfigMap": "openstack-config",
174-
"openStackConfigSecret": "openstack-config-secret",
216+
"storageClass": DefaultStorageClass,
217+
"openStackConfigMap": OpenStackConfigMapName,
218+
"openStackConfigSecret": OpenStackConfigSecretName,
175219
"tempestRun": map[string]any{
176220
"includeList": "tempest.api.identity.v3",
177221
},
@@ -207,7 +251,7 @@ func GetTobiko(name types.NamespacedName) *testv1.Tobiko {
207251

208252
func GetDefaultTobikoSpec() map[string]any {
209253
return map[string]any{
210-
"storageClass": "local-storage",
254+
"storageClass": DefaultStorageClass,
211255
"testenv": "sanity",
212256
}
213257
}

test/functional/horizontest_controller_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ var _ = Describe("HorizonTest controller", func() {
3737
}
3838
})
3939

40+
DescribeTable("Missing Openstack resources should set InputReady to false",
41+
func(createResource func()) {
42+
createResource()
43+
DeferCleanup(th.DeleteInstance, CreateHorizonTest(horizonTestName, GetDefaultHorizonTestSpec()))
44+
45+
th.ExpectCondition(
46+
horizonTestName,
47+
ConditionGetterFunc(HorizonTestConditionGetter),
48+
condition.InputReadyCondition,
49+
corev1.ConditionFalse,
50+
)
51+
},
52+
Entry("when config map is missing", func() {
53+
_, secret := CreateCommonOpenstackResources(namespace)
54+
Expect(k8sClient.Create(ctx, secret)).Should(Succeed())
55+
}),
56+
Entry("when secret is missing", func() {
57+
cm, _ := CreateCommonOpenstackResources(namespace)
58+
Expect(k8sClient.Create(ctx, cm)).Should(Succeed())
59+
}),
60+
)
61+
4062
When("A HorizonTest instance is created", func() {
4163
BeforeEach(func() {
4264
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)

test/functional/tempest_controller_test.go

Lines changed: 108 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package functional_test
1818

1919
import (
20+
"fmt"
21+
2022
. "github.com/onsi/ginkgo/v2" //revive:disable:dot-imports
2123
. "github.com/onsi/gomega" //revive:disable:dot-imports
2224

@@ -37,7 +39,29 @@ var _ = Describe("Tempest controller", func() {
3739
}
3840
})
3941

40-
When("A Tempest intance is created", func() {
42+
DescribeTable("Missing Openstack resources should set InputReady to false",
43+
func(createResource func()) {
44+
createResource()
45+
DeferCleanup(th.DeleteInstance, CreateTempest(tempestName, GetDefaultTempestSpec()))
46+
47+
th.ExpectCondition(
48+
tempestName,
49+
ConditionGetterFunc(TempestConditionGetter),
50+
condition.InputReadyCondition,
51+
corev1.ConditionFalse,
52+
)
53+
},
54+
Entry("when config map is missing", func() {
55+
_, secret := CreateCommonOpenstackResources(namespace)
56+
Expect(k8sClient.Create(ctx, secret)).Should(Succeed())
57+
}),
58+
Entry("when secret is missing", func() {
59+
cm, _ := CreateCommonOpenstackResources(namespace)
60+
Expect(k8sClient.Create(ctx, cm)).Should(Succeed())
61+
}),
62+
)
63+
64+
When("A Tempest instance is created", func() {
4165
BeforeEach(func() {
4266
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)
4367
Expect(k8sClient.Create(ctx, openstackConfigMap)).Should(Succeed())
@@ -52,13 +76,18 @@ var _ = Describe("Tempest controller", func() {
5276
}, timeout*2, interval).Should(Succeed())
5377
})
5478

55-
It("is not ready", func() {
56-
th.ExpectCondition(
57-
tempestName,
58-
ConditionGetterFunc(TempestConditionGetter),
59-
condition.ReadyCondition,
60-
corev1.ConditionUnknown,
61-
)
79+
It("should have the Spec fields initialized", func() {
80+
tempest := GetTempest(tempestName)
81+
Expect(tempest.Spec.StorageClass).Should(Equal(DefaultStorageClass))
82+
Expect(tempest.Spec.OpenStackConfigMap).Should(Equal(OpenStackConfigMapName))
83+
Expect(tempest.Spec.OpenStackConfigSecret).Should(Equal(OpenStackConfigSecretName))
84+
Expect(tempest.Spec.TempestRun.IncludeList).ShouldNot(BeEmpty())
85+
})
86+
87+
It("should have empty Status fields", func() {
88+
tempest := GetTempest(tempestName)
89+
Expect(tempest.Status.Hash).To(BeEmpty())
90+
Expect(tempest.Status.NetworkAttachments).To(BeEmpty())
6291
})
6392

6493
It("should have a finalizer", func() {
@@ -69,4 +98,75 @@ var _ = Describe("Tempest controller", func() {
6998
}, timeout, interval).Should(ContainElement("openstack.org/tempest"))
7099
})
71100
})
101+
102+
When("All dependencies are ready", func() {
103+
var customDataConfigMapName string
104+
var envVarsConfigMapName string
105+
106+
BeforeEach(func() {
107+
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)
108+
Expect(k8sClient.Create(ctx, openstackConfigMap)).Should(Succeed())
109+
Expect(k8sClient.Create(ctx, openstackSecret)).Should(Succeed())
110+
DeferCleanup(th.DeleteInstance, CreateTempest(tempestName, GetDefaultTempestSpec()))
111+
112+
customDataConfigMapName = fmt.Sprintf("%s-custom-data-s0", tempestName.Name)
113+
envVarsConfigMapName = fmt.Sprintf("%s-env-vars-s0", tempestName.Name)
114+
})
115+
116+
It("should have InputReady condition true", func() {
117+
th.ExpectCondition(
118+
tempestName,
119+
ConditionGetterFunc(TempestConditionGetter),
120+
condition.InputReadyCondition,
121+
corev1.ConditionTrue,
122+
)
123+
})
124+
125+
It("should create a PVC for logs", func() {
126+
pvc := GetTestOperatorPVC(namespace, tempestName.Name)
127+
Expect(pvc.Name).ToNot(BeEmpty())
128+
Expect(*pvc.Spec.StorageClassName).To(Equal(DefaultStorageClass))
129+
Expect(pvc.Spec.AccessModes).To(ContainElement(corev1.ReadWriteOnce))
130+
})
131+
132+
It("should create required ConfigMaps", func() {
133+
customDataCM := th.GetConfigMap(types.NamespacedName{
134+
Namespace: namespace,
135+
Name: customDataConfigMapName,
136+
})
137+
Expect(customDataCM.Data).To(HaveKey("include.txt"))
138+
139+
envVarsCM := th.GetConfigMap(types.NamespacedName{
140+
Namespace: namespace,
141+
Name: envVarsConfigMapName,
142+
})
143+
Expect(envVarsCM.Data).NotTo(BeEmpty())
144+
})
145+
146+
It("should create a pod", func() {
147+
pod := GetTestOperatorPod(namespace, tempestName.Name)
148+
Expect(pod.Name).ToNot(BeEmpty())
149+
})
150+
151+
It("should become Ready when pod succeeds", func() {
152+
pod := GetTestOperatorPod(namespace, tempestName.Name)
153+
SimulatePodSucceeded(types.NamespacedName{
154+
Namespace: namespace,
155+
Name: pod.Name,
156+
})
157+
158+
th.ExpectCondition(
159+
tempestName,
160+
ConditionGetterFunc(TempestConditionGetter),
161+
condition.DeploymentReadyCondition,
162+
corev1.ConditionTrue,
163+
)
164+
th.ExpectCondition(
165+
tempestName,
166+
ConditionGetterFunc(TempestConditionGetter),
167+
condition.ReadyCondition,
168+
corev1.ConditionTrue,
169+
)
170+
})
171+
})
72172
})

test/functional/tobiko_controller_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ var _ = Describe("Tobiko controller", func() {
3737
}
3838
})
3939

40+
DescribeTable("Missing Openstack resources should set InputReady to false",
41+
func(createResource func()) {
42+
createResource()
43+
DeferCleanup(th.DeleteInstance, CreateTobiko(tobikoName, GetDefaultTobikoSpec()))
44+
45+
th.ExpectCondition(
46+
tobikoName,
47+
ConditionGetterFunc(TobikoConditionGetter),
48+
condition.InputReadyCondition,
49+
corev1.ConditionFalse,
50+
)
51+
},
52+
Entry("when config map is missing", func() {
53+
_, secret := CreateCommonOpenstackResources(namespace)
54+
Expect(k8sClient.Create(ctx, secret)).Should(Succeed())
55+
}),
56+
Entry("when secret is missing", func() {
57+
cm, _ := CreateCommonOpenstackResources(namespace)
58+
Expect(k8sClient.Create(ctx, cm)).Should(Succeed())
59+
}),
60+
)
61+
4062
When("A Tobiko instance is created", func() {
4163
BeforeEach(func() {
4264
openstackConfigMap, openstackSecret := CreateCommonOpenstackResources(namespace)

0 commit comments

Comments
 (0)