Skip to content

Commit

Permalink
fix: enable nil-compare rule from testifylint
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Oct 8, 2024
1 parent 17fb752 commit cf17090
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 180 deletions.
4 changes: 4 additions & 0 deletions client/pkg/testutil/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@ import (
"github.com/stretchr/testify/assert"
)

// Deprectated: Use github.com/stretchr/testify/assert.Nil instead.
func AssertNil(t *testing.T, v any) {
t.Helper()
assert.Nil(t, v)
}

// Deprectated: Use github.com/stretchr/testify/require.NotNil instead.
func AssertNotNil(t *testing.T, v any) {
t.Helper()
if v == nil {
t.Fatalf("expected non-nil, got %+v", v)
}
}

// Deprectated: Use github.com/stretchr/testify/assert.True instead.
func AssertTrue(t *testing.T, v bool, msg ...string) {
t.Helper()
assert.True(t, v, msg)
}

// Deprectated: Use github.com/stretchr/testify/assert.False instead.
func AssertFalse(t *testing.T, v bool, msg ...string) {
t.Helper()
assert.False(t, v, msg)
Expand Down
2 changes: 1 addition & 1 deletion pkg/ioutil/pagewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestPageWriterPageBytes(t *testing.T) {
}, "expected panic when pageBytes is %d", tc.pageBytes)
} else {
pw := NewPageWriter(cw, tc.pageBytes, 0)
assert.NotEqual(t, pw, nil)
assert.NotNil(t, pw)
}
})
}
Expand Down
42 changes: 21 additions & 21 deletions server/etcdserver/api/v2store/store_ttl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (

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

"go.etcd.io/etcd/client/pkg/v3/testutil"
"go.etcd.io/etcd/server/v3/etcdserver/api/v2error"
)

