Skip to content

Commit

Permalink
fix: use testify instead of t.Fatal in tests/common package (part 2)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Nov 9, 2024
1 parent 0f9c128 commit 9274dc6
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 186 deletions.
2 changes: 1 addition & 1 deletion tests/common/alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestAlarmlistOnMemberRestart(t *testing.T) {
testutils.ExecuteUntil(ctx, t, func() {
for i := 0; i < 6; i++ {
_, err := cc.AlarmList(ctx)
require.NoErrorf(t, err, "Unexpected error")
require.NoError(t, err)
}

clus.Members()[0].Stop()
Expand Down
68 changes: 34 additions & 34 deletions tests/common/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func TestAuthDisable(t *testing.T) {
// confirm put succeeded
resp, err := cc.Get(ctx, "hoo", config.GetOptions{})
require.NoError(t, err)
if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "hoo" || string(resp.Kvs[0].Value) != "bar" {
t.Fatalf("want key value pair 'hoo', 'bar' but got %+v", resp.Kvs)
}
require.Lenf(t, resp.Kvs, 1, "want key value pair 'hoo', 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "hoo", string(resp.Kvs[0].Key), "want key value pair 'hoo', 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "bar", string(resp.Kvs[0].Value), "want key value pair 'hoo', 'bar' but got %+v", resp.Kvs)
})
}

Expand Down Expand Up @@ -171,9 +171,9 @@ func TestAuthRoleUpdate(t *testing.T) {
// confirm put succeeded
resp, err := testUserAuthClient.Get(ctx, "hoo", config.GetOptions{})
require.NoError(t, err)
if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "hoo" || string(resp.Kvs[0].Value) != "bar" {
t.Fatalf("want key value pair 'hoo' 'bar' but got %+v", resp.Kvs)
}
require.Lenf(t, resp.Kvs, 1, "want key value pair 'hoo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "hoo", string(resp.Kvs[0].Key), "want key value pair 'hoo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "bar", string(resp.Kvs[0].Value), "want key value pair 'hoo' 'bar' but got %+v", resp.Kvs)
// revoke the newly granted key
_, err = rootAuthClient.RoleRevokePermission(ctx, testRoleName, "hoo", "")
require.NoError(t, err)
Expand All @@ -182,9 +182,10 @@ func TestAuthRoleUpdate(t *testing.T) {
// confirm a key still granted can be accessed
resp, err = testUserAuthClient.Get(ctx, "foo", config.GetOptions{})
require.NoError(t, err)
if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "foo" || string(resp.Kvs[0].Value) != "bar" {
t.Fatalf("want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
}

require.Lenf(t, resp.Kvs, 1, "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "foo", string(resp.Kvs[0].Key), "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "bar", string(resp.Kvs[0].Value), "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
})
}

Expand All @@ -207,9 +208,9 @@ func TestAuthUserDeleteDuringOps(t *testing.T) {
// confirm put succeeded
resp, err := testUserAuthClient.Get(ctx, "foo", config.GetOptions{})
require.NoError(t, err)
if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "foo" || string(resp.Kvs[0].Value) != "bar" {
t.Fatalf("want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
}
require.Lenf(t, resp.Kvs, 1, "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "foo", string(resp.Kvs[0].Key), "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "bar", string(resp.Kvs[0].Value), "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
// delete the user
_, err = rootAuthClient.UserDelete(ctx, testUserName)
require.NoError(t, err)
Expand Down Expand Up @@ -238,9 +239,9 @@ func TestAuthRoleRevokeDuringOps(t *testing.T) {
// confirm put succeeded
resp, err := testUserAuthClient.Get(ctx, "foo", config.GetOptions{})
require.NoError(t, err)
if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "foo" || string(resp.Kvs[0].Value) != "bar" {
t.Fatalf("want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
}
require.Lenf(t, resp.Kvs, 1, "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "foo", string(resp.Kvs[0].Key), "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "bar", string(resp.Kvs[0].Value), "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
// create a new role
_, err = rootAuthClient.RoleAdd(ctx, "test-role2")
require.NoError(t, err)
Expand All @@ -256,9 +257,9 @@ func TestAuthRoleRevokeDuringOps(t *testing.T) {
// confirm put succeeded
resp, err = testUserAuthClient.Get(ctx, "hoo", config.GetOptions{})
require.NoError(t, err)
if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "hoo" || string(resp.Kvs[0].Value) != "bar" {
t.Fatalf("want key value pair 'hoo' 'bar' but got %+v", resp.Kvs)
}
require.Lenf(t, resp.Kvs, 1, "want key value pair 'hoo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "hoo", string(resp.Kvs[0].Key), "want key value pair 'hoo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "bar", string(resp.Kvs[0].Value), "want key value pair 'hoo' 'bar' but got %+v", resp.Kvs)
// revoke a role from the user
_, err = rootAuthClient.UserRevokeRole(ctx, testUserName, testRoleName)
require.NoError(t, err)
Expand All @@ -270,9 +271,9 @@ func TestAuthRoleRevokeDuringOps(t *testing.T) {
// confirm put succeeded
resp, err = testUserAuthClient.Get(ctx, "hoo", config.GetOptions{})
require.NoError(t, err)
if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "hoo" || string(resp.Kvs[0].Value) != "bar2" {
t.Fatalf("want key value pair 'hoo' 'bar2' but got %+v", resp.Kvs)
}
require.Lenf(t, resp.Kvs, 1, "want key value pair 'hoo' 'bar2' but got %+v", resp.Kvs)
require.Equalf(t, "hoo", string(resp.Kvs[0].Key), "want key value pair 'hoo' 'bar2' but got %+v", resp.Kvs)
require.Equalf(t, "bar2", string(resp.Kvs[0].Value), "want key value pair 'hoo' 'bar2' but got %+v", resp.Kvs)
})
}

Expand All @@ -294,9 +295,9 @@ func TestAuthWriteKey(t *testing.T) {
require.NoError(t, rootAuthClient.Put(ctx, "foo", "bar", config.PutOptions{}))
resp, err := rootAuthClient.Get(ctx, "foo", config.GetOptions{})
require.NoError(t, err)
if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "foo" || string(resp.Kvs[0].Value) != "bar" {
t.Fatalf("want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
}
require.Lenf(t, resp.Kvs, 1, "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "foo", string(resp.Kvs[0].Key), "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
require.Equalf(t, "bar", string(resp.Kvs[0].Value), "want key value pair 'foo' 'bar' but got %+v", resp.Kvs)
// try invalid user
_, err = clus.Client(WithAuth("a", "b"))
require.ErrorContains(t, err, AuthenticationFailed)
Expand All @@ -306,9 +307,9 @@ func TestAuthWriteKey(t *testing.T) {
// confirm put succeeded
resp, err = testUserAuthClient.Get(ctx, "foo", config.GetOptions{})
require.NoError(t, err)
if len(resp.Kvs) != 1 || string(resp.Kvs[0].Key) != "foo" || string(resp.Kvs[0].Value) != "bar2" {
t.Fatalf("want key value pair 'foo' 'bar2' but got %+v", resp.Kvs)
}
require.Lenf(t, resp.Kvs, 1, "want key value pair 'foo' 'bar2' but got %+v", resp.Kvs)
require.Equalf(t, "foo", string(resp.Kvs[0].Key), "want key value pair 'foo' 'bar2' but got %+v", resp.Kvs)
require.Equalf(t, "bar2", string(resp.Kvs[0].Value), "want key value pair 'foo' 'bar2' but got %+v", resp.Kvs)

// try bad password
_, err = clus.Client(WithAuth(testUserName, "badpass"))
Expand Down Expand Up @@ -401,7 +402,7 @@ func TestAuthTxn(t *testing.T) {
Interactive: true,
})
if req.expectError {
require.Contains(t, err.Error(), req.expectResults[0])
require.ErrorContains(t, err, req.expectResults[0])
} else {
require.NoError(t, err)
require.Equal(t, req.expectResults, getRespValues(resp))
Expand Down Expand Up @@ -466,9 +467,9 @@ func TestAuthLeaseKeepAlive(t *testing.T) {

gresp, err := rootAuthClient.Get(ctx, "key", config.GetOptions{})
require.NoError(t, err)
if len(gresp.Kvs) != 1 || string(gresp.Kvs[0].Key) != "key" || string(gresp.Kvs[0].Value) != "value" {
t.Fatalf("want kv pair ('key', 'value') but got %v", gresp.Kvs)
}
require.Lenf(t, gresp.Kvs, 1, "want kv pair ('key', 'value') but got %v", gresp.Kvs)
require.Equalf(t, "key", string(gresp.Kvs[0].Key), "want kv pair ('key', 'value') but got %v", gresp.Kvs)
require.Equalf(t, "value", string(gresp.Kvs[0].Value), "want kv pair ('key', 'value') but got %v", gresp.Kvs)
})
}

Expand Down Expand Up @@ -559,9 +560,8 @@ func TestAuthLeaseGrantLeases(t *testing.T) {
leaseID := resp.ID
lresp, err := rootAuthClient.Leases(ctx)
require.NoError(t, err)
if len(lresp.Leases) != 1 || lresp.Leases[0].ID != leaseID {
t.Fatalf("want %v leaseID but got %v leases", leaseID, lresp.Leases)
}
require.Lenf(t, lresp.Leases, 1, "want %v leaseID but got %v leases", leaseID, lresp.Leases)
require.Equalf(t, lresp.Leases[0].ID, leaseID, "want %v leaseID but got %v leases", leaseID, lresp.Leases)
})
})
}
Expand Down
17 changes: 2 additions & 15 deletions tests/common/compact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package common

import (
"context"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -65,22 +64,10 @@ func TestCompact(t *testing.T) {
require.NoErrorf(t, err, "compactTest: Compact error")

_, err = cc.Get(ctx, "key", config.GetOptions{Revision: 3})
if err != nil {
if !strings.Contains(err.Error(), "required revision has been compacted") {
t.Fatalf("compactTest: Get compact key error (%v)", err)
}
} else {
t.Fatalf("expected '...has been compacted' error, got <nil>")
}
require.ErrorContainsf(t, err, "required revision has been compacted", "compactTest: Get compact key error (%v)", err)

_, err = cc.Compact(ctx, 2, tc.options)
if err != nil {
if !strings.Contains(err.Error(), "required revision has been compacted") {
t.Fatal(err)
}
} else {
t.Fatalf("expected '...has been compacted' error, got <nil>")
}
require.ErrorContains(t, err, "required revision has been compacted")
})
})
}
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 (%v)", i, err)
}
_, 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 (%v)", err)

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 (%v)", err)
})
}
14 changes: 5 additions & 9 deletions tests/common/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func TestEndpointStatus(t *testing.T) {
cc := testutils.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
_, err := cc.Status(ctx)
if err != nil {
t.Fatalf("get endpoint status error: %v", err)
}
require.NoErrorf(t, err, "get endpoint status error: %v", err)
})
}

