Skip to content

Commit ccc7e52

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

File tree

2 files changed

+133
-48
lines changed

2 files changed

+133
-48
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

+127-47
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,87 @@ 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"
11-
kubefake "k8s.io/client-go/kubernetes/fake"
12-
clienttesting "k8s.io/client-go/testing"
1310

1411
clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
1512
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
1613

1714
testingcommon "open-cluster-management.io/ocm/pkg/common/testing"
1815
testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing"
16+
17+
kubefake "k8s.io/client-go/kubernetes/fake"
1918
)
2019

21-
func TestLeaseUpdate(t *testing.T) {
20+
func TestSync(t *testing.T) {
2221
cases := []struct {
23-
name string
24-
clusters []runtime.Object
25-
validateActions func(t *testing.T, actions []clienttesting.Action)
26-
needToStartUpdateBefore bool
27-
expectedErr string
22+
name string
23+
clusters []runtime.Object
24+
controllerlastLeaseDurationSeconds int32
25+
expectSyncErr string
26+
validateActions func(fakeLeaseUpdater *fakeLeaseUpdater)
2827
}{
2928
{
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))
29+
name: "start lease update routine",
30+
clusters: []runtime.Object{testinghelpers.NewAcceptedManagedCluster()},
31+
controllerlastLeaseDurationSeconds: testinghelpers.TestLeaseDurationSeconds,
32+
validateActions: func(fakeLeaseUpdater *fakeLeaseUpdater) {
33+
// start method should be called
34+
if !fakeLeaseUpdater.startCalled {
35+
t.Error("start method should be called")
36+
}
37+
// stop method should not be called
38+
if fakeLeaseUpdater.stopCalled {
39+
t.Error("stop method should not be called")
40+
}
3741
},
3842
},
3943
{
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: " +
44+
name: "the managed cluster can not be found",
45+
clusters: []runtime.Object{},
46+
controllerlastLeaseDurationSeconds: testinghelpers.TestLeaseDurationSeconds,
47+
expectSyncErr: "unable to get managed cluster \"testmanagedcluster\" from hub: " +
4548
"managedcluster.cluster.open-cluster-management.io \"testmanagedcluster\" not found",
49+
validateActions: func(fakeLeaseUpdater *fakeLeaseUpdater) {
50+
// start method should not be called
51+
if fakeLeaseUpdater.startCalled {
52+
t.Error("start method should not be called")
53+
}
54+
// stop method should be called
55+
if !fakeLeaseUpdater.stopCalled {
56+
t.Error("stop method should be called")
57+
}
58+
},
59+
},
60+
{
61+
name: "unaccept a managed cluster",
62+
clusters: []runtime.Object{testinghelpers.NewManagedCluster()},
63+
controllerlastLeaseDurationSeconds: testinghelpers.TestLeaseDurationSeconds,
64+
validateActions: func(fakeLeaseUpdater *fakeLeaseUpdater) {
65+
// start method should not be called
66+
if fakeLeaseUpdater.startCalled {
67+
t.Error("start method should not be called")
68+
}
69+
// stop method should be called
70+
if !fakeLeaseUpdater.stopCalled {
71+
t.Error("stop method should be called")
72+
}
73+
},
4674
},
4775
{
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,
76+
name: "update the lease duration",
77+
clusters: []runtime.Object{testinghelpers.NewAcceptedManagedCluster()},
78+
controllerlastLeaseDurationSeconds: testinghelpers.TestLeaseDurationSeconds + 1,
79+
validateActions: func(fakeLeaseUpdater *fakeLeaseUpdater) {
80+
// first stop the old lease update routine, and then start a new lease update routine
81+
// stop method should be called
82+
if !fakeLeaseUpdater.stopCalled {
83+
t.Error("stop method should be called eventually")
84+
}
85+
// start method should be called
86+
if !fakeLeaseUpdater.startCalled {
87+
t.Error("start method should not called eventually")
88+
}
89+
},
5290
},
5391
}
5492

@@ -63,32 +101,74 @@ func TestLeaseUpdate(t *testing.T) {
63101
}
64102
}
65103

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-
81104
ctrl := &managedClusterLeaseController{
82-
clusterName: testinghelpers.TestManagedClusterName,
83-
hubClusterLister: clusterInformerFactory.Cluster().V1().ManagedClusters().Lister(),
84-
leaseUpdater: leaseUpdater,
105+
clusterName: testinghelpers.TestManagedClusterName,
106+
hubClusterLister: clusterInformerFactory.Cluster().V1().ManagedClusters().Lister(),
107+
leaseUpdater: &fakeLeaseUpdater{},
108+
lastLeaseDurationSeconds: c.controllerlastLeaseDurationSeconds,
85109
}
110+
86111
syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, ""))
87-
testingcommon.AssertError(t, syncErr, c.expectedErr)
112+
testingcommon.AssertError(t, syncErr, c.expectSyncErr)
88113

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

0 commit comments

Comments
 (0)