diff --git a/pkg/adt/interval_tree_test.go b/pkg/adt/interval_tree_test.go index 1e8557078a7..7c9e8c69a30 100644 --- a/pkg/adt/interval_tree_test.go +++ b/pkg/adt/interval_tree_test.go @@ -18,6 +18,8 @@ import ( "math/rand" "reflect" "testing" + + "github.com/stretchr/testify/require" ) // TestIntervalTreeInsert tests interval tree insertion. @@ -53,9 +55,7 @@ func TestIntervalTreeInsert(t *testing.T) { tr := ivt.(*intervalTree) visits := tr.visitLevel() - if !reflect.DeepEqual(expected, visits) { - t.Fatalf("level order expected %v, got %v", expected, visits) - } + require.Truef(t, reflect.DeepEqual(expected, visits), "level order expected %v, got %v", expected, visits) } // TestIntervalTreeSelfBalanced ensures range tree is self-balanced after inserting ranges to the tree. @@ -107,13 +107,9 @@ func TestIntervalTreeSelfBalanced(t *testing.T) { tr := ivt.(*intervalTree) visits := tr.visitLevel() - if !reflect.DeepEqual(expected, visits) { - t.Fatalf("level order expected %v, got %v", expected, visits) - } + require.Truef(t, reflect.DeepEqual(expected, visits), "level order expected %v, got %v", expected, visits) - if visits[len(visits)-1].depth != 3 { - t.Fatalf("expected self-balanced tree with last level 3, but last level got %d", visits[len(visits)-1].depth) - } + require.Equalf(t, 3, visits[len(visits)-1].depth, "expected self-balanced tree with last level 3, but last level got %d", visits[len(visits)-1].depth) } // TestIntervalTreeDelete ensures delete operation maintains red-black tree properties. @@ -214,15 +210,11 @@ func TestIntervalTreeDelete(t *testing.T) { {root: NewInt64Interval(953, 954), color: red, left: newInt64EmptyInterval(), right: newInt64EmptyInterval(), depth: 4}, } visitsBeforeDelete := tr.visitLevel() - if !reflect.DeepEqual(expectedBeforeDelete, visitsBeforeDelete) { - t.Fatalf("level order after insertion expected %v, got %v", expectedBeforeDelete, visitsBeforeDelete) - } + require.Truef(t, reflect.DeepEqual(expectedBeforeDelete, visitsBeforeDelete), "level order after insertion expected %v, got %v", expectedBeforeDelete, visitsBeforeDelete) // delete the node "514" range514 := NewInt64Interval(514, 515) - if deleted := tr.Delete(NewInt64Interval(514, 515)); !deleted { - t.Fatalf("range %v not deleted", range514) - } + require.Truef(t, tr.Delete(NewInt64Interval(514, 515)), "range %v not deleted", range514) expectedAfterDelete514 := []visitedInterval{ {root: NewInt64Interval(510, 511), color: black, left: NewInt64Interval(82, 83), right: NewInt64Interval(830, 831), depth: 0}, @@ -246,15 +238,11 @@ func TestIntervalTreeDelete(t *testing.T) { {root: NewInt64Interval(953, 954), color: red, left: newInt64EmptyInterval(), right: newInt64EmptyInterval(), depth: 4}, } visitsAfterDelete514 := tr.visitLevel() - if !reflect.DeepEqual(expectedAfterDelete514, visitsAfterDelete514) { - t.Fatalf("level order after deleting '514' expected %v, got %v", expectedAfterDelete514, visitsAfterDelete514) - } + require.Truef(t, reflect.DeepEqual(expectedAfterDelete514, visitsAfterDelete514), "level order after deleting '514' expected %v, got %v", expectedAfterDelete514, visitsAfterDelete514) // delete the node "11" range11 := NewInt64Interval(11, 12) - if deleted := tr.Delete(NewInt64Interval(11, 12)); !deleted { - t.Fatalf("range %v not deleted", range11) - } + require.Truef(t, tr.Delete(NewInt64Interval(11, 12)), "range %v not deleted", range11) expectedAfterDelete11 := []visitedInterval{ {root: NewInt64Interval(510, 511), color: black, left: NewInt64Interval(383, 384), right: NewInt64Interval(830, 831), depth: 0}, @@ -277,9 +265,7 @@ func TestIntervalTreeDelete(t *testing.T) { {root: NewInt64Interval(953, 954), color: red, left: newInt64EmptyInterval(), right: newInt64EmptyInterval(), depth: 4}, } visitsAfterDelete11 := tr.visitLevel() - if !reflect.DeepEqual(expectedAfterDelete11, visitsAfterDelete11) { - t.Fatalf("level order after deleting '11' expected %v, got %v", expectedAfterDelete11, visitsAfterDelete11) - } + require.Truef(t, reflect.DeepEqual(expectedAfterDelete11, visitsAfterDelete11), "level order after deleting '11' expected %v, got %v", expectedAfterDelete11, visitsAfterDelete11) } func TestIntervalTreeIntersects(t *testing.T) { @@ -323,9 +309,7 @@ func TestIntervalTreeStab(t *testing.T) { ivt.Insert(NewStringInterval("0", "3"), 0) tr := ivt.(*intervalTree) - if tr.root.max.Compare(StringComparable("8")) != 0 { - t.Fatalf("wrong root max got %v, expected 8", tr.root.max) - } + require.Equalf(t, 0, tr.root.max.Compare(StringComparable("8")), "wrong root max got %v, expected 8", tr.root.max) if x := len(ivt.Stab(NewStringPoint("0"))); x != 3 { t.Errorf("got %d, expected 3", x) } @@ -381,12 +365,8 @@ func TestIntervalTreeRandom(t *testing.T) { for ab := range ivs { for xy := range ivs { v := xy.x + int64(rand.Intn(int(xy.y-xy.x))) - if slen := len(ivt.Stab(NewInt64Point(v))); slen == 0 { - t.Fatalf("expected %v stab non-zero for [%+v)", v, xy) - } - if !ivt.Intersects(NewInt64Point(v)) { - t.Fatalf("did not get %d as expected for [%+v)", v, xy) - } + require.NotEmptyf(t, ivt.Stab(NewInt64Point(v)), "expected %v stab non-zero for [%+v)", v, xy) + require.Truef(t, ivt.Intersects(NewInt64Point(v)), "did not get %d as expected for [%+v)", v, xy) } if !ivt.Delete(NewInt64Interval(ab.x, ab.y)) { t.Errorf("did not delete %v as expected", ab) diff --git a/pkg/crc/crc_test.go b/pkg/crc/crc_test.go index 45759640904..3c9cc3a280c 100644 --- a/pkg/crc/crc_test.go +++ b/pkg/crc/crc_test.go @@ -8,15 +8,16 @@ import ( "hash/crc32" "reflect" "testing" + + "github.com/stretchr/testify/require" ) // TestHash32 tests that Hash32 provided by this package can take an initial // crc and behaves exactly the same as the standard one in the following calls. func TestHash32(t *testing.T) { stdhash := crc32.New(crc32.IEEETable) - if _, err := stdhash.Write([]byte("test data")); err != nil { - t.Fatalf("unexpected write error: %v", err) - } + _, err := stdhash.Write([]byte("test data")) + require.NoErrorf(t, err, "unexpected write error: %v", err) // create a new hash with stdhash.Sum32() as initial crc hash := New(stdhash.Sum32(), crc32.IEEETable) @@ -38,12 +39,10 @@ func TestHash32(t *testing.T) { } // write something - if _, err := stdhash.Write([]byte("test data")); err != nil { - t.Fatalf("unexpected write error: %v", err) - } - if _, err := hash.Write([]byte("test data")); err != nil { - t.Fatalf("unexpected write error: %v", err) - } + _, err = stdhash.Write([]byte("test data")) + require.NoErrorf(t, err, "unexpected write error: %v", err) + _, err = hash.Write([]byte("test data")) + require.NoErrorf(t, err, "unexpected write error: %v", err) wsum32 = stdhash.Sum32() if g := hash.Sum32(); g != wsum32 { t.Errorf("Sum32 after write = %d, want %d", g, wsum32) diff --git a/pkg/expect/expect_test.go b/pkg/expect/expect_test.go index df69c0fa6b0..3d621b9aacb 100644 --- a/pkg/expect/expect_test.go +++ b/pkg/expect/expect_test.go @@ -29,27 +29,17 @@ import ( func TestExpectFunc(t *testing.T) { ep, err := NewExpect("echo", "hello world") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) wstr := "hello world\r\n" l, eerr := ep.ExpectFunc(context.Background(), func(a string) bool { return len(a) > 10 }) - if eerr != nil { - t.Fatal(eerr) - } - if l != wstr { - t.Fatalf(`got "%v", expected "%v"`, l, wstr) - } - if cerr := ep.Close(); cerr != nil { - t.Fatal(cerr) - } + require.NoError(t, eerr) + require.Equalf(t, l, wstr, `got "%v", expected "%v"`, l, wstr) + require.NoError(t, ep.Close()) } func TestExpectFuncTimeout(t *testing.T) { ep, err := NewExpect("tail", "-f", "/dev/null") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) go func() { // It's enough to have "talkative" process to stuck in the infinite loop of reading for { @@ -66,21 +56,15 @@ func TestExpectFuncTimeout(t *testing.T) { require.ErrorIs(t, err, context.DeadlineExceeded) - if err = ep.Stop(); err != nil { - t.Fatal(err) - } - - err = ep.Close() - require.ErrorContains(t, err, "unexpected exit code [143]") + require.NoError(t, ep.Stop()) + require.ErrorContains(t, ep.Close(), "unexpected exit code [143]") require.Equal(t, 143, ep.exitCode) } func TestExpectFuncExitFailure(t *testing.T) { // tail -x should not exist and return a non-zero exit code ep, err := NewExpect("tail", "-x") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) defer cancel() @@ -95,9 +79,7 @@ func TestExpectFuncExitFailure(t *testing.T) { func TestExpectFuncExitFailureStop(t *testing.T) { // tail -x should not exist and return a non-zero exit code ep, err := NewExpect("tail", "-x") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) defer cancel() @@ -109,12 +91,8 @@ func TestExpectFuncExitFailureStop(t *testing.T) { exitCode, err := ep.ExitCode() require.Equal(t, 1, exitCode) require.NoError(t, err) - - if err = ep.Stop(); err != nil { - t.Fatal(err) - } - err = ep.Close() - require.ErrorContains(t, err, "unexpected exit code [1]") + require.NoError(t, ep.Stop()) + require.ErrorContains(t, ep.Close(), "unexpected exit code [1]") exitCode, err = ep.ExitCode() require.Equal(t, 1, exitCode) require.NoError(t, err) @@ -122,68 +100,41 @@ func TestExpectFuncExitFailureStop(t *testing.T) { func TestEcho(t *testing.T) { ep, err := NewExpect("echo", "hello world") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) ctx := context.Background() l, eerr := ep.ExpectWithContext(ctx, ExpectedResponse{Value: "world"}) - if eerr != nil { - t.Fatal(eerr) - } + require.NoError(t, eerr) wstr := "hello world" - if l[:len(wstr)] != wstr { - t.Fatalf(`got "%v", expected "%v"`, l, wstr) - } - if cerr := ep.Close(); cerr != nil { - t.Fatal(cerr) - } - if _, eerr = ep.ExpectWithContext(ctx, ExpectedResponse{Value: "..."}); eerr == nil { - t.Fatalf("expected error on closed expect process") - } + require.Equalf(t, l[:len(wstr)], wstr, `got "%v", expected "%v"`, l, wstr) + require.NoError(t, ep.Close()) + _, eerr = ep.ExpectWithContext(ctx, ExpectedResponse{Value: "..."}) + require.Errorf(t, eerr, "expected error on closed expect process") } func TestLineCount(t *testing.T) { ep, err := NewExpect("printf", "1\n2\n3") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) wstr := "3" l, eerr := ep.ExpectWithContext(context.Background(), ExpectedResponse{Value: wstr}) - if eerr != nil { - t.Fatal(eerr) - } - if l != wstr { - t.Fatalf(`got "%v", expected "%v"`, l, wstr) - } - if ep.LineCount() != 3 { - t.Fatalf("got %d, expected 3", ep.LineCount()) - } - if cerr := ep.Close(); cerr != nil { - t.Fatal(cerr) - } + require.NoError(t, eerr) + require.Equalf(t, l, wstr, `got "%v", expected "%v"`, l, wstr) + require.Equalf(t, 3, ep.LineCount(), "got %d, expected 3", ep.LineCount()) + require.NoError(t, ep.Close()) } func TestSend(t *testing.T) { ep, err := NewExpect("tr", "a", "b") - if err != nil { - t.Fatal(err) - } - if err := ep.Send("a\r"); err != nil { - t.Fatal(err) - } - if _, err := ep.ExpectWithContext(context.Background(), ExpectedResponse{Value: "b"}); err != nil { - t.Fatal(err) - } - if err := ep.Stop(); err != nil { - t.Fatal(err) - } + require.NoError(t, err) + err = ep.Send("a\r") + require.NoError(t, err) + _, err = ep.ExpectWithContext(context.Background(), ExpectedResponse{Value: "b"}) + require.NoError(t, err) + require.NoError(t, ep.Stop()) } func TestSignal(t *testing.T) { ep, err := NewExpect("sleep", "100") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) ep.Signal(os.Interrupt) donec := make(chan struct{}) go func() { @@ -267,8 +218,7 @@ func TestResponseMatchRegularExpr(t *testing.T) { require.Error(t, err) } - cerr := ep.Close() - require.NoError(t, cerr) + require.NoError(t, ep.Close()) }) } } diff --git a/pkg/flags/flag_test.go b/pkg/flags/flag_test.go index c1d39b52ed3..f2b8ce1eb71 100644 --- a/pkg/flags/flag_test.go +++ b/pkg/flags/flag_test.go @@ -19,6 +19,7 @@ import ( "strings" "testing" + "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" ) @@ -41,9 +42,8 @@ func TestSetFlagsFromEnv(t *testing.T) { "a": "", "b": "bar", } { - if got := fs.Lookup(f).Value.String(); got != want { - t.Fatalf("flag %q=%q, want %q", f, got, want) - } + got := fs.Lookup(f).Value.String() + require.Equalf(t, want, got, "flag %q=%q, want %q", f, got, want) } // now read the env and verify flags were updated as expected @@ -85,7 +85,5 @@ func TestSetFlagsFromEnvParsingError(t *testing.T) { break } } - if err != nil { - t.Fatalf("unexpected error %v", err) - } + require.NoErrorf(t, err, "unexpected error %v", err) } diff --git a/pkg/flags/strings_test.go b/pkg/flags/strings_test.go index 3835612b052..5d4ed6db568 100644 --- a/pkg/flags/strings_test.go +++ b/pkg/flags/strings_test.go @@ -17,6 +17,8 @@ package flags import ( "reflect" "testing" + + "github.com/stretchr/testify/require" ) func TestStringsValue(t *testing.T) { @@ -30,8 +32,6 @@ func TestStringsValue(t *testing.T) { } for i := range tests { ss := []string(*NewStringsValue(tests[i].s)) - if !reflect.DeepEqual(tests[i].exp, ss) { - t.Fatalf("#%d: expected %q, got %q", i, tests[i].exp, ss) - } + require.Truef(t, reflect.DeepEqual(tests[i].exp, ss), "#%d: expected %q, got %q", i, tests[i].exp, ss) } } diff --git a/pkg/flags/uint32_test.go b/pkg/flags/uint32_test.go index f2445a079cc..949fbefb671 100644 --- a/pkg/flags/uint32_test.go +++ b/pkg/flags/uint32_test.go @@ -19,6 +19,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestUint32Value(t *testing.T) { @@ -101,9 +102,7 @@ func TestUint32FromFlag(t *testing.T) { t.Run(tc.name, func(t *testing.T) { fs := flag.NewFlagSet("etcd", flag.ContinueOnError) fs.Var(NewUint32Value(tc.defaultVal), flagName, "Maximum concurrent streams that each client can open at a time.") - if err := fs.Parse(tc.arguments); err != nil { - t.Fatalf("Unexpected error: %v\n", err) - } + require.NoError(t, fs.Parse(tc.arguments)) actualMaxStream := Uint32FromFlag(fs, flagName) assert.Equal(t, tc.expectedVal, actualMaxStream) }) diff --git a/pkg/flags/unique_strings_test.go b/pkg/flags/unique_strings_test.go index 86d2b0fc2b0..7bd9c3a2118 100644 --- a/pkg/flags/unique_strings_test.go +++ b/pkg/flags/unique_strings_test.go @@ -17,6 +17,8 @@ package flags import ( "reflect" "testing" + + "github.com/stretchr/testify/require" ) func TestNewUniqueStrings(t *testing.T) { @@ -58,11 +60,7 @@ func TestNewUniqueStrings(t *testing.T) { } for i := range tests { uv := NewUniqueStringsValue(tests[i].s) - if !reflect.DeepEqual(tests[i].exp, uv.Values) { - t.Fatalf("#%d: expected %+v, got %+v", i, tests[i].exp, uv.Values) - } - if uv.String() != tests[i].rs { - t.Fatalf("#%d: expected %q, got %q", i, tests[i].rs, uv.String()) - } + require.Truef(t, reflect.DeepEqual(tests[i].exp, uv.Values), "#%d: expected %+v, got %+v", i, tests[i].exp, uv.Values) + require.Equalf(t, uv.String(), tests[i].rs, "#%d: expected %q, got %q", i, tests[i].rs, uv.String()) } } diff --git a/pkg/flags/urls_test.go b/pkg/flags/urls_test.go index ebc9a267410..4b8429264bf 100644 --- a/pkg/flags/urls_test.go +++ b/pkg/flags/urls_test.go @@ -18,6 +18,8 @@ import ( "net/url" "reflect" "testing" + + "github.com/stretchr/testify/require" ) func TestValidateURLsValueBad(t *testing.T) { @@ -66,8 +68,6 @@ func TestNewURLsValue(t *testing.T) { } for i := range tests { uu := []url.URL(*NewURLsValue(tests[i].s)) - if !reflect.DeepEqual(tests[i].exp, uu) { - t.Fatalf("#%d: expected %+v, got %+v", i, tests[i].exp, uu) - } + require.Truef(t, reflect.DeepEqual(tests[i].exp, uu), "#%d: expected %+v, got %+v", i, tests[i].exp, uu) } } diff --git a/pkg/ioutil/pagewriter_test.go b/pkg/ioutil/pagewriter_test.go index 62ce176edf8..77f1336bbe8 100644 --- a/pkg/ioutil/pagewriter_test.go +++ b/pkg/ioutil/pagewriter_test.go @@ -19,6 +19,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestPageWriterRandom(t *testing.T) { @@ -36,12 +37,9 @@ func TestPageWriterRandom(t *testing.T) { } n += c } - if cw.writeBytes > n { - t.Fatalf("wrote %d bytes to io.Writer, but only wrote %d bytes", cw.writeBytes, n) - } - if maxPendingBytes := pageBytes + defaultBufferBytes; n-cw.writeBytes > maxPendingBytes { - t.Fatalf("got %d bytes pending, expected less than %d bytes", n-cw.writeBytes, maxPendingBytes) - } + require.LessOrEqualf(t, cw.writeBytes, n, "wrote %d bytes to io.Writer, but only wrote %d bytes", cw.writeBytes, n) + maxPendingBytes := pageBytes + defaultBufferBytes + require.LessOrEqualf(t, n-cw.writeBytes, maxPendingBytes, "got %d bytes pending, expected less than %d bytes", n-cw.writeBytes, maxPendingBytes) t.Logf("total writes: %d", cw.writes) t.Logf("total write bytes: %d (of %d)", cw.writeBytes, n) } @@ -61,9 +59,7 @@ func TestPageWriterPartialSlack(t *testing.T) { if err := w.Flush(); err != nil { t.Fatal(err) } - if cw.writes != 1 { - t.Fatalf("got %d writes, expected 1", cw.writes) - } + require.Equalf(t, 1, cw.writes, "got %d writes, expected 1", cw.writes) // nearly fill buffer if _, err := w.Write(buf[:1022]); err != nil { t.Fatal(err) @@ -72,16 +68,12 @@ func TestPageWriterPartialSlack(t *testing.T) { if _, err := w.Write(buf[:8]); err != nil { t.Fatal(err) } - if cw.writes != 1 { - t.Fatalf("got %d writes, expected 1", cw.writes) - } + require.Equalf(t, 1, cw.writes, "got %d writes, expected 1", cw.writes) // finish writing slack space if _, err := w.Write(buf[:128]); err != nil { t.Fatal(err) } - if cw.writes != 2 { - t.Fatalf("got %d writes, expected 2", cw.writes) - } + require.Equalf(t, 2, cw.writes, "got %d writes, expected 2", cw.writes) } // TestPageWriterOffset tests if page writer correctly repositions when offset is given. @@ -97,9 +89,7 @@ func TestPageWriterOffset(t *testing.T) { if err := w.Flush(); err != nil { t.Fatal(err) } - if w.pageOffset != 64 { - t.Fatalf("w.pageOffset expected 64, got %d", w.pageOffset) - } + require.Equalf(t, 64, w.pageOffset, "w.pageOffset expected 64, got %d", w.pageOffset) w = NewPageWriter(cw, w.pageOffset, pageBytes) if _, err := w.Write(buf[:64]); err != nil { @@ -108,9 +98,7 @@ func TestPageWriterOffset(t *testing.T) { if err := w.Flush(); err != nil { t.Fatal(err) } - if w.pageOffset != 0 { - t.Fatalf("w.pageOffset expected 0, got %d", w.pageOffset) - } + require.Equalf(t, 0, w.pageOffset, "w.pageOffset expected 0, got %d", w.pageOffset) } func TestPageWriterPageBytes(t *testing.T) { @@ -161,9 +149,7 @@ type checkPageWriter struct { } func (cw *checkPageWriter) Write(p []byte) (int, error) { - if len(p)%cw.pageBytes != 0 { - cw.t.Fatalf("got write len(p) = %d, expected len(p) == k*cw.pageBytes", len(p)) - } + require.Equalf(cw.t, 0, len(p)%cw.pageBytes, "got write len(p) = %d, expected len(p) == k*cw.pageBytes", len(p)) cw.writes++ cw.writeBytes += len(p) return len(p), nil diff --git a/pkg/ioutil/readcloser_test.go b/pkg/ioutil/readcloser_test.go index 381a797f7af..4b79fb44a76 100644 --- a/pkg/ioutil/readcloser_test.go +++ b/pkg/ioutil/readcloser_test.go @@ -16,9 +16,10 @@ package ioutil import ( "bytes" - "errors" "io" "testing" + + "github.com/stretchr/testify/require" ) type readerNilCloser struct{ io.Reader } @@ -29,19 +30,15 @@ func (rc *readerNilCloser) Close() error { return nil } func TestExactReadCloserExpectEOF(t *testing.T) { buf := bytes.NewBuffer(make([]byte, 10)) rc := NewExactReadCloser(&readerNilCloser{buf}, 1) - if _, err := rc.Read(make([]byte, 10)); !errors.Is(err, ErrExpectEOF) { - t.Fatalf("expected %v, got %v", ErrExpectEOF, err) - } + _, err := rc.Read(make([]byte, 10)) + require.ErrorIsf(t, err, ErrExpectEOF, "expected %v, got %v", ErrExpectEOF, err) } // TestExactReadCloserShort expects an eof when reading too little func TestExactReadCloserShort(t *testing.T) { buf := bytes.NewBuffer(make([]byte, 5)) rc := NewExactReadCloser(&readerNilCloser{buf}, 10) - if _, err := rc.Read(make([]byte, 10)); err != nil { - t.Fatalf("Read expected nil err, got %v", err) - } - if err := rc.Close(); !errors.Is(err, ErrShortRead) { - t.Fatalf("Close expected %v, got %v", ErrShortRead, err) - } + _, err := rc.Read(make([]byte, 10)) + require.NoErrorf(t, err, "Read expected nil err, got %v", err) + require.ErrorIs(t, rc.Close(), ErrShortRead) } diff --git a/pkg/report/report_test.go b/pkg/report/report_test.go index 6f073f3e8d3..d6bdc3bf95c 100644 --- a/pkg/report/report_test.go +++ b/pkg/report/report_test.go @@ -20,6 +20,8 @@ import ( "strings" "testing" "time" + + "github.com/stretchr/testify/require" ) func TestPercentiles(t *testing.T) { @@ -64,9 +66,7 @@ func TestReport(t *testing.T) { ErrorDist: map[string]int{"oops": 1}, Lats: []float64{1.0, 1.0, 1.0, 1.0, 1.0}, } - if !reflect.DeepEqual(stats, wStats) { - t.Fatalf("got %+v, want %+v", stats, wStats) - } + require.Truef(t, reflect.DeepEqual(stats, wStats), "got %+v, want %+v", stats, wStats) wstrs := []string{ "Stddev:\t0", @@ -108,7 +108,5 @@ func TestWeightedReport(t *testing.T) { ErrorDist: map[string]int{"oops": 1}, Lats: []float64{0.5, 0.5, 0.5, 0.5, 0.5}, } - if !reflect.DeepEqual(stats, wStats) { - t.Fatalf("got %+v, want %+v", stats, wStats) - } + require.Truef(t, reflect.DeepEqual(stats, wStats), "got %+v, want %+v", stats, wStats) } diff --git a/pkg/report/timeseries_test.go b/pkg/report/timeseries_test.go index 13fcbfa397d..6cdae2ef6fe 100644 --- a/pkg/report/timeseries_test.go +++ b/pkg/report/timeseries_test.go @@ -17,6 +17,8 @@ package report import ( "testing" "time" + + "github.com/stretchr/testify/require" ) func TestGetTimeseries(t *testing.T) { @@ -25,20 +27,12 @@ func TestGetTimeseries(t *testing.T) { sp.Add(now, time.Second) sp.Add(now.Add(5*time.Second), time.Second) n := sp.getTimeSeries().Len() - if n < 3 { - t.Fatalf("expected at 6 points of time series, got %s", sp.getTimeSeries()) - } + require.GreaterOrEqualf(t, n, 3, "expected at 6 points of time series, got %s", sp.getTimeSeries()) // add a point with duplicate timestamp sp.Add(now, 3*time.Second) ts := sp.getTimeSeries() - if ts[0].MinLatency != time.Second { - t.Fatalf("ts[0] min latency expected %v, got %s", time.Second, ts[0].MinLatency) - } - if ts[0].AvgLatency != 2*time.Second { - t.Fatalf("ts[0] average latency expected %v, got %s", 2*time.Second, ts[0].AvgLatency) - } - if ts[0].MaxLatency != 3*time.Second { - t.Fatalf("ts[0] max latency expected %v, got %s", 3*time.Second, ts[0].MaxLatency) - } + require.Equalf(t, time.Second, ts[0].MinLatency, "ts[0] min latency expected %v, got %s", time.Second, ts[0].MinLatency) + require.Equalf(t, 2*time.Second, ts[0].AvgLatency, "ts[0] average latency expected %v, got %s", 2*time.Second, ts[0].AvgLatency) + require.Equalf(t, 3*time.Second, ts[0].MaxLatency, "ts[0] max latency expected %v, got %s", 3*time.Second, ts[0].MaxLatency) } diff --git a/pkg/schedule/schedule_test.go b/pkg/schedule/schedule_test.go index 8b0c823eae5..af0b5e613ea 100644 --- a/pkg/schedule/schedule_test.go +++ b/pkg/schedule/schedule_test.go @@ -19,6 +19,7 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" ) @@ -34,9 +35,7 @@ func TestFIFOSchedule(t *testing.T) { fmt.Println("err: ", err) } }() - if next != i { - t.Fatalf("job#%d: got %d, want %d", i, next, i) - } + require.Equalf(t, next, i, "job#%d: got %d, want %d", i, next, i) next = i + 1 if next%3 == 0 { panic("fifo panic") diff --git a/pkg/stringutil/rand_test.go b/pkg/stringutil/rand_test.go index ab51ef263b4..5ecd0ba50a1 100644 --- a/pkg/stringutil/rand_test.go +++ b/pkg/stringutil/rand_test.go @@ -17,14 +17,14 @@ package stringutil import ( "sort" "testing" + + "github.com/stretchr/testify/require" ) func TestUniqueStrings(t *testing.T) { ss := UniqueStrings(10, 50) sort.Strings(ss) for i := 1; i < len(ss); i++ { - if ss[i-1] == ss[i] { - t.Fatalf("ss[i-1] %q == ss[i] %q", ss[i-1], ss[i]) - } + require.NotEqualf(t, ss[i-1], ss[i], "ss[i-1] %q == ss[i] %q", ss[i-1], ss[i]) } } diff --git a/pkg/traceutil/trace_test.go b/pkg/traceutil/trace_test.go index 8d3b10e07fd..4d6d3513f3a 100644 --- a/pkg/traceutil/trace_test.go +++ b/pkg/traceutil/trace_test.go @@ -23,6 +23,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" + "go.etcd.io/etcd/client/pkg/v3/logutil" ) @@ -215,9 +217,7 @@ func TestLog(t *testing.T) { tt.trace.lg = lg tt.trace.Log() data, err := os.ReadFile(logPath) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) for _, msg := range tt.expectedMsg { if !bytes.Contains(data, []byte(msg)) { @@ -293,9 +293,7 @@ func TestLogIfLong(t *testing.T) { tt.trace.lg = lg tt.trace.LogIfLong(tt.threshold) data, err := os.ReadFile(logPath) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) for _, msg := range tt.expectedMsg { if !bytes.Contains(data, []byte(msg)) { t.Errorf("Expected to find %v in log", msg)