Skip to content

Commit

Permalink
more wip errors.Is in server module
Browse files Browse the repository at this point in the history
Signed-off-by: redwrasse <[email protected]>
  • Loading branch information
redwrasse committed Sep 25, 2024
1 parent 9fe1ead commit 47cad30
Show file tree
Hide file tree
Showing 28 changed files with 92 additions and 73 deletions.
3 changes: 2 additions & 1 deletion server/auth/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package auth

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

_, aerr := verify.assign(ctx, "abc", 123)
if aerr != ErrVerifyOnly {
if !errors.Is(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 err != ErrUserAlreadyExist {
if !errors.Is(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 err != ErrUserEmpty {
if !errors.Is(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 err != ErrAuthFailed {
if !errors.Is(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 err != ErrAuthFailed {
if !errors.Is(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 err != ErrUserNotFound {
if !errors.Is(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 err != ErrUserNotFound {
if !errors.Is(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 err != ErrUserNotFound {
if !errors.Is(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 err != ErrRoleEmpty {
if !errors.Is(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 err != ErrUserNotFound {
if !errors.Is(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); err != ErrPermissionDenied {
if err := as.isOpPermitted("foo", as.Revision(), perm.Key, perm.RangeEnd, perm.PermType); !errors.Is(err, ErrPermissionDenied) {
t.Fatal(err)
}

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

if err != ErrPermissionNotGiven {
if !errors.Is(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 err != ErrInvalidAuthToken {
if !errors.Is(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 err != ErrInvalidAuthToken {
if !errors.Is(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 err != ErrAuthNotEnabled {
if !errors.Is(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 err != ErrAuthNotEnabled {
if !errors.Is(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 err != ErrUserNotFound {
if !errors.Is(err, ErrUserNotFound) {
t.Errorf("expected %v, got %v", ErrUserNotFound, err)
}

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

// non-admin user
err = as.IsAdminPermitted(&AuthInfo{Username: "foo", Revision: 1})
if err != ErrPermissionDenied {
if !errors.Is(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 err != ErrUserAlreadyExist {
if !errors.Is(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 err != ErrUserEmpty {
if !errors.Is(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 err != ErrAuthFailed {
if !errors.Is(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 err != ErrUserNotFound {
if !errors.Is(err, ErrUserNotFound) {
t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
}
}
3 changes: 2 additions & 1 deletion server/embed/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package embed

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

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

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

if _, err := StartEtcd(cfg); err != auth.ErrInvalidAuthOpts {
if _, err := StartEtcd(cfg); !errors.Is(err, auth.ErrInvalidAuthOpts) {
t.Fatalf("expected %v, got %v", auth.ErrInvalidAuthOpts, err)
}
}
Expand Down
7 changes: 4 additions & 3 deletions server/etcdmain/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package etcdmain

import (
"errors"
"flag"
"fmt"
"net/url"
Expand Down Expand Up @@ -224,7 +225,7 @@ func TestConfigParsingConflictClusteringFlags(t *testing.T) {

for i, tt := range conflictArgs {
cfg := newConfig()
if err := cfg.parse(tt); err != embed.ErrConflictBootstrapFlags {
if err := cfg.parse(tt); !errors.Is(err, embed.ErrConflictBootstrapFlags) {
t.Errorf("%d: err = %v, want %v", i, err, embed.ErrConflictBootstrapFlags)
}
}
Expand Down Expand Up @@ -267,7 +268,7 @@ func TestConfigFileConflictClusteringFlags(t *testing.T) {
args := []string{fmt.Sprintf("--config-file=%s", tmpfile.Name())}

cfg := newConfig()
if err := cfg.parse(args); err != embed.ErrConflictBootstrapFlags {
if err := cfg.parse(args); !errors.Is(err, embed.ErrConflictBootstrapFlags) {
t.Errorf("%d: err = %v, want %v", i, err, embed.ErrConflictBootstrapFlags)
}
}
Expand Down Expand Up @@ -310,7 +311,7 @@ func TestConfigParsingMissedAdvertiseClientURLsFlag(t *testing.T) {

for i, tt := range tests {
cfg := newConfig()
if err := cfg.parse(tt.args); err != tt.werr {
if err := cfg.parse(tt.args); !errors.Is(err, tt.werr) {
t.Errorf("%d: err = %v, want %v", i, err, tt.werr)
}
}
Expand Down
3 changes: 2 additions & 1 deletion server/etcdserver/api/membership/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package membership

import (
"encoding/json"
"errors"
"fmt"
"path"
"reflect"
Expand Down Expand Up @@ -458,7 +459,7 @@ func TestClusterValidateConfigurationChangeV2(t *testing.T) {
}
for i, tt := range tests {
err := cl.ValidateConfigurationChange(tt.cc)
if err != tt.werr {
if !errors.Is(err, tt.werr) {
t.Errorf("#%d: validateConfigurationChange error = %v, want %v", i, err, tt.werr)
}
}
Expand Down
5 changes: 3 additions & 2 deletions server/etcdserver/api/rafthttp/msg_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package rafthttp

import (
"bytes"
"errors"
"reflect"
"testing"

Expand Down Expand Up @@ -77,13 +78,13 @@ func TestMessage(t *testing.T) {
for i, tt := range tests {
b := &bytes.Buffer{}
enc := &messageEncoder{w: b}
if err := enc.encode(&tt.msg); err != tt.encodeErr {
if err := enc.encode(&tt.msg); !errors.Is(err, tt.encodeErr) {
t.Errorf("#%d: encode message error expected %v, got %v", i, tt.encodeErr, err)
continue
}
dec := &messageDecoder{r: b}
m, err := dec.decode()
if err != tt.decodeErr {
if !errors.Is(err, tt.decodeErr) {
t.Errorf("#%d: decode message error expected %v, got %v", i, tt.decodeErr, err)
continue
}
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/api/rafthttp/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (cr *streamReader) run() {
for {
rc, err := cr.dial(t)
if err != nil {
if err != errUnsupportedStreamType {
if !errors.Is(err, errUnsupportedStreamType) {
cr.status.deactivate(failureType{source: t.String(), action: "dial"}, err.Error())
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/api/rafthttp/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func TestStreamReaderDialDetectUnsupport(t *testing.T) {
}

_, err := sr.dial(typ)
if err != errUnsupportedStreamType {
if !errors.Is(err, errUnsupportedStreamType) {
t.Errorf("#%d: error = %v, want %v", i, err, errUnsupportedStreamType)
}
}
Expand Down
9 changes: 5 additions & 4 deletions server/etcdserver/api/snap/snapshotter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package snap

import (
"errors"
"fmt"
"hash/crc32"
"os"
Expand Down Expand Up @@ -80,7 +81,7 @@ func TestBadCRC(t *testing.T) {
crcTable = crc32.MakeTable(crc32.Koopman)

_, err = Read(zaptest.NewLogger(t), filepath.Join(dir, fmt.Sprintf("%016x-%016x.snap", 1, 1)))
if err == nil || err != ErrCRCMismatch {
if err == nil || !errors.Is(err, ErrCRCMismatch) {
t.Errorf("err = %v, want %v", err, ErrCRCMismatch)
}
}
Expand Down Expand Up @@ -221,7 +222,7 @@ func TestNoSnapshot(t *testing.T) {
defer os.RemoveAll(dir)
ss := New(zaptest.NewLogger(t), dir)
_, err = ss.Load()
if err != ErrNoSnapshot {
if !errors.Is(err, ErrNoSnapshot) {
t.Errorf("err = %v, want %v", err, ErrNoSnapshot)
}
}
Expand All @@ -240,7 +241,7 @@ func TestEmptySnapshot(t *testing.T) {
}

_, err = Read(zaptest.NewLogger(t), filepath.Join(dir, "1.snap"))
if err != ErrEmptySnapshot {
if !errors.Is(err, ErrEmptySnapshot) {
t.Errorf("err = %v, want %v", err, ErrEmptySnapshot)
}
}
Expand All @@ -262,7 +263,7 @@ func TestAllSnapshotBroken(t *testing.T) {

ss := New(zaptest.NewLogger(t), dir)
_, err = ss.Load()
if err != ErrNoSnapshot {
if !errors.Is(err, ErrNoSnapshot) {
t.Errorf("err = %v, want %v", err, ErrNoSnapshot)
}
}
Expand Down
Loading

0 comments on commit 47cad30

Please sign in to comment.