Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky event recording config-policy-controller E2E test #174

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions test/e2e/case12_list_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package e2e

import (
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -334,7 +332,7 @@ var _ = Describe("Test list handling for musthave", func() {
case12WhitespaceListCreate, testNamespace, true, defaultTimeoutSeconds)

return utils.GetComplianceState(managedPlc)
}, time.Second*20, 1).Should(Equal("Compliant"))
}, defaultConsistentlyDuration, 1).Should(Equal("Compliant"))

// Verify that the container list and its environment variable list is correct (there are no duplicates)
deploy := utils.GetWithTimeout(clientManagedDynamic, gvrDeployment,
Expand Down Expand Up @@ -388,7 +386,7 @@ var _ = Describe("Test list handling for musthave", func() {
case12ByteCreate, testNamespace, true, defaultTimeoutSeconds)

return utils.GetComplianceState(managedPlc)
}, time.Second*20, 1).Should(Equal("Compliant"))
}, defaultConsistentlyDuration, 1).Should(Equal("Compliant"))

// Verify that the container list and its environment variable list is correct (there are no duplicates)
utils.Kubectl("apply", "-f", case12ByteInformYaml, "-n", testNamespace)
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/case13_templatization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ var _ = Describe("Test templatization", Ordered, func() {
case13PruneTmpErr+"-configmap", "default", true, defaultTimeoutSeconds)

return configmap
}, defaultTimeoutSeconds, 1).ShouldNot(BeNil())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consistently timeout

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

I think this change is good - we don't need to check that the configmap is still there for 60 (or more) seconds, 30 should be fine. But I don't understand why it would be timing out right now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't any issue but I tried to reduce e2e test time. I think 60 secs is meaningless. Just suggestion. It took over 20mins before but now less than 15 mins

}, defaultConsistentlyDuration, 1).ShouldNot(BeNil())

By("Change to valid configmap")
utils.Kubectl("patch", "configurationpolicy", case13PruneTmpErr, "--type=json", "-p",
Expand Down Expand Up @@ -520,7 +520,7 @@ var _ = Describe("Test templatization", Ordered, func() {
case13PruneTmpErr+"-configmap", "default", true, defaultTimeoutSeconds)

return configmap
}, defaultTimeoutSeconds, 1).ShouldNot(BeNil())
yiraeChristineKim marked this conversation as resolved.
Show resolved Hide resolved
}, defaultConsistentlyDuration, 1).ShouldNot(BeNil())
})
AfterAll(func() {
utils.Kubectl("delete", "configurationpolicy", case13PruneTmpErr,
Expand Down
5 changes: 2 additions & 3 deletions test/e2e/case14_selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package e2e

import (
"context"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -116,7 +115,7 @@ var _ = Describe("Test policy compliance with namespace selection", Ordered, fun
policy.name, testNamespace, true, defaultTimeoutSeconds)

return utils.GetComplianceState(managedPlc)
}, time.Second*20, 1).Should(Equal("NonCompliant"))
}, defaultConsistentlyDuration, 1).Should(Equal("NonCompliant"))
}
})

Expand Down Expand Up @@ -158,7 +157,7 @@ var _ = Describe("Test policy compliance with namespace selection", Ordered, fun
policy.name, testNamespace, true, defaultTimeoutSeconds)

