Skip to content

Commit 841598a

Browse files
committed
chore: use testify instead of testing in tests/e2e
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
1 parent f07e2ae commit 841598a

30 files changed

+167
-407
lines changed

tests/e2e/cluster_downgrade_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,9 @@ func newCluster(t *testing.T, clusterSize int, snapshotCount uint64) *e2e.EtcdPr
234234
e2e.WithSnapshotCount(snapshotCount),
235235
e2e.WithKeepDataDir(true),
236236
)
237-
if err != nil {
238-
t.Fatalf("could not start etcd process cluster (%v)", err)
239-
}
237+
require.NoErrorf(t, err, "could not start etcd process cluster (%v)", err)
240238
t.Cleanup(func() {
241-
if errC := epc.Close(); errC != nil {
242-
t.Fatalf("error closing etcd processes (%v)", errC)
243-
}
239+
require.NoErrorf(t, epc.Close(), "error closing etcd processes")
244240
})
245241
return epc
246242
}

tests/e2e/corrupt_test.go

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,9 @@ func TestInPlaceRecovery(t *testing.T) {
109109
e2e.WithCorruptCheckTime(time.Second),
110110
e2e.WithBasePort(basePort),
111111
)
112-
if err != nil {
113-
t.Fatalf("could not start etcd process cluster (%v)", err)
114-
}
112+
require.NoErrorf(t, err, "could not start etcd process cluster")
115113
t.Cleanup(func() {
116-
if errC := epcOld.Close(); errC != nil {
117-
t.Fatalf("error closing etcd processes (%v)", errC)
118-
}
114+
require.NoErrorf(t, epcOld.Close(), "error closing etcd processes")
119115
})
120116
t.Log("old cluster started.")
121117

@@ -138,13 +134,9 @@ func TestInPlaceRecovery(t *testing.T) {
138134
e2e.WithInitialCorruptCheck(true),
139135
)
140136
epcNew, err := e2e.InitEtcdProcessCluster(t, epcNewConfig)
141-
if err != nil {
142-
t.Fatalf("could not init etcd process cluster (%v)", err)
143-
}
137+
require.NoErrorf(t, err, "could not init etcd process cluster (%v)", err)
144138
t.Cleanup(func() {
145-
if errC := epcNew.Close(); errC != nil {
146-
t.Fatalf("error closing etcd processes (%v)", errC)
147-
}
139+
require.NoErrorf(t, epcNew.Close(), "error closing etcd processes")
148140
})
149141