Expand All @@ -38,7 +38,7 @@ func TestMinExpireTime(t *testing.T) {
s.DeleteExpiredKeys(fc.Now())
var eidx uint64 = 1
e, err := s.Get("/foo", true, false)
testutil.AssertNil(t, err)
assert.Nil(t, err)
assert.Equal(t, e.EtcdIndex, eidx)
assert.Equal(t, e.Action, "get")
assert.Equal(t, e.Node.Key, "/foo")
Expand All @@ -60,7 +60,7 @@ func TestStoreGetDirectory(t *testing.T) {
s.Create("/foo/baz/ttl", false, "Y", false, TTLOptionSet{ExpireTime: fc.Now().Add(time.Second * 3)})
var eidx uint64 = 7
e, err := s.Get("/foo", true, false)
testutil.AssertNil(t, err)
assert.Nil(t, err)
assert.Equal(t, e.EtcdIndex, eidx)
assert.Equal(t, e.Action, "get")
assert.Equal(t, e.Node.Key, "/foo")
Expand Down Expand Up @@ -103,14 +103,14 @@ func TestStoreUpdateValueTTL(t *testing.T) {
var eidx uint64 = 2
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
_, err := s.Update("/foo", "baz", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
testutil.AssertNil(t, err)
assert.Nil(t, err)
e, _ := s.Get("/foo", false, false)
assert.Equal(t, *e.Node.Value, "baz")
assert.Equal(t, e.EtcdIndex, eidx)
fc.Advance(600 * time.Millisecond)
s.DeleteExpiredKeys(fc.Now())
e, err = s.Get("/foo", false, false)
testutil.AssertNil(t, e)
assert.Nil(t, e)
assert.Equal(t, err.(*v2error.Error).ErrorCode, v2error.EcodeKeyNotFound)
}

Expand All @@ -122,11 +122,11 @@ func TestStoreUpdateDirTTL(t *testing.T) {

var eidx uint64 = 3
_, err := s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
testutil.AssertNil(t, err)
assert.Nil(t, err)
_, err = s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
testutil.AssertNil(t, err)
assert.Nil(t, err)
e, err := s.Update("/foo/bar", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
testutil.AssertNil(t, err)
assert.Nil(t, err)
assert.False(t, e.Node.Dir)
assert.Equal(t, e.EtcdIndex, eidx)
e, _ = s.Get("/foo/bar", false, false)
Expand All @@ -136,7 +136,7 @@ func TestStoreUpdateDirTTL(t *testing.T) {
fc.Advance(600 * time.Millisecond)
s.DeleteExpiredKeys(fc.Now())
e, err = s.Get("/foo/bar", false, false)
testutil.AssertNil(t, e)
assert.Nil(t, e)
assert.Equal(t, err.(*v2error.Error).ErrorCode, v2error.EcodeKeyNotFound)
}

Expand All @@ -155,7 +155,7 @@ func TestStoreWatchExpire(t *testing.T) {
assert.Equal(t, w.StartIndex(), eidx)
c := w.EventChan()
e := nbselect(c)
testutil.AssertNil(t, e)
assert.Nil(t, e)
fc.Advance(600 * time.Millisecond)
s.DeleteExpiredKeys(fc.Now())
eidx = 4
Expand Down Expand Up @@ -193,7 +193,7 @@ func TestStoreWatchExpireRefresh(t *testing.T) {
assert.Equal(t, w.StartIndex(), eidx)
c := w.EventChan()
e := nbselect(c)
testutil.AssertNil(t, e)
assert.Nil(t, e)
fc.Advance(600 * time.Millisecond)
s.DeleteExpiredKeys(fc.Now())
eidx = 3
Expand Down Expand Up @@ -275,16 +275,16 @@ func TestStoreRefresh(t *testing.T) {
s.Create("/bar", true, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
s.Create("/bar/z", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
_, err := s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
testutil.AssertNil(t, err)
assert.Nil(t, err)

_, err = s.Set("/foo", false, "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
testutil.AssertNil(t, err)
assert.Nil(t, err)

_, err = s.Update("/bar/z", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
testutil.AssertNil(t, err)
assert.Nil(t, err)

_, err = s.CompareAndSwap("/foo", "bar", 0, "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
testutil.AssertNil(t, err)
assert.Nil(t, err)
}

// TestStoreRecoverWithExpiration ensures that the store can recover from a previously saved state that includes an expiring key.
Expand All @@ -299,7 +299,7 @@ func TestStoreRecoverWithExpiration(t *testing.T) {
s.Create("/foo/x", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
s.Create("/foo/y", false, "baz", false, TTLOptionSet{ExpireTime: fc.Now().Add(5 * time.Millisecond)})
b, err := s.Save()
testutil.AssertNil(t, err)
assert.Nil(t, err)

time.Sleep(10 * time.Millisecond)

Expand All @@ -312,13 +312,13 @@ func TestStoreRecoverWithExpiration(t *testing.T) {
s.DeleteExpiredKeys(fc.Now())

e, err := s.Get("/foo/x", false, false)
testutil.AssertNil(t, err)
assert.Nil(t, err)
assert.Equal(t, e.EtcdIndex, eidx)
assert.Equal(t, *e.Node.Value, "bar")

e, err = s.Get("/foo/y", false, false)
testutil.AssertNotNil(t, err)
testutil.AssertNil(t, e)
require.NotNil(t, err)
assert.Nil(t, e)
}

// TestStoreWatchExpireWithHiddenKey ensures that the store doesn't see expirations of hidden keys.
Expand All @@ -333,11 +333,11 @@ func TestStoreWatchExpireWithHiddenKey(t *testing.T) {
w, _ := s.Watch("/", true, false, 0)
c := w.EventChan()
e := nbselect(c)
testutil.AssertNil(t, e)
assert.Nil(t, e)
fc.Advance(600 * time.Millisecond)
s.DeleteExpiredKeys(fc.Now())
e = nbselect(c)
testutil.AssertNil(t, e)
assert.Nil(t, e)
fc.Advance(600 * time.Millisecond)
s.DeleteExpiredKeys(fc.Now())
e = nbselect(c)
Expand Down
18 changes: 9 additions & 9 deletions tests/e2e/v3_curl_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"math/rand"
"testing"

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

"go.etcd.io/etcd/api/v3/authpb"
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"go.etcd.io/etcd/client/pkg/v3/testutil"
"go.etcd.io/etcd/pkg/v3/expect"
"go.etcd.io/etcd/tests/v3/framework/e2e"
)
Expand Down Expand Up @@ -66,7 +66,7 @@ func testCurlV3Auth(cx ctlCtx) {
// create users
for i := 0; i < len(usernames); i++ {
user, err := json.Marshal(&pb.AuthUserAddRequest{Name: usernames[i], Password: pwds[i], Options: options[i]})
testutil.AssertNil(cx.t, err)
assert.Nil(cx.t, err)

if err = e2e.CURLPost(cx.epc, e2e.CURLReq{
Endpoint: "/v3/auth/user/add",
Expand All @@ -79,7 +79,7 @@ func testCurlV3Auth(cx ctlCtx) {

// create root role
rolereq, err := json.Marshal(&pb.AuthRoleAddRequest{Name: "root"})
testutil.AssertNil(cx.t, err)
assert.Nil(cx.t, err)

if err = e2e.CURLPost(cx.epc, e2e.CURLReq{
Endpoint: "/v3/auth/role/add",
Expand All @@ -92,7 +92,7 @@ func testCurlV3Auth(cx ctlCtx) {
//grant root role
for i := 0; i < len(usernames); i++ {
grantroleroot, merr := json.Marshal(&pb.AuthUserGrantRoleRequest{User: usernames[i], Role: "root"})
testutil.AssertNil(cx.t, merr)
assert.Nil(cx.t, merr)

if err = e2e.CURLPost(cx.epc, e2e.CURLReq{
Endpoint: "/v3/auth/user/grant",
Expand All @@ -115,7 +115,7 @@ func testCurlV3Auth(cx ctlCtx) {
for i := 0; i < len(usernames); i++ {
// put "bar[i]" into "foo[i]"
putreq, err := json.Marshal(&pb.PutRequest{Key: []byte(fmt.Sprintf("foo%d", i)), Value: []byte(fmt.Sprintf("bar%d", i))})
testutil.AssertNil(cx.t, err)
assert.Nil(cx.t, err)

// fail put no auth
if err = e2e.CURLPost(cx.epc, e2e.CURLReq{
Expand All @@ -128,7 +128,7 @@ func testCurlV3Auth(cx ctlCtx) {

// auth request
authreq, err := json.Marshal(&pb.AuthenticateRequest{Name: usernames[i], Password: pwds[i]})
testutil.AssertNil(cx.t, err)
assert.Nil(cx.t, err)

var (
authHeader string
Expand All @@ -141,14 +141,14 @@ func testCurlV3Auth(cx ctlCtx) {
Value: string(authreq),
})
proc, err := e2e.SpawnCmd(cmdArgs, cx.envMap)
testutil.AssertNil(cx.t, err)
assert.Nil(cx.t, err)
defer proc.Close()

cURLRes, err := proc.ExpectFunc(context.Background(), lineFunc)
testutil.AssertNil(cx.t, err)
assert.Nil(cx.t, err)

authRes := make(map[string]any)
testutil.AssertNil(cx.t, json.Unmarshal([]byte(cURLRes), &authRes))
assert.Nil(cx.t, json.Unmarshal([]byte(cURLRes), &authRes))

token, ok := authRes[rpctypes.TokenFieldNameGRPC].(string)
if !ok {
Expand Down
Loading

0 comments on commit cf17090

Please sign in to comment.