return utils.GetComplianceState(managedPlc)
}, time.Second*20, 1).Should(Equal("NonCompliant"))
}, defaultConsistentlyDuration, 1).Should(Equal("NonCompliant"))
By("Checking that " + policy.name + " has the correct relatedObjects")
plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
policy.name, testNamespace, true, defaultTimeoutSeconds)
Expand Down
75 changes: 43 additions & 32 deletions test/e2e/case15_event_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -52,18 +53,20 @@ var _ = Describe("Testing compliance event formatting", Ordered, func() {
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant"))

By("Checking events on the configurationpolicy")
compPlcEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
case15AlwaysCompliantName, "", "Policy status is Compliant", defaultTimeoutSeconds)
Expect(compPlcEvents).NotTo(BeEmpty())
Eventually(func() []v1.Event {
return utils.GetMatchingEvents(clientManaged, testNamespace,
case15AlwaysCompliantName, "", "Policy status is Compliant", defaultTimeoutSeconds)
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())
nonCompPlcEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
case15AlwaysCompliantName, "", "Policy status is NonCompliant", defaultTimeoutSeconds)
Expect(nonCompPlcEvents).To(BeEmpty())

By("Checking events on the parent policy")
compParentEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
case15AlwaysCompliantParentName, "policy: "+testNamespace+"/"+
case15AlwaysCompliantName, "^Compliant;", defaultTimeoutSeconds)
Expect(compParentEvents).NotTo(BeEmpty())
Eventually(func() []v1.Event {
return utils.GetMatchingEvents(clientManaged, testNamespace,
case15AlwaysCompliantParentName, "policy: "+testNamespace+"/"+
case15AlwaysCompliantName, "^Compliant;", defaultTimeoutSeconds)
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())
nonCompParentEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
case15AlwaysCompliantParentName, "policy: "+testNamespace+"/"+
case15AlwaysCompliantName, "^NonCompliant;", defaultTimeoutSeconds)
Expand All @@ -87,19 +90,21 @@ var _ = Describe("Testing compliance event formatting", Ordered, func() {
compPlcEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
case15NeverCompliantName, "", "Policy status is Compliant", defaultTimeoutSeconds)
Expect(compPlcEvents).To(BeEmpty())
nonCompPlcEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
case15NeverCompliantName, "", "Policy status is NonCompliant", defaultTimeoutSeconds)
Expect(nonCompPlcEvents).NotTo(BeEmpty())
Eventually(func() []v1.Event {
return utils.GetMatchingEvents(clientManaged, testNamespace,
case15NeverCompliantName, "", "Policy status is NonCompliant", defaultTimeoutSeconds)
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())

By("Checking events on the parent policy")
compParentEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
case15NeverCompliantParentName, "policy: "+testNamespace+"/"+case15NeverCompliantName,
"^Compliant;", defaultTimeoutSeconds)
Expect(compParentEvents).To(BeEmpty())
nonCompParentEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
case15NeverCompliantParentName, "policy: "+testNamespace+"/"+case15NeverCompliantName,
"^NonCompliant;", defaultTimeoutSeconds)
Expect(nonCompParentEvents).NotTo(BeEmpty())
Eventually(func() []v1.Event {
return utils.GetMatchingEvents(clientManaged, testNamespace,
case15NeverCompliantParentName, "policy: "+testNamespace+"/"+case15NeverCompliantName,
"^NonCompliant;", defaultTimeoutSeconds)
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())
})
It("Records events for a policy that becomes compliant", func() {
createConfigPolicyWithParent(case15BecomesCompliantParentYaml, case15BecomesCompliantParentName,
Expand All @@ -126,17 +131,20 @@ var _ = Describe("Testing compliance event formatting", Ordered, func() {
}, defaultTimeoutSeconds, 1).Should(Equal("Compliant"))

By("Checking for compliant events on the configurationpolicy and the parent policy")
compPlcEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
case15BecomesCompliantName, "", "Policy status is Compliant", defaultTimeoutSeconds)
Expect(compPlcEvents).NotTo(BeEmpty())
compParentEventsPreCreation := utils.GetMatchingEvents(clientManaged, testNamespace,
case15BecomesCompliantParentName, "policy: "+testNamespace+"/"+case15BecomesCompliantName,
"^NonCompliant;.*not found in namespace default$", defaultTimeoutSeconds)
Expect(compParentEventsPreCreation).NotTo(BeEmpty())
compParentEvents := utils.GetMatchingEvents(clientManaged, testNamespace, case15BecomesCompliantParentName,
"policy: "+testNamespace+"/"+case15BecomesCompliantName,
"^Compliant;.*was created successfully in namespace default$", defaultTimeoutSeconds)
Expect(compParentEvents).NotTo(BeEmpty())
Eventually(func() []v1.Event {
return utils.GetMatchingEvents(clientManaged, testNamespace,
case15BecomesCompliantName, "", "Policy status is Compliant", defaultTimeoutSeconds)
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())
Eventually(func() []v1.Event {
return utils.GetMatchingEvents(clientManaged, testNamespace,
case15BecomesCompliantParentName, "policy: "+testNamespace+"/"+case15BecomesCompliantName,
"^NonCompliant;.*not found in namespace default$", defaultTimeoutSeconds)
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())
Eventually(func() []v1.Event {
return utils.GetMatchingEvents(clientManaged, testNamespace, case15BecomesCompliantParentName,
"policy: "+testNamespace+"/"+case15BecomesCompliantName,
"^Compliant;.*was created successfully in namespace default$", defaultTimeoutSeconds)
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())
})
It("Records events for a policy that becomes noncompliant", func() {
createConfigPolicyWithParent(case15BecomesNonCompliantParentYaml, case15BecomesNonCompliantParentName,
Expand All @@ -162,13 +170,16 @@ var _ = Describe("Testing compliance event formatting", Ordered, func() {
}, defaultTimeoutSeconds, 1).Should(Equal("NonCompliant"))

By("Checking for noncompliant events on the configurationpolicy and the parent policy")
nonCompPlcEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
case15BecomesNonCompliantName, "", "Policy status is NonCompliant", defaultTimeoutSeconds)
Expect(nonCompPlcEvents).NotTo(BeEmpty())
nonCompParentEvents := utils.GetMatchingEvents(clientManaged, testNamespace,
case15BecomesNonCompliantParentName, "policy: "+testNamespace+"/"+case15BecomesNonCompliantName,
"^NonCompliant;", defaultTimeoutSeconds)
Expect(nonCompParentEvents).NotTo(BeEmpty())
Eventually(func() []v1.Event {
return utils.GetMatchingEvents(clientManaged, testNamespace,
case15BecomesNonCompliantName, "", "Policy status is NonCompliant", defaultTimeoutSeconds)
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())

Eventually(func() []v1.Event {
return utils.GetMatchingEvents(clientManaged, testNamespace,
case15BecomesNonCompliantParentName, "policy: "+testNamespace+"/"+case15BecomesNonCompliantName,
"^NonCompliant;", defaultTimeoutSeconds)
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())
})
AfterAll(func() {
policies := []string{
Expand Down
44 changes: 24 additions & 20 deletions test/e2e/case17_evaluation_interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,32 @@ var _ = Describe("Test evaluation interval", Ordered, func() {
Expect(lastEvaluatedParsed.Before(lastEvalRefreshedParsed)).To(BeTrue())

By("Verifying that only one event was sent for the configuration policy")
events := utils.GetMatchingEvents(
clientManaged,
testNamespace,
case17PolicyName,
"",
"Policy status is NonCompliant",
defaultTimeoutSeconds,
)
Expect(events).To(HaveLen(1))
Expect(events[0].Count).To(Equal(int32(1)))
Eventually(func(g Gomega) {
events := utils.GetMatchingEvents(
clientManaged,
testNamespace,
case17PolicyName,
"",
"Policy status is NonCompliant",
defaultTimeoutSeconds,
)
g.Expect(events).To(HaveLen(1))
g.Expect(events[0].Count).To(Equal(int32(1)))
}, defaultTimeoutSeconds, 1).Should(Succeed())

By("Verifying that only one event was sent for the parent policy")
parentEvents := utils.GetMatchingEvents(
clientManaged,
testNamespace,
case17ParentPolicyName,
"policy: "+testNamespace+"/"+case17PolicyName,
"^NonCompliant;",
defaultTimeoutSeconds,
)
Expect(parentEvents).To(HaveLen(1))
Expect(parentEvents[0].Count).To(Equal(int32(1)))
Eventually(func(g Gomega) {
parentEvents := utils.GetMatchingEvents(
clientManaged,
testNamespace,
case17ParentPolicyName,
"policy: "+testNamespace+"/"+case17PolicyName,
"^NonCompliant;",
defaultTimeoutSeconds,
)
g.Expect(parentEvents).To(HaveLen(1))
g.Expect(parentEvents[0].Count).To(Equal(int32(1)))
}, defaultTimeoutSeconds, 1).Should(Succeed())
})

It("Verifies that a compliant policy is not reevaluated when set to never", func() {
Expand Down
22 changes: 11 additions & 11 deletions test/e2e/case20_delete_objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ var _ = Describe("Test Object deletion", Ordered, func() {
case20PodName, "default", true, defaultTimeoutSeconds)

return pod
}, defaultTimeoutSeconds, 1).Should(Not(BeNil()))
}, defaultConsistentlyDuration, 1).Should(Not(BeNil()))
})
It("Should create DeleteIfCreated policy", func() {
// delete pod to reset
Expand Down Expand Up @@ -284,7 +284,7 @@ var _ = Describe("Test Object deletion", Ordered, func() {
case20PodName, "default", true, defaultTimeoutSeconds)

return pod
}, defaultTimeoutSeconds, 1).Should(Not(BeNil()))
}, defaultConsistentlyDuration, 1).Should(Not(BeNil()))
})
It("should handle deleteAll properly for created obj", func() {
By("Creating " + case20ConfigPolicyNameExisting + " on managed")
Expand Down Expand Up @@ -313,7 +313,7 @@ var _ = Describe("Test Object deletion", Ordered, func() {
case20ConfigPolicyNameExisting, testNamespace, false, defaultTimeoutSeconds)

return managedPlc
}, defaultTimeoutSeconds, 1).Should(BeNil())
}, defaultConsistentlyDuration, 1).Should(BeNil())
})
It("should handle deleteAll properly for non created obj", func() {
By("Creating " + case20PodName + " on default")
Expand All @@ -323,7 +323,7 @@ var _ = Describe("Test Object deletion", Ordered, func() {
case20PodName, "default", true, defaultTimeoutSeconds)

return pod
}, defaultTimeoutSeconds, 1).Should(Not(BeNil()))
}, 30, 1).Should(Not(BeNil()))
By("Creating " + case20ConfigPolicyNameExisting + " on managed")
utils.Kubectl("apply", "-f", case20PolicyYamlExisting, "-n", testNamespace)
plc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
Expand Down Expand Up @@ -378,13 +378,13 @@ var _ = Describe("Test Object deletion", Ordered, func() {
case20PodWithFinalizer, "default", true, defaultTimeoutSeconds)

return pod
}, defaultTimeoutSeconds, 1).Should(Not(BeNil()))
}, defaultConsistentlyDuration, 1).Should(Not(BeNil()))
Consistently(func() interface{} {
managedPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigPolicy,
case20ConfigPolicyNameFinalizer, testNamespace, true, defaultTimeoutSeconds)

return utils.GetComplianceState(managedPlc)
}, defaultTimeoutSeconds, 1).Should(Equal("Terminating"))
}, defaultConsistentlyDuration, 1).Should(Equal("Terminating"))
})
It("should finish delete when pod finalizer is removed", func() {
utils.Kubectl(
Expand All @@ -405,7 +405,7 @@ var _ = Describe("Test Object deletion", Ordered, func() {
case20ConfigPolicyNameFinalizer, testNamespace, false, defaultTimeoutSeconds)

return managedPlc
}, defaultTimeoutSeconds, 1).Should(BeNil())
}, defaultConsistentlyDuration, 1).Should(BeNil())
})
It("should handle changing policy from inform to enforce", func() {
By("Creating " + case20ConfigPolicyNameChange + " on managed")
Expand Down Expand Up @@ -445,7 +445,7 @@ var _ = Describe("Test Object deletion", Ordered, func() {
case20ConfigPolicyNameChange, testNamespace, false, defaultTimeoutSeconds)

return managedPlc
}, defaultTimeoutSeconds, 1).Should(BeNil())
}, defaultConsistentlyDuration, 1).Should(BeNil())
})
It("should handle changing policy from enforce to inform", func() {
By("Creating " + case20ConfigPolicyNameChange + " on managed")
Expand Down Expand Up @@ -735,7 +735,7 @@ var _ = Describe("Object Should not be deleted", Ordered, func() {
Expect(oldPod).ShouldNot(BeNil())

return oldPod
}, 20, 1).ShouldNot(BeNil())
}, defaultConsistentlyDuration, 1).ShouldNot(BeNil())
})
It("check pod is not removed when PruneObjectBehavior is none and namespace changed", func() {
oldPod := utils.GetWithTimeout(clientManagedDynamic, gvrPod,
Expand All @@ -755,7 +755,7 @@ var _ = Describe("Object Should not be deleted", Ordered, func() {
Expect(oldPod).ShouldNot(BeNil())

return oldPod
}, 20, 1).ShouldNot(BeNil())
}, defaultConsistentlyDuration, 1).ShouldNot(BeNil())
})
It("check pod is not removed when PruneObjectBehavior is DeleteAll and spec changed", func() {
By("Add PruneObjectBehavior is DeleteAll")
Expand Down Expand Up @@ -784,6 +784,6 @@ var _ = Describe("Object Should not be deleted", Ordered, func() {
Expect(oldPod).ShouldNot(BeNil())

return oldPod
}, 20, 1).ShouldNot(BeNil())
}, defaultConsistentlyDuration, 1).ShouldNot(BeNil())
})
})
8 changes: 5 additions & 3 deletions test/e2e/case21_alternative_kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -74,8 +75,9 @@ var _ = Describe("Test an alternative kubeconfig for policy evaluation", Ordered
Expect(err).ToNot(HaveOccurred())

By("Verifying that a compliance event was created on the parent policy")
compParentEvents := utils.GetMatchingEvents(clientManaged, testNamespace, parentPolicyName,
fmt.Sprintf("policy: %v/%v", testNamespace, policyName), "^Compliant;", defaultTimeoutSeconds)
Expect(compParentEvents).NotTo(BeEmpty())
Eventually(func() []v1.Event {
return utils.GetMatchingEvents(clientManaged, testNamespace, parentPolicyName,
fmt.Sprintf("policy: %v/%v", testNamespace, policyName), "^Compliant;", defaultTimeoutSeconds)
}, defaultTimeoutSeconds, 1).ShouldNot(BeEmpty())
})
})
4 changes: 2 additions & 2 deletions test/e2e/case23_invalid_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var _ = Describe("Test an objectDefinition with an invalid field", Ordered, func
}

