diff --git a/client/internal/v2/client_test.go b/client/internal/v2/client_test.go index 83aae018dada..ac84d77160a7 100644 --- a/client/internal/v2/client_test.go +++ b/client/internal/v2/client_test.go @@ -27,6 +27,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" "go.etcd.io/etcd/api/v3/version" "go.etcd.io/etcd/client/pkg/v3/testutil" ) @@ -129,10 +130,7 @@ func TestSimpleHTTPClientDoSuccess(t *testing.T) { } resp, body, err := c.Do(context.Background(), &fakeAction{}) - if err != nil { - t.Fatalf("incorrect error value: want=nil got=%v", err) - } - + require.NoErrorf(t, err, "incorrect error value") wantCode := http.StatusTeapot if wantCode != resp.StatusCode { t.Fatalf("invalid response code: want=%d got=%d", wantCode, resp.StatusCode) @@ -151,9 +149,7 @@ func TestSimpleHTTPClientDoError(t *testing.T) { tr.errchan <- errors.New("fixture") _, _, err := c.Do(context.Background(), &fakeAction{}) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } + require.Errorf(t, err, "expected non-nil error, got nil") } type nilAction struct{} @@ -182,9 +178,7 @@ func TestSimpleHTTPClientDoCancelContext(t *testing.T) { tr.finishCancel <- struct{}{} _, _, err := c.Do(context.Background(), &fakeAction{}) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } + require.Errorf(t, err, "expected non-nil error, got nil") } type checkableReadCloser struct { @@ -219,9 +213,7 @@ func TestSimpleHTTPClientDoCancelContextResponseBodyClosed(t *testing.T) { }() _, _, err := c.Do(ctx, &fakeAction{}) - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } + require.Errorf(t, err, "expected non-nil error, got nil") if !body.closed { t.Fatalf("expected closed body") @@ -309,9 +301,7 @@ func TestSimpleHTTPClientDoHeaderTimeout(t *testing.T) { select { case err := <-errc: - if err == nil { - t.Fatalf("expected non-nil error, got nil") - } + require.Errorf(t, err, "expected non-nil error, got nil") case <-time.After(time.Second): t.Fatalf("unexpected timeout when waiting for the test to finish") } @@ -796,9 +786,7 @@ func TestHTTPClusterClientSync(t *testing.T) { rand: rand.New(rand.NewSource(0)), } err := hc.SetEndpoints([]string{"http://127.0.0.1:2379"}) - if err != nil { - t.Fatalf("unexpected error during setup: %#v", err) - } + require.NoErrorf(t, err, "unexpected error during setup") want := []string{"http://127.0.0.1:2379"} got := hc.Endpoints() @@ -840,9 +828,7 @@ func TestHTTPClusterClientSyncFail(t *testing.T) { rand: rand.New(rand.NewSource(0)), } err := hc.SetEndpoints([]string{"http://127.0.0.1:2379"}) - if err != nil { - t.Fatalf("unexpected error during setup: %#v", err) - } + require.NoErrorf(t, err, "unexpected error during setup") want := []string{"http://127.0.0.1:2379"} got := hc.Endpoints() @@ -851,9 +837,7 @@ func TestHTTPClusterClientSyncFail(t *testing.T) { } err = hc.Sync(context.Background()) - if err == nil { - t.Fatalf("got nil error during Sync") - } + require.Errorf(t, err, "got nil error during Sync") got = hc.Endpoints() if !reflect.DeepEqual(want, got) { @@ -874,9 +858,7 @@ func TestHTTPClusterClientAutoSyncCancelContext(t *testing.T) { rand: rand.New(rand.NewSource(0)), } err := hc.SetEndpoints([]string{"http://127.0.0.1:2379"}) - if err != nil { - t.Fatalf("unexpected error during setup: %#v", err) - } + require.NoErrorf(t, err, "unexpected error during setup") ctx, cancel := context.WithCancel(context.Background()) cancel() @@ -896,9 +878,7 @@ func TestHTTPClusterClientAutoSyncFail(t *testing.T) { rand: rand.New(rand.NewSource(0)), } err := hc.SetEndpoints([]string{"http://127.0.0.1:2379"}) - if err != nil { - t.Fatalf("unexpected error during setup: %#v", err) - } + require.NoErrorf(t, err, "unexpected error during setup") err = hc.AutoSync(context.Background(), time.Hour) if !strings.HasPrefix(err.Error(), ErrClusterUnavailable.Error()) { @@ -920,10 +900,7 @@ func TestHTTPClusterClientGetVersion(t *testing.T) { rand: rand.New(rand.NewSource(0)), } err := hc.SetEndpoints([]string{"http://127.0.0.1:4003", "http://127.0.0.1:2379", "http://127.0.0.1:4001", "http://127.0.0.1:4002"}) - if err != nil { - t.Fatalf("unexpected error during setup: %#v", err) - } - + require.NoErrorf(t, err, "unexpected error during setup") actual, err := hc.GetVersion(context.Background()) if err != nil { t.Errorf("non-nil error: %#v", err) @@ -957,16 +934,12 @@ func TestHTTPClusterClientSyncPinEndpoint(t *testing.T) { rand: rand.New(rand.NewSource(0)), } err := hc.SetEndpoints([]string{"http://127.0.0.1:4003", "http://127.0.0.1:2379", "http://127.0.0.1:4001", "http://127.0.0.1:4002"}) - if err != nil { - t.Fatalf("unexpected error during setup: %#v", err) - } + require.NoErrorf(t, err, "unexpected error during setup") pinnedEndpoint := hc.endpoints[hc.pinned] for i := 0; i < 3; i++ { err = hc.Sync(context.Background()) - if err != nil { - t.Fatalf("#%d: unexpected error during Sync: %#v", i, err) - } + require.NoErrorf(t, err, "#%d: unexpected error during Sync", i) if g := hc.endpoints[hc.pinned]; g != pinnedEndpoint { t.Errorf("#%d: pinned endpoint = %v, want %v", i, g, pinnedEndpoint) @@ -997,16 +970,12 @@ func TestHTTPClusterClientSyncUnpinEndpoint(t *testing.T) { rand: rand.New(rand.NewSource(0)), } err := hc.SetEndpoints([]string{"http://127.0.0.1:4003", "http://127.0.0.1:2379", "http://127.0.0.1:4001", "http://127.0.0.1:4002"}) - if err != nil { - t.Fatalf("unexpected error during setup: %#v", err) - } + require.NoErrorf(t, err, "unexpected error during setup") wants := []string{"http://127.0.0.1:2379", "http://127.0.0.1:4001", "http://127.0.0.1:4002"} for i := 0; i < 3; i++ { err = hc.Sync(context.Background()) - if err != nil { - t.Fatalf("#%d: unexpected error during Sync: %#v", i, err) - } + require.NoErrorf(t, err, "#%d: unexpected error during Sync: %#v", i) if g := hc.endpoints[hc.pinned]; g.String() != wants[i] { t.Errorf("#%d: pinned endpoint = %v, want %v", i, g, wants[i]) @@ -1047,9 +1016,7 @@ func TestHTTPClusterClientSyncPinLeaderEndpoint(t *testing.T) { for i, want := range wants { err := hc.Sync(context.Background()) - if err != nil { - t.Fatalf("#%d: unexpected error during Sync: %#v", i, err) - } + require.NoErrorf(t, err, "#%d: unexpected error during Sync: %#v", i) pinned := hc.endpoints[hc.pinned].String() if pinned != want { @@ -1082,9 +1049,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"}) - if err != nil { - t.Fatalf("#%d: reset error (%v)", i, err) - } + require.NoErrorf(t, err, "#%d: reset error (%v)", i) if hc.endpoints[hc.pinned].String() == "http://127.0.0.1:4001" { pinNum++ } diff --git a/client/internal/v2/members_test.go b/client/internal/v2/members_test.go index 1ad0c4c40240..9aafa6641f9a 100644 --- a/client/internal/v2/members_test.go +++ b/client/internal/v2/members_test.go @@ -23,6 +23,7 @@ import ( "reflect" "testing" + "github.com/stretchr/testify/require" "go.etcd.io/etcd/client/pkg/v3/types" ) @@ -312,9 +313,7 @@ func TestMemberCreateRequestMarshal(t *testing.T) { want := []byte(`{"peerURLs":["http://127.0.0.1:8081","https://127.0.0.1:8080"]}`) got, err := json.Marshal(&req) - if err != nil { - t.Fatalf("Marshal returned unexpected err=%v", err) - } + require.NoErrorf(t, err, "Marshal returned unexpected err") if !reflect.DeepEqual(want, got) { t.Fatalf("Failed to marshal memberCreateRequest: want=%s, got=%s", want, got) diff --git a/client/pkg/fileutil/fileutil_test.go b/client/pkg/fileutil/fileutil_test.go index f6b22e55de6b..e51b7af5aa59 100644 --- a/client/pkg/fileutil/fileutil_test.go +++ b/client/pkg/fileutil/fileutil_test.go @@ -27,17 +27,14 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" ) func TestIsDirWriteable(t *testing.T) { tmpdir := t.TempDir() - if err := IsDirWriteable(tmpdir); err != nil { - t.Fatalf("unexpected IsDirWriteable error: %v", err) - } - if err := os.Chmod(tmpdir, 0444); err != nil { - t.Fatalf("unexpected os.Chmod error: %v", err) - } + require.NoErrorf(t, IsDirWriteable(tmpdir), "unexpected IsDirWriteable error") + require.NoErrorf(t, os.Chmod(tmpdir, 0444), "unexpected os.Chmod error") me, err := user.Current() if err != nil { // err can be non-nil when cross compiled @@ -59,13 +56,9 @@ func TestCreateDirAll(t *testing.T) { tmpdir := t.TempDir() tmpdir2 := filepath.Join(tmpdir, "testdir") - if err := CreateDirAll(zaptest.NewLogger(t), tmpdir2); err != nil { - t.Fatal(err) - } + require.NoError(t, CreateDirAll(zaptest.NewLogger(t), tmpdir2)) - if err := os.WriteFile(filepath.Join(tmpdir2, "text.txt"), []byte("test text"), PrivateFileMode); err != nil { - t.Fatal(err) - } + require.NoError(t, os.WriteFile(filepath.Join(tmpdir2, "text.txt"), []byte("test text"), PrivateFileMode)) if err := CreateDirAll(zaptest.NewLogger(t), tmpdir2); err == nil || !strings.Contains(err.Error(), "to be empty, got") { t.Fatalf("unexpected error %v", err) @@ -84,9 +77,7 @@ func TestExist(t *testing.T) { } f, err := os.CreateTemp(os.TempDir(), "fileutil") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) f.Close() if g := Exist(f.Name()); !g { @@ -107,9 +98,7 @@ func TestDirEmpty(t *testing.T) { } file, err := os.CreateTemp(dir, "new_file") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) file.Close() if DirEmpty(dir) { @@ -122,42 +111,31 @@ func TestDirEmpty(t *testing.T) { func TestZeroToEnd(t *testing.T) { f, err := os.CreateTemp(os.TempDir(), "fileutil") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) defer os.Remove(f.Name()) defer f.Close() // Ensure 0 size is a nop so zero-to-end on an empty file won't give EINVAL. - if err = ZeroToEnd(f); err != nil { - t.Fatal(err) - } + require.NoError(t, ZeroToEnd(f)) b := make([]byte, 1024) for i := range b { b[i] = 12 } - if _, err = f.Write(b); err != nil { - t.Fatal(err) - } - if _, err = f.Seek(512, io.SeekStart); err != nil { - t.Fatal(err) - } - if err = ZeroToEnd(f); err != nil { - t.Fatal(err) - } + _, err = f.Write(b) + require.NoError(t, err) + _, err = f.Seek(512, io.SeekStart) + require.NoError(t, err) + require.NoError(t, ZeroToEnd(f)) off, serr := f.Seek(0, io.SeekCurrent) - if serr != nil { - t.Fatal(serr) - } + require.NoError(t, serr) if off != 512 { t.Fatalf("expected offset 512, got %d", off) } b = make([]byte, 512) - if _, err = f.Read(b); err != nil { - t.Fatal(err) - } + _, err = f.Read(b) + require.NoError(t, err) for i := range b { if b[i] != 0 { t.Errorf("expected b[%d] = 0, got %d", i, b[i]) @@ -170,9 +148,7 @@ func TestDirPermission(t *testing.T) { tmpdir2 := filepath.Join(tmpdir, "testpermission") // create a new dir with 0700 - if err := CreateDirAll(zaptest.NewLogger(t), tmpdir2); err != nil { - t.Fatal(err) - } + require.NoError(t, CreateDirAll(zaptest.NewLogger(t), tmpdir2)) // check dir permission with mode different than created dir if err := CheckDirPermission(tmpdir2, 0600); err == nil { t.Errorf("expected error, got nil") @@ -182,14 +158,10 @@ func TestDirPermission(t *testing.T) { func TestRemoveMatchFile(t *testing.T) { tmpdir := t.TempDir() f, err := os.CreateTemp(tmpdir, "tmp") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) f.Close() f, err = os.CreateTemp(tmpdir, "foo.tmp") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) f.Close() err = RemoveMatchFile(zaptest.NewLogger(t), tmpdir, func(fileName string) bool { @@ -199,17 +171,13 @@ func TestRemoveMatchFile(t *testing.T) { t.Errorf("expected nil, got error") } fnames, err := ReadDir(tmpdir) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) if len(fnames) != 1 { t.Errorf("expected exist 1 files, got %d", len(fnames)) } f, err = os.CreateTemp(tmpdir, "tmp") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) f.Close() err = RemoveMatchFile(zaptest.NewLogger(t), tmpdir, func(fileName string) bool { os.Remove(filepath.Join(tmpdir, fileName)) @@ -226,7 +194,5 @@ func TestTouchDirAll(t *testing.T) { TouchDirAll(nil, tmpdir) }, "expected panic with nil log") - if err := TouchDirAll(zaptest.NewLogger(t), tmpdir); err != nil { - t.Fatal(err) - } + assert.NoError(t, TouchDirAll(zaptest.NewLogger(t), tmpdir)) } diff --git a/client/pkg/fileutil/lock_test.go b/client/pkg/fileutil/lock_test.go index b7f6fd5ce57b..0c5da9855ad7 100644 --- a/client/pkg/fileutil/lock_test.go +++ b/client/pkg/fileutil/lock_test.go @@ -18,36 +18,28 @@ import ( "os" "testing" "time" + + "github.com/stretchr/testify/require" ) func TestLockAndUnlock(t *testing.T) { f, err := os.CreateTemp("", "lock") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) f.Close() defer func() { - err = os.Remove(f.Name()) - if err != nil { - t.Fatal(err) - } + require.NoError(t, os.Remove(f.Name())) }() // lock the file l, err := LockFile(f.Name(), os.O_WRONLY, PrivateFileMode) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) // try lock a locked file - if _, err = TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode); err != ErrLocked { - t.Fatal(err) - } + _, err = TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode) + require.ErrorIs(t, err, ErrLocked) // unlock the file - if err = l.Close(); err != nil { - t.Fatal(err) - } + require.NoError(t, l.Close()) // try lock the unlocked file dupl, err := TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode) @@ -75,9 +67,7 @@ func TestLockAndUnlock(t *testing.T) { } // unlock - if err = dupl.Close(); err != nil { - t.Fatal(err) - } + require.NoError(t, dupl.Close()) // the previously blocked routine should be unblocked select { diff --git a/client/pkg/fileutil/preallocate_test.go b/client/pkg/fileutil/preallocate_test.go index 47a006704b24..b738ac96d891 100644 --- a/client/pkg/fileutil/preallocate_test.go +++ b/client/pkg/fileutil/preallocate_test.go @@ -17,6 +17,8 @@ package fileutil import ( "os" "testing" + + "github.com/stretchr/testify/require" ) func TestPreallocateExtend(t *testing.T) { @@ -32,14 +34,10 @@ func TestPreallocateExtendTrunc(t *testing.T) { func testPreallocateExtend(t *testing.T, f *os.File, pf func(*os.File, int64) error) { size := int64(64 * 1000) - if err := pf(f, size); err != nil { - t.Fatal(err) - } + require.NoError(t, pf(f, size)) stat, err := f.Stat() - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) if stat.Size() != size { t.Errorf("size = %d, want %d", stat.Size(), size) } @@ -48,14 +46,10 @@ func testPreallocateExtend(t *testing.T, f *os.File, pf func(*os.File, int64) er func TestPreallocateFixed(t *testing.T) { runPreallocTest(t, testPreallocateFixed) } func testPreallocateFixed(t *testing.T, f *os.File) { size := int64(64 * 1000) - if err := Preallocate(f, size, false); err != nil { - t.Fatal(err) - } + require.NoError(t, Preallocate(f, size, false)) stat, err := f.Stat() - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) if stat.Size() != 0 { t.Errorf("size = %d, want %d", stat.Size(), 0) } @@ -65,8 +59,6 @@ func runPreallocTest(t *testing.T, test func(*testing.T, *os.File)) { p := t.TempDir() f, err := os.CreateTemp(p, "") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) test(t, f) } diff --git a/client/pkg/fileutil/purge_test.go b/client/pkg/fileutil/purge_test.go index c284386ef5b6..8f2f5fd037ac 100644 --- a/client/pkg/fileutil/purge_test.go +++ b/client/pkg/fileutil/purge_test.go @@ -22,6 +22,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" ) @@ -31,9 +32,7 @@ func TestPurgeFile(t *testing.T) { // minimal file set for i := 0; i < 3; i++ { f, ferr := os.Create(filepath.Join(dir, fmt.Sprintf("%d.test", i))) - if ferr != nil { - t.Fatal(ferr) - } + require.NoError(t, ferr) f.Close() } @@ -68,9 +67,7 @@ func TestPurgeFile(t *testing.T) { } fnames, rerr := ReadDir(dir) - if rerr != nil { - t.Fatal(rerr) - } + require.NoError(t, rerr) wnames := []string{"7.test", "8.test", "9.test"} if !reflect.DeepEqual(fnames, wnames) { t.Errorf("filenames = %v, want %v", fnames, wnames) @@ -93,18 +90,14 @@ func TestPurgeFileHoldingLockFile(t *testing.T) { for i := 0; i < 10; i++ { var f *os.File f, err := os.Create(filepath.Join(dir, fmt.Sprintf("%d.test", i))) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) f.Close() } // create a purge barrier at 5 p := filepath.Join(dir, fmt.Sprintf("%d.test", 5)) l, err := LockFile(p, os.O_WRONLY, PrivateFileMode) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) stop, purgec := make(chan struct{}), make(chan string, 10) errch := purgeFile(zaptest.NewLogger(t), dir, "test", 3, time.Millisecond, stop, purgec, nil, true) @@ -118,9 +111,7 @@ func TestPurgeFileHoldingLockFile(t *testing.T) { } fnames, rerr := ReadDir(dir) - if rerr != nil { - t.Fatal(rerr) - } + require.NoError(t, rerr) wnames := []string{"5.test", "6.test", "7.test", "8.test", "9.test"} if !reflect.DeepEqual(fnames, wnames) { @@ -136,9 +127,7 @@ func TestPurgeFileHoldingLockFile(t *testing.T) { } // remove the purge barrier - if err = l.Close(); err != nil { - t.Fatal(err) - } + require.NoError(t, l.Close()) // wait for rest of purges (5, 6) for i := 0; i < 2; i++ { @@ -150,9 +139,7 @@ func TestPurgeFileHoldingLockFile(t *testing.T) { } fnames, rerr = ReadDir(dir) - if rerr != nil { - t.Fatal(rerr) - } + require.NoError(t, rerr) wnames = []string{"7.test", "8.test", "9.test"} if !reflect.DeepEqual(fnames, wnames) { t.Errorf("filenames = %v, want %v", fnames, wnames) diff --git a/client/pkg/fileutil/read_dir_test.go b/client/pkg/fileutil/read_dir_test.go index 79a37d886cad..81454ad56030 100644 --- a/client/pkg/fileutil/read_dir_test.go +++ b/client/pkg/fileutil/read_dir_test.go @@ -19,6 +19,9 @@ import ( "path/filepath" "reflect" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestReadDir(t *testing.T) { @@ -29,9 +32,7 @@ func TestReadDir(t *testing.T) { writeFunc(t, filepath.Join(tmpdir, f)) } fs, err := ReadDir(tmpdir) - if err != nil { - t.Fatalf("error calling ReadDir: %v", err) - } + require.NoErrorf(t, err, "error calling ReadDir") wfs := []string{"abc", "def", "ghi", "xyz"} if !reflect.DeepEqual(fs, wfs) { t.Fatalf("ReadDir: got %v, want %v", fs, wfs) @@ -42,9 +43,7 @@ func TestReadDir(t *testing.T) { writeFunc(t, filepath.Join(tmpdir, f)) } fs, err = ReadDir(tmpdir, WithExt(".wal")) - if err != nil { - t.Fatalf("error calling ReadDir: %v", err) - } + require.NoErrorf(t, err, "error calling ReadDir") wfs = []string{"abc.wal", "def.wal", "ghi.wal", "xyz.wal"} if !reflect.DeepEqual(fs, wfs) { t.Fatalf("ReadDir: got %v, want %v", fs, wfs) @@ -53,10 +52,6 @@ func TestReadDir(t *testing.T) { func writeFunc(t *testing.T, path string) { fh, err := os.Create(path) - if err != nil { - t.Fatalf("error creating file: %v", err) - } - if err = fh.Close(); err != nil { - t.Fatalf("error closing file: %v", err) - } + require.NoErrorf(t, err, "error creating file") + assert.NoErrorf(t, fh.Close(), "error closing file") } diff --git a/client/pkg/transport/keepalive_listener_test.go b/client/pkg/transport/keepalive_listener_test.go index cca7436e9242..f49d832c3b05 100644 --- a/client/pkg/transport/keepalive_listener_test.go +++ b/client/pkg/transport/keepalive_listener_test.go @@ -19,6 +19,8 @@ import ( "net" "net/http" "testing" + + "github.com/stretchr/testify/require" ) // TestNewKeepAliveListener tests NewKeepAliveListener returns a listener @@ -26,20 +28,14 @@ import ( // TODO: verify the keepalive option is set correctly func TestNewKeepAliveListener(t *testing.T) { ln, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatalf("unexpected listen error: %v", err) - } + require.NoErrorf(t, err, "unexpected listen error") ln, err = NewKeepAliveListener(ln, "http", nil) - if err != nil { - t.Fatalf("unexpected NewKeepAliveListener error: %v", err) - } + require.NoErrorf(t, err, "unexpected NewKeepAliveListener error") go http.Get("http://" + ln.Addr().String()) conn, err := ln.Accept() - if err != nil { - t.Fatalf("unexpected Accept error: %v", err) - } + require.NoErrorf(t, err, "unexpected Accept error") if _, ok := conn.(*keepAliveConn); !ok { t.Fatalf("Unexpected conn type: %T, wanted *keepAliveConn", conn) } @@ -47,31 +43,21 @@ func TestNewKeepAliveListener(t *testing.T) { ln.Close() ln, err = net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatalf("unexpected Listen error: %v", err) - } + require.NoErrorf(t, err, "unexpected Listen error") // tls tlsinfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create tmpfile: %v", err) - } + require.NoErrorf(t, err, "unable to create tmpfile") tlsInfo := TLSInfo{CertFile: tlsinfo.CertFile, KeyFile: tlsinfo.KeyFile} tlsInfo.parseFunc = fakeCertificateParserFunc(nil) tlscfg, err := tlsInfo.ServerConfig() - if err != nil { - t.Fatalf("unexpected serverConfig error: %v", err) - } + require.NoErrorf(t, err, "unexpected serverConfig error") tlsln, err := NewKeepAliveListener(ln, "https", tlscfg) - if err != nil { - t.Fatalf("unexpected NewKeepAliveListener error: %v", err) - } + require.NoErrorf(t, err, "unexpected NewKeepAliveListener error") go http.Get("https://" + tlsln.Addr().String()) conn, err = tlsln.Accept() - if err != nil { - t.Fatalf("unexpected Accept error: %v", err) - } + require.NoErrorf(t, err, "unexpected Accept error") if _, ok := conn.(*tls.Conn); !ok { t.Errorf("failed to accept *tls.Conn") } @@ -81,9 +67,7 @@ func TestNewKeepAliveListener(t *testing.T) { func TestNewKeepAliveListenerTLSEmptyConfig(t *testing.T) { ln, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatalf("unexpected listen error: %v", err) - } + require.NoErrorf(t, err, "unexpected listen error") _, err = NewKeepAliveListener(ln, "https", nil) if err == nil { diff --git a/client/pkg/transport/listener_test.go b/client/pkg/transport/listener_test.go index 8187c8c6fd09..84e43a27115b 100644 --- a/client/pkg/transport/listener_test.go +++ b/client/pkg/transport/listener_test.go @@ -30,6 +30,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" ) @@ -56,17 +57,13 @@ func fakeCertificateParserFunc(err error) func(certPEMBlock, keyPEMBlock []byte) // a TLS listener that accepts TLS connections. func TestNewListenerTLSInfo(t *testing.T) { tlsInfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create cert: %v", err) - } + require.NoErrorf(t, err, "unable to create cert") testNewListenerTLSInfoAccept(t, *tlsInfo) } func TestNewListenerWithOpts(t *testing.T) { tlsInfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create cert: %v", err) - } + require.NoErrorf(t, err, "unable to create cert") tests := map[string]struct { opts []ListenerOption @@ -125,9 +122,7 @@ func TestNewListenerWithOpts(t *testing.T) { func TestNewListenerWithSocketOpts(t *testing.T) { tlsInfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create cert: %v", err) - } + require.NoErrorf(t, err, "unable to create cert") tests := map[string]struct { opts []ListenerOption @@ -197,9 +192,7 @@ func TestNewListenerWithSocketOpts(t *testing.T) { for testName, test := range tests { t.Run(testName, func(t *testing.T) { ln, err := NewListenerWithOpts("127.0.0.1:0", test.scheme, test.opts...) - if err != nil { - t.Fatalf("unexpected NewListenerWithSocketOpts error: %v", err) - } + require.NoErrorf(t, err, "unexpected NewListenerWithSocketOpts error") defer ln.Close() ln2, err := NewListenerWithOpts(ln.Addr().String(), test.scheme, test.opts...) if ln2 != nil { @@ -226,9 +219,7 @@ func TestNewListenerWithSocketOpts(t *testing.T) { func testNewListenerTLSInfoAccept(t *testing.T, tlsInfo TLSInfo) { ln, err := NewListener("127.0.0.1:0", "https", &tlsInfo) - if err != nil { - t.Fatalf("unexpected NewListener error: %v", err) - } + require.NoErrorf(t, err, "unexpected NewListener error") defer ln.Close() tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} @@ -236,9 +227,7 @@ func testNewListenerTLSInfoAccept(t *testing.T, tlsInfo TLSInfo) { go cli.Get("https://" + ln.Addr().String()) conn, err := ln.Accept() - if err != nil { - t.Fatalf("unexpected Accept error: %v", err) - } + require.NoErrorf(t, err, "unexpected Accept error") defer conn.Close() if _, ok := conn.(*tls.Conn); !ok { t.Error("failed to accept *tls.Conn") @@ -266,33 +255,25 @@ func TestNewListenerTLSInfoSkipClientSANVerify(t *testing.T) { func testNewListenerTLSInfoClientCheck(t *testing.T, skipClientSANVerify, goodClientHost, acceptExpected bool) { tlsInfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create cert: %v", err) - } + require.NoErrorf(t, err, "unable to create cert") host := "127.0.0.222" if goodClientHost { host = "127.0.0.1" } clientTLSInfo, err := createSelfCertEx(t, host, x509.ExtKeyUsageClientAuth) - if err != nil { - t.Fatalf("unable to create cert: %v", err) - } + require.NoErrorf(t, err, "unable to create cert") tlsInfo.SkipClientSANVerify = skipClientSANVerify tlsInfo.TrustedCAFile = clientTLSInfo.CertFile rootCAs := x509.NewCertPool() loaded, err := os.ReadFile(tlsInfo.CertFile) - if err != nil { - t.Fatalf("unexpected missing certfile: %v", err) - } + require.NoErrorf(t, err, "unexpected missing certfile") rootCAs.AppendCertsFromPEM(loaded) clientCert, err := tls.LoadX509KeyPair(clientTLSInfo.CertFile, clientTLSInfo.KeyFile) - if err != nil { - t.Fatalf("unable to create peer cert: %v", err) - } + require.NoErrorf(t, err, "unable to create peer cert") tlsConfig := &tls.Config{} tlsConfig.InsecureSkipVerify = false @@ -300,9 +281,7 @@ func testNewListenerTLSInfoClientCheck(t *testing.T, skipClientSANVerify, goodCl tlsConfig.RootCAs = rootCAs ln, err := NewListener("127.0.0.1:0", "https", tlsInfo) - if err != nil { - t.Fatalf("unexpected NewListener error: %v", err) - } + require.NoErrorf(t, err, "unexpected NewListener error") defer ln.Close() tr := &http.Transport{TLSClientConfig: tlsConfig} @@ -351,9 +330,7 @@ func TestNewListenerTLSEmptyInfo(t *testing.T) { func TestNewTransportTLSInfo(t *testing.T) { tlsinfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create cert: %v", err) - } + require.NoErrorf(t, err, "unable to create cert") tests := []TLSInfo{ {}, @@ -374,9 +351,7 @@ func TestNewTransportTLSInfo(t *testing.T) { for i, tt := range tests { tt.parseFunc = fakeCertificateParserFunc(nil) trans, err := NewTransport(tt, time.Second) - if err != nil { - t.Fatalf("Received unexpected error from NewTransport: %v", err) - } + require.NoErrorf(t, err, "Received unexpected error from NewTransport") if trans.TLSClientConfig == nil { t.Fatalf("#%d: want non-nil TLSClientConfig", i) @@ -422,9 +397,7 @@ func TestTLSInfoEmpty(t *testing.T) { func TestTLSInfoMissingFields(t *testing.T) { tlsinfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create cert: %v", err) - } + require.NoErrorf(t, err, "unable to create cert") tests := []TLSInfo{ {CertFile: tlsinfo.CertFile}, @@ -446,9 +419,7 @@ func TestTLSInfoMissingFields(t *testing.T) { func TestTLSInfoParseFuncError(t *testing.T) { tlsinfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create cert: %v", err) - } + require.NoErrorf(t, err, "unable to create cert") tests := []struct { info TLSInfo @@ -478,9 +449,7 @@ func TestTLSInfoParseFuncError(t *testing.T) { func TestTLSInfoConfigFuncs(t *testing.T) { ln := zaptest.NewLogger(t) tlsinfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create cert: %v", err) - } + require.NoErrorf(t, err, "unable to create cert") tests := []struct { info TLSInfo @@ -536,9 +505,7 @@ func TestNewListenerTLSInfoSelfCert(t *testing.T) { tmpdir := t.TempDir() tlsinfo, err := SelfCert(zaptest.NewLogger(t), tmpdir, []string{"127.0.0.1"}, 1) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) if tlsinfo.Empty() { t.Fatalf("tlsinfo should have certs (%+v)", tlsinfo) } @@ -582,23 +549,17 @@ func TestSocktOptsEmpty(t *testing.T) { // TestNewListenerWithACRLFile tests when a revocation list is present. func TestNewListenerWithACRLFile(t *testing.T) { clientTLSInfo, err := createSelfCertEx(t, "127.0.0.1", x509.ExtKeyUsageClientAuth) - if err != nil { - t.Fatalf("unable to create client cert: %v", err) - } + require.NoErrorf(t, err, "unable to create client cert") loadFileAsPEM := func(fileName string) []byte { loaded, readErr := os.ReadFile(fileName) - if readErr != nil { - t.Fatalf("unable to read file %q: %v", fileName, readErr) - } + require.NoErrorf(t, readErr, "unable to read file %q", fileName) block, _ := pem.Decode(loaded) return block.Bytes } clientCert, err := x509.ParseCertificate(loadFileAsPEM(clientTLSInfo.CertFile)) - if err != nil { - t.Fatalf("unable to parse client cert: %v", err) - } + require.NoErrorf(t, err, "unable to parse client cert") tests := map[string]struct { expectHandshakeError bool @@ -627,21 +588,15 @@ func TestNewListenerWithACRLFile(t *testing.T) { t.Run(testName, func(t *testing.T) { tmpdir := t.TempDir() tlsInfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create server cert: %v", err) - } + require.NoErrorf(t, err, "unable to create server cert") tlsInfo.TrustedCAFile = clientTLSInfo.CertFile tlsInfo.CRLFile = filepath.Join(tmpdir, "revoked.r0") cert, err := x509.ParseCertificate(loadFileAsPEM(tlsInfo.CertFile)) - if err != nil { - t.Fatalf("unable to decode server cert: %v", err) - } + require.NoErrorf(t, err, "unable to decode server cert") key, err := x509.ParseECPrivateKey(loadFileAsPEM(tlsInfo.KeyFile)) - if err != nil { - t.Fatalf("unable to parse server key: %v", err) - } + require.NoErrorf(t, err, "unable to parse server key") revocationListContents := test.revocationListContents if len(revocationListContents) == 0 { @@ -652,14 +607,11 @@ func TestNewListenerWithACRLFile(t *testing.T) { Number: big.NewInt(1), } revocationListContents, err = x509.CreateRevocationList(rand.Reader, tmpl, cert, key) - if err != nil { - t.Fatalf("unable to create revocation list: %v", err) - } + require.NoErrorf(t, err, "unable to create revocation list") } - if err = os.WriteFile(tlsInfo.CRLFile, revocationListContents, 0600); err != nil { - t.Fatalf("unable to write revocation list: %v", err) - } + err = os.WriteFile(tlsInfo.CRLFile, revocationListContents, 0600) + require.NoErrorf(t, err, "unable to write revocation list") chHandshakeFailure := make(chan error, 1) tlsInfo.HandshakeFailure = func(_ *tls.Conn, err error) { @@ -672,14 +624,10 @@ func TestNewListenerWithACRLFile(t *testing.T) { rootCAs.AddCert(cert) clientCert, err := tls.LoadX509KeyPair(clientTLSInfo.CertFile, clientTLSInfo.KeyFile) - if err != nil { - t.Fatalf("unable to create peer cert: %v", err) - } + require.NoErrorf(t, err, "unable to create peer cert") ln, err := NewListener("127.0.0.1:0", "https", tlsInfo) - if err != nil { - t.Fatalf("unable to start listener: %v", err) - } + require.NoErrorf(t, err, "unable to start listener") tlsConfig := &tls.Config{} tlsConfig.InsecureSkipVerify = false diff --git a/client/pkg/transport/timeout_dialer_test.go b/client/pkg/transport/timeout_dialer_test.go index 854d68d14727..fe3993593b13 100644 --- a/client/pkg/transport/timeout_dialer_test.go +++ b/client/pkg/transport/timeout_dialer_test.go @@ -18,15 +18,15 @@ import ( "net" "testing" "time" + + "github.com/stretchr/testify/require" ) func TestReadWriteTimeoutDialer(t *testing.T) { stop := make(chan struct{}) ln, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatalf("unexpected listen error: %v", err) - } + require.NoErrorf(t, err, "unexpected listen error") defer func() { stop <- struct{}{} }() @@ -38,9 +38,7 @@ func TestReadWriteTimeoutDialer(t *testing.T) { rdtimeoutd: 10 * time.Millisecond, } conn, err := d.Dial("tcp", ln.Addr().String()) - if err != nil { - t.Fatalf("unexpected dial error: %v", err) - } + require.NoErrorf(t, err, "unexpected dial error") defer conn.Close() // fill the socket buffer @@ -64,9 +62,7 @@ func TestReadWriteTimeoutDialer(t *testing.T) { } conn, err = d.Dial("tcp", ln.Addr().String()) - if err != nil { - t.Fatalf("unexpected dial error: %v", err) - } + require.NoErrorf(t, err, "unexpected dial error") defer conn.Close() buf := make([]byte, 10) diff --git a/client/pkg/transport/timeout_listener_test.go b/client/pkg/transport/timeout_listener_test.go index 828ddf8620f9..036cfd71c44a 100644 --- a/client/pkg/transport/timeout_listener_test.go +++ b/client/pkg/transport/timeout_listener_test.go @@ -18,15 +18,15 @@ import ( "net" "testing" "time" + + "github.com/stretchr/testify/require" ) // TestNewTimeoutListener tests that NewTimeoutListener returns a // rwTimeoutListener struct with timeouts set. func TestNewTimeoutListener(t *testing.T) { l, err := NewTimeoutListener("127.0.0.1:0", "http", nil, time.Hour, time.Hour) - if err != nil { - t.Fatalf("unexpected NewTimeoutListener error: %v", err) - } + require.NoErrorf(t, err, "unexpected NewTimeoutListener error") defer l.Close() tln := l.(*rwTimeoutListener) if tln.readTimeout != time.Hour { @@ -39,9 +39,7 @@ func TestNewTimeoutListener(t *testing.T) { func TestWriteReadTimeoutListener(t *testing.T) { ln, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatalf("unexpected listen error: %v", err) - } + require.NoErrorf(t, err, "unexpected listen error") wln := rwTimeoutListener{ Listener: ln, writeTimeout: 10 * time.Millisecond, diff --git a/client/pkg/transport/timeout_transport_test.go b/client/pkg/transport/timeout_transport_test.go index 0dac495ab9a2..9948d2ac54ba 100644 --- a/client/pkg/transport/timeout_transport_test.go +++ b/client/pkg/transport/timeout_transport_test.go @@ -21,15 +21,15 @@ import ( "net/http/httptest" "testing" "time" + + "github.com/stretchr/testify/require" ) // TestNewTimeoutTransport tests that NewTimeoutTransport returns a transport // that can dial out timeout connections. func TestNewTimeoutTransport(t *testing.T) { tr, err := NewTimeoutTransport(TLSInfo{}, time.Hour, time.Hour, time.Hour) - if err != nil { - t.Fatalf("unexpected NewTimeoutTransport error: %v", err) - } + require.NoError(t, err) remoteAddr := func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(r.RemoteAddr)) @@ -38,9 +38,7 @@ func TestNewTimeoutTransport(t *testing.T) { defer srv.Close() conn, err := tr.Dial("tcp", srv.Listener.Addr().String()) - if err != nil { - t.Fatalf("unexpected dial error: %v", err) - } + require.NoError(t, err) defer conn.Close() tconn, ok := conn.(*timeoutConn) @@ -56,28 +54,18 @@ func TestNewTimeoutTransport(t *testing.T) { // ensure not reuse timeout connection req, err := http.NewRequest("GET", srv.URL, nil) - if err != nil { - t.Fatalf("unexpected err %v", err) - } + require.NoError(t, err) resp, err := tr.RoundTrip(req) - if err != nil { - t.Fatalf("unexpected err %v", err) - } + require.NoError(t, err) addr0, err := io.ReadAll(resp.Body) resp.Body.Close() - if err != nil { - t.Fatalf("unexpected err %v", err) - } + require.NoError(t, err) resp, err = tr.RoundTrip(req) - if err != nil { - t.Fatalf("unexpected err %v", err) - } + require.NoError(t, err) addr1, err := io.ReadAll(resp.Body) resp.Body.Close() - if err != nil { - t.Fatalf("unexpected err %v", err) - } + require.NoError(t, err) if bytes.Equal(addr0, addr1) { t.Errorf("addr0 = %s addr1= %s, want not equal", addr0, addr1) diff --git a/client/pkg/transport/tls_test.go b/client/pkg/transport/tls_test.go index 46af1db6786c..06014ad56fee 100644 --- a/client/pkg/transport/tls_test.go +++ b/client/pkg/transport/tls_test.go @@ -19,13 +19,13 @@ import ( "net/http/httptest" "reflect" "testing" + + "github.com/stretchr/testify/require" ) func TestValidateSecureEndpoints(t *testing.T) { tlsInfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create cert: %v", err) - } + require.Errorf(t, err, "unable to create cert") remoteAddr := func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(r.RemoteAddr)) diff --git a/client/pkg/transport/transport_test.go b/client/pkg/transport/transport_test.go index 315f32cf2dca..8d3e66cb36a3 100644 --- a/client/pkg/transport/transport_test.go +++ b/client/pkg/transport/transport_test.go @@ -20,15 +20,15 @@ import ( "strings" "testing" "time" + + "github.com/stretchr/testify/require" ) // TestNewTransportTLSInvalidCipherSuitesTLS12 expects a client with invalid // cipher suites fail to handshake with the server. func TestNewTransportTLSInvalidCipherSuitesTLS12(t *testing.T) { tlsInfo, err := createSelfCert(t) - if err != nil { - t.Fatalf("unable to create cert: %v", err) - } + require.NoErrorf(t, err, "unable to create cert") cipherSuites := []uint16{ tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, @@ -44,9 +44,7 @@ func TestNewTransportTLSInvalidCipherSuitesTLS12(t *testing.T) { srvTLS.CipherSuites, cliTLS.CipherSuites = cipherSuites[:2], cipherSuites[2:] ln, err := NewListener("127.0.0.1:0", "https", &srvTLS) - if err != nil { - t.Fatalf("unexpected NewListener error: %v", err) - } + require.NoError(t, err) defer ln.Close() donec := make(chan struct{}) diff --git a/client/pkg/types/id_test.go b/client/pkg/types/id_test.go index bec2853432bb..6759b119ac15 100644 --- a/client/pkg/types/id_test.go +++ b/client/pkg/types/id_test.go @@ -18,6 +18,8 @@ import ( "reflect" "sort" "testing" + + "github.com/stretchr/testify/require" ) func TestIDString(t *testing.T) { @@ -79,9 +81,7 @@ func TestIDFromStringFail(t *testing.T) { for i, tt := range tests { _, err := IDFromString(tt) - if err == nil { - t.Fatalf("#%d: IDFromString expected error, but err=nil", i) - } + require.Errorf(t, err, "#%d: IDFromString expected error", i) } } diff --git a/client/pkg/types/urlsmap_test.go b/client/pkg/types/urlsmap_test.go index da184282e790..9f74b2841623 100644 --- a/client/pkg/types/urlsmap_test.go +++ b/client/pkg/types/urlsmap_test.go @@ -18,14 +18,14 @@ import ( "reflect" "testing" + "github.com/stretchr/testify/require" + "go.etcd.io/etcd/client/pkg/v3/testutil" ) func TestParseInitialCluster(t *testing.T) { c, err := NewURLsMap("mem1=http://10.0.0.1:2379,mem1=http://128.193.4.20:2379,mem2=http://10.0.0.2:2379,default=http://127.0.0.1:2379") - if err != nil { - t.Fatalf("unexpected parse error: %v", err) - } + require.NoError(t, err) wc := URLsMap(map[string]URLs{ "mem1": testutil.MustNewURLs(t, []string{"http://10.0.0.1:2379", "http://128.193.4.20:2379"}), "mem2": testutil.MustNewURLs(t, []string{"http://10.0.0.2:2379"}), @@ -102,9 +102,7 @@ func TestParse(t *testing.T) { // URI (https://github.com/golang/go/issues/6530). func TestNewURLsMapIPV6(t *testing.T) { c, err := NewURLsMap("mem1=http://[2001:db8::1]:2380,mem1=http://[fe80::6e40:8ff:feb1:58e4%25en0]:2380,mem2=http://[fe80::92e2:baff:fe7c:3224%25ext0]:2380") - if err != nil { - t.Fatalf("unexpected parse error: %v", err) - } + require.NoError(t, err) wc := URLsMap(map[string]URLs{ "mem1": testutil.MustNewURLs(t, []string{"http://[2001:db8::1]:2380", "http://[fe80::6e40:8ff:feb1:58e4%25en0]:2380"}), "mem2": testutil.MustNewURLs(t, []string{"http://[fe80::92e2:baff:fe7c:3224%25ext0]:2380"}), diff --git a/client/v3/client_test.go b/client/v3/client_test.go index 264c86beb6e3..686edc9bc910 100644 --- a/client/v3/client_test.go +++ b/client/v3/client_test.go @@ -49,9 +49,7 @@ func TestDialCancel(t *testing.T) { // accept first connection so client is created with dial timeout ln, err := net.Listen("unix", "dialcancel:12345") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) defer ln.Close() ep := "unix://dialcancel:12345" @@ -59,9 +57,7 @@ func TestDialCancel(t *testing.T) { Endpoints: []string{ep}, DialTimeout: 30 * time.Second} c, err := NewClient(t, cfg) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) // connect to ipv4 black hole so dial blocks c.SetEndpoints("http://254.0.0.1:12345") @@ -150,9 +146,10 @@ func TestDialTimeout(t *testing.T) { func TestDialNoTimeout(t *testing.T) { cfg := Config{Endpoints: []string{"127.0.0.1:12345"}} c, err := NewClient(t, cfg) - if c == nil || err != nil { + if c == nil { t.Fatalf("new client with DialNoWait should succeed, got %v", err) } + require.NoErrorf(t, err, "new client with DialNoWait should succeed") c.Close() } @@ -289,9 +286,7 @@ func TestAuthTokenBundleNoOverwrite(t *testing.T) { // Create a mock AuthServer to handle Authenticate RPCs. lis, err := net.Listen("unix", "etcd-auth-test:0") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) defer lis.Close() addr := "unix://" + lis.Addr().String() srv := grpc.NewServer() @@ -307,18 +302,14 @@ func TestAuthTokenBundleNoOverwrite(t *testing.T) { Username: "foo", Password: "bar", }) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) defer c.Close() oldTokenBundle := c.authTokenBundle // Call the public Dial again, which should preserve the original // authTokenBundle. gc, err := c.Dial(addr) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) defer gc.Close() newTokenBundle := c.authTokenBundle diff --git a/client/v3/config_test.go b/client/v3/config_test.go index a99c3fd5864f..20c175d9e081 100644 --- a/client/v3/config_test.go +++ b/client/v3/config_test.go @@ -20,6 +20,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.uber.org/zap" "go.etcd.io/etcd/client/pkg/v3/logutil" @@ -138,9 +139,7 @@ func TestNewClientConfig(t *testing.T) { lg, _ := logutil.CreateDefaultZapLogger(zap.InfoLevel) cfg, err := NewClientConfig(&tc.spec, lg) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } + require.NoError(t, err) assert.Equal(t, tc.expectedConf, *cfg) }) @@ -149,9 +148,7 @@ func TestNewClientConfig(t *testing.T) { func TestNewClientConfigWithSecureCfg(t *testing.T) { tls, err := transport.SelfCert(zap.NewNop(), t.TempDir(), []string{"localhost"}, 1) - if err != nil { - t.Fatalf("Unexpected error: %v", err) - } + require.NoError(t, err) scfg := &SecureConfig{ Cert: tls.CertFile, @@ -166,7 +163,8 @@ func TestNewClientConfigWithSecureCfg(t *testing.T) { KeepAliveTimeout: 5 * time.Second, Secure: scfg, }, nil) - if err != nil || cfg == nil || cfg.TLS == nil { + require.NoErrorf(t, err, "Unexpected result client config") + if cfg == nil || cfg.TLS == nil { t.Fatalf("Unexpected result client config: %v", err) } } diff --git a/client/v3/yaml/config_test.go b/client/v3/yaml/config_test.go index ec8441b1b631..4d23f27494e1 100644 --- a/client/v3/yaml/config_test.go +++ b/client/v3/yaml/config_test.go @@ -20,6 +20,7 @@ import ( "reflect" "testing" + "github.com/stretchr/testify/require" "sigs.k8s.io/yaml" ) @@ -78,18 +79,11 @@ func TestConfigFromFile(t *testing.T) { } b, err := yaml.Marshal(tt.ym) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) _, err = tmpfile.Write(b) - if err != nil { - t.Fatal(err) - } - err = tmpfile.Close() - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) + require.NoError(t, tmpfile.Close()) cfg, cerr := NewConfig(tmpfile.Name()) if cerr != nil && !tt.werr {