Skip to content

Commit

Permalink
Adjust test helper for owned objects
Browse files Browse the repository at this point in the history
The previous version of this function was only applicable to a Policy
owning a ConfigurationPolicy. Now it is more broadly applicable.

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
  • Loading branch information
JustinKuli committed Jan 26, 2024
1 parent c0ca63e commit 2978f4e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
16 changes: 8 additions & 8 deletions test/e2e/case15_event_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const (

var _ = Describe("Testing compliance event formatting", Ordered, func() {
It("Records the right events for a policy that is always compliant", func() {
createConfigPolicyWithParent(case15AlwaysCompliantParentYaml, case15AlwaysCompliantParentName,
case15AlwaysCompliantYaml)
createObjWithParent(case15AlwaysCompliantParentYaml, case15AlwaysCompliantParentName,
case15AlwaysCompliantYaml, testNamespace, gvrPolicy, gvrConfigPolicy)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15AlwaysCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down Expand Up @@ -73,8 +73,8 @@ var _ = Describe("Testing compliance event formatting", Ordered, func() {
Expect(nonCompParentEvents).To(BeEmpty())
})
It("Records the right events for a policy that is never compliant", func() {
createConfigPolicyWithParent(case15NeverCompliantParentYaml, case15NeverCompliantParentName,
case15NeverCompliantYaml)
createObjWithParent(case15NeverCompliantParentYaml, case15NeverCompliantParentName,
case15NeverCompliantYaml, testNamespace, gvrPolicy, gvrConfigPolicy)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15NeverCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down Expand Up @@ -107,8 +107,8 @@ var _ = Describe("Testing compliance event formatting", Ordered, func() {
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())
})
It("Records events for a policy that becomes compliant", func() {
createConfigPolicyWithParent(case15BecomesCompliantParentYaml, case15BecomesCompliantParentName,
case15BecomesCompliantYaml)
createObjWithParent(case15BecomesCompliantParentYaml, case15BecomesCompliantParentName,
case15BecomesCompliantYaml, testNamespace, gvrPolicy, gvrConfigPolicy)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15BecomesCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down Expand Up @@ -147,8 +147,8 @@ var _ = Describe("Testing compliance event formatting", Ordered, func() {
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())
})
It("Records events for a policy that becomes noncompliant", func() {
createConfigPolicyWithParent(case15BecomesNonCompliantParentYaml, case15BecomesNonCompliantParentName,
case15BecomesNonCompliantYaml)
createObjWithParent(case15BecomesNonCompliantParentYaml, case15BecomesNonCompliantParentName,
case15BecomesNonCompliantYaml, testNamespace, gvrPolicy, gvrConfigPolicy)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15BecomesNonCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/case17_evaluation_interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const (

var _ = Describe("Test evaluation interval", Ordered, func() {
It("Verifies that status.lastEvaluated is properly set", func() {
createConfigPolicyWithParent(case17ParentPolicy, case17ParentPolicyName, case17Policy)
createObjWithParent(case17ParentPolicy, case17ParentPolicyName,
case17Policy, testNamespace, gvrPolicy, gvrConfigPolicy)

By("Getting status.lastEvaluated")
var managedPlc *unstructured.Unstructured
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/case21_alternative_kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var _ = Describe("Test an alternative kubeconfig for policy evaluation", Ordered
})

It("should create the namespace using the alternative kubeconfig", func() {
createConfigPolicyWithParent(parentPolicyYAML, parentPolicyName, policyYAML)
createObjWithParent(parentPolicyYAML, parentPolicyName, policyYAML, testNamespace, gvrPolicy, gvrConfigPolicy)

By("Verifying that the " + policyName + " policy is compliant")
Eventually(func() interface{} {
Expand Down
39 changes: 21 additions & 18 deletions test/e2e/case31_policy_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

"open-cluster-management.io/config-policy-controller/test/utils"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ var _ = Describe("Test policy history messages when KubeAPI omits values in the
)

It("sets up a configuration policy with an omitempty boolean set to false", func() {
createConfigPolicyWithParent(policyYAML, policyName, configPolicyYAML)
createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy)
})

It("checks the policy's history", func() {
Expand All @@ -82,7 +83,7 @@ var _ = Describe("Test policy history messages when KubeAPI omits values in the
)

It("sets up a configuration policy with an omitempty number set to 0", func() {
createConfigPolicyWithParent(policyYAML, policyName, configPolicyYAML)
createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy)
})

It("checks the policy's history", func() {
Expand All @@ -109,7 +110,7 @@ var _ = Describe("Test policy history messages when KubeAPI omits values in the
)

It("sets up a configuration policy with an omitempty number set to 0", func() {
createConfigPolicyWithParent(policyYAML, policyName, configPolicyYAML)
createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy)
})

It("checks the policy's history", func() {
Expand All @@ -136,7 +137,7 @@ var _ = Describe("Test policy history messages when KubeAPI omits values in the
)

It("sets up a configuration policy with struct set to null", func() {
createConfigPolicyWithParent(policyYAML, policyName, configPolicyYAML)
createObjWithParent(policyYAML, policyName, configPolicyYAML, testNamespace, gvrPolicy, gvrConfigPolicy)
})

It("checks the policy's history", func() {
Expand Down Expand Up @@ -221,27 +222,29 @@ var _ = Describe("Test policy history messages when KubeAPI omits values in the
})
})

func createConfigPolicyWithParent(parentPolicyYAML, parentPolicyName, configPolicyYAML string) {
By("Creating the parent policy")
utils.Kubectl("apply", "-f", parentPolicyYAML, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy,
parentPolicyName, testNamespace, true, defaultTimeoutSeconds)
func createObjWithParent(
parentYAML, parentName, childYAML, namespace string, parentGVR, childGVR schema.GroupVersionResource,
) {
By("Creating the parent object")
utils.Kubectl("apply", "-f", parentYAML, "-n", namespace)
parent := utils.GetWithTimeout(clientManagedDynamic, parentGVR,
parentName, namespace, true, defaultTimeoutSeconds)
Expect(parent).NotTo(BeNil())

plcDef := utils.ParseYaml(configPolicyYAML)
ownerRefs := plcDef.GetOwnerReferences()
child := utils.ParseYaml(childYAML)
ownerRefs := child.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
child.SetOwnerReferences(ownerRefs)

By("Creating the configuration policy with the owner reference")
By("Creating the child object with the owner reference")

_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).
Create(context.TODO(), plcDef, metav1.CreateOptions{})
_, err := clientManagedDynamic.Resource(childGVR).Namespace(namespace).
Create(context.TODO(), child, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())

By("Verifying the configuration policy exists")
By("Verifying the child object exists")

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
plcDef.GetName(), testNamespace, true, defaultTimeoutSeconds)
plc := utils.GetWithTimeout(clientManagedDynamic, childGVR,
child.GetName(), namespace, true, defaultTimeoutSeconds)
Expect(plc).NotTo(BeNil())
}
2 changes: 1 addition & 1 deletion test/e2e/case34_enforce_w_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ = Describe("Test compliance events of enforced policies that define a stat

It("Should have the expected events", func() {
By("Setting up the policy")
createConfigPolicyWithParent(policyYAML, policyName, cfgPlcYAML)
createObjWithParent(policyYAML, policyName, cfgPlcYAML, testNamespace, gvrPolicy, gvrConfigPolicy)

By("Checking there is a NonCompliant event on the policy")
Eventually(func() interface{} {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/case35_no_apiversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ = Describe("Test a policy with an objectDefinition that is missing apiVers

It("Should have the expected events", func() {
By("Setting up the policy")
createConfigPolicyWithParent(policyYAML, policyName, cfgPlcYAML)
createObjWithParent(policyYAML, policyName, cfgPlcYAML, testNamespace, gvrPolicy, gvrConfigPolicy)

By("Checking there is a NonCompliant event on the policy")
Eventually(func() interface{} {
Expand Down

0 comments on commit 2978f4e

Please sign in to comment.