Skip to content

Commit

Permalink
fix: use require.NoError instead of t.Fatal(err) in tests and tools p…
Browse files Browse the repository at this point in the history
…ackages

Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Oct 10, 2024
1 parent f5acd17 commit 3840876
Show file tree
Hide file tree
Showing 28 changed files with 120 additions and 261 deletions.
42 changes: 15 additions & 27 deletions tests/common/alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/testutils"
Expand All @@ -39,9 +41,7 @@ func TestAlarm(t *testing.T) {
testutils.ExecuteUntil(ctx, t, func() {
// test small put still works
smallbuf := strings.Repeat("a", 64)
if err := cc.Put(ctx, "1st_test", smallbuf, config.PutOptions{}); err != nil {
t.Fatalf("alarmTest: put kv error (%v)", err)
}
require.NoErrorf(t, cc.Put(ctx, "1st_test", smallbuf, config.PutOptions{}), "alarmTest: put kv error")

// write some chunks to fill up the database
buf := strings.Repeat("b", os.Getpagesize())
Expand All @@ -56,9 +56,7 @@ func TestAlarm(t *testing.T) {

// quota alarm should now be on
alarmResp, err := cc.AlarmList(ctx)
if err != nil {
t.Fatalf("alarmTest: Alarm error (%v)", err)
}
require.NoErrorf(t, err, "alarmTest: Alarm error")

// check that Put is rejected when alarm is on
if err = cc.Put(ctx, "3rd_test", smallbuf, config.PutOptions{}); err != nil {
Expand All @@ -69,9 +67,7 @@ func TestAlarm(t *testing.T) {

// get latest revision to compact
sresp, err := cc.Status(ctx)
if err != nil {
t.Fatalf("get endpoint status error: %v", err)
}
require.NoErrorf(t, err, "get endpoint status error")
var rvs int64
for _, resp := range sresp {
if resp != nil && resp.Header != nil {
Expand All @@ -82,13 +78,10 @@ func TestAlarm(t *testing.T) {

// make some space
_, err = cc.Compact(ctx, rvs, config.CompactOption{Physical: true, Timeout: 10 * time.Second})
if err != nil {
t.Fatalf("alarmTest: Compact error (%v)", err)
}
require.NoErrorf(t, err, "alarmTest: Compact error")

if err = cc.Defragment(ctx, config.DefragOption{Timeout: 10 * time.Second}); err != nil {
t.Fatalf("alarmTest: defrag error (%v)", err)
}
err = cc.Defragment(ctx, config.DefragOption{Timeout: 10 * time.Second})
require.NoErrorf(t, err, "alarmTest: defrag error")

// turn off alarm
for _, alarm := range alarmResp.Alarms {
Expand All @@ -97,15 +90,12 @@ func TestAlarm(t *testing.T) {
Alarm: alarm.Alarm,
}
_, err = cc.AlarmDisarm(ctx, alarmMember)
if err != nil {
t.Fatalf("alarmTest: Alarm error (%v)", err)
}
require.NoErrorf(t, err, "alarmTest: Alarm error")
}

// put one more key below quota
if err := cc.Put(ctx, "4th_test", smallbuf, config.PutOptions{}); err != nil {
t.Fatal(err)
}
err = cc.Put(ctx, "4th_test", smallbuf, config.PutOptions{})
require.NoError(t, err)
})
}

Expand All @@ -123,14 +113,12 @@ func TestAlarmlistOnMemberRestart(t *testing.T) {

testutils.ExecuteUntil(ctx, t, func() {
for i := 0; i < 6; i++ {
if _, err := cc.AlarmList(ctx); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
_, err := cc.AlarmList(ctx)
require.NoErrorf(t, err, "Unexpected error")
}

clus.Members()[0].Stop()
if err := clus.Members()[0].Start(ctx); err != nil {
t.Fatalf("failed to start etcdserver: %v", err)
}
err := clus.Members()[0].Start(ctx)
require.NoErrorf(t, err, "failed to start etcdserver")
})
}
15 changes: 6 additions & 9 deletions tests/common/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,12 @@ func TestAuthTxn(t *testing.T) {
// keys with 2 suffix are granted to test-user, see Line 399
grantedKeys := []string{"c2", "s2", "f2"}
for _, key := range keys {
if err := cc.Put(ctx, key, "v", config.PutOptions{}); err != nil {
t.Fatal(err)
}
err := cc.Put(ctx, key, "v", config.PutOptions{})
require.NoError(t, err)
}
for _, key := range grantedKeys {
if err := cc.Put(ctx, key, "v", config.PutOptions{}); err != nil {
t.Fatal(err)
}
err := cc.Put(ctx, key, "v", config.PutOptions{})
require.NoError(t, err)
}

require.NoErrorf(t, setupAuth(cc, []authRole{testRole}, []authUser{rootUser, testUser}), "failed to enable auth")
Expand All @@ -394,9 +392,8 @@ func TestAuthTxn(t *testing.T) {

// grant keys to test-user
for _, key := range grantedKeys {
if _, err := rootAuthClient.RoleGrantPermission(ctx, testRoleName, key, "", clientv3.PermissionType(clientv3.PermReadWrite)); err != nil {
t.Fatal(err)
}
_, err := rootAuthClient.RoleGrantPermission(ctx, testRoleName, key, "", clientv3.PermissionType(clientv3.PermReadWrite))
require.NoError(t, err)
}
for _, req := range reqs {
resp, err := testUserAuthClient.Txn(ctx, req.compare, req.ifSuccess, req.ifFail, config.TxnOptions{
Expand Down
14 changes: 5 additions & 9 deletions tests/common/compact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/testutils"
Expand Down Expand Up @@ -52,22 +53,17 @@ func TestCompact(t *testing.T) {
testutils.ExecuteUntil(ctx, t, func() {
var kvs = []testutils.KV{{Key: "key", Val: "val1"}, {Key: "key", Val: "val2"}, {Key: "key", Val: "val3"}}
for i := range kvs {
if err := cc.Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{}); err != nil {
t.Fatalf("compactTest #%d: put kv error (%v)", i, err)
}
err := cc.Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{})
require.NoErrorf(t, err, "compactTest #%d: put kv error", i)
}
get, err := cc.Get(ctx, "key", config.GetOptions{Revision: 3})
if err != nil {
t.Fatalf("compactTest: Get kv by revision error (%v)", err)
}
require.NoErrorf(t, err, "compactTest: Get kv by revision error")

getkvs := testutils.KeyValuesFromGetResponse(get)
assert.Equal(t, kvs[1:2], getkvs)

_, err = cc.Compact(ctx, 4, tc.options)
if err != nil {
t.Fatalf("compactTest: Compact error (%v)", err)
}
require.NoErrorf(t, err, "compactTest: Compact error")

_, err = cc.Get(ctx, "key", config.GetOptions{Revision: 3})
if err != nil {
Expand Down
16 changes: 7 additions & 9 deletions tests/common/defrag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"go.etcd.io/etcd/tests/v3/framework/config"
"go.etcd.io/etcd/tests/v3/framework/testutils"
)
Expand All @@ -34,17 +36,13 @@ func TestDefragOnline(t *testing.T) {
defer clus.Close()
var kvs = []testutils.KV{{Key: "key", Val: "val1"}, {Key: "key", Val: "val2"}, {Key: "key", Val: "val3"}}
for i := range kvs {
if err := cc.Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{}); err != nil {
t.Fatalf("compactTest #%d: put kv error (%v)", i, err)
}
err := cc.Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{})
require.NoErrorf(t, err, "compactTest #%d: put kv error", i)
}
_, err := cc.Compact(ctx, 4, config.CompactOption{Physical: true, Timeout: 10 * time.Second})
if err != nil {
t.Fatalf("defrag_test: compact with revision error (%v)", err)
}
require.NoErrorf(t, err, "defrag_test: compact with revision error")

if err = cc.Defragment(ctx, options); err != nil {
t.Fatalf("defrag_test: defrag error (%v)", err)
}
err = cc.Defragment(ctx, options)
require.NoErrorf(t, err, "defrag_test: defrag error")
})
}
8 changes: 2 additions & 6 deletions tests/common/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,15 @@ func TestMemberRemove(t *testing.T) {
// It ensures that `MemberRemove` function does not return an "etcdserver: server stopped" error.
func memberToRemove(ctx context.Context, t *testing.T, client intf.Client, clusterSize int) (memberID uint64, clusterID uint64) {
listResp, err := client.MemberList(ctx, false)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

clusterID = listResp.Header.ClusterId
if clusterSize == 1 {
memberID = listResp.Members[0].ID
} else {
// get status of the specific member that client has connected to
statusResp, err := client.Status(ctx)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

// choose a member that client has not connected to
for _, m := range listResp.Members {
Expand Down
17 changes: 5 additions & 12 deletions tests/e2e/cluster_downgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,13 @@ func downgradeEnable(t *testing.T, epc *e2e.EtcdProcessCluster, ver *semver.Vers
c := epc.Etcdctl()
testutils.ExecuteWithTimeout(t, 20*time.Second, func() {
err := c.DowngradeEnable(context.TODO(), ver.String())
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
})
}

func stopEtcd(t *testing.T, ep e2e.EtcdProcess) {
if err := ep.Stop(); err != nil {
t.Fatal(err)
}
err := ep.Stop()
require.NoError(t, err)
}

func validateVersion(t *testing.T, cfg *e2e.EtcdProcessClusterConfig, member e2e.EtcdProcess, expect version.Versions) {
Expand Down Expand Up @@ -260,14 +257,10 @@ func leader(t *testing.T, epc *e2e.EtcdProcessCluster) e2e.EtcdProcess {
Endpoints: endpoints,
DialTimeout: 3 * time.Second,
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer cli.Close()
resp, err := cli.Status(ctx, endpoints[0])
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if resp.Header.GetMemberId() == resp.Leader {
return epc.Procs[i]
}
Expand Down
17 changes: 5 additions & 12 deletions tests/e2e/corrupt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,12 @@ func corruptTest(cx ctlCtx) {
cx.t.Log("connecting clientv3...")
eps := cx.epc.EndpointsGRPC()
cli1, err := clientv3.New(clientv3.Config{Endpoints: []string{eps[1]}, DialTimeout: 3 * time.Second})
if err != nil {
cx.t.Fatal(err)
}
require.NoError(cx.t, err)
defer cli1.Close()

sresp, err := cli1.Status(context.TODO(), eps[0])
cx.t.Logf("checked status sresp:%v err:%v", sresp, err)
if err != nil {
cx.t.Fatal(err)
}
require.NoError(cx.t, err)
id0 := sresp.Header.GetMemberId()

cx.t.Log("stopping etcd[0]...")
Expand All @@ -86,16 +82,13 @@ func corruptTest(cx ctlCtx) {
// corrupting first member by modifying backend offline.
fp := datadir.ToBackendFileName(cx.epc.Procs[0].Config().DataDirPath)
cx.t.Logf("corrupting backend: %v", fp)
if err = cx.corruptFunc(fp); err != nil {
cx.t.Fatal(err)
}
err = cx.corruptFunc(fp)
require.NoError(cx.t, err)

cx.t.Log("restarting etcd[0]")
ep := cx.epc.Procs[0]
proc, err := e2e.SpawnCmd(append([]string{ep.Config().ExecPath}, ep.Config().Args...), cx.envMap)
if err != nil {
cx.t.Fatal(err)
}
require.NoError(cx.t, err)
defer proc.Stop()

cx.t.Log("waiting for etcd[0] failure...")
Expand Down
5 changes: 2 additions & 3 deletions tests/e2e/ctl_v3_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.etcd.io/etcd/pkg/v3/expect"
"go.etcd.io/etcd/tests/v3/framework/config"
Expand Down Expand Up @@ -166,9 +167,7 @@ func assertAuthority(t *testing.T, expectAuthorityPattern string, clus *e2e.Etcd
line = strings.TrimSuffix(line, "\r")

u, err := url.Parse(clus.Procs[i].EndpointsGRPC()[0])
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
expectAuthority := strings.ReplaceAll(expectAuthorityPattern, "${MEMBER_PORT}", u.Port())
expectLine := fmt.Sprintf(`http2: decoded hpack field header field ":authority" = %q`, expectAuthority)
assert.True(t, strings.HasSuffix(line, expectLine), fmt.Sprintf("Got %q expected suffix %q", line, expectLine))
Expand Down
8 changes: 2 additions & 6 deletions tests/e2e/ctl_v3_move_leader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig, envVars
}
var err error
tcfg, err = tinfo.ClientConfig()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
}

var leadIdx int
Expand All @@ -85,9 +83,7 @@ func testCtlV3MoveLeader(t *testing.T, cfg e2e.EtcdProcessClusterConfig, envVars
DialTimeout: 3 * time.Second,
TLS: tcfg,
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
resp, err := cli.Status(ctx, ep)
if err != nil {
Expand Down
24 changes: 6 additions & 18 deletions tests/e2e/etcd_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ func TestEtcdMultiPeer(t *testing.T) {
"--initial-cluster", ic,
}
p, err := e2e.SpawnCmd(args, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
procs[i] = p
}

Expand Down Expand Up @@ -234,9 +232,7 @@ func TestEtcdPeerCNAuth(t *testing.T) {
commonArgs = append(commonArgs, args...)

p, err := e2e.SpawnCmd(commonArgs, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
procs[i] = p
}

Expand Down Expand Up @@ -324,9 +320,7 @@ func TestEtcdPeerMultiCNAuth(t *testing.T) {

commonArgs = append(commonArgs, args...)
p, err := e2e.SpawnCmd(commonArgs, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
procs[i] = p
}

Expand Down Expand Up @@ -400,9 +394,7 @@ func TestEtcdPeerNameAuth(t *testing.T) {
commonArgs = append(commonArgs, args...)

p, err := e2e.SpawnCmd(commonArgs, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
procs[i] = p
}

Expand Down Expand Up @@ -509,9 +501,7 @@ func TestEtcdPeerLocalAddr(t *testing.T) {
commonArgs = append(commonArgs, args...)

p, err := e2e.SpawnCmd(commonArgs, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
procs[i] = p
}

Expand Down Expand Up @@ -594,9 +584,7 @@ func TestGrpcproxyAndListenCipherSuite(t *testing.T) {
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
pw, err := e2e.SpawnCmd(test.args, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if err = pw.Stop(); err != nil {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit 3840876

Please sign in to comment.