Skip to content

Commit 5711c68

Browse files
authored
Stabilize the testcases (#2)
* Use patch instead of update when tests patch policies This prevents race conditions of the different controllers updating the policy when the test tries to update the remediation action. Signed-off-by: mprahl <[email protected]> * Print the events when case3 fails for easier to debugging Signed-off-by: mprahl <[email protected]> * Handle when the template sync encounters a conflict When there is a conflict, the reconcile should be retried rather than emit a policy violation. This will make testcase 3 more reliable. Signed-off-by: mprahl <[email protected]> * Make testcase 9 more reliable This checks just the remediationAction rather than the whole spec in certain sections since that is what is being verified has changed. Signed-off-by: mprahl <[email protected]> Signed-off-by: mprahl <[email protected]>
1 parent e41d0eb commit 5711c68

File tree

7 files changed

+33
-43
lines changed

7 files changed

+33
-43
lines changed

controllers/templatesync/template_sync.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
303303

304304
_, err = res.Update(ctx, eObject, metav1.UpdateOptions{})
305305
if err != nil {
306+
// If the policy template retrieved from the cache has since changed, there will be a conflict error
307+
// and the reconcile should be retried since this is recoverable.
308+
if errors.IsConflict(err) {
309+
return reconcile.Result{}, err
310+
}
311+
306312
resultError = err
307313
errMsg := fmt.Sprintf("Failed to update policy template %s: %s", tName, err)
308314

test/e2e/case1_mutation_recovery_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ var _ = Describe("Test mutation recovery", func() {
4747
managedPlc := utils.GetWithTimeout(
4848
clientManagedDynamic, gvrPolicy, case1PolicyName, clusterNamespace, true, defaultTimeoutSeconds,
4949
)
50-
Expect(managedPlc.Object["spec"].(map[string]interface{})["remediationAction"]).To(Equal("inform"))
51-
managedPlc.Object["spec"].(map[string]interface{})["remediationAction"] = "enforce"
52-
_, err := clientManagedDynamic.Resource(gvrPolicy).Namespace(clusterNamespace).Update(
53-
context.TODO(), managedPlc, metav1.UpdateOptions{},
54-
)
50+
_, err := patchRemediationAction(clientManagedDynamic, managedPlc, "enforce")
5551

5652
return err
5753
},

test/e2e/case3_multiple_templates_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ var _ = Describe("Test status sync with multiple templates", func() {
4747
Expect(hubPlc).NotTo(BeNil())
4848
})
4949
AfterEach(func() {
50+
if CurrentSpecReport().Failed() {
51+
_, err := utils.KubectlWithOutput("-n", clusterNamespace, "get", "events")
52+
53+
Expect(err).To(BeNil())
54+
}
55+
5056
By("Deleting a policy on hub cluster in ns:" + clusterNamespaceOnHub)
5157
_, err := kubectlHub("delete", "-f", case3PolicyYaml, "-n", clusterNamespaceOnHub)
5258
Expect(err).To(BeNil())

test/e2e/case7_spec_sync_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package e2e
55

66
import (
7-
"context"
8-
97
. "github.com/onsi/ginkgo/v2"
108
. "github.com/onsi/gomega"
119
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -59,11 +57,7 @@ var _ = Describe("Test spec sync", func() {
5957
defaultTimeoutSeconds)
6058
Expect(hubPlc).NotTo(BeNil())
6159
Expect(hubPlc.Object["spec"].(map[string]interface{})["remediationAction"]).To(Equal("inform"))
62-
hubPlc.Object["spec"].(map[string]interface{})["remediationAction"] = "enforce"
63-
hubPlc, err := clientHubDynamic.Resource(gvrPolicy).Namespace(clusterNamespaceOnHub).Update(
64-
context.TODO(),
65-
hubPlc,
66-
metav1.UpdateOptions{})
60+
hubPlc, err := patchRemediationAction(clientHubDynamic, hubPlc, "enforce")
6761
Expect(err).To(BeNil())
6862
Eventually(func() interface{} {
6963
managedPlc := utils.GetWithTimeout(

test/e2e/case9_template_sync_test.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package e2e
55

66
import (
7-
"context"
87
"errors"
98
"os/exec"
109

@@ -55,36 +54,29 @@ var _ = Describe("Test template sync", func() {
5554
plc := utils.GetWithTimeout(
5655
clientHubDynamic, gvrPolicy, case9PolicyName, clusterNamespaceOnHub, true, defaultTimeoutSeconds,
5756
)
58-
plc.Object["spec"].(map[string]interface{})["remediationAction"] = "enforce"
59-
plc, err := clientHubDynamic.Resource(gvrPolicy).Namespace(clusterNamespaceOnHub).Update(
60-
context.TODO(), plc, metav1.UpdateOptions{},
61-
)
57+
plc, err := patchRemediationAction(clientHubDynamic, plc, "enforce")
6258
Expect(err).To(BeNil())
6359
Expect(plc.Object["spec"].(map[string]interface{})["remediationAction"]).To(Equal("enforce"))
6460
By("Checking template policy remediationAction")
65-
yamlStr := "../resources/case9_template_sync/case9-config-policy-enforce.yaml"
66-
yamlTrustedPlc := utils.ParseYaml(yamlStr)
6761
Eventually(func() interface{} {
6862
trustedPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigurationPolicy,
6963
case9ConfigPolicyName, clusterNamespace, true, defaultTimeoutSeconds)
7064

71-
return trustedPlc.Object["spec"]
72-
}, defaultTimeoutSeconds, 1).Should(utils.SemanticEqual(yamlTrustedPlc.Object["spec"]))
65+
return trustedPlc.Object["spec"].(map[string]interface{})["remediationAction"]
66+
}, defaultTimeoutSeconds, 1).Should(Equal("enforce"))
7367
})
7468
It("should still override remediationAction in spec when there is no remediationAction", func() {
7569
By("Updating policy with no remediationAction")
7670
_, err := kubectlHub("apply", "-f",
7771
"../resources/case9_template_sync/case9-test-policy-no-remediation.yaml", "-n", clusterNamespaceOnHub)
7872
Expect(err).Should(BeNil())
7973
By("Checking template policy remediationAction")
80-
yamlTrustedPlc := utils.ParseYaml(
81-
"../resources/case9_template_sync/case9-config-policy-enforce.yaml")
8274
Eventually(func() interface{} {
8375
trustedPlc := utils.GetWithTimeout(clientManagedDynamic, gvrConfigurationPolicy,
8476
case9ConfigPolicyName, clusterNamespace, true, defaultTimeoutSeconds)
8577

86-
return trustedPlc.Object["spec"]
87-
}, defaultTimeoutSeconds, 1).Should(utils.SemanticEqual(yamlTrustedPlc.Object["spec"]))
78+
return trustedPlc.Object["spec"].(map[string]interface{})["remediationAction"]
79+
}, defaultTimeoutSeconds, 1).Should(Equal("enforce"))
8880
})
8981
It("should contains labels from parent policy", func() {
9082
By("Checking labels of template policy")

test/e2e/e2e_suite_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import (
1717
corev1 "k8s.io/api/core/v1"
1818
"k8s.io/apimachinery/pkg/api/errors"
1919
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2021
"k8s.io/apimachinery/pkg/runtime/schema"
22+
"k8s.io/apimachinery/pkg/types"
2123
"k8s.io/client-go/dynamic"
2224
"k8s.io/client-go/kubernetes"
2325
"k8s.io/client-go/rest"
@@ -229,3 +231,15 @@ func kubectlManaged(args ...string) (string, error) {
229231

230232
return propagatorutils.KubectlWithOutput(args...)
231233
}
234+
235+
func patchRemediationAction(
236+
client dynamic.Interface, plc *unstructured.Unstructured, remediationAction string,
237+
) (
238+
*unstructured.Unstructured, error,
239+
) {
240+
patch := []byte(`[{"op": "replace", "path": "/spec/remediationAction", "value": "` + remediationAction + `"}]`)
241+
242+
return client.Resource(gvrPolicy).Namespace(plc.GetNamespace()).Patch(
243+
context.TODO(), plc.GetName(), types.JSONPatchType, patch, metav1.PatchOptions{},
244+
)
245+
}

test/resources/case9_template_sync/case9-config-policy-enforce.yaml

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)