Expand All @@ -53,9 +51,8 @@ func TestEndpointHashKV(t *testing.T) {
for i := 0; i < 10; i++ {
key := fmt.Sprintf("key-%d", i)
value := fmt.Sprintf("value-%d", i)
if err := cc.Put(ctx, key, value, config.PutOptions{}); err != nil {
t.Fatalf("count not put key %q, err: %s", key, err)
}
err := cc.Put(ctx, key, value, config.PutOptions{})
require.NoErrorf(t, err, "count not put key %q, err: %s", key, err)
}

t.Log("Check all members' Hash and HashRevision")
Expand All @@ -82,8 +79,7 @@ func TestEndpointHealth(t *testing.T) {
defer clus.Close()
cc := testutils.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
if err := cc.Health(ctx); err != nil {
t.Fatalf("get endpoint health error: %v", err)
}
err := cc.Health(ctx)
require.NoErrorf(t, err, "get endpoint health error: %v", err)
})
}
34 changes: 10 additions & 24 deletions tests/common/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,13 @@ func TestKVPut(t *testing.T) {
testutils.ExecuteUntil(ctx, t, func() {
key, value := "foo", "bar"

if err := cc.Put(ctx, key, value, config.PutOptions{}); err != nil {
t.Fatalf("count not put key %q, err: %s", key, err)
}
err := cc.Put(ctx, key, value, config.PutOptions{})
require.NoErrorf(t, err, "count not put key %q, err: %s", key, err)
resp, err := cc.Get(ctx, key, config.GetOptions{})
if err != nil {
t.Fatalf("count not get key %q, err: %s", key, err)
}
if len(resp.Kvs) != 1 {
t.Errorf("Unexpected length of response, got %d", len(resp.Kvs))
}
if string(resp.Kvs[0].Key) != key {
t.Errorf("Unexpected key, want %q, got %q", key, resp.Kvs[0].Key)
}
if string(resp.Kvs[0].Value) != value {
t.Errorf("Unexpected value, want %q, got %q", value, resp.Kvs[0].Value)
}
require.NoErrorf(t, err, "count not get key %q, err: %s", key, err)
assert.Lenf(t, resp.Kvs, 1, "Unexpected length of response, got %d", len(resp.Kvs))
assert.Equalf(t, string(resp.Kvs[0].Key), key, "Unexpected key, want %q, got %q", key, resp.Kvs[0].Key)
assert.Equalf(t, string(resp.Kvs[0].Value), value, "Unexpected value, want %q, got %q", value, resp.Kvs[0].Value)
})
})
}
Expand All @@ -80,9 +71,8 @@ func TestKVGet(t *testing.T) {
)

