Skip to content

Commit e4d98aa

Browse files
committed
thoroughly test BeforeHookCreation
Signed-off-by: Krzysztof Nazarewski <3494992+nazarewk@users.noreply.github.com>
1 parent 327f8d9 commit e4d98aa

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

pkg/sync/sync_context.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ func (sc *syncContext) getOperationPhase(hook *unstructured.Unstructured) (commo
302302
// https://github.com/argoproj/argo-cd/blob/9fac0f6ae6e52d6f4978a1eaaf51fbffb9c0958a/controller/sync.go#L465-L485
303303
for _, policy := range hookutil.DeletePolicies(hook) {
304304
if policy == common.HookDeletePolicyBeforeHookCreation && sc.startedAt.After(hook.GetCreationTimestamp().Time) {
305-
return common.OperationRunning, fmt.Sprintf("%s pending recreation", hook.GetName()), nil
305+
key := kube.GetResourceKey(hook)
306+
return common.OperationRunning, fmt.Sprintf("%s is recreating", key.String()), nil
306307
}
307308
}
308309

pkg/sync/sync_context_test.go

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http/httptest"
99
"reflect"
1010
"testing"
11+
"time"
1112

1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
@@ -25,7 +26,6 @@ import (
2526

2627
"github.com/argoproj/gitops-engine/pkg/diff"
2728
"github.com/argoproj/gitops-engine/pkg/health"
28-
"github.com/argoproj/gitops-engine/pkg/sync/common"
2929
synccommon "github.com/argoproj/gitops-engine/pkg/sync/common"
3030
"github.com/argoproj/gitops-engine/pkg/utils/kube"
3131
"github.com/argoproj/gitops-engine/pkg/utils/kube/kubetest"
@@ -568,47 +568,47 @@ func TestServerResourcesRetry(t *testing.T) {
568568
apiFailureCount: 0,
569569
expectedAPICalls: 1,
570570
expectedResources: 1,
571-
expectedPhase: common.OperationSucceeded,
571+
expectedPhase: synccommon.OperationSucceeded,
572572
expectedMessage: "success",
573573
},
574574
{
575575
desc: "will return success after 1 api failure attempt",
576576
apiFailureCount: 1,
577577
expectedAPICalls: 2,
578578
expectedResources: 1,
579-
expectedPhase: common.OperationSucceeded,
579+
expectedPhase: synccommon.OperationSucceeded,
580580
expectedMessage: "success",
581581
},
582582
{
583583
desc: "will return success after 2 api failure attempt",
584584
apiFailureCount: 2,
585585
expectedAPICalls: 3,
586586
expectedResources: 1,
587-
expectedPhase: common.OperationSucceeded,
587+
expectedPhase: synccommon.OperationSucceeded,
588588
expectedMessage: "success",
589589
},
590590
{
591591
desc: "will return success after 3 api failure attempt",
592592
apiFailureCount: 3,
593593
expectedAPICalls: 4,
594594
expectedResources: 1,
595-
expectedPhase: common.OperationSucceeded,
595+
expectedPhase: synccommon.OperationSucceeded,
596596
expectedMessage: "success",
597597
},
598598
{
599599
desc: "will return success after 4 api failure attempt",
600600
apiFailureCount: 4,
601601
expectedAPICalls: 5,
602602
expectedResources: 1,
603-
expectedPhase: common.OperationSucceeded,
603+
expectedPhase: synccommon.OperationSucceeded,
604604
expectedMessage: "success",
605605
},
606606
{
607607
desc: "will fail after 5 api failure attempt",
608608
apiFailureCount: 5,
609609
expectedAPICalls: 5,
610610
expectedResources: 1,
611-
expectedPhase: common.OperationFailed,
611+
expectedPhase: synccommon.OperationFailed,
612612
expectedMessage: "not valid",
613613
},
614614
{
@@ -617,7 +617,7 @@ func TestServerResourcesRetry(t *testing.T) {
617617
apiFailureCount: 1,
618618
expectedAPICalls: 1,
619619
expectedResources: 1,
620-
expectedPhase: common.OperationFailed,
620+
expectedPhase: synccommon.OperationFailed,
621621
expectedMessage: "not valid",
622622
},
623623
}
@@ -1029,21 +1029,60 @@ func TestSyncFailureHookWithFailedSync(t *testing.T) {
10291029

10301030
func TestBeforeHookCreation(t *testing.T) {
10311031
syncCtx := newTestSyncCtx()
1032-
hook := Annotate(Annotate(NewPod(), synccommon.AnnotationKeyHook, "Sync"), synccommon.AnnotationKeyHookDeletePolicy, "BeforeHookCreation")
1032+
1033+
syncCtx.startedAt = time.Date(2022, 9, 14, 0, 0, 0, 0, time.UTC)
1034+
previousCreatedAt := syncCtx.startedAt.Add(-time.Hour)
1035+
newCreatedAt := syncCtx.startedAt.Add(time.Second)
1036+
1037+
hook := NewPod()
10331038
hook.SetNamespace(FakeArgoCDNamespace)
1039+
hook = Annotate(hook, synccommon.AnnotationKeyHook, string(synccommon.HookTypePreSync))
1040+
hook = Annotate(hook, synccommon.AnnotationKeyHookDeletePolicy, string(synccommon.HookDeletePolicyBeforeHookCreation))
1041+
hook.SetCreationTimestamp(metav1.NewTime(previousCreatedAt))
1042+
10341043
syncCtx.resources = groupResources(ReconciliationResult{
10351044
Live: []*unstructured.Unstructured{hook},
10361045
Target: []*unstructured.Unstructured{nil},
10371046
})
10381047
syncCtx.hooks = []*unstructured.Unstructured{hook}
10391048
syncCtx.dynamicIf = fake.NewSimpleDynamicClient(runtime.NewScheme())
10401049

1050+
// Should delete and recreate Pod, but not set status on hook
10411051
syncCtx.Sync()
1042-
1043-
_, _, resources := syncCtx.GetState()
1052+
phase, message, resources := syncCtx.GetState()
1053+
assert.Equal(t, synccommon.OperationRunning, phase)
10441054
assert.Len(t, resources, 1)
10451055
assert.Empty(t, resources[0].Message)
1046-
assert.Equal(t, "waiting for completion of hook /Pod/my-pod", syncCtx.message)
1056+
assert.Equal(t, "waiting for completion of hook /Pod/my-pod", message)
1057+
1058+
// Should mark hook as running, because fresh object was not registered yet
1059+
syncCtx.Sync()
1060+
phase, message, resources = syncCtx.GetState()
1061+
assert.Equal(t, synccommon.OperationRunning, phase)
1062+
assert.Len(t, resources, 1)
1063+
assert.Equal(t, "/Pod/fake-argocd-ns/my-pod is recreating", resources[0].Message)
1064+
assert.Equal(t, "waiting for completion of hook /Pod/my-pod", message)
1065+
1066+
// fresh hook was registered in Pending state, so should still be running
1067+
hook.SetCreationTimestamp(metav1.NewTime(newCreatedAt))
1068+
assert.Nil(t, unstructured.SetNestedField(hook.Object, string(corev1.PodPending), "status", "phase"))
1069+
syncCtx.Sync()
1070+
phase, message, resources = syncCtx.GetState()
1071+
assert.Equal(t, synccommon.OperationRunning, phase)
1072+
assert.Len(t, resources, 1)
1073+
assert.Equal(t, "/Pod/fake-argocd-ns/my-pod is recreating", resources[0].Message)
1074+
assert.Equal(t, "waiting for completion of hook /Pod/my-pod", message)
1075+
1076+
// hook finished so should succeed
1077+
statusMessage := "finished"
1078+
assert.Nil(t, unstructured.SetNestedField(hook.Object, string(corev1.PodSucceeded), "status", "phase"))
1079+
assert.Nil(t, unstructured.SetNestedField(hook.Object, statusMessage, "status", "message"))
1080+
syncCtx.Sync()
1081+
phase, message, resources = syncCtx.GetState()
1082+
assert.Equal(t, synccommon.OperationSucceeded, phase)
1083+
assert.Len(t, resources, 1)
1084+
assert.Equal(t, statusMessage, resources[0].Message)
1085+
assert.Equal(t, "successfully synced (no more tasks)", message)
10471086
}
10481087

10491088
func TestRunSyncFailHooksFailed(t *testing.T) {

0 commit comments

Comments
 (0)