Skip to content

Commit

Permalink
tests: remaining errors.Is conversions
Browse files Browse the repository at this point in the history
Signed-off-by: redwrasse <[email protected]>
  • Loading branch information
redwrasse committed Oct 15, 2024
1 parent 54db7f0 commit 1c825ad
Show file tree
Hide file tree
Showing 18 changed files with 58 additions and 43 deletions.
5 changes: 3 additions & 2 deletions tests/e2e/ctl_v3_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package e2e
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -193,7 +194,7 @@ func getMemberList(cx ctlCtx, serializable bool) (etcdserverpb.MemberListRespons

resp := etcdserverpb.MemberListResponse{}
dec := json.NewDecoder(strings.NewReader(txt))
if err := dec.Decode(&resp); err == io.EOF {
if err := dec.Decode(&resp); errors.Is(err, io.EOF) {
return etcdserverpb.MemberListResponse{}, err
}
return resp, nil
Expand Down Expand Up @@ -221,7 +222,7 @@ func memberListWithHexTest(cx ctlCtx) {
}
hexResp := etcdserverpb.MemberListResponse{}
dec := json.NewDecoder(strings.NewReader(txt))
if err := dec.Decode(&hexResp); err == io.EOF {
if err := dec.Decode(&hexResp); errors.Is(err, io.EOF) {
cx.t.Fatalf("memberListWithHexTest error (%v)", err)
}
num := len(resp.Members)
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/ctl_v3_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package e2e
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -156,7 +157,7 @@ func getSnapshotStatus(cx ctlCtx, fpath string) (snapshot.Status, error) {

resp := snapshot.Status{}
dec := json.NewDecoder(strings.NewReader(txt))
if err := dec.Decode(&resp); err == io.EOF {
if err := dec.Decode(&resp); errors.Is(err, io.EOF) {
return snapshot.Status{}, err
}
return resp, nil
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/clientv3/concurrency/example_mutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package concurrency_test

import (
"context"
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -64,7 +65,7 @@ func ExampleMutex_TryLock() {
if err = m2.TryLock(context.TODO()); err == nil {
log.Fatal("should not acquire lock")
}
if err == concurrency.ErrLocked {
if errors.Is(err, concurrency.ErrLocked) {
fmt.Println("cannot acquire lock for s2, as already locked in another session")
}

Expand Down
11 changes: 6 additions & 5 deletions tests/integration/clientv3/connectivity/black_hole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package connectivity_test

import (
"context"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -113,7 +114,7 @@ func TestBalancerUnderBlackholeKeepAliveWatch(t *testing.T) {
func TestBalancerUnderBlackholeNoKeepAlivePut(t *testing.T) {
testBalancerUnderBlackholeNoKeepAlive(t, func(cli *clientv3.Client, ctx context.Context) error {
_, err := cli.Put(ctx, "foo", "bar")
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || err == rpctypes.ErrTimeout {
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || errors.Is(err, rpctypes.ErrTimeout) {
return errExpected
}
return err
Expand All @@ -123,7 +124,7 @@ func TestBalancerUnderBlackholeNoKeepAlivePut(t *testing.T) {
func TestBalancerUnderBlackholeNoKeepAliveDelete(t *testing.T) {
testBalancerUnderBlackholeNoKeepAlive(t, func(cli *clientv3.Client, ctx context.Context) error {
_, err := cli.Delete(ctx, "foo")
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || err == rpctypes.ErrTimeout {
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || errors.Is(err, rpctypes.ErrTimeout) {
return errExpected
}
return err
Expand All @@ -136,7 +137,7 @@ func TestBalancerUnderBlackholeNoKeepAliveTxn(t *testing.T) {
If(clientv3.Compare(clientv3.Version("foo"), "=", 0)).
Then(clientv3.OpPut("foo", "bar")).
Else(clientv3.OpPut("foo", "baz")).Commit()
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || err == rpctypes.ErrTimeout {
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || errors.Is(err, rpctypes.ErrTimeout) {
return errExpected
}
return err
Expand All @@ -146,7 +147,7 @@ func TestBalancerUnderBlackholeNoKeepAliveTxn(t *testing.T) {
func TestBalancerUnderBlackholeNoKeepAliveLinearizableGet(t *testing.T) {
testBalancerUnderBlackholeNoKeepAlive(t, func(cli *clientv3.Client, ctx context.Context) error {
_, err := cli.Get(ctx, "a")
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || err == rpctypes.ErrTimeout {
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || errors.Is(err, rpctypes.ErrTimeout) {
return errExpected
}
return err
Expand Down Expand Up @@ -207,7 +208,7 @@ func testBalancerUnderBlackholeNoKeepAlive(t *testing.T, op func(*clientv3.Clien
cancel()
if err == nil {
break
} else if err == errExpected {
} else if errors.Is(err, errExpected) {
t.Logf("#%d: current error %v", i, err)
} else {
t.Errorf("#%d: failed with error %v", i, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var errExpected = errors.New("expected error")

func isErrorExpected(err error) bool {
return clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) ||
err == rpctypes.ErrTimeout || err == rpctypes.ErrTimeoutDueToLeaderFail
errors.Is(err, rpctypes.ErrTimeout) || errors.Is(err, rpctypes.ErrTimeoutDueToLeaderFail)
}

// TestBalancerUnderNetworkPartitionPut tests when one member becomes isolated,
Expand Down Expand Up @@ -145,7 +145,7 @@ func testBalancerUnderNetworkPartition(t *testing.T, op func(*clientv3.Client, c
if err == nil {
break
}
if err != errExpected {
if !errors.Is(err, errExpected) {
t.Errorf("#%d: expected '%v', got '%v'", i, errExpected, err)
}
// give enough time for endpoint switch
Expand Down Expand Up @@ -267,7 +267,7 @@ func testBalancerUnderNetworkPartitionWatch(t *testing.T, isolateLeader bool) {
if len(ev.Events) != 0 {
t.Fatal("expected no event")
}
if err = ev.Err(); err != rpctypes.ErrNoLeader {
if err = ev.Err(); !errors.Is(err, rpctypes.ErrNoLeader) {
t.Fatalf("expected %v, got %v", rpctypes.ErrNoLeader, err)
}
case <-time.After(integration2.RequestWaitTimeout): // enough time to detect leader lost
Expand Down Expand Up @@ -313,7 +313,7 @@ func TestDropReadUnderNetworkPartition(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
_, err = kvc.Get(ctx, "a")
cancel()
if err != rpctypes.ErrLeaderChanged {
if !errors.Is(err, rpctypes.ErrLeaderChanged) {
t.Fatalf("expected %v, got %v", rpctypes.ErrLeaderChanged, err)
}

Expand All @@ -322,7 +322,7 @@ func TestDropReadUnderNetworkPartition(t *testing.T) {
_, err = kvc.Get(ctx, "a")
cancel()
if err != nil {
if err == rpctypes.ErrTimeout {
if errors.Is(err, rpctypes.ErrTimeout) {
<-time.After(time.Second)
i++
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package connectivity_test
import (
"bytes"
"context"
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestBalancerUnderServerShutdownWatch(t *testing.T) {
if err == nil {
break
}
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || err == rpctypes.ErrTimeout || err == rpctypes.ErrTimeoutDueToLeaderFail {
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || errors.Is(err, rpctypes.ErrTimeout) || errors.Is(err, rpctypes.ErrTimeoutDueToLeaderFail) {
continue
}
t.Fatal(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package recipes_test

import (
"context"
"errors"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -149,7 +150,7 @@ func TestDoubleBarrierTooManyClients(t *testing.T) {
// no any other client can enter the barrier.
wgEntered.Wait()
t.Log("Try to enter into double barrier")
if err = b.Enter(); err != recipe.ErrTooManyClients {
if err = b.Enter(); !errors.Is(err, recipe.ErrTooManyClients) {
t.Errorf("Unexcepted error, expected: ErrTooManyClients, got: %v", err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package recipes_test

import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
Expand Down Expand Up @@ -139,7 +140,7 @@ func testMutexTryLock(t *testing.T, lockers int, chooseClient func() *clientv3.C
case <-ctx.Done():
t.Errorf("Thread: %v, Context failed: %v", i, err)
}
} else if err == concurrency.ErrLocked {
} else if errors.Is(err, concurrency.ErrLocked) {
select {
case notlockedC <- m:
case <-ctx.Done():
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/clientv3/lease/leasing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package lease_test

import (
"context"
"errors"
"fmt"
"math/rand"
"reflect"
Expand Down Expand Up @@ -894,7 +895,7 @@ func TestLeasingTxnCancel(t *testing.T) {
time.Sleep(100 * time.Millisecond)
cancel()
}()
if _, err := lkv2.Txn(ctx).Then(clientv3.OpPut("k", "v")).Commit(); err != context.Canceled {
if _, err := lkv2.Txn(ctx).Then(clientv3.OpPut("k", "v")).Commit(); !errors.Is(err, context.Canceled) {
t.Fatalf("expected %v, got %v", context.Canceled, err)
}
}
Expand Down Expand Up @@ -2015,7 +2016,7 @@ func TestLeasingSessionExpireCancel(t *testing.T) {

select {
case err := <-errc:
if err != ctx.Err() {
if !errors.Is(err, ctx.Err()) {
t.Errorf("#%d: expected %v of server unavailable, got %v", i, ctx.Err(), err)
}
case <-time.After(5 * time.Second):
Expand Down Expand Up @@ -2046,7 +2047,7 @@ func waitForExpireAck(t *testing.T, kv clientv3.KV) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
_, err := kv.Get(ctx, "abc")
cancel()
if err == ctx.Err() {
if errors.Is(err, ctx.Err()) {
return
} else if err != nil {
t.Logf("current error: %v", err)
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/clientv3/maintenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"io"
"math"
Expand Down Expand Up @@ -135,7 +136,7 @@ func TestMaintenanceMoveLeader(t *testing.T) {

cli := clus.Client(targetIdx)
_, err := cli.MoveLeader(context.Background(), target)
if err != rpctypes.ErrNotLeader {
if !errors.Is(err, rpctypes.ErrNotLeader) {
t.Fatalf("error expected %v, got %v", rpctypes.ErrNotLeader, err)
}

Expand Down Expand Up @@ -186,7 +187,7 @@ func TestMaintenanceSnapshotCancel(t *testing.T) {

cancel()
_, err = io.Copy(io.Discard, rc1)
if err != context.Canceled {
if !errors.Is(err, context.Canceled) {
t.Errorf("expected %v, got %v", context.Canceled, err)
}
}
Expand Down Expand Up @@ -303,7 +304,7 @@ func testMaintenanceSnapshotErrorInflight(t *testing.T, snapshot func(context.Co
close(donec)
}()
_, err = io.Copy(io.Discard, rc1)
if err != nil && err != context.Canceled {
if err != nil && !errors.Is(err, context.Canceled) {
t.Errorf("expected %v, got %v", context.Canceled, err)
}
<-donec
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/clientv3/ordering_kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestDetectKvOrderViolation(t *testing.T) {
t.Logf("Quering m2 after restart")
v, err = orderingKv.Get(ctx, "foo", clientv3.WithSerializable())
t.Logf("Quering m2 returned: v:%v err:%v ", v, err)
if err != errOrderViolation {
if !errors.Is(err, errOrderViolation) {
t.Fatalf("expected %v, got err:%v v:%v", errOrderViolation, err, v)
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestDetectTxnOrderViolation(t *testing.T) {
cli.SetEndpoints(clus.Members[2].GRPCURL)
time.Sleep(2 * time.Second) // FIXME: Figure out how pause SetEndpoints sufficiently that this is not needed
_, err = orderingKv.Get(ctx, "foo", clientv3.WithSerializable())
if err != errOrderViolation {
if !errors.Is(err, errOrderViolation) {
t.Fatalf("expected %v, got %v", errOrderViolation, err)
}
orderingTxn = orderingKv.Txn(ctx)
Expand All @@ -164,7 +164,7 @@ func TestDetectTxnOrderViolation(t *testing.T) {
).Then(
clientv3.OpGet("foo", clientv3.WithSerializable()),
).Commit()
if err != errOrderViolation {
if !errors.Is(err, errOrderViolation) {
t.Fatalf("expected %v, got %v", errOrderViolation, err)
}
}
5 changes: 3 additions & 2 deletions tests/integration/clientv3/ordering_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package clientv3test

import (
"context"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -78,7 +79,7 @@ func TestEndpointSwitchResolvesViolation(t *testing.T) {
cli.SetEndpoints(clus.Members[2].GRPCURL)
time.Sleep(1 * time.Second) // give enough time for the operation
_, err = orderingKv.Get(ctx, "foo", clientv3.WithSerializable())
if err != ordering.ErrNoGreaterRev {
if !errors.Is(err, ordering.ErrNoGreaterRev) {
t.Fatal("While speaking to partitioned leader, we should get ErrNoGreaterRev error")
}

Expand Down Expand Up @@ -156,7 +157,7 @@ func TestUnresolvableOrderViolation(t *testing.T) {
time.Sleep(1 * time.Second) // give enough time for operation

_, err = OrderingKv.Get(ctx, "foo", clientv3.WithSerializable())
if err != ordering.ErrNoGreaterRev {
if !errors.Is(err, ordering.ErrNoGreaterRev) {
t.Fatalf("expected %v, got %v", ordering.ErrNoGreaterRev, err)
}
}
5 changes: 3 additions & 2 deletions tests/integration/clientv3/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package clientv3test

import (
"context"
"errors"
"fmt"
"testing"
"time"
Expand All @@ -36,7 +37,7 @@ func TestTxnError(t *testing.T) {
ctx := context.TODO()

_, err := kv.Txn(ctx).Then(clientv3.OpPut("foo", "bar1"), clientv3.OpPut("foo", "bar2")).Commit()
if err != rpctypes.ErrDuplicateKey {
if !errors.Is(err, rpctypes.ErrDuplicateKey) {
t.Fatalf("expected %v, got %v", rpctypes.ErrDuplicateKey, err)
}

Expand All @@ -45,7 +46,7 @@ func TestTxnError(t *testing.T) {
ops[i] = clientv3.OpPut(fmt.Sprintf("foo%d", i), "")
}
_, err = kv.Txn(ctx).Then(ops...).Commit()
if err != rpctypes.ErrTooManyOps {
if !errors.Is(err, rpctypes.ErrTooManyOps) {
t.Fatalf("expected %v, got %v", rpctypes.ErrTooManyOps, err)
}
}
Expand Down
Loading

0 comments on commit 1c825ad

Please sign in to comment.