diff --git a/go/tools/asthelpergen/asthelpergen_test.go b/go/tools/asthelpergen/asthelpergen_test.go index ce5a59c84e7..19ac865575d 100644 --- a/go/tools/asthelpergen/asthelpergen_test.go +++ b/go/tools/asthelpergen/asthelpergen_test.go @@ -42,8 +42,6 @@ func TestFullGeneration(t *testing.T) { require.Contains(t, contents, "http://www.apache.org/licenses/LICENSE-2.0") applyIdx := strings.Index(contents, "func (a *application) apply(parent, node AST, replacer replacerFunc)") cloneIdx := strings.Index(contents, "CloneAST(in AST) AST") - if applyIdx == 0 && cloneIdx == 0 { - t.Fatalf("file doesn't contain expected contents") - } + require.False(t, applyIdx == 0 && cloneIdx == 0, "file doesn't contain expected contents") } } diff --git a/go/trace/trace_test.go b/go/trace/trace_test.go index 7f1f6d8c528..688d60551e3 100644 --- a/go/trace/trace_test.go +++ b/go/trace/trace_test.go @@ -60,14 +60,9 @@ func TestRegisterService(t *testing.T) { serviceName := "vtservice" closer := StartTracing(serviceName) - tracer, ok := closer.(*fakeTracer) - if !ok { - t.Fatalf("did not get the expected tracer, got %+v (%T)", tracer, tracer) - } + tracer := closer.(*fakeTracer) - if tracer.name != serviceName { - t.Fatalf("expected the name to be `%v` but it was `%v`", serviceName, tracer.name) - } + require.Equal(t, serviceName, tracer.name, fmt.Sprintf("tracer name mismatch: expected %s, got %s", serviceName, tracer.name)) } func TestNewFromString(t *testing.T) { diff --git a/go/vt/binlog/binlog_streamer_rbr_test.go b/go/vt/binlog/binlog_streamer_rbr_test.go index 1678b086719..6ae9bc4afcf 100644 --- a/go/vt/binlog/binlog_streamer_rbr_test.go +++ b/go/vt/binlog/binlog_streamer_rbr_test.go @@ -18,9 +18,10 @@ package binlog import ( "context" - "reflect" "testing" + "github.com/stretchr/testify/assert" + "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/mysql/binlog" "vitess.io/vitess/go/mysql/collations" @@ -270,19 +271,8 @@ func TestStreamerParseRBREvents(t *testing.T) { go sendTestEvents(events, input) _, err := bls.parseEvents(context.Background(), events, errs) - if err != ErrServerEOF { - t.Errorf("unexpected error: %v", err) - } - - if !reflect.DeepEqual(got, want) { - t.Errorf("binlogConnStreamer.parseEvents(): got:\n%+v\nwant:\n%+v", got, want) - for i, fbt := range got { - t.Errorf("Got (%v)=%v", i, fbt.statements) - } - for i, fbt := range want { - t.Errorf("Want(%v)=%v", i, fbt.statements) - } - } + assert.EqualError(t, err, ErrServerEOF.Error(), "unexpected error") + assert.Equal(t, want, got, "binlogConnStreamer.parseEvents()") } func TestStreamerParseRBRNameEscapes(t *testing.T) { @@ -519,17 +509,6 @@ func TestStreamerParseRBRNameEscapes(t *testing.T) { go sendTestEvents(events, input) _, err := bls.parseEvents(context.Background(), events, errs) - if err != ErrServerEOF { - t.Errorf("unexpected error: %v", err) - } - - if !reflect.DeepEqual(got, want) { - t.Errorf("binlogConnStreamer.parseEvents(): got:\n%+v\nwant:\n%+v", got, want) - for i, fbt := range got { - t.Errorf("Got (%v)=%v", i, fbt.statements) - } - for i, fbt := range want { - t.Errorf("Want(%v)=%v", i, fbt.statements) - } - } + assert.EqualError(t, err, ErrServerEOF.Error(), "unexpected error") + assert.Equal(t, want, got, "binlogConnStreamer.parseEvents()") } diff --git a/go/vt/binlog/tables_filter_test.go b/go/vt/binlog/tables_filter_test.go index 0c6aeef3fcc..c65f64b5aef 100644 --- a/go/vt/binlog/tables_filter_test.go +++ b/go/vt/binlog/tables_filter_test.go @@ -20,6 +20,8 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/assert" + binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" querypb "vitess.io/vitess/go/vt/proto/query" ) @@ -60,9 +62,7 @@ func TestTablesFilterPass(t *testing.T) { }) _ = f(eventToken, statements) want := `statement: <6, "set1"> statement: <7, "dml1 /* _stream included1 (id ) (500 ); */"> statement: <7, "dml2 /* _stream included2 (id ) (500 ); */"> position: "MariaDB/0-41983-1" ` - if want != got { - t.Errorf("want\n%s, got\n%s", want, got) - } + assert.Equal(t, want, got, "binlogConnStreamer.tablesFilterFunc()") } func TestTablesFilterSkip(t *testing.T) { @@ -90,9 +90,7 @@ func TestTablesFilterSkip(t *testing.T) { }) _ = f(eventToken, statements) want := `position: "MariaDB/0-41983-1" ` - if want != got { - t.Errorf("want %s, got %s", want, got) - } + assert.Equal(t, want, got, "binlogConnStreamer.tablesFilterFunc()") } func TestTablesFilterDDL(t *testing.T) { @@ -120,9 +118,7 @@ func TestTablesFilterDDL(t *testing.T) { }) _ = f(eventToken, statements) want := `position: "MariaDB/0-41983-1" ` - if want != got { - t.Errorf("want %s, got %s", want, got) - } + assert.Equal(t, want, got, "binlogConnStreamer.tablesFilterFunc()") } func TestTablesFilterMalformed(t *testing.T) { @@ -156,9 +152,7 @@ func TestTablesFilterMalformed(t *testing.T) { }) _ = f(eventToken, statements) want := `position: "MariaDB/0-41983-1" ` - if want != got { - t.Errorf("want %s, got %s", want, got) - } + assert.Equal(t, want, got, "binlogConnStreamer.tablesFilterFunc()") } func bltToString(tx *binlogdatapb.BinlogTransaction) string { diff --git a/go/vt/wrangler/tablet_test.go b/go/vt/wrangler/tablet_test.go index c5ae032fe07..113743d9c19 100644 --- a/go/vt/wrangler/tablet_test.go +++ b/go/vt/wrangler/tablet_test.go @@ -18,9 +18,10 @@ package wrangler import ( "context" - "strings" "testing" + "github.com/stretchr/testify/require" + "vitess.io/vitess/go/vt/logutil" topodatapb "vitess.io/vitess/go/vt/proto/topodata" "vitess.io/vitess/go/vt/topo" @@ -48,20 +49,14 @@ func TestInitTabletShardConversion(t *testing.T) { Shard: "80-C0", } - if err := wr.TopoServer().InitTablet(context.Background(), tablet, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/); err != nil { - t.Fatalf("InitTablet failed: %v", err) - } + err := wr.TopoServer().InitTablet(context.Background(), tablet, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/) + require.NoError(t, err) ti, err := ts.GetTablet(context.Background(), tablet.Alias) - if err != nil { - t.Fatalf("GetTablet failed: %v", err) - } - if ti.Shard != "80-c0" { - t.Errorf("Got wrong tablet.Shard, got %v expected 80-c0", ti.Shard) - } - if string(ti.KeyRange.Start) != "\x80" || string(ti.KeyRange.End) != "\xc0" { - t.Errorf("Got wrong tablet.KeyRange, got %v expected 80-c0", ti.KeyRange) - } + require.NoError(t, err) + require.Equal(t, "80-c0", ti.Shard, "Got wrong tablet.Shard") + require.Equal(t, "\x80", string(ti.KeyRange.Start), "Got wrong tablet.KeyRange start") + require.Equal(t, "\xc0", string(ti.KeyRange.End), "Got wrong tablet.KeyRange end") } // TestDeleteTabletBasic tests delete of non-primary tablet @@ -82,17 +77,14 @@ func TestDeleteTabletBasic(t *testing.T) { Keyspace: "test", } - if err := wr.TopoServer().InitTablet(context.Background(), tablet, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/); err != nil { - t.Fatalf("InitTablet failed: %v", err) - } + err := wr.TopoServer().InitTablet(context.Background(), tablet, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/) + require.NoError(t, err) - if _, err := ts.GetTablet(context.Background(), tablet.Alias); err != nil { - t.Fatalf("GetTablet failed: %v", err) - } + _, err = ts.GetTablet(context.Background(), tablet.Alias) + require.NoError(t, err) - if err := wr.DeleteTablet(context.Background(), tablet.Alias, false); err != nil { - t.Fatalf("DeleteTablet failed: %v", err) - } + err = wr.DeleteTablet(context.Background(), tablet.Alias, false) + require.NoError(t, err) } // TestDeleteTabletTruePrimary tests that you can delete a true primary tablet @@ -115,31 +107,26 @@ func TestDeleteTabletTruePrimary(t *testing.T) { Type: topodatapb.TabletType_PRIMARY, } - if err := wr.TopoServer().InitTablet(context.Background(), tablet, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/); err != nil { - t.Fatalf("InitTablet failed: %v", err) - } - if _, err := ts.GetTablet(context.Background(), tablet.Alias); err != nil { - t.Fatalf("GetTablet failed: %v", err) - } + err := wr.TopoServer().InitTablet(context.Background(), tablet, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/) + require.NoError(t, err) + + _, err = ts.GetTablet(context.Background(), tablet.Alias) + require.NoError(t, err) // set PrimaryAlias and PrimaryTermStartTime on shard to match chosen primary tablet - if _, err := ts.UpdateShardFields(context.Background(), "test", "0", func(si *topo.ShardInfo) error { + _, err = ts.UpdateShardFields(context.Background(), "test", "0", func(si *topo.ShardInfo) error { si.PrimaryAlias = tablet.Alias si.PrimaryTermStartTime = tablet.PrimaryTermStartTime return nil - }); err != nil { - t.Fatalf("UpdateShardFields failed: %v", err) - } + }) + require.NoError(t, err) - err := wr.DeleteTablet(context.Background(), tablet.Alias, false) + err = wr.DeleteTablet(context.Background(), tablet.Alias, false) wantError := "as it is a primary, use allow_primary flag" - if err == nil || !strings.Contains(err.Error(), wantError) { - t.Fatalf("DeleteTablet on primary: want error = %v, got error = %v", wantError, err) - } + require.ErrorContains(t, err, wantError, "DeleteTablet on primary: want specific error message") - if err := wr.DeleteTablet(context.Background(), tablet.Alias, true); err != nil { - t.Fatalf("DeleteTablet failed: %v", err) - } + err = wr.DeleteTablet(context.Background(), tablet.Alias, true) + require.NoError(t, err) } // TestDeleteTabletFalsePrimary tests that you can delete a false primary tablet @@ -162,9 +149,8 @@ func TestDeleteTabletFalsePrimary(t *testing.T) { Type: topodatapb.TabletType_PRIMARY, } - if err := wr.TopoServer().InitTablet(context.Background(), tablet1, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/); err != nil { - t.Fatalf("InitTablet failed: %v", err) - } + err := wr.TopoServer().InitTablet(context.Background(), tablet1, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/) + require.NoError(t, err) tablet2 := &topodatapb.Tablet{ Alias: &topodatapb.TabletAlias{ @@ -175,23 +161,20 @@ func TestDeleteTabletFalsePrimary(t *testing.T) { Shard: "0", Type: topodatapb.TabletType_PRIMARY, } - if err := wr.TopoServer().InitTablet(context.Background(), tablet2, true /*allowPrimaryOverride*/, false /*createShardAndKeyspace*/, false /*allowUpdate*/); err != nil { - t.Fatalf("InitTablet failed: %v", err) - } + err = wr.TopoServer().InitTablet(context.Background(), tablet2, true /*allowPrimaryOverride*/, false /*createShardAndKeyspace*/, false /*allowUpdate*/) + require.NoError(t, err) // set PrimaryAlias and PrimaryTermStartTime on shard to match chosen primary tablet - if _, err := ts.UpdateShardFields(context.Background(), "test", "0", func(si *topo.ShardInfo) error { + _, err = ts.UpdateShardFields(context.Background(), "test", "0", func(si *topo.ShardInfo) error { si.PrimaryAlias = tablet2.Alias si.PrimaryTermStartTime = tablet2.PrimaryTermStartTime return nil - }); err != nil { - t.Fatalf("UpdateShardFields failed: %v", err) - } + }) + require.NoError(t, err) // Should be able to delete old (false) primary with allowPrimary = false - if err := wr.DeleteTablet(context.Background(), tablet1.Alias, false); err != nil { - t.Fatalf("DeleteTablet failed: %v", err) - } + err = wr.DeleteTablet(context.Background(), tablet1.Alias, false) + require.NoError(t, err) } // TestDeleteTabletShardNonExisting tests that you can delete a true primary @@ -214,29 +197,25 @@ func TestDeleteTabletShardNonExisting(t *testing.T) { Type: topodatapb.TabletType_PRIMARY, } - if err := wr.TopoServer().InitTablet(context.Background(), tablet, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/); err != nil { - t.Fatalf("InitTablet failed: %v", err) - } - if _, err := ts.GetTablet(context.Background(), tablet.Alias); err != nil { - t.Fatalf("GetTablet failed: %v", err) - } + err := wr.TopoServer().InitTablet(context.Background(), tablet, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/) + require.NoError(t, err) + + _, err = ts.GetTablet(context.Background(), tablet.Alias) + require.NoError(t, err) // set PrimaryAlias and PrimaryTermStartTime on shard to match chosen primary tablet - if _, err := ts.UpdateShardFields(context.Background(), "test", "0", func(si *topo.ShardInfo) error { + _, err = ts.UpdateShardFields(context.Background(), "test", "0", func(si *topo.ShardInfo) error { si.PrimaryAlias = tablet.Alias si.PrimaryTermStartTime = tablet.PrimaryTermStartTime return nil - }); err != nil { - t.Fatalf("UpdateShardFields failed: %v", err) - } + }) + require.NoError(t, err) // trigger a shard deletion - if err := ts.DeleteShard(context.Background(), "test", "0"); err != nil { - t.Fatalf("DeleteShard failed: %v", err) - } + err = ts.DeleteShard(context.Background(), "test", "0") + require.NoError(t, err) // DeleteTablet should not fail if a shard no longer exist - if err := wr.DeleteTablet(context.Background(), tablet.Alias, true); err != nil { - t.Fatalf("DeleteTablet failed: %v", err) - } + err = wr.DeleteTablet(context.Background(), tablet.Alias, true) + require.NoError(t, err) } diff --git a/go/vt/wrangler/vexec_test.go b/go/vt/wrangler/vexec_test.go index 27efbe61a9f..80cd2aef565 100644 --- a/go/vt/wrangler/vexec_test.go +++ b/go/vt/wrangler/vexec_test.go @@ -26,6 +26,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "vitess.io/vitess/go/sqltypes" @@ -151,15 +152,11 @@ func TestVExec(t *testing.T) { if testCase.errorString == "" { require.NoError(t, err) for _, result := range results { - if !testCase.result.Equal(result) { - t.Errorf("mismatched result:\nwant: %v\ngot: %v", testCase.result, result) - } + assert.True(t, testCase.result.Equal(result), "mismatched result") } } else { - require.Error(t, err) - if !strings.Contains(err.Error(), testCase.errorString) { - t.Fatalf("Wrong error, want %s, got %s", testCase.errorString, err.Error()) - } + require.ErrorContains(t, err, testCase.errorString, "Wrong error, want %s, got %s", testCase.errorString, err.Error()) + } }) } diff --git a/go/vt/zkctl/zkctl_test.go b/go/vt/zkctl/zkctl_test.go index 5e4c856b5a7..a0c03a58df2 100644 --- a/go/vt/zkctl/zkctl_test.go +++ b/go/vt/zkctl/zkctl_test.go @@ -18,8 +18,9 @@ package zkctl import ( "fmt" - "strings" "testing" + + "github.com/stretchr/testify/require" ) // This test depend on starting and stopping a ZK instance, @@ -39,29 +40,21 @@ func TestLifeCycle(t *testing.T) { adminServerCfg := "admin.serverPort=8081" zkConf.Extra = []string{tpcKeepAliveCfg, adminServerCfg} - if zkObservedConf, err := MakeZooCfg([]string{zkConf.ConfigFile()}, zkConf, "header"); err != nil { - t.Fatalf("MakeZooCfg err: %v", err) - } else if !strings.Contains(zkObservedConf, fmt.Sprintf("\n%s\n", tpcKeepAliveCfg)) { - t.Fatalf("Expected tpcKeepAliveCfg in zkObservedConf") - } else if !strings.Contains(zkObservedConf, fmt.Sprintf("\n%s\n", adminServerCfg)) { - t.Fatalf("Expected adminServerCfg in zkObservedConf") - } + zkObservedConf, err := MakeZooCfg([]string{zkConf.ConfigFile()}, zkConf, "header") + require.NoError(t, err) + require.Contains(t, zkObservedConf, fmt.Sprintf("\n%s\n", tpcKeepAliveCfg), "Expected tpcKeepAliveCfg in zkObservedConf") + require.Contains(t, zkObservedConf, fmt.Sprintf("\n%s\n", adminServerCfg), "Expected adminServerCfg in zkObservedConf") zkd := NewZkd(zkConf) - if err := zkd.Init(); err != nil { - t.Fatalf("Init() err: %v", err) - } + err = zkd.Init() + require.NoError(t, err) - if err := zkd.Shutdown(); err != nil { - t.Fatalf("Shutdown() err: %v", err) - } + err = zkd.Shutdown() + require.NoError(t, err) - if err := zkd.Start(); err != nil { - t.Fatalf("Start() err: %v", err) - } - - if err := zkd.Teardown(); err != nil { - t.Fatalf("Teardown() err: %v", err) - } + err = zkd.Start() + require.NoError(t, err) + err = zkd.Teardown() + require.NoError(t, err) }