@@ -19,6 +19,9 @@ import (
19
19
"fmt"
20
20
"strconv"
21
21
"testing"
22
+ "time"
23
+
24
+ "github.com/onsi/gomega"
22
25
23
26
"istio.io/operator/pkg/apis/istio/v1alpha2"
24
27
"istio.io/operator/pkg/helmreconciler"
@@ -29,10 +32,26 @@ import (
29
32
"k8s.io/client-go/kubernetes/scheme"
30
33
"sigs.k8s.io/controller-runtime/pkg/client"
31
34
"sigs.k8s.io/controller-runtime/pkg/client/fake"
35
+ "sigs.k8s.io/controller-runtime/pkg/manager"
32
36
"sigs.k8s.io/controller-runtime/pkg/reconcile"
33
37
)
34
38
39
+ const (
40
+ timeout = time .Second * 2
41
+ defaultProfile = "default"
42
+ demoProfile = "demo"
43
+ minimalProfile = "minimal"
44
+ sdsProfile = "sds"
45
+ )
46
+
35
47
var (
48
+ c client.Client
49
+ icpkey = types.NamespacedName {
50
+ Name : "test-istiocontrolplane" ,
51
+ Namespace : "test-istio-operator" ,
52
+ }
53
+ expectedRequest = reconcile.Request {NamespacedName : icpkey }
54
+
36
55
minimalStatus = map [string ]* v1alpha2.InstallStatus_VersionStatus {
37
56
"Pilot" : {
38
57
Status : v1alpha2 .InstallStatus_HEALTHY ,
@@ -156,28 +175,28 @@ func TestICPController_SwitchProfile(t *testing.T) {
156
175
cases := []testCase {
157
176
{
158
177
description : "switch profile from minimal to default" ,
159
- initialProfile : "minimal" ,
160
- targetProfile : "default" ,
178
+ initialProfile : minimalProfile ,
179
+ targetProfile : defaultProfile ,
161
180
},
162
181
{
163
182
description : "switch profile from default to minimal" ,
164
- initialProfile : "default" ,
165
- targetProfile : "minimal" ,
183
+ initialProfile : defaultProfile ,
184
+ targetProfile : minimalProfile ,
166
185
},
167
186
{
168
187
description : "switch profile from default to demo" ,
169
- initialProfile : "default" ,
170
- targetProfile : "demo" ,
188
+ initialProfile : defaultProfile ,
189
+ targetProfile : demoProfile ,
171
190
},
172
191
{
173
192
description : "switch profile from demo to sds" ,
174
- initialProfile : "demo" ,
175
- targetProfile : "sds" ,
193
+ initialProfile : demoProfile ,
194
+ targetProfile : sdsProfile ,
176
195
},
177
196
{
178
197
description : "switch profile from sds to default" ,
179
- initialProfile : "sds" ,
180
- targetProfile : "default" ,
198
+ initialProfile : sdsProfile ,
199
+ targetProfile : defaultProfile ,
181
200
},
182
201
}
183
202
for i , c := range cases {
@@ -186,16 +205,15 @@ func TestICPController_SwitchProfile(t *testing.T) {
186
205
})
187
206
}
188
207
}
208
+
189
209
func testSwitchProfile (t * testing.T , c testCase ) {
190
210
t .Helper ()
191
- name := "example-istiocontrolplane"
192
- namespace := "istio-system"
193
211
icp := & v1alpha2.IstioControlPlane {
194
212
Kind : "IstioControlPlane" ,
195
213
ApiVersion : "install.istio.io/v1alpha2" ,
196
214
ObjectMeta : metav1.ObjectMeta {
197
- Name : name ,
198
- Namespace : namespace ,
215
+ Name : icpkey . Name ,
216
+ Namespace : icpkey . Namespace ,
199
217
},
200
218
Spec : & v1alpha2.IstioControlPlaneSpec {
201
219
Profile : c .initialProfile ,
@@ -213,8 +231,8 @@ func testSwitchProfile(t *testing.T, c testCase) {
213
231
214
232
req := reconcile.Request {
215
233
NamespacedName : types.NamespacedName {
216
- Name : name ,
217
- Namespace : namespace ,
234
+ Name : icpkey . Name ,
235
+ Namespace : icpkey . Namespace ,
218
236
},
219
237
}
220
238
res , err := r .Reconcile (req )
@@ -272,13 +290,13 @@ func checkICPStatus(cl client.Client, key client.ObjectKey, profile string) (boo
272
290
}
273
291
var status map [string ]* v1alpha2.InstallStatus_VersionStatus
274
292
switch profile {
275
- case "minimal" :
293
+ case minimalProfile :
276
294
status = minimalStatus
277
- case "default" :
295
+ case defaultProfile :
278
296
status = defaultStatus
279
- case "sds" :
297
+ case sdsProfile :
280
298
status = sdsStatus
281
- case "demo" :
299
+ case demoProfile :
282
300
status = demoStatus
283
301
}
284
302
installStatus := instance .GetStatus ()
@@ -298,3 +316,45 @@ func checkICPStatus(cl client.Client, key client.ObjectKey, profile string) (boo
298
316
}
299
317
return true , nil
300
318
}
319
+
320
+ // TestReconcile test the reconciler process with manager from end to end.
321
+ func TestReconcile (t * testing.T ) {
322
+ g := gomega .NewGomegaWithT (t )
323
+
324
+ // Setup the Manager and Controller. Wrap the Controller Reconcile function so it writes each request to a
325
+ // channel when it is finished.
326
+ mgr , err := manager .New (cfg , manager.Options {})
327
+ g .Expect (err ).NotTo (gomega .HaveOccurred ())
328
+
329
+ c = mgr .GetClient ()
330
+
331
+ rec := newReconciler (mgr )
332
+ recFn , requests := SetupTestReconcile (rec )
333
+
334
+ g .Expect (add (mgr , recFn )).NotTo (gomega .HaveOccurred ())
335
+
336
+ stopMgr , mgrStopped := StartTestManager (mgr , g )
337
+
338
+ defer func () {
339
+ close (stopMgr )
340
+ mgrStopped .Wait ()
341
+ }()
342
+
343
+ icp := & v1alpha2.IstioControlPlane {
344
+ Kind : "IstioControlPlane" ,
345
+ ApiVersion : "install.istio.io/v1alpha2" ,
346
+ ObjectMeta : metav1.ObjectMeta {
347
+ Name : icpkey .Name ,
348
+ Namespace : icpkey .Namespace ,
349
+ },
350
+ Spec : & v1alpha2.IstioControlPlaneSpec {
351
+ Profile : defaultProfile ,
352
+ },
353
+ }
354
+
355
+ g .Expect (c .Create (context .TODO (), icp )).NotTo (gomega .HaveOccurred ())
356
+
357
+ defer c .Delete (context .TODO (), icp )
358
+
359
+ g .Eventually (requests , timeout ).Should (gomega .Receive (gomega .Equal (expectedRequest )))
360
+ }
0 commit comments