for i := range kvs {
if err := cc.Put(ctx, kvs[i], "bar", config.PutOptions{}); err != nil {
t.Fatalf("count not put key %q, err: %s", kvs[i], err)
}
err := cc.Put(ctx, kvs[i], "bar", config.PutOptions{})
require.NoErrorf(t, err, "count not put key %q, err: %s", kvs[i], err)
}
tests := []struct {
begin string
Expand Down Expand Up @@ -110,9 +100,7 @@ func TestKVGet(t *testing.T) {
}
for _, tt := range tests {
resp, err := cc.Get(ctx, tt.begin, tt.options)
if err != nil {
t.Fatalf("count not get key %q, err: %s", tt.begin, err)
}
require.NoErrorf(t, err, "count not get key %q, err: %s", tt.begin, err)
kvs := testutils.KeysFromGetResponse(resp)
assert.Equal(t, tt.wkv, kvs)
}
Expand Down Expand Up @@ -229,9 +217,7 @@ func TestKVGetNoQuorum(t *testing.T) {
key := "foo"
_, err := cc.Get(ctx, key, tc.options)
gotError := err != nil
if gotError != tc.wantError {
t.Fatalf("Unexpected result, wantError: %v, gotErr: %v, err: %s", tc.wantError, gotError, err)
}
require.Equalf(t, tc.wantError, gotError, "Unexpected result, wantError: %v, gotErr: %v, err: %s", tc.wantError, gotError, err)
})
})
}
Expand Down
32 changes: 8 additions & 24 deletions tests/common/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ func TestMemberList(t *testing.T) {
require.NoErrorf(t, err, "could not get member list")
expectNum := len(clus.Members())
gotNum := len(resp.Members)
if expectNum != gotNum {
t.Fatalf("number of members not equal, expect: %d, got: %d", expectNum, gotNum)
}
require.Equalf(t, expectNum, gotNum, "number of members not equal, expect: %d, got: %d", expectNum, gotNum)
assert.Eventually(t, func() bool {
resp, err := cc.MemberList(ctx, false)
if err != nil {
Expand Down Expand Up @@ -138,15 +136,9 @@ func TestMemberAdd(t *testing.T) {
require.ErrorContains(t, err, "etcdserver: unhealthy cluster")
} else {
require.NoErrorf(t, err, "MemberAdd failed")
if addResp.Member == nil {
t.Fatalf("MemberAdd failed, expected: member != nil, got: member == nil")
}
if addResp.Member.ID == 0 {
t.Fatalf("MemberAdd failed, expected: ID != 0, got: ID == 0")
}
if len(addResp.Member.PeerURLs) == 0 {
t.Fatalf("MemberAdd failed, expected: non-empty PeerURLs, got: empty PeerURLs")
}
require.NotNilf(t, addResp.Member, "MemberAdd failed, expected: member != nil, got: member == nil")
require.NotEqualf(t, 0, addResp.Member.ID, "MemberAdd failed, expected: ID != 0, got: ID == 0")
require.NotEmptyf(t, addResp.Member.PeerURLs, "MemberAdd failed, expected: non-empty PeerURLs, got: empty PeerURLs")
}
})
})
Expand Down Expand Up @@ -224,16 +216,10 @@ func TestMemberRemove(t *testing.T) {

require.NoErrorf(t, err, "MemberRemove failed")
t.Logf("removeResp.Members:%v", removeResp.Members)
if removeResp.Header.ClusterId != clusterID {
t.Fatalf("MemberRemove failed, expected ClusterID: %d, got: %d", clusterID, removeResp.Header.ClusterId)
}
if len(removeResp.Members) != c.ClusterSize-1 {
t.Fatalf("MemberRemove failed, expected length of members: %d, got: %d", c.ClusterSize-1, len(removeResp.Members))
}
require.Equalf(t, removeResp.Header.ClusterId, clusterID, "MemberRemove failed, expected ClusterID: %d, got: %d", clusterID, removeResp.Header.ClusterId)
require.Equalf(t, len(removeResp.Members), c.ClusterSize-1, "MemberRemove failed, expected length of members: %d, got: %d", c.ClusterSize-1, len(removeResp.Members))
for _, m := range removeResp.Members {
if m.ID == memberID {
t.Fatalf("MemberRemove failed, member(id=%d) is still in cluster", memberID)
}
require.NotEqualf(t, m.ID, memberID, "MemberRemove failed, member(id=%d) is still in cluster", memberID)
}
})
})
Expand Down Expand Up @@ -264,9 +250,7 @@ func memberToRemove(ctx context.Context, t *testing.T, client intf.Client, clust
break
}
}
if memberID == 0 {
t.Fatalf("memberToRemove failed. listResp:%v, statusResp:%v", listResp, statusResp)
}
require.NotEqualf(t, 0, memberID, "memberToRemove failed. listResp:%v, statusResp:%v", listResp, statusResp)
}
return memberID, clusterID
}
Expand Down
Loading

0 comments on commit 9274dc6

Please sign in to comment.