Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use require instead of t.Fatal(err) in tests/common package #18823

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 13 additions & 30 deletions tests/common/alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,29 @@ 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())
for {
if err := cc.Put(ctx, "2nd_test", buf, config.PutOptions{}); err != nil {
if !strings.Contains(err.Error(), "etcdserver: mvcc: database space exceeded") {
t.Fatal(err)
}
require.ErrorContains(t, err, "etcdserver: mvcc: database space exceeded")
break
}
}

// 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 {
if !strings.Contains(err.Error(), "etcdserver: mvcc: database space exceeded") {
t.Fatal(err)
}
require.ErrorContains(t, err, "etcdserver: mvcc: database space exceeded")
}

// 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 @@ -84,13 +74,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 @@ -99,9 +86,7 @@ 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
Expand All @@ -124,14 +109,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")
})
}
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 @@ -51,22 +52,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
14 changes: 5 additions & 9 deletions tests/common/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

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

clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework/config"
Expand Down Expand Up @@ -179,19 +180,14 @@ func TestKVDelete(t *testing.T) {
}
for _, tt := range tests {
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", kvs[i])
}
del, err := cc.Delete(ctx, tt.deleteKey, tt.options)
if err != nil {
t.Fatalf("count not get key %q, err: %s", tt.deleteKey, err)
}
require.NoErrorf(t, err, "count not get key %q, err", tt.deleteKey)
assert.Equal(t, tt.wantDeleted, int(del.Deleted))
get, err := cc.Get(ctx, "", config.GetOptions{Prefix: true})
if err != nil {
t.Fatalf("count not get key, err: %s", err)
}
require.NoErrorf(t, err, "count not get key")
kvs := testutils.KeysFromGetResponse(get)
assert.Equal(t, tt.wantKeys, kvs)
}
Expand Down
14 changes: 4 additions & 10 deletions tests/common/maintenance_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,12 @@ func testMaintenanceOperationWithAuth(t *testing.T, expectConnectError, expectOp

ccWithAuth, err := clus.Client(opts...)
if expectConnectError {
if err == nil {
t.Fatalf("%s: expected connection error, but got successful response", t.Name())
}
require.Errorf(t, err, "%s: expected connection error, but got successful response", t.Name())
t.Logf("%s: connection error: %v", t.Name(), err)
return
}
if err != nil {
t.Fatalf("%s: unexpected connection error (%v)", t.Name(), err)
require.NoErrorf(t, err, "%s: unexpected connection error", t.Name())
return
}

Expand All @@ -233,15 +231,11 @@ func testMaintenanceOperationWithAuth(t *testing.T, expectConnectError, expectOp
err := f(ctx, ccWithAuth)

if expectOperationError {
if err == nil {
t.Fatalf("%s: expected error, but got successful response", t.Name())
}
require.Errorf(t, err, "%s: expected error, but got successful response", t.Name())
t.Logf("%s: operation error: %v", t.Name(), err)
return
}

if err != nil {
t.Fatalf("%s: unexpected operation error (%v)", t.Name(), err)
}
require.NoErrorf(t, err, "%s: unexpected operation error", t.Name())
})
}
4 changes: 1 addition & 3 deletions tests/common/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ func TestMemberList(t *testing.T) {

testutils.ExecuteUntil(ctx, t, func() {
resp, err := cc.MemberList(ctx, false)
if err != nil {
t.Fatalf("could not get member list, err: %s", err)
}
require.NoErrorf(t, err, "could not get member list")
expectNum := len(clus.Members())
gotNum := len(resp.Members)
if expectNum != gotNum {
Expand Down
54 changes: 15 additions & 39 deletions tests/common/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/framework/config"
Expand All @@ -38,9 +40,7 @@ func TestRoleAdd_Simple(t *testing.T) {

testutils.ExecuteUntil(ctx, t, func() {
_, err := cc.RoleAdd(ctx, "root")
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
})
})
}
Expand All @@ -55,9 +55,7 @@ func TestRoleAdd_Error(t *testing.T) {
cc := testutils.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
_, err := cc.RoleAdd(ctx, "test-role")
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
_, err = cc.RoleAdd(ctx, "test-role")
if err == nil || !strings.Contains(err.Error(), rpctypes.ErrRoleAlreadyExist.Error()) {
t.Fatalf("want (%v) error, but got (%v)", rpctypes.ErrRoleAlreadyExist, err)
Expand All @@ -78,23 +76,15 @@ func TestRootRole(t *testing.T) {
cc := testutils.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
_, err := cc.RoleAdd(ctx, "root")
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
resp, err := cc.RoleGet(ctx, "root")
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
t.Logf("get role resp %+v", resp)
// granting to root should be refused by server and a no-op
_, err = cc.RoleGrantPermission(ctx, "root", "foo", "", clientv3.PermissionType(clientv3.PermReadWrite))
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
resp2, err := cc.RoleGet(ctx, "root")
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
t.Logf("get role resp %+v", resp2)
})
}
Expand All @@ -108,29 +98,19 @@ func TestRoleGrantRevokePermission(t *testing.T) {
cc := testutils.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
_, err := cc.RoleAdd(ctx, "role1")
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
_, err = cc.RoleGrantPermission(ctx, "role1", "bar", "", clientv3.PermissionType(clientv3.PermRead))
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
_, err = cc.RoleGrantPermission(ctx, "role1", "bar", "", clientv3.PermissionType(clientv3.PermWrite))
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
_, err = cc.RoleGrantPermission(ctx, "role1", "bar", "foo", clientv3.PermissionType(clientv3.PermReadWrite))
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
_, err = cc.RoleRevokePermission(ctx, "role1", "foo", "")
if err == nil || !strings.Contains(err.Error(), rpctypes.ErrPermissionNotGranted.Error()) {
t.Fatalf("want error (%v), but got (%v)", rpctypes.ErrPermissionNotGranted, err)
}
_, err = cc.RoleRevokePermission(ctx, "role1", "bar", "foo")
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
})
}

