diff --git a/client/pkg/fileutil/fileutil_test.go b/client/pkg/fileutil/fileutil_test.go index f6b22e55de6b..f0f51d03d197 100644 --- a/client/pkg/fileutil/fileutil_test.go +++ b/client/pkg/fileutil/fileutil_test.go @@ -27,6 +27,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" ) @@ -59,13 +60,11 @@ 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) - } + err := CreateDirAll(zaptest.NewLogger(t), tmpdir2) + require.NoError(t, err) - if err := os.WriteFile(filepath.Join(tmpdir2, "text.txt"), []byte("test text"), PrivateFileMode); err != nil { - t.Fatal(err) - } + err = os.WriteFile(filepath.Join(tmpdir2, "text.txt"), []byte("test text"), PrivateFileMode) + require.NoError(t, err) 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 +83,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 +104,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 +117,33 @@ 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) - } + err = ZeroToEnd(f) + require.NoError(t, err) 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) + err = ZeroToEnd(f) + require.NoError(t, err) 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 +156,8 @@ 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) - } + err := CreateDirAll(zaptest.NewLogger(t), tmpdir2) + require.NoError(t, err) // check dir permission with mode different than created dir if err := CheckDirPermission(tmpdir2, 0600); err == nil { t.Errorf("expected error, got nil") @@ -182,14 +167,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 +180,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 +203,6 @@ 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) - } + err := TouchDirAll(zaptest.NewLogger(t), tmpdir) + assert.NoError(t, err) } diff --git a/client/pkg/fileutil/lock_test.go b/client/pkg/fileutil/lock_test.go index b7f6fd5ce57b..d1eb052434de 100644 --- a/client/pkg/fileutil/lock_test.go +++ b/client/pkg/fileutil/lock_test.go @@ -18,36 +18,30 @@ 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, err) }() // 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) - } + err = l.Close() + require.NoError(t, err) // try lock the unlocked file dupl, err := TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode) @@ -75,9 +69,8 @@ func TestLockAndUnlock(t *testing.T) { } // unlock - if err = dupl.Close(); err != nil { - t.Fatal(err) - } + err = dupl.Close() + require.NoError(t, err) // 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..f90ccb8d16d6 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,11 @@ 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) - } + err := pf(f, size) + require.NoError(t, err) 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 +47,11 @@ 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) - } + err := Preallocate(f, size, false) + require.NoError(t, err) 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 +61,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..92944f1871e7 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,8 @@ func TestPurgeFileHoldingLockFile(t *testing.T) { } // remove the purge barrier - if err = l.Close(); err != nil { - t.Fatal(err) - } + err = l.Close() + require.NoError(t, err) // wait for rest of purges (5, 6) for i := 0; i < 2; i++ { @@ -150,9 +140,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/tlsutil/cipher_suites_test.go b/client/pkg/tlsutil/cipher_suites_test.go index e94c3ab0f565..92895c0bb72a 100644 --- a/client/pkg/tlsutil/cipher_suites_test.go +++ b/client/pkg/tlsutil/cipher_suites_test.go @@ -17,13 +17,13 @@ package tlsutil import ( "crypto/tls" "testing" + + "github.com/stretchr/testify/require" ) func TestGetCipherSuite_not_existing(t *testing.T) { _, ok := GetCipherSuite("not_existing") - if ok { - t.Fatal("Expected not ok") - } + require.Falsef(t, ok, "Expected not ok") } func CipherSuiteExpectedToExist(tb testing.TB, cipher string, expectedID uint16) { diff --git a/client/pkg/transport/listener_test.go b/client/pkg/transport/listener_test.go index 8187c8c6fd09..cc5d5816a6a3 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" ) @@ -536,9 +537,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) } diff --git a/client/v3/client_test.go b/client/v3/client_test.go index 264c86beb6e3..5a58cca0d613 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") @@ -289,9 +285,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 +301,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/ctx_test.go b/client/v3/ctx_test.go index 097b6a3e6b1f..23c49639f143 100644 --- a/client/v3/ctx_test.go +++ b/client/v3/ctx_test.go @@ -19,6 +19,7 @@ import ( "reflect" "testing" + "github.com/stretchr/testify/require" "google.golang.org/grpc/metadata" "go.etcd.io/etcd/api/v3/v3rpc/rpctypes" @@ -28,9 +29,7 @@ import ( func TestMetadataWithRequireLeader(t *testing.T) { ctx := context.TODO() _, ok := metadata.FromOutgoingContext(ctx) - if ok { - t.Fatal("expected no outgoing metadata ctx key") - } + require.Falsef(t, ok, "expected no outgoing metadata ctx key") // add a conflicting key with some other value md := metadata.Pairs(rpctypes.MetadataRequireLeaderKey, "invalid") @@ -41,9 +40,7 @@ func TestMetadataWithRequireLeader(t *testing.T) { // expect overwrites but still keep other keys ctx = WithRequireLeader(ctx) md, ok = metadata.FromOutgoingContext(ctx) - if !ok { - t.Fatal("expected outgoing metadata ctx key") - } + require.Truef(t, ok, "expected outgoing metadata ctx key") if ss := md.Get(rpctypes.MetadataRequireLeaderKey); !reflect.DeepEqual(ss, []string{rpctypes.MetadataHasLeader}) { t.Fatalf("unexpected metadata for %q %v", rpctypes.MetadataRequireLeaderKey, ss) } @@ -56,9 +53,7 @@ func TestMetadataWithClientAPIVersion(t *testing.T) { ctx := withVersion(WithRequireLeader(context.TODO())) md, ok := metadata.FromOutgoingContext(ctx) - if !ok { - t.Fatal("expected outgoing metadata ctx key") - } + require.Truef(t, ok, "expected outgoing metadata ctx key") if ss := md.Get(rpctypes.MetadataRequireLeaderKey); !reflect.DeepEqual(ss, []string{rpctypes.MetadataHasLeader}) { t.Fatalf("unexpected metadata for %q %v", rpctypes.MetadataRequireLeaderKey, ss) } diff --git a/client/v3/yaml/config_test.go b/client/v3/yaml/config_test.go index ec8441b1b631..4e912098d526 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,12 @@ 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) - } + require.NoError(t, err) err = tmpfile.Close() - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) cfg, cerr := NewConfig(tmpfile.Name()) if cerr != nil && !tt.werr {