@@ -5,13 +5,14 @@ import (
5
5
"encoding/json"
6
6
"errors"
7
7
"fmt"
8
- apierrors "k8s.io/apimachinery/pkg/api/errors"
9
- "k8s.io/apimachinery/pkg/runtime/schema"
10
8
"net/http"
11
9
"net/http/httptest"
12
10
"reflect"
13
11
"testing"
14
12
13
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
14
+ "k8s.io/apimachinery/pkg/runtime/schema"
15
+
15
16
"github.com/stretchr/testify/assert"
16
17
"github.com/stretchr/testify/require"
17
18
corev1 "k8s.io/api/core/v1"
@@ -70,11 +71,15 @@ func newTestSyncCtx(getResourceFunc *func(ctx context.Context, config *rest.Conf
70
71
return nil
71
72
}
72
73
mockKubectl := kubetest.MockKubectlCmd {}
74
+
75
+ sc .kubectl = & mockKubectl
76
+ mockResourceOps := kubetest.MockResourceOps {}
77
+ sc .resourceOps = & mockResourceOps
73
78
if getResourceFunc != nil {
74
79
mockKubectl .WithGetResourceFunc (* getResourceFunc )
80
+ mockResourceOps .WithGetResourceFunc (* getResourceFunc )
75
81
}
76
- sc .kubectl = & mockKubectl
77
- sc .resourceOps = & mockKubectl
82
+
78
83
for _ , opt := range opts {
79
84
opt (& sc )
80
85
}
@@ -94,8 +99,9 @@ func TestSyncValidate(t *testing.T) {
94
99
95
100
syncCtx .Sync ()
96
101
97
- kubectl := syncCtx .kubectl .(* kubetest.MockKubectlCmd )
98
- assert .False (t , kubectl .GetLastValidate ())
102
+ // kubectl := syncCtx.kubectl.(*kubetest.MockKubectlCmd)
103
+ resourceOps , _ := syncCtx .resourceOps .(* kubetest.MockResourceOps )
104
+ assert .False (t , resourceOps .GetLastValidate ())
99
105
}
100
106
101
107
func TestSyncNotPermittedNamespace (t * testing.T ) {
@@ -307,7 +313,15 @@ func TestSyncCreateFailure(t *testing.T) {
307
313
},
308
314
}
309
315
syncCtx .kubectl = mockKubectl
310
- syncCtx .resourceOps = mockKubectl
316
+ mockResourceOps := & kubetest.MockResourceOps {
317
+ Commands : map [string ]kubetest.KubectlOutput {
318
+ testSvc .GetName (): {
319
+ Output : "" ,
320
+ Err : fmt .Errorf ("foo" ),
321
+ },
322
+ },
323
+ }
324
+ syncCtx .resourceOps = mockResourceOps
311
325
syncCtx .resources = groupResources (ReconciliationResult {
312
326
Live : []* unstructured.Unstructured {nil },
313
327
Target : []* unstructured.Unstructured {testSvc },
@@ -449,7 +463,15 @@ func TestSyncPruneFailure(t *testing.T) {
449
463
},
450
464
}
451
465
syncCtx .kubectl = mockKubectl
452
- syncCtx .resourceOps = mockKubectl
466
+ mockResourceOps := kubetest.MockResourceOps {
467
+ Commands : map [string ]kubetest.KubectlOutput {
468
+ "test-service" : {
469
+ Output : "" ,
470
+ Err : fmt .Errorf ("foo" ),
471
+ },
472
+ },
473
+ }
474
+ syncCtx .resourceOps = & mockResourceOps
453
475
testSvc := NewService ()
454
476
testSvc .SetName ("test-service" )
455
477
testSvc .SetNamespace (FakeArgoCDNamespace )
@@ -718,8 +740,9 @@ func TestSyncOptionValidate(t *testing.T) {
718
740
719
741
syncCtx .Sync ()
720
742
721
- kubectl , _ := syncCtx .kubectl .(* kubetest.MockKubectlCmd )
722
- assert .Equal (t , tt .want , kubectl .GetLastValidate ())
743
+ // kubectl, _ := syncCtx.kubectl.(*kubetest.MockKubectlCmd)
744
+ resourceOps , _ := syncCtx .resourceOps .(* kubetest.MockResourceOps )
745
+ assert .Equal (t , tt .want , resourceOps .GetLastValidate ())
723
746
})
724
747
}
725
748
}
@@ -756,8 +779,9 @@ func TestSync_Replace(t *testing.T) {
756
779
757
780
syncCtx .Sync ()
758
781
759
- kubectl , _ := syncCtx .kubectl .(* kubetest.MockKubectlCmd )
760
- assert .Equal (t , tc .commandUsed , kubectl .GetLastResourceCommand (kube .GetResourceKey (tc .target )))
782
+ // kubectl, _ := syncCtx.kubectl.(*kubetest.MockKubectlCmd)
783
+ resourceOps , _ := syncCtx .resourceOps .(* kubetest.MockResourceOps )
784
+ assert .Equal (t , tc .commandUsed , resourceOps .GetLastResourceCommand (kube .GetResourceKey (tc .target )))
761
785
})
762
786
}
763
787
}
@@ -806,10 +830,11 @@ func TestSync_ServerSideApply(t *testing.T) {
806
830
807
831
syncCtx .Sync ()
808
832
809
- kubectl , _ := syncCtx .kubectl .(* kubetest.MockKubectlCmd )
810
- assert .Equal (t , tc .commandUsed , kubectl .GetLastResourceCommand (kube .GetResourceKey (tc .target )))
811
- assert .Equal (t , tc .serverSideApply , kubectl .GetLastServerSideApply ())
812
- assert .Equal (t , tc .manager , kubectl .GetLastServerSideApplyManager ())
833
+ // kubectl, _ := syncCtx.kubectl.(*kubetest.MockKubectlCmd)
834
+ resourceOps , _ := syncCtx .resourceOps .(* kubetest.MockResourceOps )
835
+ assert .Equal (t , tc .commandUsed , resourceOps .GetLastResourceCommand (kube .GetResourceKey (tc .target )))
836
+ assert .Equal (t , tc .serverSideApply , resourceOps .GetLastServerSideApply ())
837
+ assert .Equal (t , tc .manager , resourceOps .GetLastServerSideApplyManager ())
813
838
})
814
839
}
815
840
}
@@ -1123,7 +1148,10 @@ func TestSyncFailureHookWithFailedSync(t *testing.T) {
1123
1148
Commands : map [string ]kubetest.KubectlOutput {pod .GetName (): {Err : fmt .Errorf ("" )}},
1124
1149
}
1125
1150
syncCtx .kubectl = mockKubectl
1126
- syncCtx .resourceOps = mockKubectl
1151
+ mockResourceOps := kubetest.MockResourceOps {
1152
+ Commands : map [string ]kubetest.KubectlOutput {pod .GetName (): {Err : fmt .Errorf ("" )}},
1153
+ }
1154
+ syncCtx .resourceOps = & mockResourceOps
1127
1155
1128
1156
syncCtx .Sync ()
1129
1157
syncCtx .Sync ()
@@ -1175,7 +1203,14 @@ func TestRunSyncFailHooksFailed(t *testing.T) {
1175
1203
failedSyncFailHook .GetName (): {Err : fmt .Errorf ("" )}},
1176
1204
}
1177
1205
syncCtx .kubectl = mockKubectl
1178
- syncCtx .resourceOps = mockKubectl
1206
+ mockResourceOps := kubetest.MockResourceOps {
1207
+ Commands : map [string ]kubetest.KubectlOutput {
1208
+ // Fail operation
1209
+ pod .GetName (): {Err : fmt .Errorf ("" )},
1210
+ // Fail a single SyncFail hook
1211
+ failedSyncFailHook .GetName (): {Err : fmt .Errorf ("" )}},
1212
+ }
1213
+ syncCtx .resourceOps = & mockResourceOps
1179
1214
1180
1215
syncCtx .Sync ()
1181
1216
syncCtx .Sync ()
0 commit comments