Expand All @@ -143,12 +123,8 @@ func TestRoleDelete(t *testing.T) {
cc := testutils.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
_, err := cc.RoleAdd(ctx, "role1")
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
_, err = cc.RoleDelete(ctx, "role1")
if err != nil {
t.Fatalf("want no error, but got (%v)", err)
}
require.NoErrorf(t, err, "want no error, but got")
})
}
6 changes: 3 additions & 3 deletions tests/common/status_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 @@ -36,9 +38,7 @@ func TestStatus(t *testing.T) {

testutils.ExecuteUntil(ctx, t, func() {
rs, err := cc.Status(ctx)
if err != nil {
t.Fatalf("could not get status, err: %s", err)
}
require.NoErrorf(t, err, "could not get status")
if len(rs) != tc.config.ClusterSize {
t.Fatalf("wrong number of status responses. expected:%d, got:%d ", tc.config.ClusterSize, len(rs))
}
Expand Down
16 changes: 7 additions & 9 deletions tests/common/txn_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"

pb "go.etcd.io/etcd/api/v3/etcdserverpb"
clientv3 "go.etcd.io/etcd/client/v3"
Expand Down Expand Up @@ -64,12 +65,10 @@ func TestTxnSucc(t *testing.T) {
defer clus.Close()
cc := testutils.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
if err := cc.Put(ctx, "key1", "value1", config.PutOptions{}); err != nil {
t.Fatalf("could not create key:%s, value:%s", "key1", "value1")
}
if err := cc.Put(ctx, "key2", "value2", config.PutOptions{}); err != nil {
t.Fatalf("could not create key:%s, value:%s", "key2", "value2")
}
err := cc.Put(ctx, "key1", "value1", config.PutOptions{})
require.NoErrorf(t, err, "could not create key:%s, value:%s", "key1", "value1")
err = cc.Put(ctx, "key2", "value2", config.PutOptions{})
require.NoErrorf(t, err, "could not create key:%s, value:%s", "key2", "value2")
for _, req := range reqs {
resp, err := cc.Txn(ctx, req.compare, req.ifSuccess, req.ifFail, config.TxnOptions{
Interactive: true,
Expand Down Expand Up @@ -108,9 +107,8 @@ func TestTxnFail(t *testing.T) {
defer clus.Close()
cc := testutils.MustClient(clus.Client())
testutils.ExecuteUntil(ctx, t, func() {
if err := cc.Put(ctx, "key1", "value1", config.PutOptions{}); err != nil {
t.Fatalf("could not create key:%s, value:%s", "key1", "value1")
}
err := cc.Put(ctx, "key1", "value1", config.PutOptions{})
require.NoErrorf(t, err, "could not create key:%s, value:%s", "key1", "value1")
for _, req := range reqs {
resp, err := cc.Txn(ctx, req.compare, req.ifSuccess, req.ifFail, config.TxnOptions{
Interactive: true,
Expand Down
Loading