@@ -29,6 +29,7 @@ import (
29
29
"github.com/argoproj/gitops-engine/pkg/diff"
30
30
"github.com/argoproj/gitops-engine/pkg/health"
31
31
synccommon "github.com/argoproj/gitops-engine/pkg/sync/common"
32
+ "github.com/argoproj/gitops-engine/pkg/sync/hook"
32
33
"github.com/argoproj/gitops-engine/pkg/utils/kube"
33
34
"github.com/argoproj/gitops-engine/pkg/utils/kube/kubetest"
34
35
testingutils "github.com/argoproj/gitops-engine/pkg/utils/testing"
@@ -1285,15 +1286,19 @@ func (r resourceNameHealthOverride) GetResourceHealth(obj *unstructured.Unstruct
1285
1286
}
1286
1287
1287
1288
func TestRunSync_HooksNotDeletedIfPhaseNotCompleted (t * testing.T ) {
1288
- completedHook := newHook (synccommon .HookTypePreSync )
1289
- completedHook .SetName ("completed-hook" )
1290
- completedHook .SetNamespace (testingutils .FakeArgoCDNamespace )
1291
- _ = testingutils .Annotate (completedHook , synccommon .AnnotationKeyHookDeletePolicy , "HookSucceeded" )
1292
-
1293
- inProgressHook := newHook (synccommon .HookTypePreSync )
1294
- inProgressHook .SetNamespace (testingutils .FakeArgoCDNamespace )
1295
- inProgressHook .SetName ("in-progress-hook" )
1296
- _ = testingutils .Annotate (inProgressHook , synccommon .AnnotationKeyHookDeletePolicy , "HookSucceeded" )
1289
+ hook1 := newHook (synccommon .HookTypePreSync )
1290
+ hook1 .SetName ("completed-hook" )
1291
+ hook1 .SetNamespace (testingutils .FakeArgoCDNamespace )
1292
+ _ = testingutils .Annotate (hook1 , synccommon .AnnotationKeyHookDeletePolicy , string (synccommon .HookDeletePolicyHookSucceeded ))
1293
+ completedHook := hook1 .DeepCopy ()
1294
+ completedHook .SetFinalizers (append (completedHook .GetFinalizers (), hook .HookFinalizer ))
1295
+
1296
+ hook2 := newHook (synccommon .HookTypePreSync )
1297
+ hook2 .SetNamespace (testingutils .FakeArgoCDNamespace )
1298
+ hook2 .SetName ("in-progress-hook" )
1299
+ _ = testingutils .Annotate (hook2 , synccommon .AnnotationKeyHookDeletePolicy , string (synccommon .HookDeletePolicyHookSucceeded ))
1300
+ inProgressHook := hook2 .DeepCopy ()
1301
+ inProgressHook .SetFinalizers (append (inProgressHook .GetFinalizers (), hook .HookFinalizer ))
1297
1302
1298
1303
syncCtx := newTestSyncCtx (nil ,
1299
1304
WithHealthOverride (resourceNameHealthOverride (map [string ]health.HealthStatusCode {
@@ -1312,6 +1317,12 @@ func TestRunSync_HooksNotDeletedIfPhaseNotCompleted(t *testing.T) {
1312
1317
))
1313
1318
fakeDynamicClient := fake .NewSimpleDynamicClient (runtime .NewScheme ())
1314
1319
syncCtx .dynamicIf = fakeDynamicClient
1320
+ updatedCount := 0
1321
+ fakeDynamicClient .PrependReactor ("update" , "*" , func (_ testcore.Action ) (handled bool , ret runtime.Object , err error ) {
1322
+ // Removing the finalizers
1323
+ updatedCount ++
1324
+ return true , nil , nil
1325
+ })
1315
1326
deletedCount := 0
1316
1327
fakeDynamicClient .PrependReactor ("delete" , "*" , func (_ testcore.Action ) (handled bool , ret runtime.Object , err error ) {
1317
1328
deletedCount ++
@@ -1321,7 +1332,7 @@ func TestRunSync_HooksNotDeletedIfPhaseNotCompleted(t *testing.T) {
1321
1332
Live : []* unstructured.Unstructured {completedHook , inProgressHook },
1322
1333
Target : []* unstructured.Unstructured {nil , nil },
1323
1334
})
1324
- syncCtx .hooks = []* unstructured.Unstructured {completedHook , inProgressHook }
1335
+ syncCtx .hooks = []* unstructured.Unstructured {hook1 , hook2 }
1325
1336
1326
1337
syncCtx .kubectl = & kubetest.MockKubectlCmd {
1327
1338
Commands : map [string ]kubetest.KubectlOutput {},
@@ -1330,19 +1341,24 @@ func TestRunSync_HooksNotDeletedIfPhaseNotCompleted(t *testing.T) {
1330
1341
syncCtx .Sync ()
1331
1342
1332
1343
assert .Equal (t , synccommon .OperationRunning , syncCtx .phase )
1344
+ assert .Equal (t , 0 , updatedCount )
1333
1345
assert .Equal (t , 0 , deletedCount )
1334
1346
}
1335
1347
1336
1348
func TestRunSync_HooksDeletedAfterPhaseCompleted (t * testing.T ) {
1337
- completedHook1 := newHook (synccommon .HookTypePreSync )
1338
- completedHook1 .SetName ("completed-hook1" )
1339
- completedHook1 .SetNamespace (testingutils .FakeArgoCDNamespace )
1340
- _ = testingutils .Annotate (completedHook1 , synccommon .AnnotationKeyHookDeletePolicy , "HookSucceeded" )
1341
-
1342
- completedHook2 := newHook (synccommon .HookTypePreSync )
1343
- completedHook2 .SetNamespace (testingutils .FakeArgoCDNamespace )
1344
- completedHook2 .SetName ("completed-hook2" )
1345
- _ = testingutils .Annotate (completedHook2 , synccommon .AnnotationKeyHookDeletePolicy , "HookSucceeded" )
1349
+ hook1 := newHook (synccommon .HookTypePreSync )
1350
+ hook1 .SetName ("completed-hook1" )
1351
+ hook1 .SetNamespace (testingutils .FakeArgoCDNamespace )
1352
+ _ = testingutils .Annotate (hook1 , synccommon .AnnotationKeyHookDeletePolicy , string (synccommon .HookDeletePolicyHookSucceeded ))
1353
+ completedHook1 := hook1 .DeepCopy ()
1354
+ completedHook1 .SetFinalizers (append (completedHook1 .GetFinalizers (), hook .HookFinalizer ))
1355
+
1356
+ hook2 := newHook (synccommon .HookTypePreSync )
1357
+ hook2 .SetNamespace (testingutils .FakeArgoCDNamespace )
1358
+ hook2 .SetName ("completed-hook2" )
1359
+ _ = testingutils .Annotate (hook2 , synccommon .AnnotationKeyHookDeletePolicy , string (synccommon .HookDeletePolicyHookSucceeded ))
1360
+ completedHook2 := hook2 .DeepCopy ()
1361
+ completedHook2 .SetFinalizers (append (completedHook1 .GetFinalizers (), hook .HookFinalizer ))
1346
1362
1347
1363
syncCtx := newTestSyncCtx (nil ,
1348
1364
WithInitialState (synccommon .OperationRunning , "" , []synccommon.ResourceSyncResult {{
@@ -1358,6 +1374,12 @@ func TestRunSync_HooksDeletedAfterPhaseCompleted(t *testing.T) {
1358
1374
))
1359
1375
fakeDynamicClient := fake .NewSimpleDynamicClient (runtime .NewScheme ())
1360
1376
syncCtx .dynamicIf = fakeDynamicClient
1377
+ updatedCount := 0
1378
+ fakeDynamicClient .PrependReactor ("update" , "*" , func (_ testcore.Action ) (handled bool , ret runtime.Object , err error ) {
1379
+ // Removing the finalizers
1380
+ updatedCount ++
1381
+ return true , nil , nil
1382
+ })
1361
1383
deletedCount := 0
1362
1384
fakeDynamicClient .PrependReactor ("delete" , "*" , func (_ testcore.Action ) (handled bool , ret runtime.Object , err error ) {
1363
1385
deletedCount ++
@@ -1367,7 +1389,7 @@ func TestRunSync_HooksDeletedAfterPhaseCompleted(t *testing.T) {
1367
1389
Live : []* unstructured.Unstructured {completedHook1 , completedHook2 },
1368
1390
Target : []* unstructured.Unstructured {nil , nil },
1369
1391
})
1370
- syncCtx .hooks = []* unstructured.Unstructured {completedHook1 , completedHook2 }
1392
+ syncCtx .hooks = []* unstructured.Unstructured {hook1 , hook2 }
1371
1393
1372
1394
syncCtx .kubectl = & kubetest.MockKubectlCmd {
1373
1395
Commands : map [string ]kubetest.KubectlOutput {},
@@ -1376,19 +1398,24 @@ func TestRunSync_HooksDeletedAfterPhaseCompleted(t *testing.T) {
1376
1398
syncCtx .Sync ()
1377
1399
1378
1400
assert .Equal (t , synccommon .OperationSucceeded , syncCtx .phase )
1401
+ assert .Equal (t , 2 , updatedCount )
1379
1402
assert .Equal (t , 2 , deletedCount )
1380
1403
}
1381
1404
1382
1405
func TestRunSync_HooksDeletedAfterPhaseCompletedFailed (t * testing.T ) {
1383
- completedHook1 := newHook (synccommon .HookTypeSync )
1384
- completedHook1 .SetName ("completed-hook1" )
1385
- completedHook1 .SetNamespace (testingutils .FakeArgoCDNamespace )
1386
- _ = testingutils .Annotate (completedHook1 , synccommon .AnnotationKeyHookDeletePolicy , "HookFailed" )
1387
-
1388
- completedHook2 := newHook (synccommon .HookTypeSync )
1389
- completedHook2 .SetNamespace (testingutils .FakeArgoCDNamespace )
1390
- completedHook2 .SetName ("completed-hook2" )
1391
- _ = testingutils .Annotate (completedHook2 , synccommon .AnnotationKeyHookDeletePolicy , "HookFailed" )
1406
+ hook1 := newHook (synccommon .HookTypeSync )
1407
+ hook1 .SetName ("completed-hook1" )
1408
+ hook1 .SetNamespace (testingutils .FakeArgoCDNamespace )
1409
+ _ = testingutils .Annotate (hook1 , synccommon .AnnotationKeyHookDeletePolicy , string (synccommon .HookDeletePolicyHookFailed ))
1410
+ completedHook1 := hook1 .DeepCopy ()
1411
+ completedHook1 .SetFinalizers (append (completedHook1 .GetFinalizers (), hook .HookFinalizer ))
1412
+
1413
+ hook2 := newHook (synccommon .HookTypeSync )
1414
+ hook2 .SetNamespace (testingutils .FakeArgoCDNamespace )
1415
+ hook2 .SetName ("completed-hook2" )
1416
+ _ = testingutils .Annotate (hook2 , synccommon .AnnotationKeyHookDeletePolicy , string (synccommon .HookDeletePolicyHookFailed ))
1417
+ completedHook2 := hook2 .DeepCopy ()
1418
+ completedHook2 .SetFinalizers (append (completedHook1 .GetFinalizers (), hook .HookFinalizer ))
1392
1419
1393
1420
syncCtx := newTestSyncCtx (nil ,
1394
1421
WithInitialState (synccommon .OperationRunning , "" , []synccommon.ResourceSyncResult {{
@@ -1404,6 +1431,12 @@ func TestRunSync_HooksDeletedAfterPhaseCompletedFailed(t *testing.T) {
1404
1431
))
1405
1432
fakeDynamicClient := fake .NewSimpleDynamicClient (runtime .NewScheme ())
1406
1433
syncCtx .dynamicIf = fakeDynamicClient
1434
+ updatedCount := 0
1435
+ fakeDynamicClient .PrependReactor ("update" , "*" , func (_ testcore.Action ) (handled bool , ret runtime.Object , err error ) {
1436
+ // Removing the finalizers
1437
+ updatedCount ++
1438
+ return true , nil , nil
1439
+ })
1407
1440
deletedCount := 0
1408
1441
fakeDynamicClient .PrependReactor ("delete" , "*" , func (_ testcore.Action ) (handled bool , ret runtime.Object , err error ) {
1409
1442
deletedCount ++
@@ -1413,7 +1446,7 @@ func TestRunSync_HooksDeletedAfterPhaseCompletedFailed(t *testing.T) {
1413
1446
Live : []* unstructured.Unstructured {completedHook1 , completedHook2 },
1414
1447
Target : []* unstructured.Unstructured {nil , nil },
1415
1448
})
1416
- syncCtx .hooks = []* unstructured.Unstructured {completedHook1 , completedHook2 }
1449
+ syncCtx .hooks = []* unstructured.Unstructured {hook1 , hook2 }
1417
1450
1418
1451
syncCtx .kubectl = & kubetest.MockKubectlCmd {
1419
1452
Commands : map [string ]kubetest.KubectlOutput {},
@@ -1422,6 +1455,7 @@ func TestRunSync_HooksDeletedAfterPhaseCompletedFailed(t *testing.T) {
1422
1455
syncCtx .Sync ()
1423
1456
1424
1457
assert .Equal (t , synccommon .OperationFailed , syncCtx .phase )
1458
+ assert .Equal (t , 2 , updatedCount )
1425
1459
assert .Equal (t , 2 , deletedCount )
1426
1460
}
1427
1461
0 commit comments