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 b8868c1
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 204 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
20 changes: 3 additions & 17 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 @@ -52,8 +51,7 @@ 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 {
err := cc.Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{})
require.NoErrorf(t, err, "compactTest #%d: put kv error", i)
require.NoErrorf(t, cc.Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{}), "compactTest #%d: put kv error", i)
}
get, err := cc.Get(ctx, "key", config.GetOptions{Revision: 3})
require.NoErrorf(t, err, "compactTest: Get kv by revision error")
Expand All @@ -65,22 +63,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
15 changes: 5 additions & 10 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,10 @@ 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)
}
require.NoErrorf(t, cc.Put(ctx, kvs[i].Key, kvs[i].Val, config.PutOptions{}), "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)
}

if err = cc.Defragment(ctx, options); err != nil {
t.Fatalf("defrag_test: defrag error (%v)", err)
}
require.NoErrorf(t, err, "defrag_test: compact with revision error (%v)", err)
require.NoErrorf(t, cc.Defragment(ctx, options), "defrag_test: defrag error (%v)", err)
})
}
12 changes: 3 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,7 @@ 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)
}
require.NoErrorf(t, cc.Put(ctx, key, value, config.PutOptions{}), "count not put key %q", key)
}

t.Log("Check all members' Hash and HashRevision")
Expand All @@ -82,8 +78,6 @@ 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)
}
require.NoErrorf(t, cc.Health(ctx), "get endpoint health error")
})
}
38 changes: 12 additions & 26 deletions tests/common/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,12 @@ 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)
}
require.NoErrorf(t, cc.Put(ctx, key, value, config.PutOptions{}), "count not put key %q", key)
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 +70,7 @@ 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)
}
require.NoErrorf(t, cc.Put(ctx, kvs[i], "bar", config.PutOptions{}), "count not put key %q", kvs[i])
}
tests := []struct {
begin string
Expand Down Expand Up @@ -110,9 +98,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 @@ -180,8 +166,7 @@ func TestKVDelete(t *testing.T) {
}
for _, tt := range tests {
for i := range kvs {
err := cc.Put(ctx, kvs[i], "bar", config.PutOptions{})
require.NoErrorf(t, err, "count not put key %q", kvs[i])
require.NoErrorf(t, cc.Put(ctx, kvs[i], "bar", config.PutOptions{}), "count not put key %q", kvs[i])
}
del, err := cc.Delete(ctx, tt.deleteKey, tt.options)
require.NoErrorf(t, err, "count not get key %q, err", tt.deleteKey)
Expand Down Expand Up @@ -228,9 +213,10 @@ func TestKVGetNoQuorum(t *testing.T) {
testutils.ExecuteUntil(ctx, t, func() {
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)
if tc.wantError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
})
Expand Down
Loading

0 comments on commit b8868c1

Please sign in to comment.