Skip to content

Commit

Permalink
fix: enable formatter rule from testifylint
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Gosteli <thomas.gosteli@protonmail.ch>
  • Loading branch information
ghouscht committed Oct 16, 2024
1 parent 2e003eb commit 2d144f0
Show file tree
Hide file tree
Showing 43 changed files with 162 additions and 163 deletions.
2 changes: 1 addition & 1 deletion client/internal/v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ func TestHTTPClusterClientResetPinRandom(t *testing.T) {
for i := 0; i < round; i++ {
hc := &httpClusterClient{rand: rand.New(rand.NewSource(int64(i)))}
err := hc.SetEndpoints([]string{"http://127.0.0.1:4001", "http://127.0.0.1:4002", "http://127.0.0.1:4003"})
require.NoErrorf(t, err, "#%d: reset error (%v)", i)
require.NoErrorf(t, err, "#%d: reset error", i)
if hc.endpoints[hc.pinned].String() == "http://127.0.0.1:4001" {
pinNum++
}
Expand Down
2 changes: 1 addition & 1 deletion client/pkg/fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestRemoveMatchFile(t *testing.T) {

func TestTouchDirAll(t *testing.T) {
tmpdir := t.TempDir()
assert.Panics(t, func() {
assert.Panicsf(t, func() {
TouchDirAll(nil, tmpdir)
}, "expected panic with nil log")

Expand Down
4 changes: 2 additions & 2 deletions client/pkg/testutil/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func AssertNotNil(t *testing.T, v any) {
// Deprecated: use github.com/stretchr/testify/assert.True instead.
func AssertTrue(t *testing.T, v bool, msg ...string) {
t.Helper()
assert.True(t, v, msg)
assert.True(t, v, msg) //nolint:testifylint
}

// AssertFalse
// Deprecated: use github.com/stretchr/testify/assert.False instead.
func AssertFalse(t *testing.T, v bool, msg ...string) {
t.Helper()
assert.False(t, v, msg)
assert.False(t, v, msg) //nolint:testifylint
}
2 changes: 1 addition & 1 deletion client/pkg/tlsutil/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestGetVersion(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := GetTLSVersion(tt.version)
if err != nil {
assert.True(t, tt.expectError, "GetTLSVersion() returned error while expecting success: %v", err)
assert.Truef(t, tt.expectError, "GetTLSVersion() returned error while expecting success: %v", err)
return
}
assert.Equal(t, tt.want, got)
Expand Down
2 changes: 1 addition & 1 deletion client/pkg/transport/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ func TestNewListenerTLSInfoSelfCert(t *testing.T) {
}
testNewListenerTLSInfoAccept(t, tlsinfo)

assert.Panics(t, func() {
assert.Panicsf(t, func() {
SelfCert(nil, tmpdir, []string{"127.0.0.1"}, 1)
}, "expected panic with nil log")
}
Expand Down
31 changes: 15 additions & 16 deletions client/v3/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package clientv3
import (
"context"
"errors"
"fmt"
"io"
"net"
"sync"
Expand Down Expand Up @@ -196,50 +195,50 @@ func TestBackoffJitterFraction(t *testing.T) {
}

func TestIsHaltErr(t *testing.T) {
assert.True(t,
assert.Truef(t,
isHaltErr(context.TODO(), errors.New("etcdserver: some etcdserver error")),
"error created by errors.New should be unavailable error",
)
assert.False(t,
assert.Falsef(t,
isHaltErr(context.TODO(), rpctypes.ErrGRPCStopped),
fmt.Sprintf(`error "%v" should not be halt error`, rpctypes.ErrGRPCStopped),
`error "%v" should not be halt error`, rpctypes.ErrGRPCStopped,
)
assert.False(t,
assert.Falsef(t,
isHaltErr(context.TODO(), rpctypes.ErrGRPCNoLeader),
fmt.Sprintf(`error "%v" should not be halt error`, rpctypes.ErrGRPCNoLeader),
`error "%v" should not be halt error`, rpctypes.ErrGRPCNoLeader,
)
ctx, cancel := context.WithCancel(context.TODO())
assert.False(t,
assert.Falsef(t,
isHaltErr(ctx, nil),
"no error and active context should be halt error",
)
cancel()
assert.True(t,
assert.Truef(t,
isHaltErr(ctx, nil),
"cancel on context should be halte error",
"cancel on context should be halt error",
)
}

func TestIsUnavailableErr(t *testing.T) {
assert.False(t,
assert.Falsef(t,
isUnavailableErr(context.TODO(), errors.New("etcdserver: some etcdserver error")),
"error created by errors.New should not be unavailable error",
)
assert.True(t,
assert.Truef(t,
isUnavailableErr(context.TODO(), rpctypes.ErrGRPCStopped),
fmt.Sprintf(`error "%v" should be unavailable error`, rpctypes.ErrGRPCStopped),
`error "%v" should be unavailable error`, rpctypes.ErrGRPCStopped,
)
assert.False(t,
assert.Falsef(t,
isUnavailableErr(context.TODO(), rpctypes.ErrGRPCNotCapable),
fmt.Sprintf("error %v should not be unavailable error", rpctypes.ErrGRPCNotCapable),
"error %v should not be unavailable error", rpctypes.ErrGRPCNotCapable,
)
ctx, cancel := context.WithCancel(context.TODO())
assert.False(t,
assert.Falsef(t,
isUnavailableErr(ctx, nil),
"no error and active context should not be unavailable error",
)
cancel()
assert.False(t,
assert.Falsef(t,
isUnavailableErr(ctx, nil),
"cancel on context should not be unavailable error",
)
Expand Down
8 changes: 4 additions & 4 deletions server/embed/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ func TestConfigFileOtherFields(t *testing.T) {
t.Errorf("PeerTLS = %v, want %v", cfg.PeerTLSInfo, ptls)
}

assert.True(t, cfg.ForceNewCluster, "ForceNewCluster does not match")
assert.Truef(t, cfg.ForceNewCluster, "ForceNewCluster does not match")

assert.True(t, cfg.SocketOpts.ReusePort, "ReusePort does not match")
assert.Truef(t, cfg.SocketOpts.ReusePort, "ReusePort does not match")

assert.False(t, cfg.SocketOpts.ReuseAddress, "ReuseAddress does not match")
assert.Falsef(t, cfg.SocketOpts.ReuseAddress, "ReuseAddress does not match")
}

func TestConfigFileFeatureGates(t *testing.T) {
Expand Down Expand Up @@ -806,7 +806,7 @@ func TestTLSVersionMinMax(t *testing.T) {

err := cfg.Validate()
if err != nil {
assert.True(t, tt.expectError, "Validate() returned error while expecting success: %v", err)
assert.Truef(t, tt.expectError, "Validate() returned error while expecting success: %v", err)
return
}

Expand Down
10 changes: 5 additions & 5 deletions server/etcdserver/api/membership/storev2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ func TestIsMetaStoreOnly(t *testing.T) {

metaOnly, err := IsMetaStoreOnly(s)
assert.NoError(t, err)
assert.True(t, metaOnly, "Just created v2store should be meta-only")
assert.Truef(t, metaOnly, "Just created v2store should be meta-only")

mustSaveClusterVersionToStore(lg, s, semver.New("3.5.17"))
metaOnly, err = IsMetaStoreOnly(s)
assert.NoError(t, err)
assert.True(t, metaOnly, "Just created v2store should be meta-only")
assert.Truef(t, metaOnly, "Just created v2store should be meta-only")

mustSaveMemberToStore(lg, s, &Member{ID: 0x00abcd})
metaOnly, err = IsMetaStoreOnly(s)
assert.NoError(t, err)
assert.True(t, metaOnly, "Just created v2store should be meta-only")
assert.Truef(t, metaOnly, "Just created v2store should be meta-only")

_, err = s.Create("/1/foo", false, "v1", false, v2store.TTLOptionSet{ExpireTime: v2store.Permanent})
assert.NoError(t, err)
metaOnly, err = IsMetaStoreOnly(s)
assert.NoError(t, err)
assert.False(t, metaOnly, "Just created v2store should be meta-only")
assert.Falsef(t, metaOnly, "Just created v2store should be meta-only")

_, err = s.Delete("/1/foo", false, false)
assert.NoError(t, err)
assert.NoError(t, err)
assert.False(t, metaOnly, "Just created v2store should be meta-only")
assert.Falsef(t, metaOnly, "Just created v2store should be meta-only")
}
24 changes: 12 additions & 12 deletions server/etcdserver/api/v2store/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,76 +26,76 @@ func TestStoreStatsGetSuccess(t *testing.T) {
s := newStore()
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
s.Get("/foo", false, false)
assert.Equal(t, uint64(1), s.Stats.GetSuccess, "")
assert.Equal(t, uint64(1), s.Stats.GetSuccess)
}

// TestStoreStatsGetFail ensures that a failed Get is recorded in the stats.
func TestStoreStatsGetFail(t *testing.T) {
s := newStore()
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
s.Get("/no_such_key", false, false)
assert.Equal(t, uint64(1), s.Stats.GetFail, "")
assert.Equal(t, uint64(1), s.Stats.GetFail)
}

// TestStoreStatsCreateSuccess ensures that a successful Create is recorded in the stats.
func TestStoreStatsCreateSuccess(t *testing.T) {
s := newStore()
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
assert.Equal(t, uint64(1), s.Stats.CreateSuccess, "")
assert.Equal(t, uint64(1), s.Stats.CreateSuccess)
}

// TestStoreStatsCreateFail ensures that a failed Create is recorded in the stats.
func TestStoreStatsCreateFail(t *testing.T) {
s := newStore()
s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
assert.Equal(t, uint64(1), s.Stats.CreateFail, "")
assert.Equal(t, uint64(1), s.Stats.CreateFail)
}

// TestStoreStatsUpdateSuccess ensures that a successful Update is recorded in the stats.
func TestStoreStatsUpdateSuccess(t *testing.T) {
s := newStore()
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
s.Update("/foo", "baz", TTLOptionSet{ExpireTime: Permanent})
assert.Equal(t, uint64(1), s.Stats.UpdateSuccess, "")
assert.Equal(t, uint64(1), s.Stats.UpdateSuccess)
}

// TestStoreStatsUpdateFail ensures that a failed Update is recorded in the stats.
func TestStoreStatsUpdateFail(t *testing.T) {
s := newStore()
s.Update("/foo", "bar", TTLOptionSet{ExpireTime: Permanent})
assert.Equal(t, uint64(1), s.Stats.UpdateFail, "")
assert.Equal(t, uint64(1), s.Stats.UpdateFail)
}

// TestStoreStatsCompareAndSwapSuccess ensures that a successful CAS is recorded in the stats.
func TestStoreStatsCompareAndSwapSuccess(t *testing.T) {
s := newStore()
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
s.CompareAndSwap("/foo", "bar", 0, "baz", TTLOptionSet{ExpireTime: Permanent})
assert.Equal(t, uint64(1), s.Stats.CompareAndSwapSuccess, "")
assert.Equal(t, uint64(1), s.Stats.CompareAndSwapSuccess)
}

// TestStoreStatsCompareAndSwapFail ensures that a failed CAS is recorded in the stats.
func TestStoreStatsCompareAndSwapFail(t *testing.T) {
s := newStore()
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
s.CompareAndSwap("/foo", "wrong_value", 0, "baz", TTLOptionSet{ExpireTime: Permanent})
assert.Equal(t, uint64(1), s.Stats.CompareAndSwapFail, "")
assert.Equal(t, uint64(1), s.Stats.CompareAndSwapFail)
}

// TestStoreStatsDeleteSuccess ensures that a successful Delete is recorded in the stats.
func TestStoreStatsDeleteSuccess(t *testing.T) {
s := newStore()
s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
s.Delete("/foo", false, false)
assert.Equal(t, uint64(1), s.Stats.DeleteSuccess, "")
assert.Equal(t, uint64(1), s.Stats.DeleteSuccess)
}

// TestStoreStatsDeleteFail ensures that a failed Delete is recorded in the stats.
func TestStoreStatsDeleteFail(t *testing.T) {
s := newStore()
s.Delete("/foo", false, false)
assert.Equal(t, uint64(1), s.Stats.DeleteFail, "")
assert.Equal(t, uint64(1), s.Stats.DeleteFail)
}

// TestStoreStatsExpireCount ensures that the number of expirations is recorded in the stats.
Expand All @@ -105,8 +105,8 @@ func TestStoreStatsExpireCount(t *testing.T) {
s.clock = fc

s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
assert.Equal(t, uint64(0), s.Stats.ExpireCount, "")
assert.Equal(t, uint64(0), s.Stats.ExpireCount)
fc.Advance(600 * time.Millisecond)
s.DeleteExpiredKeys(fc.Now())
assert.Equal(t, uint64(1), s.Stats.ExpireCount, "")
assert.Equal(t, uint64(1), s.Stats.ExpireCount)
}
2 changes: 1 addition & 1 deletion server/etcdserver/api/v2store/store_ttl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestMinExpireTime(t *testing.T) {
fc := clockwork.NewFakeClockAt(time.Date(1984, time.April, 4, 0, 0, 0, 0, time.UTC))
s.clock = fc
// FakeClock starts at 0, so minExpireTime should be far in the future.. but just in case
assert.True(t, minExpireTime.After(fc.Now()), "minExpireTime should be ahead of FakeClock!")
assert.Truef(t, minExpireTime.After(fc.Now()), "minExpireTime should be ahead of FakeClock!")
s.Create("/foo", false, "Y", false, TTLOptionSet{ExpireTime: fc.Now().Add(3 * time.Second)})
fc.Advance(5 * time.Second)
// Ensure it hasn't expired
Expand Down
25 changes: 12 additions & 13 deletions server/etcdserver/apply/apply_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,7 @@ func TestAuthApplierV3_AdminPermission(t *testing.T) {
tc.request.Header = &pb.RequestHeader{Username: userReadOnly}
}
result := authApplier.Apply(tc.request, dummyApplyFunc)
require.Equal(t, errors.Is(result.Err, auth.ErrPermissionDenied), tc.adminPermissionNeeded,
"Admin permission needed: got %v, expect: %v", errors.Is(result.Err, auth.ErrPermissionDenied), tc.adminPermissionNeeded)
require.Equal(t, errors.Is(result.Err, auth.ErrPermissionDenied), tc.adminPermissionNeeded, "Admin permission needed")
})
}
}
Expand Down Expand Up @@ -783,28 +782,28 @@ func TestAuthApplierV3_RoleGet(t *testing.T) {

func TestCheckLeasePutsKeys(t *testing.T) {
aa := defaultAuthApplierV3(t)
assert.NoError(t, aa.checkLeasePutsKeys(lease.NewLease(lease.LeaseID(1), 3600)), "auth is disabled, should allow puts")
assert.NoErrorf(t, aa.checkLeasePutsKeys(lease.NewLease(lease.LeaseID(1), 3600)), "auth is disabled, should allow puts")
mustCreateRolesAndEnableAuth(t, aa)
aa.authInfo = auth.AuthInfo{Username: "root"}
assert.NoError(t, aa.checkLeasePutsKeys(lease.NewLease(lease.LeaseID(1), 3600)), "auth is enabled, should allow puts for root")
assert.NoErrorf(t, aa.checkLeasePutsKeys(lease.NewLease(lease.LeaseID(1), 3600)), "auth is enabled, should allow puts for root")

l := lease.NewLease(lease.LeaseID(1), 3600)
l.SetLeaseItem(lease.LeaseItem{Key: "a"})
aa.authInfo = auth.AuthInfo{Username: "bob", Revision: 0}
assert.ErrorIs(t, aa.checkLeasePutsKeys(l), auth.ErrUserEmpty, "auth is enabled, should not allow bob, non existing at rev 0")
assert.ErrorIsf(t, aa.checkLeasePutsKeys(l), auth.ErrUserEmpty, "auth is enabled, should not allow bob, non existing at rev 0")
aa.authInfo = auth.AuthInfo{Username: "bob", Revision: 1}
assert.ErrorIs(t, aa.checkLeasePutsKeys(l), auth.ErrAuthOldRevision, "auth is enabled, old revision")
assert.ErrorIsf(t, aa.checkLeasePutsKeys(l), auth.ErrAuthOldRevision, "auth is enabled, old revision")

aa.authInfo = auth.AuthInfo{Username: "bob", Revision: aa.as.Revision()}
assert.ErrorIs(t, aa.checkLeasePutsKeys(l), auth.ErrPermissionDenied, "auth is enabled, bob does not have permissions, bob does not exist")
assert.ErrorIsf(t, aa.checkLeasePutsKeys(l), auth.ErrPermissionDenied, "auth is enabled, bob does not have permissions, bob does not exist")
_, err := aa.as.UserAdd(&pb.AuthUserAddRequest{Name: "bob", Options: &authpb.UserAddOptions{NoPassword: true}})
assert.NoError(t, err, "bob should be added without error")
assert.NoErrorf(t, err, "bob should be added without error")
aa.authInfo = auth.AuthInfo{Username: "bob", Revision: aa.as.Revision()}
assert.ErrorIs(t, aa.checkLeasePutsKeys(l), auth.ErrPermissionDenied, "auth is enabled, bob exists yet does not have permissions")
assert.ErrorIsf(t, aa.checkLeasePutsKeys(l), auth.ErrPermissionDenied, "auth is enabled, bob exists yet does not have permissions")

// allow bob to access "a"
_, err = aa.as.RoleAdd(&pb.AuthRoleAddRequest{Name: "bobsrole"})
assert.NoError(t, err, "bobsrole should be added without error")
assert.NoErrorf(t, err, "bobsrole should be added without error")
_, err = aa.as.RoleGrantPermission(&pb.AuthRoleGrantPermissionRequest{
Name: "bobsrole",
Perm: &authpb.Permission{
Expand All @@ -813,13 +812,13 @@ func TestCheckLeasePutsKeys(t *testing.T) {
RangeEnd: nil,
},
})
assert.NoError(t, err, "bobsrole should be granted permissions without error")
assert.NoErrorf(t, err, "bobsrole should be granted permissions without error")
_, err = aa.as.UserGrantRole(&pb.AuthUserGrantRoleRequest{
User: "bob",
Role: "bobsrole",
})
assert.NoError(t, err, "bob should be granted bobsrole without error")
assert.NoErrorf(t, err, "bob should be granted bobsrole without error")

aa.authInfo = auth.AuthInfo{Username: "bob", Revision: aa.as.Revision()}
assert.NoError(t, aa.checkLeasePutsKeys(l), "bob should be able to access key 'a'")
assert.NoErrorf(t, aa.checkLeasePutsKeys(l), "bob should be able to access key 'a'")
}
2 changes: 1 addition & 1 deletion server/etcdserver/cindex/cindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestConsistentIndexDecrease(t *testing.T) {
tx.Lock()
defer tx.Unlock()
if tc.panicExpected {
assert.Panics(t, func() { ci.UnsafeSave(tx) }, "Should refuse to decrease cindex")
assert.Panicsf(t, func() { ci.UnsafeSave(tx) }, "Should refuse to decrease cindex")
return
}
ci.UnsafeSave(tx)
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/txn/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ etcd_server_range_duration_seconds_count{success="true"} 1
`

err := testutil.CollectAndCompare(rangeSec, strings.NewReader(expected))
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
require.NoErrorf(t, err, "Collected metrics did not match expected metrics")
}
2 changes: 1 addition & 1 deletion server/etcdserver/txn/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func TestWriteTxnPanic(t *testing.T) {
},
}

assert.Panics(t, func() { Txn(ctx, zaptest.NewLogger(t), txn, false, s, &lease.FakeLessor{}) }, "Expected panic in Txn with writes")
assert.Panicsf(t, func() { Txn(ctx, zaptest.NewLogger(t), txn, false, s, &lease.FakeLessor{}) }, "Expected panic in Txn with writes")
}

func TestCheckTxnAuth(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions server/storage/backend/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func TestBackendPreCommitHook(t *testing.T) {
// Empty commit.
tx.Commit()

assert.Equal(t, ">cc", getCommitsKey(t, be), "expected 2 explicit commits")
assert.Equalf(t, ">cc", getCommitsKey(t, be), "expected 2 explicit commits")
tx.Commit()
assert.Equal(t, ">ccc", getCommitsKey(t, be), "expected 3 explicit commits")
assert.Equalf(t, ">ccc", getCommitsKey(t, be), "expected 3 explicit commits")
}

func TestBackendAutoCommitLimitHook(t *testing.T) {
Expand Down
Loading

0 comments on commit 2d144f0

Please sign in to comment.