8
8
"net/http/httptest"
9
9
"reflect"
10
10
"testing"
11
+ "time"
11
12
12
13
"github.com/stretchr/testify/assert"
13
14
"github.com/stretchr/testify/require"
@@ -25,7 +26,6 @@ import (
25
26
26
27
"github.com/argoproj/gitops-engine/pkg/diff"
27
28
"github.com/argoproj/gitops-engine/pkg/health"
28
- "github.com/argoproj/gitops-engine/pkg/sync/common"
29
29
synccommon "github.com/argoproj/gitops-engine/pkg/sync/common"
30
30
"github.com/argoproj/gitops-engine/pkg/utils/kube"
31
31
"github.com/argoproj/gitops-engine/pkg/utils/kube/kubetest"
@@ -568,47 +568,47 @@ func TestServerResourcesRetry(t *testing.T) {
568
568
apiFailureCount : 0 ,
569
569
expectedAPICalls : 1 ,
570
570
expectedResources : 1 ,
571
- expectedPhase : common .OperationSucceeded ,
571
+ expectedPhase : synccommon .OperationSucceeded ,
572
572
expectedMessage : "success" ,
573
573
},
574
574
{
575
575
desc : "will return success after 1 api failure attempt" ,
576
576
apiFailureCount : 1 ,
577
577
expectedAPICalls : 2 ,
578
578
expectedResources : 1 ,
579
- expectedPhase : common .OperationSucceeded ,
579
+ expectedPhase : synccommon .OperationSucceeded ,
580
580
expectedMessage : "success" ,
581
581
},
582
582
{
583
583
desc : "will return success after 2 api failure attempt" ,
584
584
apiFailureCount : 2 ,
585
585
expectedAPICalls : 3 ,
586
586
expectedResources : 1 ,
587
- expectedPhase : common .OperationSucceeded ,
587
+ expectedPhase : synccommon .OperationSucceeded ,
588
588
expectedMessage : "success" ,
589
589
},
590
590
{
591
591
desc : "will return success after 3 api failure attempt" ,
592
592
apiFailureCount : 3 ,
593
593
expectedAPICalls : 4 ,
594
594
expectedResources : 1 ,
595
- expectedPhase : common .OperationSucceeded ,
595
+ expectedPhase : synccommon .OperationSucceeded ,
596
596
expectedMessage : "success" ,
597
597
},
598
598
{
599
599
desc : "will return success after 4 api failure attempt" ,
600
600
apiFailureCount : 4 ,
601
601
expectedAPICalls : 5 ,
602
602
expectedResources : 1 ,
603
- expectedPhase : common .OperationSucceeded ,
603
+ expectedPhase : synccommon .OperationSucceeded ,
604
604
expectedMessage : "success" ,
605
605
},
606
606
{
607
607
desc : "will fail after 5 api failure attempt" ,
608
608
apiFailureCount : 5 ,
609
609
expectedAPICalls : 5 ,
610
610
expectedResources : 1 ,
611
- expectedPhase : common .OperationFailed ,
611
+ expectedPhase : synccommon .OperationFailed ,
612
612
expectedMessage : "not valid" ,
613
613
},
614
614
{
@@ -617,7 +617,7 @@ func TestServerResourcesRetry(t *testing.T) {
617
617
apiFailureCount : 1 ,
618
618
expectedAPICalls : 1 ,
619
619
expectedResources : 1 ,
620
- expectedPhase : common .OperationFailed ,
620
+ expectedPhase : synccommon .OperationFailed ,
621
621
expectedMessage : "not valid" ,
622
622
},
623
623
}
@@ -1029,21 +1029,60 @@ func TestSyncFailureHookWithFailedSync(t *testing.T) {
1029
1029
1030
1030
func TestBeforeHookCreation (t * testing.T ) {
1031
1031
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 ()
1033
1038
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
+
1034
1043
syncCtx .resources = groupResources (ReconciliationResult {
1035
1044
Live : []* unstructured.Unstructured {hook },
1036
1045
Target : []* unstructured.Unstructured {nil },
1037
1046
})
1038
1047
syncCtx .hooks = []* unstructured.Unstructured {hook }
1039
1048
syncCtx .dynamicIf = fake .NewSimpleDynamicClient (runtime .NewScheme ())
1040
1049
1050
+ // Should delete and recreate Pod, but not set status on hook
1041
1051
syncCtx .Sync ()
1042
-
1043
- _ , _ , resources := syncCtx . GetState ( )
1052
+ phase , message , resources := syncCtx . GetState ()
1053
+ assert . Equal ( t , synccommon . OperationRunning , phase )
1044
1054
assert .Len (t , resources , 1 )
1045
1055
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 )
1047
1086
}
1048
1087
1049
1088
func TestRunSyncFailHooksFailed (t * testing.T ) {
0 commit comments