Skip to content

Commit

Permalink
Add checkCompliance function for debugging tests
Browse files Browse the repository at this point in the history
The full policy status is often helpful when understanding what might be
unexpectedly happening in some of the operatorpolicy tests.

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
  • Loading branch information
JustinKuli committed May 1, 2024
1 parent dfb236a commit 6055b85
Showing 1 changed file with 37 additions and 63 deletions.
100 changes: 37 additions & 63 deletions test/e2e/case38_install_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@ var _ = Describe("Testing OperatorPolicy", Ordered, func() {
olmWaitTimeout = 60
)

// checks that the compliance state eventually matches what is desired
checkCompliance := func(polName string, ns string, timeoutSeconds int, comp policyv1.ComplianceState) {
var debugMessage string

DeferCleanup(func() {
if CurrentSpecReport().Failed() {
GinkgoWriter.Println(debugMessage)
}
})

EventuallyWithOffset(1, func(g Gomega) {
unstructPolicy, err := clientManagedDynamic.Resource(gvrOperatorPolicy).Namespace(ns).
Get(context.TODO(), polName, metav1.GetOptions{})
g.Expect(err).NotTo(HaveOccurred())

unstructured.RemoveNestedField(unstructPolicy.Object, "metadata", "managedFields")

policyJSON, err := json.MarshalIndent(unstructPolicy.Object, "", " ")
g.Expect(err).NotTo(HaveOccurred())

debugMessage = fmt.Sprintf("Debug info for failure.\npolicy JSON: %s", policyJSON)

policy := policyv1beta1.OperatorPolicy{}
err = json.Unmarshal(policyJSON, &policy)
g.Expect(err).NotTo(HaveOccurred())

g.Expect(policy.Status.ComplianceState).To(Equal(comp))
}, timeoutSeconds, 3).Should(Succeed())
}

// checks that the policy has the proper compliance, that the relatedObjects of a given
// type exactly match the list given (no extras or omissions), that the condition is present,
// and that an event was emitted that matches the given snippet.
Expand Down Expand Up @@ -375,15 +405,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, func() {

It("Should be compliant when enforced", func() {
By("Waiting for the operator policy " + opPolName + " to be compliant")
Eventually(func() string {
opPolicy := utils.GetWithTimeout(
clientManagedDynamic, gvrOperatorPolicy, opPolName, testNamespace, true, defaultTimeoutSeconds,
)

compliance, _, _ := unstructured.NestedString(opPolicy.Object, "status", "compliant")

return compliance
}, defaultTimeoutSeconds*2, 1).Should(Equal("Compliant"))
checkCompliance(opPolName, testNamespace, defaultTimeoutSeconds*2, policyv1.Compliant)
})
})

Expand Down Expand Up @@ -1385,15 +1407,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, func() {
}, olmWaitTimeout, 5, ctx).ShouldNot(BeNil())

By("Waiting for the policy to become compliant, indicating the operator is installed")
Eventually(func(g Gomega) string {
pol := utils.GetWithTimeout(clientManagedDynamic, gvrOperatorPolicy, opPolName,
opPolTestNS, true, eventuallyTimeout)
compliance, found, err := unstructured.NestedString(pol.Object, "status", "compliant")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(found).To(BeTrue())

return compliance
}, olmWaitTimeout, 5, ctx).Should(Equal("Compliant"))
checkCompliance(opPolName, opPolTestNS, olmWaitTimeout, policyv1.Compliant)

check(
opPolName,
Expand Down Expand Up @@ -1736,15 +1750,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, func() {
}, olmWaitTimeout, 5, ctx).ShouldNot(BeNil())

By("Waiting for the policy to become compliant, indicating the operator is installed")
Eventually(func(g Gomega) string {
pol := utils.GetWithTimeout(clientManagedDynamic, gvrOperatorPolicy, opPolName,
opPolTestNS, true, eventuallyTimeout)
compliance, found, err := unstructured.NestedString(pol.Object, "status", "compliant")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(found).To(BeTrue())

return compliance
}, olmWaitTimeout, 5, ctx).Should(Equal("Compliant"))
checkCompliance(opPolName, opPolTestNS, olmWaitTimeout, policyv1.Compliant)

