@@ -446,6 +446,9 @@ func Test_DeleteEvents_ManagedMode(t *testing.T) {
446446			wq .On ("Done" , & ev )
447447			s , err  :=  NewServer (context .Background (), fac , "argocd" , WithGeneratedTokenSigningKey ())
448448			require .NoError (t , err )
449+ 			s .events  =  event .NewEventSource ("test" )
450+ 			err  =  s .queues .Create ("foo" )
451+ 			require .NoError (t , err )
449452			s .setAgentMode ("foo" , types .AgentModeManaged )
450453
451454			_ , err  =  fac .ApplicationsClientset .ArgoprojV1alpha1 ().Applications (delApp .Namespace ).Create (context .Background (), delApp , v1.CreateOptions {})
@@ -920,3 +923,95 @@ func Test_processClusterCacheInfoUpdateEvent(t *testing.T) {
920923		assert .ErrorContains (t , err , "not mapped to any cluster" )
921924	})
922925}
926+ 
927+ func  Test_ManagedAppRecreationLogic (t  * testing.T ) {
928+ 
929+ 	type  test  struct  {
930+ 		name                             string 
931+ 		deletionTimestampSetOnPrincipal  bool 
932+ 		expectRecreation                 bool 
933+ 	}
934+ 
935+ 	tests  :=  []test {
936+ 		{
937+ 			name :                            "Delete event from managed agent should recreate app when principal is not in deletion state" ,
938+ 			deletionTimestampSetOnPrincipal : false ,
939+ 			expectRecreation :                true ,
940+ 		},
941+ 		{
942+ 			name :                            "Delete event from managed agent should not recreate app when principal is in deletion state" ,
943+ 			deletionTimestampSetOnPrincipal : true ,
944+ 			expectRecreation :                false ,
945+ 		},
946+ 	}
947+ 
948+ 	for  _ , test  :=  range  tests  {
949+ 
950+ 		t .Run (test .name , func (t  * testing.T ) {
951+ 			delApp  :=  & v1alpha1.Application {
952+ 				ObjectMeta : v1.ObjectMeta {
953+ 					Name :       "test-app" ,
954+ 					Namespace :  "agent-managed" ,
955+ 					Finalizers : []string {"test-finalizer" },
956+ 				},
957+ 				Spec : v1alpha1.ApplicationSpec {
958+ 					Project : "default" ,
959+ 					Source : & v1alpha1.ApplicationSource {
960+ 						RepoURL :        "https://github.com/argoproj/argocd-example-apps" ,
961+ 						TargetRevision : "HEAD" ,
962+ 						Path :           "kustomize-guestbook" ,
963+ 					},
964+ 					Destination : v1alpha1.ApplicationDestination {
965+ 						Server :    "https://kubernetes.default.svc" ,
966+ 						Namespace : "guestbook" ,
967+ 					},
968+ 				},
969+ 				Status : v1alpha1.ApplicationStatus {
970+ 					Sync : v1alpha1.SyncStatus {Status : v1alpha1 .SyncStatusCodeSynced },
971+ 				},
972+ 			}
973+ 
974+ 			if  test .deletionTimestampSetOnPrincipal  {
975+ 				delApp .DeletionTimestamp  =  ptr .To (v1.Time {Time : time .Now ()})
976+ 			}
977+ 
978+ 			fac  :=  kube .NewKubernetesFakeClientWithApps ("argocd" )
979+ 			ev  :=  cloudevents .NewEvent ()
980+ 			ev .SetDataSchema ("application" )
981+ 			ev .SetType (event .Delete .String ())
982+ 			ev .SetData (cloudevents .ApplicationJSON , delApp )
983+ 			wq  :=  wqmock.NewTypedRateLimitingInterface [* cloudevents.Event ](t )
984+ 			wq .On ("Get" ).Return (& ev , false )
985+ 			wq .On ("Done" , & ev )
986+ 			s , err  :=  NewServer (context .Background (), fac , "argocd" , WithGeneratedTokenSigningKey ())
987+ 			require .NoError (t , err )
988+ 			s .setAgentMode ("agent-managed" , types .AgentModeManaged )
989+ 			s .events  =  event .NewEventSource ("test" )
990+ 
991+ 			// Set up the send queue for normal operation 
992+ 			err  =  s .queues .Create ("agent-managed" )
993+ 			require .NoError (t , err )
994+ 
995+ 			// Create the application in the principal 
996+ 			_ , err  =  fac .ApplicationsClientset .ArgoprojV1alpha1 ().Applications (delApp .Namespace ).Create (context .Background (), delApp , v1.CreateOptions {})
997+ 			require .NoError (t , err )
998+ 
999+ 			got , err  :=  s .processRecvQueue (context .Background (), "agent-managed" , wq )
1000+ 			require .NoError (t , err )
1001+ 			require .Equal (t , ev , * got )
1002+ 
1003+ 			if  test .expectRecreation  {
1004+ 				// When recreation is expected, the application should still exist in the principal 
1005+ 				// because the deletion was prevented and a recreation event was sent 
1006+ 				_ , err  =  fac .ApplicationsClientset .ArgoprojV1alpha1 ().Applications (delApp .Namespace ).Get (context .Background (), delApp .Name , v1.GetOptions {})
1007+ 				require .NoError (t , err , "Application should still exist in principal when recreation is expected" )
1008+ 			} else  {
1009+ 				// When recreation is not expected, the app should be deleted 
1010+ 				// This happens when the principal app is already in deletion state 
1011+ 				_ , err  =  fac .ApplicationsClientset .ArgoprojV1alpha1 ().Applications (delApp .Namespace ).Get (context .Background (), delApp .Name , v1.GetOptions {})
1012+ 				require .True (t , apierrors .IsNotFound (err ), "Application should be deleted when recreation is not expected" )
1013+ 			}
1014+ 		})
1015+ 	}
1016+ 
1017+ }
0 commit comments