@@ -126,106 +126,57 @@ func TestClusterHealthy(t *testing.T) {
126
126
_ = testEnv .Test (t , feature .Feature ())
127
127
}
128
128
129
- func TestScaleDownFrom3To1 (t * testing.T ) {
130
- feature := features .New ("scale-down" )
131
- etcdClusterName := "etcd-scale-down"
132
-
133
- feature .Setup (func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
134
- createEtcdCluster (ctx , t , c , etcdClusterName , 3 )
135
- waitForStsReady (ctx , t , c , etcdClusterName , 3 )
136
- return ctx
137
- })
138
-
139
- feature .Assess ("scale down to 1" , func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
140
- var etcdCluster ec_v1alpha1.EtcdCluster
141
- if err := c .Client ().Resources ().Get (ctx , etcdClusterName , namespace , & etcdCluster ); err != nil {
142
- t .Fatalf ("Failed to get EtcdCluster: %v" , err )
143
- }
144
-
145
- etcdCluster .Spec .Size = 1
146
- if err := c .Client ().Resources ().Update (ctx , & etcdCluster ); err != nil {
147
- t .Fatalf ("Failed to update EtcdCluster: %v" , err )
148
- }
149
-
150
- waitForStsReady (ctx , t , c , etcdClusterName , 1 )
151
- return ctx
152
- })
153
-
154
- feature .Assess ("verify member list" , func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
155
- podName := fmt .Sprintf ("%s-0" , etcdClusterName )
156
- stdout , stderr , err := execInPod (t , c , podName , namespace , []string {"etcdctl" , "member" , "list" })
157
- if err != nil {
158
- t .Fatalf ("Failed to exec in pod: %v, stderr: %s" , err , stderr )
159
- }
160
-
161
- if len (strings .Split (strings .TrimSpace (stdout ), "\n " )) != 1 {
162
- t .Errorf ("Expected to find 1 member in member list, but got: %s" , stdout )
163
- }
164
- return ctx
165
- })
166
-
167
- feature .Teardown (func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
168
- var etcdCluster ec_v1alpha1.EtcdCluster
169
- if err := c .Client ().Resources ().Get (ctx , etcdClusterName , namespace , & etcdCluster ); err == nil {
170
- if err := c .Client ().Resources ().Delete (ctx , & etcdCluster ); err != nil {
171
- t .Logf ("Failed to delete EtcdCluster: %v" , err )
172
- }
173
- }
174
- return ctx
175
- })
176
-
177
- _ = testEnv .Test (t , feature .Feature ())
178
- }
179
-
180
- func TestScaleUpFrom1To3 (t * testing.T ) {
181
- feature := features .New ("scale-up" )
182
- etcdClusterName := "etcd-scale-up"
183
-
184
- feature .Setup (func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
185
- createEtcdCluster (ctx , t , c , etcdClusterName , 1 )
186
- waitForStsReady (ctx , t , c , etcdClusterName , 1 )
187
- return ctx
188
- })
129
+ func TestScaling (t * testing.T ) {
130
+ testCases := []struct {
131
+ name string
132
+ initialSize int
133
+ scaleTo int
134
+ expectedMembers int
135
+ }{
136
+ {"ScaleDownFrom3To1" , 3 , 1 , 1 },
137
+ {"ScaleUpFrom1To3" , 1 , 3 , 3 },
138
+ }
189
139
190
- feature .Assess ("scale up to 3" , func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
191
- var etcdCluster ec_v1alpha1.EtcdCluster
192
- if err := c .Client ().Resources ().Get (ctx , etcdClusterName , namespace , & etcdCluster ); err != nil {
193
- t .Fatalf ("Failed to get EtcdCluster: %v" , err )
194
- }
140
+ for _ , tc := range testCases {
141
+ t .Run (tc .name , func (t * testing.T ) {
142
+ feature := features .New (tc .name )
143
+ etcdClusterName := fmt .Sprintf ("etcd-%s" , strings .ToLower (tc .name ))
195
144
196
- etcdCluster .Spec .Size = 3
197
- if err := c .Client ().Resources ().Update (ctx , & etcdCluster ); err != nil {
198
- t .Fatalf ("Failed to update EtcdCluster: %v" , err )
199
- }
200
-
201
- waitForStsReady (ctx , t , c , etcdClusterName , 3 )
202
- return ctx
203
- })
204
-
205
- feature .Assess ("verify member list" , func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
206
- podName := fmt .Sprintf ("%s-0" , etcdClusterName )
207
- stdout , stderr , err := execInPod (t , c , podName , namespace , []string {"etcdctl" , "member" , "list" })
208
- if err != nil {
209
- t .Fatalf ("Failed to exec in pod: %v, stderr: %s" , err , stderr )
210
- }
145
+ feature .Setup (func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
146
+ createEtcdCluster (ctx , t , c , etcdClusterName , tc .initialSize )
147
+ waitForStsReady (t , c , etcdClusterName , tc .initialSize )
148
+ return ctx
149
+ })
150
+
151
+ feature .Assess (
152
+ fmt .Sprintf ("scale to %d" , tc .scaleTo ),
153
+ func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
154
+ scaleEtcdCluster (ctx , t , c , etcdClusterName , tc .scaleTo )
155
+ return ctx
156
+ },
157
+ )
158
+
159
+ feature .Assess ("verify member list" , func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
160
+ podName := fmt .Sprintf ("%s-0" , etcdClusterName )
161
+ stdout , stderr , err := execInPod (t , c , podName , namespace , []string {"etcdctl" , "member" , "list" })
162
+ if err != nil {
163
+ t .Fatalf ("Failed to exec in pod: %v, stderr: %s" , err , stderr )
164
+ }
211
165
212
- if len (strings .Split (strings .TrimSpace (stdout ), "\n " )) != 3 {
213
- t .Errorf ("Expected to find 3 members in member list, but got: %s" , stdout )
214
- }
215
- return ctx
216
- })
166
+ if len (strings .Split (strings .TrimSpace (stdout ), "\n " )) != tc . expectedMembers {
167
+ t .Errorf ("Expected to find %d members in member list, but got: %s" , tc . expectedMembers , stdout )
168
+ }
169
+ return ctx
170
+ })
217
171
218
- feature .Teardown (func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
219
- var etcdCluster ec_v1alpha1.EtcdCluster
220
- if err := c .Client ().Resources ().Get (ctx , etcdClusterName , namespace , & etcdCluster ); err == nil {
221
- if err := c .Client ().Resources ().Delete (ctx , & etcdCluster ); err != nil {
222
- t .Logf ("Failed to delete EtcdCluster: %v" , err )
223
- }
224
- }
225
- return ctx
226
- })
172
+ feature .Teardown (func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
173
+ cleanupEtcdCluster (ctx , t , c , etcdClusterName )
174
+ return ctx
175
+ })
227
176
228
- _ = testEnv .Test (t , feature .Feature ())
177
+ _ = testEnv .Test (t , feature .Feature ())
178
+ })
179
+ }
229
180
}
230
181
231
182
func TestPromoteReadyLearner (t * testing.T ) {
@@ -234,37 +185,36 @@ func TestPromoteReadyLearner(t *testing.T) {
234
185
235
186
feature .Setup (func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
236
187
createEtcdCluster (ctx , t , c , etcdClusterName , 2 )
237
- waitForStsReady (ctx , t , c , etcdClusterName , 2 )
188
+ waitForStsReady (t , c , etcdClusterName , 2 )
238
189
// Manually add a third member as a learner
239
190
podName := fmt .Sprintf ("%s-0" , etcdClusterName )
240
- _ , stderr , err := execInPod (t , c , podName , namespace , []string {"etcdctl" , "member" , "add" , "etcd-promote-learner-2" , "--peer-urls=http://etcd-promote-learner-2.etcd-promote-learner.etcd-operator-system.svc.cluster.local:2380" , "--learner" })
191
+ command := []string {
192
+ "etcdctl" , "member" , "add" , "etcd-promote-learner-2" ,
193
+ "--peer-urls=http://etcd-promote-learner-2.etcd-promote-learner.etcd-operator-system.svc.cluster.local:2380" ,
194
+ "--learner" ,
195
+ }
196
+ _ , stderr , err := execInPod (t , c , podName , namespace , command )
241
197
if err != nil {
242
198
t .Fatalf ("Failed to add learner: %v, stderr: %s" , err , stderr )
243
199
}
244
200
return ctx
245
201
})
246
202
247
203
feature .Assess ("promote learner" , func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
248
- var etcdCluster ec_v1alpha1.EtcdCluster
249
- if err := c .Client ().Resources ().Get (ctx , etcdClusterName , namespace , & etcdCluster ); err != nil {
250
- t .Fatalf ("Failed to get EtcdCluster: %v" , err )
251
- }
252
-
253
- etcdCluster .Spec .Size = 3
254
- if err := c .Client ().Resources ().Update (ctx , & etcdCluster ); err != nil {
255
- t .Fatalf ("Failed to update EtcdCluster: %v" , err )
256
- }
204
+ scaleEtcdCluster (ctx , t , c , etcdClusterName , 3 )
257
205
258
206
// Wait for the learner to be promoted.
259
- wait .For (func (ctx context.Context ) (done bool , err error ) {
207
+ if err := wait .For (func (ctx context.Context ) (done bool , err error ) {
260
208
podName := fmt .Sprintf ("%s-0" , etcdClusterName )
261
209
command := []string {"etcdctl" , "member" , "list" , "-w" , "table" }
262
210
stdout , _ , err := execInPod (t , c , podName , namespace , command )
263
211
if err != nil {
264
212
return false , nil
265
213
}
266
214
return ! strings .Contains (stdout , "true" ), nil
267
- }, wait .WithTimeout (2 * time .Minute ), wait .WithInterval (5 * time .Second ))
215
+ }, wait .WithTimeout (2 * time .Minute ), wait .WithInterval (5 * time .Second )); err != nil {
216
+ t .Fatalf ("Failed to wait for learner promotion: %v" , err )
217
+ }
268
218
269
219
return ctx
270
220
})
@@ -284,12 +234,7 @@ func TestPromoteReadyLearner(t *testing.T) {
284
234
})
285
235
286
236
feature .Teardown (func (ctx context.Context , t * testing.T , c * envconf.Config ) context.Context {
287
- var etcdCluster ec_v1alpha1.EtcdCluster
288
- if err := c .Client ().Resources ().Get (ctx , etcdClusterName , namespace , & etcdCluster ); err == nil {
289
- if err := c .Client ().Resources ().Delete (ctx , & etcdCluster ); err != nil {
290
- t .Logf ("Failed to delete EtcdCluster: %v" , err )
291
- }
292
- }
237
+ cleanupEtcdCluster (ctx , t , c , etcdClusterName )
293
238
return ctx
294
239
})
295
240
0 commit comments