Skip to content

Commit fa7f810

Browse files
committed
fix(lint): Resolve linting errors and refactor E2E tests
Signed-off-by: Wenxue Zhao <ballista01@outlook.com>
1 parent 962ad81 commit fa7f810

File tree

2 files changed

+93
-121
lines changed

2 files changed

+93
-121
lines changed

test/e2e/e2e_test.go

Lines changed: 59 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -126,106 +126,57 @@ func TestClusterHealthy(t *testing.T) {
126126
_ = testEnv.Test(t, feature.Feature())
127127
}
128128

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+
}
189139

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))
195144

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+
}
211165

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+
})
217171

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+
})
227176

228-
_ = testEnv.Test(t, feature.Feature())
177+
_ = testEnv.Test(t, feature.Feature())
178+
})
179+
}
229180
}
230181

231182
func TestPromoteReadyLearner(t *testing.T) {
@@ -234,37 +185,36 @@ func TestPromoteReadyLearner(t *testing.T) {
234185

235186
feature.Setup(func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
236187
createEtcdCluster(ctx, t, c, etcdClusterName, 2)
237-
waitForStsReady(ctx, t, c, etcdClusterName, 2)
188+
waitForStsReady(t, c, etcdClusterName, 2)
238189
// Manually add a third member as a learner
239190
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)
241197
if err != nil {
242198
t.Fatalf("Failed to add learner: %v, stderr: %s", err, stderr)
243199
}
244200
return ctx
245201
})
246202

247203
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)
257205

258206
// 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) {
260208
podName := fmt.Sprintf("%s-0", etcdClusterName)
261209
command := []string{"etcdctl", "member", "list", "-w", "table"}
262210
stdout, _, err := execInPod(t, c, podName, namespace, command)
263211
if err != nil {
264212
return false, nil
265213
}
266214
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+
}
268218

269219
return ctx
270220
})
@@ -284,12 +234,7 @@ func TestPromoteReadyLearner(t *testing.T) {
284234
})
285235

286236
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)
293238
return ctx
294239
})
295240

test/e2e/helpers.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ import (
77
"testing"
88
"time"
99

10-
ecv1alpha1 "go.etcd.io/etcd-operator/api/v1alpha1"
1110
appsv1 "k8s.io/api/apps/v1"
1211
corev1 "k8s.io/api/core/v1"
1312
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1413
"sigs.k8s.io/e2e-framework/klient/k8s"
1514
"sigs.k8s.io/e2e-framework/klient/wait"
1615
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
1716
"sigs.k8s.io/e2e-framework/pkg/envconf"
17+
18+
ecv1alpha1 "go.etcd.io/etcd-operator/api/v1alpha1"
1819
)
1920

20-
func createEtcdCluster(ctx context.Context, t *testing.T, c *envconf.Config, name string, size int) *ecv1alpha1.EtcdCluster {
21+
func createEtcdCluster(ctx context.Context, t *testing.T, c *envconf.Config, name string, size int) {
2122
t.Helper()
2223
etcdCluster := &ecv1alpha1.EtcdCluster{
2324
ObjectMeta: metav1.ObjectMeta{
@@ -32,10 +33,9 @@ func createEtcdCluster(ctx context.Context, t *testing.T, c *envconf.Config, nam
3233
if err := c.Client().Resources().Create(ctx, etcdCluster); err != nil {
3334
t.Fatalf("Failed to create EtcdCluster: %v", err)
3435
}
35-
return etcdCluster
3636
}
3737

38-
func waitForStsReady(ctx context.Context, t *testing.T, c *envconf.Config, name string, expectedReplicas int) {
38+
func waitForStsReady(t *testing.T, c *envconf.Config, name string, expectedReplicas int) {
3939
t.Helper()
4040
var sts appsv1.StatefulSet
4141
err := wait.For(func(ctx context.Context) (done bool, err error) {
@@ -63,14 +63,16 @@ func waitForStsReady(ctx context.Context, t *testing.T, c *envconf.Config, name
6363
}
6464
}
6565

66-
func execInPod(t *testing.T, cfg *envconf.Config, podName string, namespace string, command []string) (string, string, error) {
66+
func execInPod(
67+
t *testing.T, cfg *envconf.Config, podName string, namespace string, command []string,
68+
) (string, string, error) {
6769
t.Helper()
6870
var stdout, stderr bytes.Buffer
6971
client := cfg.Client()
7072

7173
// Find the pod
7274
var pod corev1.Pod
73-
if err := client.Resources().Get(context.Background(), podName, namespace, &pod); err != nil {
75+
if err := client.Resources().Get(t.Context(), podName, namespace, &pod); err != nil {
7476
return "", "", fmt.Errorf("failed to get pod %s/%s: %w", namespace, podName, err)
7577
}
7678

@@ -81,6 +83,31 @@ func execInPod(t *testing.T, cfg *envconf.Config, podName string, namespace stri
8183
containerName := pod.Spec.Containers[0].Name
8284

8385
// Exec command
84-
err := client.Resources().ExecInPod(context.Background(), namespace, podName, containerName, command, &stdout, &stderr)
86+
err := client.Resources().ExecInPod(t.Context(), namespace, podName, containerName, command, &stdout, &stderr)
8587
return stdout.String(), stderr.String(), err
8688
}
89+
90+
func scaleEtcdCluster(ctx context.Context, t *testing.T, c *envconf.Config, name string, size int) {
91+
t.Helper()
92+
var etcdCluster ecv1alpha1.EtcdCluster
93+
if err := c.Client().Resources().Get(ctx, name, namespace, &etcdCluster); err != nil {
94+
t.Fatalf("Failed to get EtcdCluster: %v", err)
95+
}
96+
97+
etcdCluster.Spec.Size = size
98+
if err := c.Client().Resources().Update(ctx, &etcdCluster); err != nil {
99+
t.Fatalf("Failed to update EtcdCluster: %v", err)
100+
}
101+
102+
waitForStsReady(t, c, name, size)
103+
}
104+
105+
func cleanupEtcdCluster(ctx context.Context, t *testing.T, c *envconf.Config, name string) {
106+
t.Helper()
107+
var etcdCluster ecv1alpha1.EtcdCluster
108+
if err := c.Client().Resources().Get(ctx, name, namespace, &etcdCluster); err == nil {
109+
if err := c.Client().Resources().Delete(ctx, &etcdCluster); err != nil {
110+
t.Logf("Failed to delete EtcdCluster: %v", err)
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)