Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit ad7d152

Browse files
author
llcao
committed
add test for manager.
1 parent a0bb04a commit ad7d152

File tree

3 files changed

+162
-20
lines changed

3 files changed

+162
-20
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ require (
4141
github.com/kylelemons/godebug v1.1.0
4242
github.com/mholt/archiver v3.1.1+incompatible
4343
github.com/nwaples/rardecode v1.0.0 // indirect
44+
github.com/onsi/gomega v1.5.0
4445
github.com/openshift/api v3.9.1-0.20191008181517-e4fd21196097+incompatible // indirect
4546
github.com/openshift/cluster-network-operator v0.0.0-20191009144453-fdceef8e1a7b
4647
github.com/pierrec/lz4 v2.2.5+incompatible // indirect
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2019 Istio Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package istiocontrolplane
16+
17+
import (
18+
"os"
19+
"path/filepath"
20+
"sync"
21+
"testing"
22+
23+
"github.com/onsi/gomega"
24+
"k8s.io/client-go/kubernetes/scheme"
25+
"k8s.io/client-go/rest"
26+
"sigs.k8s.io/controller-runtime/pkg/envtest"
27+
"sigs.k8s.io/controller-runtime/pkg/manager"
28+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
29+
30+
"istio.io/operator/pkg/apis"
31+
"istio.io/pkg/log"
32+
)
33+
34+
var cfg *rest.Config
35+
36+
func TestMain(m *testing.M) {
37+
t := &envtest.Environment{
38+
CRDDirectoryPaths: []string{
39+
filepath.Join("..", "..", "..", "deploy", "crds"),
40+
},
41+
}
42+
43+
apis.AddToScheme(scheme.Scheme)
44+
45+
var err error
46+
if cfg, err = t.Start(); err != nil {
47+
log.Fatalf("error to start the test env: %s", err)
48+
}
49+
50+
code := m.Run()
51+
52+
t.Stop()
53+
os.Exit(code)
54+
}
55+
56+
// SetupTestReconcile returns a reconcile.Reconcile implementation that delegates to inner and
57+
// writes the request to requests after Reconcile is finished.
58+
func SetupTestReconcile(inner reconcile.Reconciler) (reconcile.Reconciler, chan reconcile.Request) {
59+
requests := make(chan reconcile.Request)
60+
fn := reconcile.Func(func(req reconcile.Request) (reconcile.Result, error) {
61+
result, err := inner.Reconcile(req)
62+
requests <- req
63+
return result, err
64+
})
65+
66+
return fn, requests
67+
}
68+
69+
// StartTestManager adds recFn
70+
func StartTestManager(mgr manager.Manager, g *gomega.GomegaWithT) (chan struct{}, *sync.WaitGroup) {
71+
stop := make(chan struct{})
72+
wg := &sync.WaitGroup{}
73+
wg.Add(1)
74+
75+
go func() {
76+
defer wg.Done()
77+
g.Expect(mgr.Start(stop)).NotTo(gomega.HaveOccurred())
78+
}()
79+
80+
return stop, wg
81+
}

pkg/controller/istiocontrolplane/istiocontrolplane_controller_test.go

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3547
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+
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,16 +205,15 @@ func TestICPController_SwitchProfile(t *testing.T) {
186205
})
187206
}
188207
}
208+
189209
func testSwitchProfile(t *testing.T, c testCase) {
190210
t.Helper()
191-
name := "example-istiocontrolplane"
192-
namespace := "istio-system"
193211
icp := &v1alpha2.IstioControlPlane{
194212
Kind: "IstioControlPlane",
195213
ApiVersion: "install.istio.io/v1alpha2",
196214
ObjectMeta: metav1.ObjectMeta{
197-
Name: name,
198-
Namespace: namespace,
215+
Name: icpkey.Name,
216+
Namespace: icpkey.Namespace,
199217
},
200218
Spec: &v1alpha2.IstioControlPlaneSpec{
201219
Profile: c.initialProfile,
@@ -213,8 +231,8 @@ func testSwitchProfile(t *testing.T, c testCase) {
213231

214232
req := reconcile.Request{
215233
NamespacedName: types.NamespacedName{
216-
Name: name,
217-
Namespace: namespace,
234+
Name: icpkey.Name,
235+
Namespace: icpkey.Namespace,
218236
},
219237
}
220238
res, err := r.Reconcile(req)
@@ -272,13 +290,13 @@ func checkICPStatus(cl client.Client, key client.ObjectKey, profile string) (boo
272290
}
273291
var status map[string]*v1alpha2.InstallStatus_VersionStatus
274292
switch profile {
275-
case "minimal":
293+
case minimalProfile:
276294
status = minimalStatus
277-
case "default":
295+
case defaultProfile:
278296
status = defaultStatus
279-
case "sds":
297+
case sdsProfile:
280298
status = sdsStatus
281-
case "demo":
299+
case demoProfile:
282300
status = demoStatus
283301
}
284302
installStatus := instance.GetStatus()
@@ -298,3 +316,45 @@ func checkICPStatus(cl client.Client, key client.ObjectKey, profile string) (boo
298316
}
299317
return true, nil
300318
}
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

Comments
 (0)