From 2c2a2d23f84ec07ccd0028b6a79083ed42b2270f Mon Sep 17 00:00:00 2001 From: Dejan Zele Pejchev Date: Tue, 10 Dec 2024 00:44:38 +0100 Subject: [PATCH] feat: add test for hook finalizer which prevents external resource deletion Signed-off-by: Dejan Zele Pejchev --- go.mod | 2 ++ test/e2e/hook_test.go | 28 +++++++++++++++++++ .../hook.yaml | 18 ++++++++++++ .../hook-resource-deleted-externally/pod.yaml | 12 ++++++++ 4 files changed, 60 insertions(+) create mode 100644 test/e2e/testdata/hook-resource-deleted-externally/hook.yaml create mode 100644 test/e2e/testdata/hook-resource-deleted-externally/pod.yaml diff --git a/go.mod b/go.mod index b756f2fe16b110..45fdbf78e7cfa3 100644 --- a/go.mod +++ b/go.mod @@ -337,3 +337,5 @@ replace ( k8s.io/sample-cli-plugin => k8s.io/sample-cli-plugin v0.31.0 k8s.io/sample-controller => k8s.io/sample-controller v0.31.0 ) + +replace github.com/argoproj/gitops-engine => github.com/dejanzele/gitops-engine v0.0.0-20241209160843-57d66fd87fa7 diff --git a/test/e2e/hook_test.go b/test/e2e/hook_test.go index a5385669862132..31753962e42ced 100644 --- a/test/e2e/hook_test.go +++ b/test/e2e/hook_test.go @@ -427,3 +427,31 @@ func TestAutomaticallyNamingUnnamedHook(t *testing.T) { assert.Contains(t, resources[2].Name, "postsync") }) } + +func TestHookFinalizerPreSync(t *testing.T) { + testHookFinalizer(t, HookTypePreSync) +} + +func TestHookFinalizerSync(t *testing.T) { + testHookFinalizer(t, HookTypeSync) +} + +func TestHookFinalizerPostSync(t *testing.T) { + testHookFinalizer(t, HookTypePostSync) +} + +func testHookFinalizer(t *testing.T, hookType HookType) { + Given(t). + Path("hook-resource-deleted-externally"). + When(). + PatchFile("hook.yaml", fmt.Sprintf(`[{"op": "replace", "path": "/metadata/annotations", "value": {"argocd.argoproj.io/hook": "%s"}}]`, hookType)). + CreateApp(). + Sync(). + Then(). + Expect(OperationPhaseIs(OperationSucceeded)). + Expect(SyncStatusIs(SyncStatusCodeSynced)). + Expect(ResourceSyncStatusIs("Pod", "pod", SyncStatusCodeSynced)). + Expect(ResourceHealthIs("Pod", "pod", health.HealthStatusHealthy)). + Expect(ResourceResultNumbering(2)). + Expect(ResourceResultIs(ResourceResult{Group: "batch", Version: "v1", Kind: "Job", Namespace: DeploymentNamespace(), Name: "hook", Message: "Reached expected number of succeeded pods", HookType: hookType, HookPhase: OperationSucceeded, SyncPhase: SyncPhase(hookType)})) +} diff --git a/test/e2e/testdata/hook-resource-deleted-externally/hook.yaml b/test/e2e/testdata/hook-resource-deleted-externally/hook.yaml new file mode 100644 index 00000000000000..1bb04369c95160 --- /dev/null +++ b/test/e2e/testdata/hook-resource-deleted-externally/hook.yaml @@ -0,0 +1,18 @@ +apiVersion: batch/v1 +kind: Job +metadata: + annotations: + argocd.argoproj.io/hook: Sync + name: hook +spec: + ttlSecondsAfterFinished: 0 + backoffLimit: 0 + template: + spec: + containers: + - command: + - "true" + image: quay.io/argoprojlabs/argocd-e2e-container:0.1 + imagePullPolicy: IfNotPresent + name: main + restartPolicy: Never \ No newline at end of file diff --git a/test/e2e/testdata/hook-resource-deleted-externally/pod.yaml b/test/e2e/testdata/hook-resource-deleted-externally/pod.yaml new file mode 100644 index 00000000000000..001aa52b04cec9 --- /dev/null +++ b/test/e2e/testdata/hook-resource-deleted-externally/pod.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Pod +metadata: + name: pod +spec: + containers: + - name: main + image: quay.io/argoprojlabs/argocd-e2e-container:0.1 + imagePullPolicy: IfNotPresent + command: + - "true" + restartPolicy: Never