diff --git a/test/e2e/case15_event_format_test.go b/test/e2e/case15_event_format_test.go index f620d1df..306f2f6a 100644 --- a/test/e2e/case15_event_format_test.go +++ b/test/e2e/case15_event_format_test.go @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/test/e2e/case17_evaluation_interval_test.go b/test/e2e/case17_evaluation_interval_test.go index 44df988b..9ab9b288 100644 --- a/test/e2e/case17_evaluation_interval_test.go +++ b/test/e2e/case17_evaluation_interval_test.go @@ -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 diff --git a/test/e2e/case21_alternative_kubeconfig_test.go b/test/e2e/case21_alternative_kubeconfig_test.go index 7ecd01f6..055444c7 100644 --- a/test/e2e/case21_alternative_kubeconfig_test.go +++ b/test/e2e/case21_alternative_kubeconfig_test.go @@ -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{} { diff --git a/test/e2e/case31_policy_history_test.go b/test/e2e/case31_policy_history_test.go index 6e6f7681..cc0a8664 100644 --- a/test/e2e/case31_policy_history_test.go +++ b/test/e2e/case31_policy_history_test.go @@ -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" ) @@ -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() { @@ -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() { @@ -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() { @@ -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() { @@ -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()) } diff --git a/test/e2e/case34_enforce_w_status_test.go b/test/e2e/case34_enforce_w_status_test.go index 796e98bf..6afdbaa4 100644 --- a/test/e2e/case34_enforce_w_status_test.go +++ b/test/e2e/case34_enforce_w_status_test.go @@ -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{} { diff --git a/test/e2e/case35_no_apiversion_test.go b/test/e2e/case35_no_apiversion_test.go index 2cc4c917..e0aa2b61 100644 --- a/test/e2e/case35_no_apiversion_test.go +++ b/test/e2e/case35_no_apiversion_test.go @@ -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{} {