@@ -27,6 +27,7 @@ import (
27
27
"k8s.io/client-go/rest"
28
28
testcore "k8s.io/client-go/testing"
29
29
"k8s.io/klog/v2/textlogger"
30
+ cmdutil "k8s.io/kubectl/pkg/cmd/util"
30
31
31
32
"github.com/argoproj/gitops-engine/pkg/diff"
32
33
"github.com/argoproj/gitops-engine/pkg/health"
@@ -860,9 +861,9 @@ func TestSyncContext_ServerSideApplyWithDryRun(t *testing.T) {
860
861
objToUse func (* unstructured.Unstructured ) * unstructured.Unstructured
861
862
}{
862
863
{"BothFlagsFalseAnnotated" , false , false , true , withServerSideApplyAnnotation },
863
- {"scDryRunTrueAnnotated" , true , false , false , withServerSideApplyAnnotation },
864
- {"dryRunTrueAnnotated" , false , true , false , withServerSideApplyAnnotation },
865
- {"BothFlagsTrueAnnotated" , true , true , false , withServerSideApplyAnnotation },
864
+ {"scDryRunTrueAnnotated" , true , false , true , withServerSideApplyAnnotation },
865
+ {"dryRunTrueAnnotated" , false , true , true , withServerSideApplyAnnotation },
866
+ {"BothFlagsTrueAnnotated" , true , true , true , withServerSideApplyAnnotation },
866
867
{"AnnotatedDisabledSSA" , false , false , false , withDisableServerSideApplyAnnotation },
867
868
}
868
869
@@ -873,7 +874,7 @@ func TestSyncContext_ServerSideApplyWithDryRun(t *testing.T) {
873
874
targetObj := tc .objToUse (testingutils .NewPod ())
874
875
875
876
// Execute the shouldUseServerSideApply method and assert expectations
876
- serverSideApply := sc .shouldUseServerSideApply (targetObj , tc . dryRun )
877
+ serverSideApply := sc .shouldUseServerSideApply (targetObj )
877
878
assert .Equal (t , tc .expectedSSA , serverSideApply )
878
879
})
879
880
}
@@ -2180,3 +2181,70 @@ func BenchmarkSync(b *testing.B) {
2180
2181
syncCtx .Sync ()
2181
2182
}
2182
2183
}
2184
+
2185
+ func TestSyncContext_ApplyObjectDryRun (t * testing.T ) {
2186
+ testCases := []struct {
2187
+ name string
2188
+ target * unstructured.Unstructured
2189
+ live * unstructured.Unstructured
2190
+ dryRun bool
2191
+ syncNamespace func (* unstructured.Unstructured , * unstructured.Unstructured ) (bool , error )
2192
+ serverSideApply bool
2193
+ expectedDryRun cmdutil.DryRunStrategy
2194
+ }{
2195
+ {
2196
+ name : "DryRunWithNoAutoCreateNamespace" ,
2197
+ target : withServerSideApplyAnnotation (testingutils .NewPod ()),
2198
+ live : testingutils .NewPod (),
2199
+ dryRun : true ,
2200
+ syncNamespace : nil ,
2201
+ serverSideApply : true ,
2202
+ expectedDryRun : cmdutil .DryRunServer ,
2203
+ },
2204
+ {
2205
+ name : "DryRunWithAutoCreateNamespace" ,
2206
+ target : withServerSideApplyAnnotation (testingutils .NewPod ()),
2207
+ live : testingutils .NewPod (),
2208
+ dryRun : true ,
2209
+ syncNamespace : func (* unstructured.Unstructured , * unstructured.Unstructured ) (bool , error ) { return true , nil },
2210
+ serverSideApply : true ,
2211
+ expectedDryRun : cmdutil .DryRunClient ,
2212
+ },
2213
+ {
2214
+ name : "NoDryRun" ,
2215
+ target : withServerSideApplyAnnotation (testingutils .NewPod ()),
2216
+ live : testingutils .NewPod (),
2217
+ dryRun : false ,
2218
+ syncNamespace : nil ,
2219
+ serverSideApply : true ,
2220
+ expectedDryRun : cmdutil .DryRunNone ,
2221
+ },
2222
+ }
2223
+
2224
+ for _ , tc := range testCases {
2225
+ tc := tc
2226
+ t .Run (tc .name , func (t * testing.T ) {
2227
+ t .Parallel ()
2228
+ syncCtx := newTestSyncCtx (nil )
2229
+ syncCtx .serverSideApply = tc .serverSideApply
2230
+ syncCtx .syncNamespace = tc .syncNamespace
2231
+
2232
+ tc .target .SetNamespace (testingutils .FakeArgoCDNamespace )
2233
+ if tc .live != nil {
2234
+ tc .live .SetNamespace (testingutils .FakeArgoCDNamespace )
2235
+ }
2236
+
2237
+ task := & syncTask {
2238
+ targetObj : tc .target ,
2239
+ liveObj : tc .live ,
2240
+ }
2241
+
2242
+ result , _ := syncCtx .applyObject (task , tc .dryRun , true )
2243
+
2244
+ assert .Equal (t , synccommon .ResultCodeSynced , result )
2245
+
2246
+ resourceOps , _ := syncCtx .resourceOps .(* kubetest.MockResourceOps )
2247
+ assert .Equal (t , tc .expectedDryRun , resourceOps .GetLastDryRunStrategy ())
2248
+ })
2249
+ }
2250
+ }
0 commit comments