Skip to content

Commit 07444da

Browse files
JustinKuliopenshift-ci[bot]
authored andcommitted
Improve tests
There were many instances where `unstructured.Nested*` methods could be used for better safety (preventing panics if types failed to be converted). Additionally, this removes the 5 minute wait for the new config-policy rollout, in favor of an `Eventually` section that doesn't wait for the pod to become fully Ready. We really only care how it's configured, which we can check without that wait. Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent d9d1d88 commit 07444da

File tree

2 files changed

+58
-62
lines changed

2 files changed

+58
-62
lines changed

test/e2e/case1_framework_deployment_test.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ var _ = Describe("Test framework deployment", func() {
9292

9393
checkContainersAndAvailability(cluster, 0)
9494

95+
// Adding this annotation and later verifying the cluster namespace is not removed checks
96+
// that the helm values annotation and the logging level annotation are stackable.
9597
By(logPrefix + "annotating the managedclusteraddon with the " + loggingLevelAnnotation + " annotation")
9698
Kubectl("annotate", "-n", cluster.clusterName, "-f", case1ManagedClusterAddOnCR, loggingLevelAnnotation)
9799

@@ -125,6 +127,8 @@ var _ = Describe("Test framework deployment", func() {
125127

126128
checkContainersAndAvailability(cluster, 0)
127129

130+
// Adding this annotation and later verifying the cluster namespace is not removed checks
131+
// that the multiclusterhub annotation and the logging level annotation are stackable.
128132
By(logPrefix + "annotating the managedclusteraddon with the " + loggingLevelAnnotation + " annotation")
129133
Kubectl("annotate", "-n", cluster.clusterName, "-f", case1ManagedClusterAddOnCR, loggingLevelAnnotation)
130134

@@ -319,10 +323,10 @@ func checkContainersAndAvailability(cluster managedClusterConfig, clusterIdx int
319323
Eventually(func() int {
320324
deploy := GetWithTimeout(cluster.clusterClient, gvrDeployment,
321325
case1DeploymentName, addonNamespace, true, 30)
322-
spec := deploy.Object["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"]
323-
containers := spec.(map[string]interface{})["containers"]
324326

325-
return len(containers.([]interface{}))
327+
containers, _, _ := unstructured.NestedSlice(deploy.Object, "spec", "template", "spec", "containers")
328+
329+
return len(containers)
326330
}, 60, 1).Should(Equal(desiredContainerCount))
327331

328332
if startupProbeInCluster(clusterIdx) {
@@ -331,11 +335,18 @@ func checkContainersAndAvailability(cluster managedClusterConfig, clusterIdx int
331335
deploy := GetWithTimeout(
332336
cluster.clusterClient, gvrDeployment, case1DeploymentName, addonNamespace, true, 30,
333337
)
334-
status := deploy.Object["status"]
335-
replicas := status.(map[string]interface{})["replicas"]
336-
availableReplicas := status.(map[string]interface{})["availableReplicas"]
337338

338-
return (availableReplicas != nil) && replicas.(int64) == availableReplicas.(int64)
339+
replicas, found, err := unstructured.NestedInt64(deploy.Object, "status", "replicas")
340+
if !found || err != nil {
341+
return false
342+
}
343+
344+
available, found, err := unstructured.NestedInt64(deploy.Object, "status", "availableReplicas")
345+
if !found || err != nil {
346+
return false
347+
}
348+
349+
return available == replicas
339350
}, 240, 1).Should(Equal(true))
340351
}
341352

@@ -345,9 +356,10 @@ func checkContainersAndAvailability(cluster managedClusterConfig, clusterIdx int
345356
LabelSelector: case1PodSelector,
346357
}
347358
pods := ListWithTimeoutByNamespace(cluster.clusterClient, gvrPod, opts, addonNamespace, 1, true, 30)
348-
phase := pods.Items[0].Object["status"].(map[string]interface{})["phase"]
349359

350-
return phase.(string) == "Running"
360+
phase, _, _ := unstructured.NestedString(pods.Items[0].Object, "status", "phase")
361+
362+
return phase == "Running"
351363
}, 60, 1).Should(Equal(true))
352364

353365
By(logPrefix + "showing the framework managedclusteraddon as available")

test/e2e/case2_config_deployment_test.go

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
package e2e
44

55
import (
6-
"fmt"
7-
86
. "github.com/onsi/ginkgo/v2"
97
. "github.com/onsi/gomega"
108
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -33,10 +31,9 @@ var _ = Describe("Test config-policy-controller deployment", func() {
3331
deploy = GetWithTimeout(
3432
cluster.clusterClient, gvrDeployment, case2DeploymentName, addonNamespace, true, 30,
3533
)
36-
spec := deploy.Object["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"]
37-
containers := spec.(map[string]interface{})["containers"]
34+
containers, _, _ := unstructured.NestedSlice(deploy.Object, "spec", "template", "spec", "containers")
3835

39-
return len(containers.([]interface{}))
36+
return len(containers)
4037
}, 60, 1).Should(Equal(1))
4138

4239
if startupProbeInCluster(i) {
@@ -45,11 +42,18 @@ var _ = Describe("Test config-policy-controller deployment", func() {
4542
deploy = GetWithTimeout(
4643
cluster.clusterClient, gvrDeployment, case2DeploymentName, addonNamespace, true, 30,
4744
)
48-
status := deploy.Object["status"]
49-
replicas := status.(map[string]interface{})["replicas"]
50-
availableReplicas := status.(map[string]interface{})["availableReplicas"]
5145

52-
return (availableReplicas != nil) && replicas.(int64) == availableReplicas.(int64)
46+
replicas, found, err := unstructured.NestedInt64(deploy.Object, "status", "replicas")
47+
if !found || err != nil {
48+
return false
49+
}
50+
51+
available, found, err := unstructured.NestedInt64(deploy.Object, "status", "availableReplicas")
52+
if !found || err != nil {
53+
return false
54+
}
55+
56+
return available == replicas
5357
}, 240, 1).Should(Equal(true))
5458
}
5559

@@ -59,9 +63,10 @@ var _ = Describe("Test config-policy-controller deployment", func() {
5963
LabelSelector: case2PodSelector,
6064
}
6165
pods := ListWithTimeoutByNamespace(cluster.clusterClient, gvrPod, opts, addonNamespace, 1, true, 30)
62-
phase := pods.Items[0].Object["status"].(map[string]interface{})["phase"]
6366

64-
return phase.(string) == "Running"
67+
phase, _, _ := unstructured.NestedString(pods.Items[0].Object, "status", "phase")
68+
69+
return phase == "Running"
6570
}, 60, 1).Should(Equal(true))
6671

6772
By(logPrefix + "showing the config-policy-controller managedclusteraddon as available")
@@ -84,7 +89,7 @@ var _ = Describe("Test config-policy-controller deployment", func() {
8489
})
8590

8691
It("should create a config-policy-controller deployment with custom logging levels and concurrency", func() {
87-
for i, cluster := range managedClusterList {
92+
for _, cluster := range managedClusterList {
8893
logPrefix := cluster.clusterType + " " + cluster.clusterName + ": "
8994
By(logPrefix + "deploying the default config-policy-controller managedclusteraddon")
9095
Kubectl("apply", "-n", cluster.clusterName, "-f", case2ManagedClusterAddOnCR)
@@ -117,54 +122,33 @@ var _ = Describe("Test config-policy-controller deployment", func() {
117122
evaluationConcurrencyAnnotation,
118123
)
119124

120-
By(logPrefix + "restarting the config-policy-controller deployment")
121-
Kubectl(
122-
"-n",
123-
addonNamespace,
124-
"rollout",
125-
"restart",
126-
"deployments/config-policy-controller",
127-
fmt.Sprintf("--kubeconfig=%s%d.kubeconfig", kubeconfigFilename, i+1),
128-
)
129-
Kubectl(
130-
"-n",
131-
addonNamespace,
132-
"rollout",
133-
"status",
134-
"deployments/config-policy-controller",
135-
"--watch",
136-
"--timeout=360s", // to allow for the 5 minute delay on old k8s
137-
fmt.Sprintf("--kubeconfig=%s%d.kubeconfig", kubeconfigFilename, i+1),
138-
)
139-
140125
By(logPrefix + "verifying the pod has been deployed with a new logging level and concurrency")
141-
opts := metav1.ListOptions{
142-
LabelSelector: case2PodSelector,
143-
}
144-
pods := ListWithTimeoutByNamespace(cluster.clusterClient, gvrPod, opts, addonNamespace, 1, true, 60)
145-
phase := pods.Items[0].Object["status"].(map[string]interface{})["phase"]
126+
Eventually(func(g Gomega) {
127+
opts := metav1.ListOptions{
128+
LabelSelector: case2PodSelector,
129+
}
130+
pods := ListWithTimeoutByNamespace(cluster.clusterClient, gvrPod, opts, addonNamespace, 1, true, 60)
131+
phase := pods.Items[0].Object["status"].(map[string]interface{})["phase"]
146132

147-
Expect(phase.(string)).To(Equal("Running"))
148-
containerList, _, err := unstructured.NestedSlice(pods.Items[0].Object, "spec", "containers")
149-
if err != nil {
150-
panic(err)
151-
}
152-
for _, container := range containerList {
153-
if containerObj, ok := container.(map[string]interface{}); ok {
154-
if Expect(containerObj).To(HaveKey("name")) && containerObj["name"] != case2DeploymentName {
133+
g.Expect(phase.(string)).To(Equal("Running"))
134+
containerList, _, err := unstructured.NestedSlice(pods.Items[0].Object, "spec", "containers")
135+
g.Expect(err).To(BeNil())
136+
for _, container := range containerList {
137+
containerObj, ok := container.(map[string]interface{})
138+
g.Expect(ok).To(BeTrue())
139+
140+
if g.Expect(containerObj).To(HaveKey("name")) && containerObj["name"] != case2DeploymentName {
155141
continue
156142
}
157-
if Expect(containerObj).To(HaveKey("args")) {
143+
if g.Expect(containerObj).To(HaveKey("args")) {
158144
args := containerObj["args"]
159-
Expect(args).To(ContainElement("--log-encoder=console"))
160-
Expect(args).To(ContainElement("--log-level=8"))
161-
Expect(args).To(ContainElement("--v=6"))
162-
Expect(args).To(ContainElement("--evaluation-concurrency=5"))
145+
g.Expect(args).To(ContainElement("--log-encoder=console"))
146+
g.Expect(args).To(ContainElement("--log-level=8"))
147+
g.Expect(args).To(ContainElement("--v=6"))
148+
g.Expect(args).To(ContainElement("--evaluation-concurrency=5"))
163149
}
164-
} else {
165-
panic(fmt.Errorf("containerObj type assertion failed"))
166150
}
167-
}
151+
}, 180, 10).Should(Succeed())
168152

169153
By(logPrefix +
170154
"removing the config-policy-controller deployment when the ManagedClusterAddOn CR is removed")

0 commit comments

Comments
 (0)