Skip to content

Commit

Permalink
more wip
Browse files Browse the repository at this point in the history
  • Loading branch information
redwrasse committed Sep 6, 2024
1 parent dcf1981 commit c63872b
Show file tree
Hide file tree
Showing 35 changed files with 103 additions and 81 deletions.
3 changes: 2 additions & 1 deletion etcdctl/ctlv3/command/watch_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package command

import (
"errors"
"reflect"
"testing"
)
Expand Down Expand Up @@ -534,7 +535,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 err != ts.err {
if !errors.Is(err, ts.err) {
t.Fatalf("#%d: error expected %v, got %v", i, ts.err, err)
}
if !reflect.DeepEqual(watchArgs, ts.watchArgs) {
Expand Down
26 changes: 13 additions & 13 deletions server/auth/store_test.go
Original file line number Diff line number Diff line change
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
8 changes: 4 additions & 4 deletions server/etcdserver/api/v2discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestCheckCluster(t *testing.T) {
}
}()
ns, size, index, err := d.checkCluster()
if err != tt.werr {
if !errors.Is(err, tt.werr) {
t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
}
if reflect.DeepEqual(ns, tt.nodes) {
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestCreateSelf(t *testing.T) {

for i, tt := range tests {
d := newTestDiscovery(t, "1000", 1, tt.c)
if err := d.createSelf(""); err != tt.werr {
if err := d.createSelf(""); !errors.Is(err, tt.werr) {
t.Errorf("#%d: err = %v, want %v", i, err, nil)
}
}
Expand Down Expand Up @@ -383,7 +383,7 @@ func TestNodesToCluster(t *testing.T) {

for i, tt := range tests {
cluster, err := nodesToCluster(tt.nodes, tt.size)
if err != tt.werr {
if !errors.Is(err, tt.werr) {
t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
}
if !reflect.DeepEqual(cluster, tt.wcluster) {
Expand Down Expand Up @@ -435,7 +435,7 @@ func TestRetryFailure(t *testing.T) {
fc.Advance(time.Second * (0x1 << i))
}
}()
if _, _, _, err := d.checkCluster(); err != ErrTooManyRetries {
if _, _, _, err := d.checkCluster(); !errors.Is(err, ErrTooManyRetries) {
t.Errorf("err = %v, want %v", err, ErrTooManyRetries)
}
}
Expand Down
6 changes: 3 additions & 3 deletions server/etcdserver/api/v3discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestGetClusterSize(t *testing.T) {
clusterToken: "fakeToken",
}

if cs, err := d.getClusterSize(); err != tc.expectedErr {
if cs, err := d.getClusterSize(); !errors.Is(err, tc.expectedErr) {
t.Errorf("Unexpected error, expected: %v got: %v", tc.expectedErr, err)
} else {
if err == nil && cs != tc.expectedSize {
Expand Down Expand Up @@ -387,7 +387,7 @@ func TestCheckCluster(t *testing.T) {
}

clsInfo, _, _, err := d.checkCluster()
if err != tc.expectedError {
if !errors.Is(err, tc.expectedError) {
t.Errorf("Unexpected error, expected: %v, got: %v", tc.expectedError, err)
}

Expand Down Expand Up @@ -724,7 +724,7 @@ func TestGetInitClusterStr(t *testing.T) {
}

retStr, err := clsInfo.getInitClusterStr(tc.clusterSize)
if err != tc.expectedError {
if !errors.Is(err, tc.expectedError) {
t.Errorf("Unexpected error, expected: %v, got: %v", tc.expectedError, err)
}

Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/api/v3rpc/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestGRPCError(t *testing.T) {
{err: errors.New("foo"), exp: status.Error(codes.Unknown, "foo")},
}
for i := range tt {
if err := togRPCError(tt[i].err); err != tt[i].exp {
if err := togRPCError(tt[i].err); !errors.Is(err, tt[i].exp) {
if _, ok := status.FromError(err); ok {
if err.Error() == tt[i].exp.Error() {
continue
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/api/v3rpc/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (sws *serverWatchStream) recvLoop() error {
case auth.ErrUserEmpty:
cancelReason = rpctypes.ErrGRPCUserEmpty.Error()
default:
if err != auth.ErrPermissionDenied {
if !errors.Is(err, auth.ErrPermissionDenied) {
sws.lg.Error("unexpected error code", zap.Error(err))
}
cancelReason = rpctypes.ErrGRPCPermissionDenied.Error()
Expand Down
3 changes: 2 additions & 1 deletion server/etcdserver/api/v3rpc/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package v3rpc

import (
"bytes"
"errors"
"math"
"testing"

Expand Down Expand Up @@ -69,7 +70,7 @@ func TestSendFragment(t *testing.T) {
return nil
}
err := sendFragments(tt[i].wr, tt[i].maxRequestBytes, testSend)
if err != tt[i].werr {
if !errors.Is(err, tt[i].werr) {
t.Errorf("#%d: expected error %v, got %v", i, tt[i].werr, err)
}
got := len(fragmentedResp)
Expand Down
Loading

0 comments on commit c63872b

Please sign in to comment.