return startTime.After(compPlcEvents[len(compPlcEvents)-1].LastTimestamp.Time)
}, defaultTimeoutSeconds, 1).Should(BeTrue())
}, 30, 1).Should(BeTrue())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yiraeChristineKim did you forget to update this value and the other value in this file to use the new global variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I missed these 2 Thanks Let me open pr


By("Verifying the message is correct when the " + configMapName + " ConfigMap already exists")
configmap := &corev1.ConfigMap{
Expand Down Expand Up @@ -118,7 +118,7 @@ var _ = Describe("Test an objectDefinition with an invalid field", Ordered, func
}

return alreadyExistsStartTime.After(compPlcEvents[len(compPlcEvents)-1].LastTimestamp.Time)
}, defaultTimeoutSeconds, 1).Should(BeTrue())
}, 30, 1).Should(BeTrue())
})

AfterAll(func() {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/case31_policy_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = Describe("Test policy history messages when KubeAPI omits values in the
configPolicyName, configPolicyName, "NonCompliant;", defaultTimeoutSeconds))

return eventlen
}, 30, 5).Should(BeNumerically("<", 2))
}, defaultConsistentlyDuration, 5).Should(BeNumerically("<", 2))

By("Checking the events on the parent policy")
// NOTE: pick policy event, these event's reason include ConfigPolicyName
Expand All @@ -39,7 +39,7 @@ var _ = Describe("Test policy history messages when KubeAPI omits values in the
policyName, configPolicyName, "NonCompliant;", defaultTimeoutSeconds))

return eventlen
}, 30, 5).Should(BeNumerically("<", 3))
}, defaultConsistentlyDuration, 5).Should(BeNumerically("<", 3))
}

const (
Expand Down
Loading