@@ -121,6 +121,52 @@ func TestSyncNotPermittedNamespace(t *testing.T) {
121121 assert .Contains (t , resources [0 ].Message , "not permitted in project" )
122122}
123123
124+ func TestSyncNamespaceCreatedBeforeDryRunWithoutFailure (t * testing.T ) {
125+ pod := testingutils .NewPod ()
126+ syncCtx := newTestSyncCtx (nil , WithNamespaceModifier (func (_ , _ * unstructured.Unstructured ) (bool , error ) {
127+ return true , nil
128+ }))
129+ syncCtx .resources = groupResources (ReconciliationResult {
130+ Live : []* unstructured.Unstructured {nil , nil },
131+ Target : []* unstructured.Unstructured {pod },
132+ })
133+ syncCtx .Sync ()
134+ phase , msg , resources := syncCtx .GetState ()
135+ assert .Equal (t , synccommon .OperationRunning , phase )
136+ assert .Equal (t , "waiting for healthy state of /Namespace/fake-argocd-ns" , msg )
137+ require .Len (t , resources , 1 )
138+ assert .Equal (t , "Namespace" , resources [0 ].ResourceKey .Kind )
139+ assert .Equal (t , synccommon .ResultCodeSynced , resources [0 ].Status )
140+ }
141+
142+ func TestSyncNamespaceCreatedBeforeDryRunWithFailure (t * testing.T ) {
143+ pod := testingutils .NewPod ()
144+ syncCtx := newTestSyncCtx (nil , WithNamespaceModifier (func (_ , _ * unstructured.Unstructured ) (bool , error ) {
145+ return true , nil
146+ }), func (ctx * syncContext ) {
147+ resourceOps := ctx .resourceOps .(* kubetest.MockResourceOps )
148+ resourceOps .Commands = map [string ]kubetest.KubectlOutput {}
149+ resourceOps .Commands [pod .GetName ()] = kubetest.KubectlOutput {
150+ Output : "should not be returned" ,
151+ Err : errors .New ("invalid object failing dry-run" ),
152+ }
153+ })
154+ syncCtx .resources = groupResources (ReconciliationResult {
155+ Live : []* unstructured.Unstructured {nil , nil },
156+ Target : []* unstructured.Unstructured {pod },
157+ })
158+ syncCtx .Sync ()
159+ phase , msg , resources := syncCtx .GetState ()
160+ assert .Equal (t , synccommon .OperationFailed , phase )
161+ assert .Equal (t , "one or more objects failed to apply (dry run)" , msg )
162+ require .Len (t , resources , 2 )
163+ assert .Equal (t , "Namespace" , resources [0 ].ResourceKey .Kind )
164+ assert .Equal (t , synccommon .ResultCodeSynced , resources [0 ].Status )
165+ assert .Equal (t , "Pod" , resources [1 ].ResourceKey .Kind )
166+ assert .Equal (t , synccommon .ResultCodeSyncFailed , resources [1 ].Status )
167+ assert .Equal (t , "invalid object failing dry-run" , resources [1 ].Message )
168+ }
169+
124170func TestSyncCreateInSortedOrder (t * testing.T ) {
125171 syncCtx := newTestSyncCtx (nil )
126172 syncCtx .resources = groupResources (ReconciliationResult {
@@ -1045,7 +1091,7 @@ func TestNamespaceAutoCreation(t *testing.T) {
10451091
10461092 // Namespace auto creation pre-sync task should not be there
10471093 // since there is namespace resource in syncCtx.resources
1048- t .Run ("no pre-sync task 1 " , func (t * testing.T ) {
1094+ t .Run ("no pre-sync task if resource is managed " , func (t * testing.T ) {
10491095 syncCtx .resources = groupResources (ReconciliationResult {
10501096 Live : []* unstructured.Unstructured {nil },
10511097 Target : []* unstructured.Unstructured {namespace },
@@ -1057,23 +1103,21 @@ func TestNamespaceAutoCreation(t *testing.T) {
10571103 assert .NotContains (t , tasks , task )
10581104 })
10591105
1060- // Namespace auto creation pre-sync task should not be there
1061- // since there is no existing sync result
1062- t .Run ("no pre-sync task 2" , func (t * testing.T ) {
1106+ // Namespace auto creation pre-sync task should be there when it is not managed
1107+ t .Run ("pre-sync task when resource is not managed" , func (t * testing.T ) {
10631108 syncCtx .resources = groupResources (ReconciliationResult {
10641109 Live : []* unstructured.Unstructured {nil },
10651110 Target : []* unstructured.Unstructured {pod },
10661111 })
10671112 tasks , successful := syncCtx .getSyncTasks ()
10681113
10691114 assert .True (t , successful )
1070- assert .Len (t , tasks , 1 )
1071- assert .NotContains (t , tasks , task )
1115+ assert .Len (t , tasks , 2 )
1116+ assert .Contains (t , tasks , task )
10721117 })
10731118
1074- // Namespace auto creation pre-sync task should be there
1075- // since there is existing sync result which means that task created this namespace
1076- t .Run ("pre-sync task created" , func (t * testing.T ) {
1119+ // Namespace auto creation pre-sync task should be there after sync
1120+ t .Run ("pre-sync task when resource is not managed with existing sync" , func (t * testing.T ) {
10771121 syncCtx .resources = groupResources (ReconciliationResult {
10781122 Live : []* unstructured.Unstructured {nil },
10791123 Target : []* unstructured.Unstructured {pod },
@@ -1100,24 +1144,13 @@ func TestNamespaceAutoCreation(t *testing.T) {
11001144
11011145 // Namespace auto creation pre-sync task not should be there
11021146 // since there is no namespace modifier present
1103- t .Run ("no pre-sync task created" , func (t * testing.T ) {
1147+ t .Run ("no pre-sync task created if no modifier " , func (t * testing.T ) {
11041148 syncCtx .resources = groupResources (ReconciliationResult {
11051149 Live : []* unstructured.Unstructured {nil },
11061150 Target : []* unstructured.Unstructured {pod },
11071151 })
1108- syncCtx .syncNamespace = nil
11091152
1110- res := synccommon.ResourceSyncResult {
1111- ResourceKey : kube .GetResourceKey (task .obj ()),
1112- Version : task .version (),
1113- Status : task .syncStatus ,
1114- Message : task .message ,
1115- HookType : task .hookType (),
1116- HookPhase : task .operationState ,
1117- SyncPhase : task .phase ,
1118- }
1119- syncCtx .syncRes = map [string ]synccommon.ResourceSyncResult {}
1120- syncCtx .syncRes [task .resultKey ()] = res
1153+ syncCtx .syncNamespace = nil
11211154
11221155 tasks , successful := syncCtx .getSyncTasks ()
11231156
0 commit comments