Skip to content

Commit

Permalink
Use common parent policy creation function
Browse files Browse the repository at this point in the history
Creating a configuration policy that "belongs" to a parent policy is
slightly tricky, because it needs an owner reference with a UID. A
common function makes this a bit easier.

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
  • Loading branch information
JustinKuli committed Apr 25, 2023
1 parent 5778c54 commit 14a11f9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 95 deletions.
68 changes: 8 additions & 60 deletions test/e2e/case15_event_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,8 @@ const (

var _ = Describe("Testing compliance event formatting", func() {
It("Records the right events for a policy that is always compliant", func() {
By("Creating parent policy " + case15AlwaysCompliantParentName + " on " + testNamespace)
utils.Kubectl("apply", "-f", case15AlwaysCompliantParentYaml, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy,
case15AlwaysCompliantParentName, testNamespace, true, defaultTimeoutSeconds)
Expect(parent).NotTo(BeNil())

By("Creating compliant policy " + case15AlwaysCompliantName + " on " + testNamespace + " with parent " +
case15AlwaysCompliantParentName)
plcDef := utils.ParseYaml(case15AlwaysCompliantYaml)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).
Create(context.TODO(), plcDef, metav1.CreateOptions{})
Expect(err).To(BeNil())
createConfigPolicyWithParent(case15AlwaysCompliantParentYaml, case15AlwaysCompliantParentName,
case15AlwaysCompliantYaml)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15AlwaysCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down Expand Up @@ -81,21 +68,8 @@ var _ = Describe("Testing compliance event formatting", func() {
Expect(nonCompParentEvents).To(BeEmpty())
})
It("Records the right events for a policy that is never compliant", func() {
By("Creating parent policy " + case15NeverCompliantParentName + " on " + testNamespace)
utils.Kubectl("apply", "-f", case15NeverCompliantParentYaml, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy,
case15NeverCompliantParentName, testNamespace, true, defaultTimeoutSeconds)
Expect(parent).NotTo(BeNil())

By("Creating noncompliant policy " + case15NeverCompliantName + " on " + testNamespace + " with parent " +
case15NeverCompliantParentName)
plcDef := utils.ParseYaml(case15NeverCompliantYaml)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).
Create(context.TODO(), plcDef, metav1.CreateOptions{})
Expect(err).To(BeNil())
createConfigPolicyWithParent(case15NeverCompliantParentYaml, case15NeverCompliantParentName,
case15NeverCompliantYaml)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15NeverCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down Expand Up @@ -126,21 +100,8 @@ var _ = Describe("Testing compliance event formatting", func() {
Expect(nonCompParentEvents).NotTo(BeEmpty())
})
It("Records events for a policy that becomes compliant", func() {
By("Creating parent policy " + case15BecomesCompliantParentName + " on " + testNamespace)
utils.Kubectl("apply", "-f", case15BecomesCompliantParentYaml, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy,
case15BecomesCompliantParentName, testNamespace, true, defaultTimeoutSeconds)
Expect(parent).NotTo(BeNil())

By("Creating noncompliant policy " + case15BecomesCompliantName + " on " + testNamespace + " with parent " +
case15BecomesCompliantParentName)
plcDef := utils.ParseYaml(case15BecomesCompliantYaml)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).
Create(context.TODO(), plcDef, metav1.CreateOptions{})
Expect(err).To(BeNil())
createConfigPolicyWithParent(case15BecomesCompliantParentYaml, case15BecomesCompliantParentName,
case15BecomesCompliantYaml)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15BecomesCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down Expand Up @@ -176,21 +137,8 @@ var _ = Describe("Testing compliance event formatting", func() {
Expect(compParentEvents).NotTo(BeEmpty())
})
It("Records events for a policy that becomes noncompliant", func() {
By("Creating parent policy " + case15BecomesNonCompliantParentName + " on " + testNamespace)
utils.Kubectl("apply", "-f", case15BecomesNonCompliantParentYaml, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy,
case15BecomesNonCompliantParentName, testNamespace, true, defaultTimeoutSeconds)
Expect(parent).NotTo(BeNil())

By("Creating compliant policy " + case15BecomesNonCompliantName + " on " + testNamespace + " with parent " +
case15BecomesNonCompliantParentName)
plcDef := utils.ParseYaml(case15BecomesNonCompliantYaml)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).
Create(context.TODO(), plcDef, metav1.CreateOptions{})
Expect(err).To(BeNil())
createConfigPolicyWithParent(case15BecomesNonCompliantParentYaml, case15BecomesNonCompliantParentName,
case15BecomesNonCompliantYaml)

plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case15BecomesNonCompliantName, testNamespace, true, defaultTimeoutSeconds)
Expand Down
21 changes: 1 addition & 20 deletions test/e2e/case17_evaluation_interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,7 @@ const (

var _ = Describe("Test evaluation interval", func() {
It("Verifies that status.lastEvaluated is properly set", func() {
By("Creating the parent policy " + case17ParentPolicyName + " on the managed cluster")
utils.Kubectl("apply", "-f", case17ParentPolicy, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic,
gvrPolicy,
case17ParentPolicyName,
testNamespace,
true,
defaultTimeoutSeconds,
)
Expect(parent).NotTo(BeNil())

By("Creating " + case17PolicyName + " on the managed cluster")
plcDef := utils.ParseYaml(case17Policy)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).Create(
context.TODO(), plcDef, v1.CreateOptions{},
)
Expect(err).To(BeNil())
createConfigPolicyWithParent(case17ParentPolicy, case17ParentPolicyName, case17Policy)

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

It("should create the namespace using the alternative kubeconfig", func() {
By("Creating parent policy " + parentPolicyName + " on " + testNamespace)
utils.Kubectl("apply", "-f", parentPolicyYAML, "-n", testNamespace)
parent := utils.GetWithTimeout(clientManagedDynamic, gvrPolicy,
parentPolicyName, testNamespace, true, defaultTimeoutSeconds)
Expect(parent).NotTo(BeNil())

By("Creating the " + policyName + " policy")
plcDef := utils.ParseYaml(policyYAML)
ownerRefs := plcDef.GetOwnerReferences()
ownerRefs[0].UID = parent.GetUID()
plcDef.SetOwnerReferences(ownerRefs)
_, err := clientManagedDynamic.Resource(gvrConfigPolicy).Namespace(testNamespace).
Create(context.TODO(), plcDef, metav1.CreateOptions{})
Expect(err).To(BeNil())
createConfigPolicyWithParent(parentPolicyYAML, parentPolicyName, policyYAML)

By("Verifying that the " + policyName + " policy is compliant")
Eventually(func() interface{} {
Expand All @@ -83,7 +70,7 @@ var _ = Describe("Test an alternative kubeconfig for policy evaluation", Ordered
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant"))

By("Verifying that the " + policyName + " was created using the alternative kubeconfig")
_, err = targetK8sClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
_, err := targetK8sClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
Expect(err).To(BeNil())

By("Verifying that a compliance event was created on the parent policy")
Expand Down

0 comments on commit 14a11f9

Please sign in to comment.