@@ -6,49 +6,87 @@ 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
- kubefake "k8s.io/client-go/kubernetes/fake"
12
- clienttesting "k8s.io/client-go/testing"
13
10
14
11
clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
15
12
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
16
13
17
14
testingcommon "open-cluster-management.io/ocm/pkg/common/testing"
18
15
testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing"
16
+
17
+ kubefake "k8s.io/client-go/kubernetes/fake"
19
18
)
20
19
21
- func TestLeaseUpdate (t * testing.T ) {
20
+ func TestSync (t * testing.T ) {
22
21
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 )
28
27
}{
29
28
{
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
+ }
37
41
},
38
42
},
39
43
{
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: " +
45
48
"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
+ },
46
74
},
47
75
{
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
+ },
52
90
},
53
91
}
54
92
@@ -63,32 +101,74 @@ func TestLeaseUpdate(t *testing.T) {
63
101
}
64
102
}
65
103
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
104
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 ,
85
109
}
110
+
86
111
syncErr := ctrl .sync (context .TODO (), testingcommon .NewFakeSyncContext (t , "" ))
87
- testingcommon .AssertError (t , syncErr , c .expectedErr )
112
+ testingcommon .AssertError (t , syncErr , c .expectSyncErr )
88
113
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
+ }
92
117
})
93
118
}
94
119
}
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