Skip to content

Commit

Permalink
client modules: add in remaining errors.Is for error equality and ine…
Browse files Browse the repository at this point in the history
…quality checks

Signed-off-by: redwrasse <[email protected]>
  • Loading branch information
redwrasse committed Sep 22, 2024
1 parent d59fa33 commit a244390
Show file tree
Hide file tree
Showing 61 changed files with 146 additions and 191 deletions.
7 changes: 4 additions & 3 deletions client/internal/v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ if err != nil {
kapi := client.NewKeysAPI(c)
resp, err := kapi.Set(ctx, "test", "bar", nil)
if err != nil {
if err == context.Canceled {
var cerr *client.ClusterError
if errors.Is(err, context.Canceled) {
// ctx is canceled by another routine
} else if err == context.DeadlineExceeded {
} else if errors.Is(err, context.DeadlineExceeded) {
// ctx is attached with a deadline and it exceeded
} else if cerr, ok := err.(*client.ClusterError); ok {
} else if errors.As(err, &cerr) {
// process (cerr.Errors)
} else {
// bad cluster endpoints, which are not etcd servers
Expand Down
2 changes: 1 addition & 1 deletion client/pkg/fileutil/preallocate_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func preallocFixed(f *os.File, sizeInBytes int64) error {
Length: sizeInBytes,
}
err := unix.FcntlFstore(f.Fd(), unix.F_PREALLOCATE, fstore)
if err == nil || err == unix.ENOTSUP {
if err == nil || errors.Is(err, unix.ENOTSUP) {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion client/v3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (c *Client) autoSync() {
ctx, cancel := context.WithTimeout(c.ctx, 5*time.Second)
err := c.Sync(ctx)
cancel()
if err != nil && err != c.ctx.Err() {
if err != nil && !errors.Is(err, c.ctx.Err()) {
c.lg.Info("Auto sync endpoints failed.", zap.Error(err))
}
}
Expand Down
6 changes: 3 additions & 3 deletions client/v3/retry_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ func (c *Client) streamClientInterceptor(optFuncs ...retryOption) grpc.StreamCli
// shouldRefreshToken checks whether there's a need to refresh the token based on the error and callOptions,
// and returns a boolean value.
func (c *Client) shouldRefreshToken(err error, callOpts *options) bool {
if rpctypes.Error(err) == rpctypes.ErrUserEmpty {
if errors.Is(rpctypes.Error(err), rpctypes.ErrUserEmpty) {
// refresh the token when username, password is present but the server returns ErrUserEmpty
// which is possible when the client token is cleared somehow
return c.authTokenBundle != nil // equal to c.Username != "" && c.Password != ""
}

return callOpts.retryAuth &&
(rpctypes.Error(err) == rpctypes.ErrInvalidAuthToken || rpctypes.Error(err) == rpctypes.ErrAuthOldRevision)
(errors.Is(rpctypes.Error(err), rpctypes.ErrInvalidAuthToken) || errors.Is(rpctypes.Error(err), rpctypes.ErrAuthOldRevision))
}

func (c *Client) refreshToken(ctx context.Context) error {
Expand Down Expand Up @@ -254,7 +254,7 @@ func (s *serverStreamingRetryingStream) receiveMsgAndIndicateRetry(m any) (bool,
wasGood := s.receivedGood
s.mu.RUnlock()
err := s.getStream().RecvMsg(m)
if err == nil || err == io.EOF {
if err == nil || errors.Is(err, io.EOF) {
s.mu.Lock()
s.receivedGood = true
s.mu.Unlock()
Expand Down
3 changes: 1 addition & 2 deletions contrib/raftexample/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"encoding/gob"
"encoding/json"
"errors"
"log"
"strings"
"sync"
Expand Down Expand Up @@ -114,7 +113,7 @@ func (s *kvstore) getSnapshot() ([]byte, error) {

func (s *kvstore) loadSnapshot() (*raftpb.Snapshot, error) {
snapshot, err := s.snapshotter.Load()
if errors.Is(err, snap.ErrNoSnapshot) {
if err == snap.ErrNoSnapshot {
return nil, nil
}
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions contrib/raftexample/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"context"
"errors"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -393,7 +392,7 @@ func (rc *raftNode) maybeTriggerSnapshot(applyDoneC <-chan struct{}) {
compactIndex = rc.appliedIndex - snapshotCatchUpEntriesN
}
if err := rc.raftStorage.Compact(compactIndex); err != nil {
if !errors.Is(err, raft.ErrCompacted) {
if err != raft.ErrCompacted {
panic(err)
}
} else {
Expand Down
3 changes: 1 addition & 2 deletions etcdctl/ctlv3/command/auth_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package command

import (
"errors"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -83,7 +82,7 @@ func authEnableCommandFunc(cmd *cobra.Command, args []string) {
if _, err = cli.AuthEnable(ctx); err == nil {
break
}
if errors.Is(err, rpctypes.ErrRootRoleNotExist) {
if err == rpctypes.ErrRootRoleNotExist {
if _, err = cli.RoleAdd(ctx, "root"); err != nil {
break
}
Expand Down
3 changes: 1 addition & 2 deletions etcdctl/ctlv3/command/watch_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package command

import (
"errors"
"reflect"
"testing"
)
Expand Down Expand Up @@ -535,7 +534,7 @@ func Test_parseWatchArgs(t *testing.T) {
}
for i, ts := range tt {
watchArgs, execArgs, err := parseWatchArgs(ts.osArgs, ts.commandArgs, ts.envKey, ts.envRange, ts.interactive)
if !errors.Is(err, ts.err) {
if err != ts.err {
t.Fatalf("#%d: error expected %v, got %v", i, ts.err, err)
}
if !reflect.DeepEqual(watchArgs, ts.watchArgs) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/ioutil/readcloser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package ioutil

import (
"bytes"
"errors"
"io"
"testing"
)
Expand All @@ -29,7 +28,7 @@ func (rc *readerNilCloser) Close() error { return nil }
func TestExactReadCloserExpectEOF(t *testing.T) {
buf := bytes.NewBuffer(make([]byte, 10))
rc := NewExactReadCloser(&readerNilCloser{buf}, 1)
if _, err := rc.Read(make([]byte, 10)); !errors.Is(err, ErrExpectEOF) {
if _, err := rc.Read(make([]byte, 10)); err != ErrExpectEOF {
t.Fatalf("expected %v, got %v", ErrExpectEOF, err)
}
}
Expand All @@ -41,7 +40,7 @@ func TestExactReadCloserShort(t *testing.T) {
if _, err := rc.Read(make([]byte, 10)); err != nil {
t.Fatalf("Read expected nil err, got %v", err)
}
if err := rc.Close(); !errors.Is(err, ErrShortRead) {
if err := rc.Close(); err != ErrShortRead {
t.Fatalf("Close expected %v, got %v", ErrShortRead, err)
}
}
3 changes: 1 addition & 2 deletions server/auth/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package auth

import (
"context"
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -140,7 +139,7 @@ func testJWTInfo(t *testing.T, opts map[string]string) {
}

_, aerr := verify.assign(ctx, "abc", 123)
if !errors.Is(aerr, ErrVerifyOnly) {
if aerr != ErrVerifyOnly {
t.Fatalf("unexpected error when attempting to sign with public key: %v", aerr)
}

Expand Down
44 changes: 22 additions & 22 deletions server/auth/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ func TestUserAdd(t *testing.T) {
if err == nil {
t.Fatalf("expected %v, got %v", ErrUserAlreadyExist, err)
}
if !errors.Is(err, ErrUserAlreadyExist) {
if err != ErrUserAlreadyExist {
t.Fatalf("expected %v, got %v", ErrUserAlreadyExist, err)
}

ua = &pb.AuthUserAddRequest{Name: "", Options: &authpb.UserAddOptions{NoPassword: false}}
_, err = as.UserAdd(ua) // add a user with empty name
if !errors.Is(err, ErrUserEmpty) {
if err != ErrUserEmpty {
t.Fatal(err)
}

Expand Down Expand Up @@ -227,7 +227,7 @@ func TestCheckPassword(t *testing.T) {
if err == nil {
t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
}
if !errors.Is(err, ErrAuthFailed) {
if err != ErrAuthFailed {
t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
}

Expand All @@ -242,7 +242,7 @@ func TestCheckPassword(t *testing.T) {
if err == nil {
t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
}
if !errors.Is(err, ErrAuthFailed) {
if err != ErrAuthFailed {
t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
}
}
Expand All @@ -264,7 +264,7 @@ func TestUserDelete(t *testing.T) {
if err == nil {
t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
}
if !errors.Is(err, ErrUserNotFound) {
if err != ErrUserNotFound {
t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
}

Expand All @@ -288,7 +288,7 @@ func TestUserDeleteAndPermCache(t *testing.T) {

// delete a non-existing user
_, err = as.UserDelete(ud)
if !errors.Is(err, ErrUserNotFound) {
if err != ErrUserNotFound {
t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
}

Expand Down Expand Up @@ -336,7 +336,7 @@ func TestUserChangePassword(t *testing.T) {
if err == nil {
t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
}
if !errors.Is(err, ErrUserNotFound) {
if err != ErrUserNotFound {
t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
}

Expand All @@ -359,7 +359,7 @@ func TestRoleAdd(t *testing.T) {

// add a role with empty name
_, err = as.RoleAdd(&pb.AuthRoleAddRequest{Name: ""})
if !errors.Is(err, ErrRoleEmpty) {
if err != ErrRoleEmpty {
t.Fatal(err)
}
}
Expand All @@ -379,7 +379,7 @@ func TestUserGrant(t *testing.T) {
if err == nil {
t.Errorf("expected %v, got %v", ErrUserNotFound, err)
}
if !errors.Is(err, ErrUserNotFound) {
if err != ErrUserNotFound {
t.Errorf("expected %v, got %v", ErrUserNotFound, err)
}
}
Expand Down Expand Up @@ -455,7 +455,7 @@ func TestIsOpPermitted(t *testing.T) {
as.rangePermCacheMu.Lock()
delete(as.rangePermCache, "foo")
as.rangePermCacheMu.Unlock()
if err := as.isOpPermitted("foo", as.Revision(), perm.Key, perm.RangeEnd, perm.PermType); !errors.Is(err, ErrPermissionDenied) {
if err := as.isOpPermitted("foo", as.Revision(), perm.Key, perm.RangeEnd, perm.PermType); err != ErrPermissionDenied {
t.Fatal(err)
}

Expand Down Expand Up @@ -545,7 +545,7 @@ func TestRoleGrantPermission(t *testing.T) {
Name: "role-test-1",
})

if !errors.Is(err, ErrPermissionNotGiven) {
if err != ErrPermissionNotGiven {
t.Error(err)
}

Expand Down Expand Up @@ -887,13 +887,13 @@ func TestAuthInfoFromCtx(t *testing.T) {

ctx = metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: "Invalid Token"}))
_, err = as.AuthInfoFromCtx(ctx)
if !errors.Is(err, ErrInvalidAuthToken) {
if err != ErrInvalidAuthToken {
t.Errorf("expected %v, got %v", ErrInvalidAuthToken, err)
}

ctx = metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: "Invalid.Token"}))
_, err = as.AuthInfoFromCtx(ctx)
if !errors.Is(err, ErrInvalidAuthToken) {
if err != ErrInvalidAuthToken {
t.Errorf("expected %v, got %v", ErrInvalidAuthToken, err)
}

Expand All @@ -914,14 +914,14 @@ func TestAuthDisable(t *testing.T) {
as.AuthDisable()
ctx := context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(2)), AuthenticateParamSimpleTokenPrefix{}, "dummy")
_, err := as.Authenticate(ctx, "foo", "bar")
if !errors.Is(err, ErrAuthNotEnabled) {
if err != ErrAuthNotEnabled {
t.Errorf("expected %v, got %v", ErrAuthNotEnabled, err)
}

// Disabling disabled auth to make sure it can return safely if store is already disabled.
as.AuthDisable()
_, err = as.Authenticate(ctx, "foo", "bar")
if !errors.Is(err, ErrAuthNotEnabled) {
if err != ErrAuthNotEnabled {
t.Errorf("expected %v, got %v", ErrAuthNotEnabled, err)
}
}
Expand Down Expand Up @@ -980,19 +980,19 @@ func TestIsAdminPermitted(t *testing.T) {

// invalid user
err = as.IsAdminPermitted(&AuthInfo{Username: "rooti", Revision: 1})
if !errors.Is(err, ErrUserNotFound) {
if err != ErrUserNotFound {
t.Errorf("expected %v, got %v", ErrUserNotFound, err)
}

// empty user
err = as.IsAdminPermitted(&AuthInfo{Username: "", Revision: 1})
if !errors.Is(err, ErrUserEmpty) {
if err != ErrUserEmpty {
t.Errorf("expected %v, got %v", ErrUserEmpty, err)
}

// non-admin user
err = as.IsAdminPermitted(&AuthInfo{Username: "foo", Revision: 1})
if !errors.Is(err, ErrPermissionDenied) {
if err != ErrPermissionDenied {
t.Errorf("expected %v, got %v", ErrPermissionDenied, err)
}

Expand All @@ -1013,13 +1013,13 @@ func TestRecoverFromSnapshot(t *testing.T) {
if err == nil {
t.Fatalf("expected %v, got %v", ErrUserAlreadyExist, err)
}
if !errors.Is(err, ErrUserAlreadyExist) {
if err != ErrUserAlreadyExist {
t.Fatalf("expected %v, got %v", ErrUserAlreadyExist, err)
}

ua = &pb.AuthUserAddRequest{Name: "", Options: &authpb.UserAddOptions{NoPassword: false}}
_, err = as.UserAdd(ua) // add a user with empty name
if !errors.Is(err, ErrUserEmpty) {
if err != ErrUserEmpty {
t.Fatal(err)
}

Expand Down Expand Up @@ -1195,7 +1195,7 @@ func TestUserNoPasswordAdd(t *testing.T) {

ctx := context.WithValue(context.WithValue(context.TODO(), AuthenticateParamIndex{}, uint64(1)), AuthenticateParamSimpleTokenPrefix{}, "dummy")
_, err = as.Authenticate(ctx, username, "")
if !errors.Is(err, ErrAuthFailed) {
if err != ErrAuthFailed {
t.Fatalf("expected %v, got %v", ErrAuthFailed, err)
}
}
Expand Down Expand Up @@ -1237,7 +1237,7 @@ func TestUserChangePasswordWithOldLog(t *testing.T) {
if err == nil {
t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
}
if !errors.Is(err, ErrUserNotFound) {
if err != ErrUserNotFound {
t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
}
}
3 changes: 1 addition & 2 deletions server/embed/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package embed

import (
"errors"
"net/url"
"testing"

Expand All @@ -33,7 +32,7 @@ func TestEmptyClientTLSInfo_createMetricsListener(t *testing.T) {
Scheme: "https",
Host: "localhost:8080",
}
if _, err := e.createMetricsListener(murl); !errors.Is(err, ErrMissingClientTLSInfoForMetricsURL) {
if _, err := e.createMetricsListener(murl); err != ErrMissingClientTLSInfoForMetricsURL {
t.Fatalf("expected error %v, got %v", ErrMissingClientTLSInfoForMetricsURL, err)
}
}
3 changes: 1 addition & 2 deletions server/embed/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package embed

import (
"errors"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -44,7 +43,7 @@ func TestStartEtcdWrongToken(t *testing.T) {
cfg.Dir = tdir
cfg.AuthToken = "wrong-token"

if _, err := StartEtcd(cfg); !errors.Is(err, auth.ErrInvalidAuthOpts) {
if _, err := StartEtcd(cfg); err != auth.ErrInvalidAuthOpts {
t.Fatalf("expected %v, got %v", auth.ErrInvalidAuthOpts, err)
}
}
Expand Down
Loading

0 comments on commit a244390

Please sign in to comment.