Skip to content

Commit 170c7a9

Browse files
authored
Refactor Lease controller test cases.
Signed-off-by: GitHub <[email protected]>
1 parent a78571c commit 170c7a9

File tree

2 files changed

+131
-47
lines changed

2 files changed

+131
-47
lines changed

pkg/registration/spoke/lease/lease_controller.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type managedClusterLeaseController struct {
2626
clusterName string
2727
hubClusterLister clusterv1listers.ManagedClusterLister
2828
lastLeaseDurationSeconds int32
29-
leaseUpdater *leaseUpdater
29+
leaseUpdater leaseUpdaterInterface
3030
}
3131

3232
// NewManagedClusterLeaseController creates a new managed cluster lease controller on the managed cluster.
@@ -85,6 +85,11 @@ func (c *managedClusterLeaseController) sync(ctx context.Context, syncCtx factor
8585
return nil
8686
}
8787

88+
type leaseUpdaterInterface interface {
89+
start(ctx context.Context, leaseDuration time.Duration)
90+
stop()
91+
}
92+
8893
// leaseUpdater periodically updates the lease of a managed cluster
8994
type leaseUpdater struct {
9095
hubClient clientset.Interface

pkg/registration/spoke/lease/lease_controller_test.go

+125-46
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import (
66
"time"
77

88
"github.com/openshift/library-go/pkg/operator/events/eventstesting"
9-
coordinationv1 "k8s.io/api/coordination/v1"
109
"k8s.io/apimachinery/pkg/runtime"
1110
kubefake "k8s.io/client-go/kubernetes/fake"
12-
clienttesting "k8s.io/client-go/testing"
1311

1412
clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
1513
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
@@ -18,37 +16,76 @@ import (
1816
testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing"
1917
)
2018

21-
func TestLeaseUpdate(t *testing.T) {
19+
func TestSync(t *testing.T) {
2220
cases := []struct {
23-
name string
24-
clusters []runtime.Object
25-
validateActions func(t *testing.T, actions []clienttesting.Action)
26-
needToStartUpdateBefore bool
27-
expectedErr string
21+
name string
22+
clusters []runtime.Object
23+
controllerlastLeaseDurationSeconds int32
24+
expectSyncErr string
25+
validateActions func(fakeLeaseUpdater *fakeLeaseUpdater)
2826
}{
2927
{
30-
name: "start lease update routine",
31-
clusters: []runtime.Object{testinghelpers.NewAcceptedManagedCluster()},
32-
validateActions: func(t *testing.T, actions []clienttesting.Action) {
33-
testingcommon.AssertUpdateActions(t, actions)
34-
leaseObj := actions[1].(clienttesting.UpdateActionImpl).Object
35-
lastLeaseObj := actions[len(actions)-1].(clienttesting.UpdateActionImpl).Object
36-
testinghelpers.AssertLeaseUpdated(t, leaseObj.(*coordinationv1.Lease), lastLeaseObj.(*coordinationv1.Lease))
28+
name: "start lease update routine",
29+
clusters: []runtime.Object{testinghelpers.NewAcceptedManagedCluster()},
30+
controllerlastLeaseDurationSeconds: testinghelpers.TestLeaseDurationSeconds,
31+
validateActions: func(fakeLeaseUpdater *fakeLeaseUpdater) {
32+
// start method should be called
33+
if !fakeLeaseUpdater.startCalled {
34+
t.Error("start method should be called")
35+
}
36+
// stop method should not be called
37+
if fakeLeaseUpdater.stopCalled {
38+
t.Error("stop method should not be called")
39+
}
3740
},
3841
},
3942
{
40-
name: "delete a managed cluster after lease update routine is started",
41-
clusters: []runtime.Object{},
42-
needToStartUpdateBefore: true,
43-
validateActions: testingcommon.AssertNoMoreUpdates,
44-
expectedErr: "unable to get managed cluster \"testmanagedcluster\" from hub: " +
43+
name: "the managed cluster can not be found",
44+
clusters: []runtime.Object{},
45+
controllerlastLeaseDurationSeconds: testinghelpers.TestLeaseDurationSeconds,
46+
expectSyncErr: "unable to get managed cluster \"testmanagedcluster\" from hub: " +
4547
"managedcluster.cluster.open-cluster-management.io \"testmanagedcluster\" not found",
48+
validateActions: func(fakeLeaseUpdater *fakeLeaseUpdater) {
49+
// start method should not be called
50+
if fakeLeaseUpdater.startCalled {
51+
t.Error("start method should not be called")
52+
}
53+
// stop method should be called
54+
if !fakeLeaseUpdater.stopCalled {
55+
t.Error("stop method should be called")
56+
}
57+
},
58+
},
59+
{
60+
name: "unaccept a managed cluster",
61+
clusters: []runtime.Object{testinghelpers.NewManagedCluster()},
62+
controllerlastLeaseDurationSeconds: testinghelpers.TestLeaseDurationSeconds,
63+
validateActions: func(fakeLeaseUpdater *fakeLeaseUpdater) {
64+
// start method should not be called
65+
if fakeLeaseUpdater.startCalled {
66+
t.Error("start method should not be called")
67+
}
68+
// stop method should be called
69+
if !fakeLeaseUpdater.stopCalled {
70+
t.Error("stop method should be called")
71+
}
72+
},
4673
},
4774
{
48-
name: "unaccept a managed cluster after lease update routine is started",
49-
clusters: []runtime.Object{testinghelpers.NewManagedCluster()},
50-
needToStartUpdateBefore: true,
51-
validateActions: testingcommon.AssertNoMoreUpdates,
75+
name: "update the lease duration",
76+
clusters: []runtime.Object{testinghelpers.NewAcceptedManagedCluster()},
77+
controllerlastLeaseDurationSeconds: testinghelpers.TestLeaseDurationSeconds + 1,
78+
validateActions: func(fakeLeaseUpdater *fakeLeaseUpdater) {
79+
// first stop the old lease update routine, and then start a new lease update routine
80+
// stop method should be called
81+
if !fakeLeaseUpdater.stopCalled {
82+
t.Error("stop method should be called eventually")
83+
}
84+
// start method should be called
85+
if !fakeLeaseUpdater.startCalled {
86+
t.Error("start method should not called eventually")
87+
}
88+
},
5289
},
5390
}
5491

@@ -63,32 +100,74 @@ func TestLeaseUpdate(t *testing.T) {
63100
}
64101
}
65102

66-
hubClient := kubefake.NewSimpleClientset(testinghelpers.NewManagedClusterLease("managed-cluster-lease", time.Now()))
67-
68-
leaseUpdater := &leaseUpdater{
69-
hubClient: hubClient,
70-
clusterName: testinghelpers.TestManagedClusterName,
71-
leaseName: "managed-cluster-lease",
72-
recorder: eventstesting.NewTestingEventRecorder(t),
73-
}
74-
75-
if c.needToStartUpdateBefore {
76-
leaseUpdater.start(context.TODO(), time.Duration(testinghelpers.TestLeaseDurationSeconds)*time.Second)
77-
// wait a few milliseconds to start the lease update routine
78-
time.Sleep(500 * time.Millisecond)
79-
}
80-
81103
ctrl := &managedClusterLeaseController{
82-
clusterName: testinghelpers.TestManagedClusterName,
83-
hubClusterLister: clusterInformerFactory.Cluster().V1().ManagedClusters().Lister(),
84-
leaseUpdater: leaseUpdater,
104+
clusterName: testinghelpers.TestManagedClusterName,
105+
hubClusterLister: clusterInformerFactory.Cluster().V1().ManagedClusters().Lister(),
106+
leaseUpdater: &fakeLeaseUpdater{},
107+
lastLeaseDurationSeconds: c.controllerlastLeaseDurationSeconds,
85108
}
109+
86110
syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, ""))
87-
testingcommon.AssertError(t, syncErr, c.expectedErr)
111+
testingcommon.AssertError(t, syncErr, c.expectSyncErr)
88112

89-
// wait one cycle (1 ~ 1.25s)
90-
time.Sleep(2000 * time.Millisecond)
91-
c.validateActions(t, hubClient.Actions())
113+
if c.validateActions != nil {
114+
c.validateActions(ctrl.leaseUpdater.(*fakeLeaseUpdater))
115+
}
92116
})
93117
}
94118
}
119+
120+
type fakeLeaseUpdater struct {
121+
startCalled bool
122+
stopCalled bool
123+
}
124+
125+
func (f *fakeLeaseUpdater) start(ctx context.Context, leaseDuration time.Duration) {
126+
f.startCalled = true
127+
}
128+
129+
func (f *fakeLeaseUpdater) stop() {
130+
f.stopCalled = true
131+
}
132+
133+
func TestLeaseUpdater(t *testing.T) {
134+
initRenewTime := time.Now()
135+
hubClient := kubefake.NewSimpleClientset(testinghelpers.NewManagedClusterLease("managed-cluster-lease", initRenewTime))
136+
leaseUpdater := &leaseUpdater{
137+
hubClient: hubClient,
138+
clusterName: testinghelpers.TestManagedClusterName,
139+
leaseName: "managed-cluster-lease",
140+
recorder: eventstesting.NewTestingEventRecorder(t),
141+
}
142+
143+
// start the updater
144+
ctx := context.Background()
145+
leaseUpdater.start(ctx, time.Second*1)
146+
147+
// wait for 3 second, the all actions should be in get,update pairs
148+
time.Sleep(time.Second * 3)
149+
actions := hubClient.Actions()
150+
if len(actions) == 0 {
151+
t.Error("expect at least 1 update actions, but got 0")
152+
return
153+
}
154+
for i := 0; i < len(actions); i += 2 {
155+
if actions[i].GetVerb() != "get" {
156+
t.Errorf("expect get action, but got %s", actions[i].GetVerb())
157+
}
158+
if actions[i+1].GetVerb() != "update" {
159+
t.Errorf("expect update action, but got %s", actions[i+1].GetVerb())
160+
}
161+
}
162+
163+
// stop the updater
164+
leaseUpdater.stop()
165+
actionLen := len(actions)
166+
167+
// wait for 3 second, no new actions should be added
168+
time.Sleep(time.Second * 3)
169+
actions = hubClient.Actions()
170+
if len(actions) != actionLen {
171+
t.Errorf("expect %d actions, but got %d", actionLen, len(actions))
172+
}
173+
}

0 commit comments

Comments
 (0)