@@ -121,6 +121,52 @@ func TestSyncNotPermittedNamespace(t *testing.T) {
121
121
assert .Contains (t , resources [0 ].Message , "not permitted in project" )
122
122
}
123
123
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
+
124
170
func TestSyncCreateInSortedOrder (t * testing.T ) {
125
171
syncCtx := newTestSyncCtx (nil )
126
172
syncCtx .resources = groupResources (ReconciliationResult {
@@ -1045,7 +1091,7 @@ func TestNamespaceAutoCreation(t *testing.T) {
1045
1091
1046
1092
// Namespace auto creation pre-sync task should not be there
1047
1093
// 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 ) {
1049
1095
syncCtx .resources = groupResources (ReconciliationResult {
1050
1096
Live : []* unstructured.Unstructured {nil },
1051
1097
Target : []* unstructured.Unstructured {namespace },
@@ -1057,23 +1103,21 @@ func TestNamespaceAutoCreation(t *testing.T) {
1057
1103
assert .NotContains (t , tasks , task )
1058
1104
})
1059
1105
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 ) {
1063
1108
syncCtx .resources = groupResources (ReconciliationResult {
1064
1109
Live : []* unstructured.Unstructured {nil },
1065
1110
Target : []* unstructured.Unstructured {pod },
1066
1111
})
1067
1112
tasks , successful := syncCtx .getSyncTasks ()
1068
1113
1069
1114
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 )
1072
1117
})
1073
1118
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 ) {
1077
1121
syncCtx .resources = groupResources (ReconciliationResult {
1078
1122
Live : []* unstructured.Unstructured {nil },
1079
1123
Target : []* unstructured.Unstructured {pod },
@@ -1100,24 +1144,13 @@ func TestNamespaceAutoCreation(t *testing.T) {
1100
1144
1101
1145
// Namespace auto creation pre-sync task not should be there
1102
1146
// 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 ) {
1104
1148
syncCtx .resources = groupResources (ReconciliationResult {
1105
1149
Live : []* unstructured.Unstructured {nil },
1106
1150
Target : []* unstructured.Unstructured {pod },
1107
1151
})
1108
- syncCtx .syncNamespace = nil
1109
1152
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
1121
1154
1122
1155
tasks , successful := syncCtx .getSyncTasks ()
1123
1156
0 commit comments