@@ -6,10 +6,8 @@ import (
6
6
"time"
7
7
8
8
"github.com/openshift/library-go/pkg/operator/events/eventstesting"
9
- coordinationv1 "k8s.io/api/coordination/v1"
10
9
"k8s.io/apimachinery/pkg/runtime"
11
10
kubefake "k8s.io/client-go/kubernetes/fake"
12
- clienttesting "k8s.io/client-go/testing"
13
11
14
12
clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
15
13
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
@@ -18,37 +16,76 @@ import (
18
16
testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing"
19
17
)
20
18
21
- func TestLeaseUpdate (t * testing.T ) {
19
+ func TestSync (t * testing.T ) {
22
20
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 )
28
26
}{
29
27
{
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
+ }
37
40
},
38
41
},
39
42
{
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: " +
45
47
"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
+ },
46
73
},
47
74
{
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
+ },
52
89
},
53
90
}
54
91
@@ -63,32 +100,74 @@ func TestLeaseUpdate(t *testing.T) {
63
100
}
64
101
}
65
102
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
-
81
103
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 ,
85
108
}
109
+
86
110
syncErr := ctrl .sync (context .TODO (), testingcommon .NewFakeSyncContext (t , "" ))
87
- testingcommon .AssertError (t , syncErr , c .expectedErr )
111
+ testingcommon .AssertError (t , syncErr , c .expectSyncErr )
88
112
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
+ }
92
116
})
93
117
}
94
118
}
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