@@ -19,6 +19,9 @@ import (
1919 "fmt"
2020 "strconv"
2121 "testing"
22+ "time"
23+
24+ "github.com/onsi/gomega"
2225
2326 "istio.io/operator/pkg/apis/istio/v1alpha2"
2427 "istio.io/operator/pkg/helmreconciler"
@@ -29,10 +32,26 @@ import (
2932 "k8s.io/client-go/kubernetes/scheme"
3033 "sigs.k8s.io/controller-runtime/pkg/client"
3134 "sigs.k8s.io/controller-runtime/pkg/client/fake"
35+ "sigs.k8s.io/controller-runtime/pkg/manager"
3236 "sigs.k8s.io/controller-runtime/pkg/reconcile"
3337)
3438
39+ const (
40+ timeout = time .Second * 2
41+ defaultProfile = "default"
42+ demoProfile = "demo"
43+ minimalProfile = "minimal"
44+ sdsProfile = "sds"
45+ )
46+
3547var (
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+
3655 minimalStatus = map [string ]* v1alpha2.InstallStatus_VersionStatus {
3756 "Pilot" : {
3857 Status : v1alpha2 .InstallStatus_HEALTHY ,
@@ -156,28 +175,28 @@ func TestICPController_SwitchProfile(t *testing.T) {
156175 cases := []testCase {
157176 {
158177 description : "switch profile from minimal to default" ,
159- initialProfile : "minimal" ,
160- targetProfile : "default" ,
178+ initialProfile : minimalProfile ,
179+ targetProfile : defaultProfile ,
161180 },
162181 {
163182 description : "switch profile from default to minimal" ,
164- initialProfile : "default" ,
165- targetProfile : "minimal" ,
183+ initialProfile : defaultProfile ,
184+ targetProfile : minimalProfile ,
166185 },
167186 {
168187 description : "switch profile from default to demo" ,
169- initialProfile : "default" ,
170- targetProfile : "demo" ,
188+ initialProfile : defaultProfile ,
189+ targetProfile : demoProfile ,
171190 },
172191 {
173192 description : "switch profile from demo to sds" ,
174- initialProfile : "demo" ,
175- targetProfile : "sds" ,
193+ initialProfile : demoProfile ,
194+ targetProfile : sdsProfile ,
176195 },
177196 {
178197 description : "switch profile from sds to default" ,
179- initialProfile : "sds" ,
180- targetProfile : "default" ,
198+ initialProfile : sdsProfile ,
199+ targetProfile : defaultProfile ,
181200 },
182201 }
183202 for i , c := range cases {
@@ -186,14 +205,15 @@ func TestICPController_SwitchProfile(t *testing.T) {
186205 })
187206 }
188207}
208+
189209func testSwitchProfile (t * testing.T , c testCase ) {
190210 t .Helper ()
191- name := "example-istiocontrolplane"
192- namespace := "istio-system"
193211 icp := & v1alpha2.IstioControlPlane {
212+ Kind : "IstioControlPlane" ,
213+ ApiVersion : "install.istio.io/v1alpha2" ,
194214 ObjectMeta : metav1.ObjectMeta {
195- Name : name ,
196- Namespace : namespace ,
215+ Name : icpkey . Name ,
216+ Namespace : icpkey . Namespace ,
197217 },
198218 Spec : & v1alpha2.IstioControlPlaneSpec {
199219 Profile : c .initialProfile ,
@@ -211,8 +231,8 @@ func testSwitchProfile(t *testing.T, c testCase) {
211231
212232 req := reconcile.Request {
213233 NamespacedName : types.NamespacedName {
214- Name : name ,
215- Namespace : namespace ,
234+ Name : icpkey . Name ,
235+ Namespace : icpkey . Namespace ,
216236 },
217237 }
218238 res , err := r .Reconcile (req )
@@ -270,13 +290,13 @@ func checkICPStatus(cl client.Client, key client.ObjectKey, profile string) (boo
270290 }
271291 var status map [string ]* v1alpha2.InstallStatus_VersionStatus
272292 switch profile {
273- case "minimal" :
293+ case minimalProfile :
274294 status = minimalStatus
275- case "default" :
295+ case defaultProfile :
276296 status = defaultStatus
277- case "sds" :
297+ case sdsProfile :
278298 status = sdsStatus
279- case "demo" :
299+ case demoProfile :
280300 status = demoStatus
281301 }
282302 installStatus := instance .GetStatus ()
@@ -296,3 +316,45 @@ func checkICPStatus(cl client.Client, key client.ObjectKey, profile string) (boo
296316 }
297317 return true , nil
298318}
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