150142
newCc, err := e2e.NewEtcdctl(epcNew.Cfg.Client, epcNew.EndpointsGRPC())
@@ -155,20 +147,14 @@ func TestInPlaceRecovery(t *testing.T) {
155147
t.Log("rolling updating servers in place...")
156148
for i := range epcNew.Procs {
157149
oldProc := epcOld.Procs[i]
158-
err = oldProc.Close()
159-
if err != nil {
160-
t.Fatalf("could not stop etcd process (%v)", err)
161-
}
150+
require.NoErrorf(t, oldProc.Close(), "could not stop etcd process")
162151
t.Logf("old cluster server %d: %s stopped.", i, oldProc.Config().Name)
163152
wg.Add(1)
164153
// Start servers in background to avoid blocking on server start.
165154
// EtcdProcess.Start waits until etcd becomes healthy, which will not happen here until we restart at least 2 members.
166155
go func(proc e2e.EtcdProcess) {
167156
defer wg.Done()
168-
err = proc.Start(ctx)
169-
if err != nil {
170-
t.Errorf("could not start etcd process (%v)", err)
171-
}
157+
assert.NoErrorf(t, proc.Start(ctx), "could not start etcd process")
172158
t.Logf("new cluster server: %s started in-place with blank db.", proc.Config().Name)
173159
}(epcNew.Procs[i])
174160
t.Log("sleeping 5 sec to let nodes do periodical check...")
@@ -180,9 +166,7 @@ func TestInPlaceRecovery(t *testing.T) {
180166
alarmResponse, err := newCc.AlarmList(ctx)
181167
require.NoErrorf(t, err, "error on alarm list")
182168
for _, alarm := range alarmResponse.Alarms {
183-
if alarm.Alarm == etcdserverpb.AlarmType_CORRUPT {
184-
t.Fatalf("there is no corruption after in-place recovery, but corruption reported.")
185-
}
169+
require.NotEqualf(t, etcdserverpb.AlarmType_CORRUPT, alarm.Alarm, "there is no corruption after in-place recovery, but corruption reported.")
186170
}
187171
t.Log("no corruption detected.")
188172
}
@@ -210,13 +194,9 @@ func testPeriodicCheckDetectsCorruption(t *testing.T, useExperimentalFlag bool)
210194
e2e.WithKeepDataDir(true),
211195
corruptCheckTime,
212196
)
213-
if err != nil {
214-
t.Fatalf("could not start etcd process cluster (%v)", err)
215-
}
197+
require.NoErrorf(t, err, "could not start etcd process cluster (%v)", err)
216198
t.Cleanup(func() {
217-
if errC := epc.Close(); errC != nil {
218-
t.Fatalf("error closing etcd processes (%v)", errC)
219-
}
199+
require.NoErrorf(t, epc.Close(), "error closing etcd processes")
220200
})
221201

222202
cc := epc.Etcdctl()
@@ -261,13 +241,9 @@ func testCompactHashCheckDetectCorruption(t *testing.T, useFeatureGate bool) {
261241
opts = append(opts, e2e.WithCompactHashCheckEnabled(true))
262242
}
263243
epc, err := e2e.NewEtcdProcessCluster(ctx, t, opts...)
264-
if err != nil {
265-
t.Fatalf("could not start etcd process cluster (%v)", err)
266-
}
244+
require.NoErrorf(t, err, "could not start etcd process cluster (%v)", err)
267245
t.Cleanup(func() {
268-
if errC := epc.Close(); errC != nil {
269-
t.Fatalf("error closing etcd processes (%v)", errC)
270-
}
246+
require.NoErrorf(t, epc.Close(), "error closing etcd processes")
271247
})
272248

273249
cc := epc.Etcdctl()
@@ -351,9 +327,7 @@ func testCompactHashCheckDetectCorruptionInterrupt(t *testing.T, useFeatureGate
351327
require.NoError(t, err)
352328

353329
t.Cleanup(func() {
354-
if errC := epc.Close(); errC != nil {
355-
t.Fatalf("error closing etcd processes (%v)", errC)
356-
}
330+
require.NoErrorf(t, epc.Close(), "error closing etcd processes")
357331
})
358332

359333
// Put 10 identical keys to the cluster, so that the compaction will drop some stale values.
@@ -389,9 +363,7 @@ func testCompactHashCheckDetectCorruptionInterrupt(t *testing.T, useFeatureGate
389363
alarmResponse, err := cc.AlarmList(ctx)
390364
require.NoErrorf(t, err, "error on alarm list")
391365
for _, alarm := range alarmResponse.Alarms {
392-
if alarm.Alarm == etcdserverpb.AlarmType_CORRUPT {
393-
t.Fatal("there should be no corruption after resuming the compaction, but corruption detected")
394-
}
366+
require.NotEqualf(t, etcdserverpb.AlarmType_CORRUPT, alarm.Alarm, "there should be no corruption after resuming the compaction, but corruption detected")
395367
}
396368
t.Log("no corruption detected.")
397369
}

tests/e2e/ctl_v3_auth_cluster_test.go

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
2526

2627
clientv3 "go.etcd.io/etcd/client/v3"
2728
"go.etcd.io/etcd/tests/v3/framework/config"
@@ -37,44 +38,33 @@ func TestAuthCluster(t *testing.T) {
3738
e2e.WithClusterSize(1),
3839
e2e.WithSnapshotCount(2),
3940
)
40-
if err != nil {
41-
t.Fatalf("could not start etcd process cluster (%v)", err)
42-
}
41+
require.NoErrorf(t, err, "could not start etcd process cluster (%v)", err)
4342
defer func() {
44-
if err := epc.Close(); err != nil {
45-
t.Fatalf("could not close test cluster (%v)", err)
46-
}
43+
require.NoErrorf(t, epc.Close(), "could not close test cluster")
4744
}()
4845

4946
epcClient := epc.Etcdctl()
5047
createUsers(ctx, t, epcClient)
5148

52-
if err := epcClient.AuthEnable(ctx); err != nil {
53-
t.Fatalf("could not enable Auth: (%v)", err)
54-
}
49+
require.NoErrorf(t, epcClient.AuthEnable(ctx), "could not enable Auth")
5550

5651
testUserClientOpts := e2e.WithAuth("test", "testPassword")
5752
rootUserClientOpts := e2e.WithAuth("root", "rootPassword")
5853

5954
// write more than SnapshotCount keys to single leader to make sure snapshot is created
6055
for i := 0; i <= 10; i++ {
61-
if err := epc.Etcdctl(testUserClientOpts).Put(ctx, fmt.Sprintf("/test/%d", i), "test", config.PutOptions{}); err != nil {
62-
t.Fatalf("failed to Put (%v)", err)
63-
}
56+
require.NoErrorf(t, epc.Etcdctl(testUserClientOpts).Put(ctx, fmt.Sprintf("/test/%d", i), "test", config.PutOptions{}), "failed to Put")
6457
}
6558

6659
// start second process
67-
if _, err := epc.StartNewProc(ctx, nil, t, false /* addAsLearner */, rootUserClientOpts); err != nil {
68-
t.Fatalf("could not start second etcd process (%v)", err)
69-
}
60+
_, err = epc.StartNewProc(ctx, nil, t, false /* addAsLearner */, rootUserClientOpts)
61+
require.NoErrorf(t, err, "could not start second etcd process")
7062

7163
// make sure writes to both endpoints are successful
7264
endpoints := epc.EndpointsGRPC()
7365
assert.Len(t, endpoints, 2)
7466
for _, endpoint := range epc.EndpointsGRPC() {
75-
if err := epc.Etcdctl(testUserClientOpts, e2e.WithEndpoints([]string{endpoint})).Put(ctx, "/test/key", endpoint, config.PutOptions{}); err != nil {
76-
t.Fatalf("failed to write to Put to %q (%v)", endpoint, err)
77-
}
67+
require.NoErrorf(t, epc.Etcdctl(testUserClientOpts, e2e.WithEndpoints([]string{endpoint})).Put(ctx, "/test/key", endpoint, config.PutOptions{}), "failed to write to Put to %q", endpoint)
7868
}
7969

8070
// verify all nodes have exact same revision and hash
@@ -120,26 +110,19 @@ func applyTLSWithRootCommonName() func() {
120110
}
121111

122112
func createUsers(ctx context.Context, t *testing.T, client *e2e.EtcdctlV3) {
123-
if _, err := client.UserAdd(ctx, "root", "rootPassword", config.UserAddOptions{}); err != nil {
124-
t.Fatalf("could not add root user (%v)", err)
125-
}
126-
if _, err := client.RoleAdd(ctx, "root"); err != nil {
127-
t.Fatalf("could not create 'root' role (%v)", err)
128-
}
129-
if _, err := client.UserGrantRole(ctx, "root", "root"); err != nil {
130-
t.Fatalf("could not grant root role to root user (%v)", err)
131-
}
132-
133-
if _, err := client.RoleAdd(ctx, "test"); err != nil {
134-
t.Fatalf("could not create 'test' role (%v)", err)
135-
}
136-
if _, err := client.RoleGrantPermission(ctx, "test", "/test/", "/test0", clientv3.PermissionType(clientv3.PermReadWrite)); err != nil {
137-
t.Fatalf("could not RoleGrantPermission (%v)", err)
138-
}
139-
if _, err := client.UserAdd(ctx, "test", "testPassword", config.UserAddOptions{}); err != nil {
140-
t.Fatalf("could not add user test (%v)", err)
141-
}
142-
if _, err := client.UserGrantRole(ctx, "test", "test"); err != nil {
143-
t.Fatalf("could not grant test role user (%v)", err)
144-
}
113+
_, err := client.UserAdd(ctx, "root", "rootPassword", config.UserAddOptions{})
114+
require.NoErrorf(t, err, "could not add root user")
115+
_, err = client.RoleAdd(ctx, "root")
116+
require.NoErrorf(t, err, "could not create 'root' role")
117+
_, err = client.UserGrantRole(ctx, "root", "root")
118+
require.NoErrorf(t, err, "could not grant root role to root user")
119+
120+
_, err = client.RoleAdd(ctx, "test")
121+
require.NoErrorf(t, err, "could not create 'test' role")
122+
_, err = client.RoleGrantPermission(ctx, "test", "/test/", "/test0", clientv3.PermissionType(clientv3.PermReadWrite))
123+
require.NoErrorf(t, err, "could not RoleGrantPermission")
124+
_, err = client.UserAdd(ctx, "test", "testPassword", config.UserAddOptions{})
125+
require.NoErrorf(t, err, "could not add user test")
126+
_, err = client.UserGrantRole(ctx, "test", "test")
127+
require.NoErrorf(t, err, "could not grant test role user")
145128
}

tests/e2e/ctl_v3_auth_no_proxy_test.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"testing"
2626
"time"
2727

28+
"github.com/stretchr/testify/require"
29+
2830
"go.etcd.io/etcd/tests/v3/framework/config"
2931
"go.etcd.io/etcd/tests/v3/framework/e2e"
3032
)
@@ -60,23 +62,17 @@ func TestCtlV3AuthCertCNWithWithConcurrentOperation(t *testing.T) {
6062
e2e.WithClientConnType(e2e.ClientTLS),
6163
e2e.WithClientCertAuthority(true),
6264
)
63-
if err != nil {
64-
t.Fatalf("could not start etcd process cluster (%v)", err)
65-
}
65+
require.NoErrorf(t, err, "could not start etcd process cluster")
6666
defer func() {
67-
if err := epc.Close(); err != nil {
68-
t.Fatalf("could not close test cluster (%v)", err)
69-
}
67+
require.NoErrorf(t, epc.Close(), "could not close test cluster")
7068
}()
7169

7270
epcClient := epc.Etcdctl()
7371
t.Log("Create users")
7472
createUsers(ctx, t, epcClient)
7573

7674
t.Log("Enable auth")
77-
if err := epcClient.AuthEnable(ctx); err != nil {
78-
t.Fatalf("could not enable Auth: (%v)", err)
79-
}
75+
require.NoErrorf(t, epcClient.AuthEnable(ctx), "could not enable Auth")
8076

8177
// Create two goroutines, one goroutine keeps creating & deleting users,
8278
// and the other goroutine keeps writing & deleting K/V entries.

tests/e2e/ctl_v3_auth_security_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package e2e
1818

1919
import (
20-
"strings"
2120
"testing"
2221

22+
"github.com/stretchr/testify/assert"
2323
"github.com/stretchr/testify/require"
2424

2525
"go.etcd.io/etcd/tests/v3/framework/e2e"
@@ -51,7 +51,5 @@ func authTestCVE2021_28235(cx ctlCtx) {
5151
respData, err := curl(httpEndpoint, "GET", req, e2e.ClientNonTLS)
5252
require.NoError(cx.t, err)
5353

54-
if strings.Contains(respData, rootPass) {
55-
cx.t.Errorf("The root password is included in the request.\n %s", respData)
56-
}
54+
assert.NotContainsf(cx.t, respData, rootPass, "The root password is included in the request.\n %s", respData)
5755
}

tests/e2e/ctl_v3_auth_test.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"os"
2020
"testing"
2121

22+
"github.com/stretchr/testify/assert"
2223
"github.com/stretchr/testify/require"
2324

2425
"go.etcd.io/etcd/pkg/v3/expect"
@@ -92,9 +93,7 @@ func authTestMemberUpdate(cx ctlCtx) {
9293
cx.user, cx.pass = "test-user", "pass"
9394
peerURL := fmt.Sprintf("http://localhost:%d", e2e.EtcdProcessBasePort+11)
9495
memberID := fmt.Sprintf("%x", mr.Members[0].ID)
95-
if err = ctlV3MemberUpdate(cx, memberID, peerURL); err == nil {
96-
cx.t.Fatalf("ordinary user must not be allowed to update a member")
97-
}
96+
require.Errorf(cx.t, ctlV3MemberUpdate(cx, memberID, peerURL), "ordinary user must not be allowed to update a member")
9897

9998
// root can update a member
10099
cx.user, cx.pass = "root", "root"
@@ -115,9 +114,7 @@ func authTestCertCN(cx ctlCtx) {
115114

116115
// try a granted key
117116
cx.user, cx.pass = "", ""
118-
if err := ctlV3Put(cx, "hoo", "bar", ""); err != nil {
119-
cx.t.Error(err)
120-
}
117+
assert.NoError(cx.t, ctlV3Put(cx, "hoo", "bar", ""))
121118

122119
// try a non-granted key
123120
cx.user, cx.pass = "", ""
@@ -233,9 +230,7 @@ func authTestWatch(cx ctlCtx) {
233230
go func(i int, puts []kv) {
234231
defer close(donec)
235232
for j := range puts {
236-
if err := ctlV3Put(cx, puts[j].key, puts[j].val, ""); err != nil {
237-
cx.t.Errorf("watchTest #%d-%d: ctlV3Put error (%v)", i, j, err)
238-
}
233+
assert.NoErrorf(cx.t, ctlV3Put(cx, puts[j].key, puts[j].val, ""), "watchTest #%d-%d: ctlV3Put error", i, j)
239234
}
240235
}(i, tt.puts)
241236

@@ -276,12 +271,8 @@ func authTestSnapshot(cx ctlCtx) {
276271

277272
st, err := getSnapshotStatus(cx, fpath)
278273
require.NoErrorf(cx.t, err, "snapshotTest getSnapshotStatus error")
279-
if st.Revision != 4 {
280-
cx.t.Fatalf("expected 4, got %d", st.Revision)
281-
}
282-
if st.TotalKey < 1 {
283-
cx.t.Fatalf("expected at least 1, got %d", st.TotalKey)
284-
}
274+
require.Equalf(cx.t, 4, st.Revision, "expected 4, got %d", st.Revision)
275+
require.GreaterOrEqualf(cx.t, st.TotalKey, 1, "expected at least 1, got %d", st.TotalKey)
285276
}
286277

287278
func authTestEndpointHealth(cx ctlCtx) {
@@ -300,9 +291,7 @@ func authTestEndpointHealth(cx ctlCtx) {
300291
cx.user, cx.pass = "root", "root"
301292
require.NoError(cx.t, ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "health", "", false}))
302293
cx.user, cx.pass = "test-user", "pass"
303-
if err := ctlV3EndpointHealth(cx); err != nil {
304-
cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
305-
}
294+
require.NoErrorf(cx.t, ctlV3EndpointHealth(cx), "endpointStatusTest ctlV3EndpointHealth error")
306295
}
307296

308297
func certCNAndUsername(cx ctlCtx, noPassword bool) {

tests/e2e/ctl_v3_defrag_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,5 @@ func ctlV3OfflineDefrag(cx ctlCtx) error {
4141
}
4242

4343
func defragOfflineTest(cx ctlCtx) {
44-
if err := ctlV3OfflineDefrag(cx); err != nil {
45-
cx.t.Fatalf("defragTest ctlV3Defrag error (%v)", err)
46-
}
44+
require.NoErrorf(cx.t, ctlV3OfflineDefrag(cx), "defragTest ctlV3Defrag error")
4745
}

tests/e2e/ctl_v3_elect_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222
"time"
2323

24+
"github.com/stretchr/testify/assert"
2425
"github.com/stretchr/testify/require"
2526

2627
"go.etcd.io/etcd/pkg/v3/expect"
@@ -42,9 +43,7 @@ func testElect(cx ctlCtx) {
4243
case <-time.After(2 * time.Second):
4344
cx.t.Fatalf("timed out electing")
4445
case l1 = <-ch:
45-
if !strings.HasPrefix(l1, name) {
46-
cx.t.Errorf("got %q, expected %q prefix", l1, name)
47-
}
46+
assert.Truef(cx.t, strings.HasPrefix(l1, name), "got %q, expected %q prefix", l1, name)
4847
}
4948

5049
// blocked process that won't win the election
@@ -109,9 +108,7 @@ func ctlV3Elect(cx ctlCtx, name, proposal string, expectFailure bool) (*expect.E
109108

110109
s, xerr := proc.ExpectFunc(ctx, func(string) bool { return true })
111110
if xerr != nil {
112-
if !expectFailure {
113-
cx.t.Errorf("expect failed (%v)", xerr)
114-
}
111+
assert.Truef(cx.t, expectFailure, "expect failed (%v)", xerr)
115112
}
116113
outc <- s
117114
}()

0 commit comments

Comments
 (0)