// Revert to the original mustnothave policy
utils.Kubectl("patch", "operatorpolicy", opPolName, "-n", opPolTestNS, "--type=json", "-p",
Expand Down Expand Up @@ -2397,15 +2403,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, func() {
}, olmWaitTimeout, 5, ctx).ShouldNot(BeNil())

By("Waiting for the policy to become compliant, indicating the operator is installed")
Eventually(func(g Gomega) string {
pol := utils.GetWithTimeout(clientManagedDynamic, gvrOperatorPolicy, opPolName,
opPolTestNS, true, eventuallyTimeout)
compliance, found, err := unstructured.NestedString(pol.Object, "status", "compliant")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(found).To(BeTrue())

return compliance
}, olmWaitTimeout, 5, ctx).Should(Equal("Compliant"))
checkCompliance(opPolName, opPolTestNS, olmWaitTimeout, policyv1.Compliant)

By("Verifying that an operator group exists")
Eventually(func(g Gomega) []unstructured.Unstructured {
Expand Down Expand Up @@ -2448,15 +2446,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, func() {
}, olmWaitTimeout, 5, ctx).ShouldNot(BeNil())

By("Waiting for the policy to become compliant, indicating the operator is installed")
Eventually(func(g Gomega) string {
pol := utils.GetWithTimeout(clientManagedDynamic, gvrOperatorPolicy, opPolName,
opPolTestNS, true, eventuallyTimeout)
compliance, found, err := unstructured.NestedString(pol.Object, "status", "compliant")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(found).To(BeTrue())

return compliance
}, olmWaitTimeout, 5, ctx).Should(Equal("Compliant"))
checkCompliance(opPolName, opPolTestNS, olmWaitTimeout, policyv1.Compliant)

By("Verifying that an operator group exists")
Eventually(func(g Gomega) []unstructured.Unstructured {
Expand Down Expand Up @@ -2499,15 +2489,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, func() {
}, olmWaitTimeout, 5, ctx).ShouldNot(BeNil())

By("Waiting for the policy to become compliant, indicating the operator is installed")
Eventually(func(g Gomega) string {
pol := utils.GetWithTimeout(clientManagedDynamic, gvrOperatorPolicy, opPolName,
opPolTestNS, true, eventuallyTimeout)
compliance, found, err := unstructured.NestedString(pol.Object, "status", "compliant")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(found).To(BeTrue())

return compliance
}, olmWaitTimeout, 5, ctx).Should(Equal("Compliant"))
checkCompliance(opPolName, opPolTestNS, olmWaitTimeout, policyv1.Compliant)

By("Verifying that an operator group exists")
Eventually(func(g Gomega) []unstructured.Unstructured {
Expand Down Expand Up @@ -2560,15 +2542,7 @@ var _ = Describe("Testing OperatorPolicy", Ordered, func() {
}, olmWaitTimeout, 5, ctx).ShouldNot(BeNil())

By("Waiting for the policy to become compliant, indicating the operator is installed")
Eventually(func(g Gomega) string {
pol := utils.GetWithTimeout(clientManagedDynamic, gvrOperatorPolicy, opPolName,
opPolTestNS, true, eventuallyTimeout)
compliance, found, err := unstructured.NestedString(pol.Object, "status", "compliant")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(found).To(BeTrue())

return compliance
}, olmWaitTimeout, 5, ctx).Should(Equal("Compliant"))
checkCompliance(opPolName, opPolTestNS, olmWaitTimeout, policyv1.Compliant)

By("Verifying that an operator group exists")
Eventually(func(g Gomega) []unstructured.Unstructured {
Expand Down

0 comments on commit 6055b85

Please sign in to comment.