diff --git a/go/vt/schemamanager/local_controller_test.go b/go/vt/schemamanager/local_controller_test.go index 9b6b7c5369e..1784a76e133 100644 --- a/go/vt/schemamanager/local_controller_test.go +++ b/go/vt/schemamanager/local_controller_test.go @@ -21,10 +21,11 @@ import ( "fmt" "os" "path" - "reflect" "strings" "testing" + "github.com/stretchr/testify/require" + querypb "vitess.io/vitess/go/vt/proto/query" ) @@ -32,64 +33,50 @@ func TestLocalControllerNoSchemaChanges(t *testing.T) { schemaChangeDir := t.TempDir() controller := NewLocalController(schemaChangeDir) ctx := context.Background() - if err := controller.Open(ctx); err != nil { - t.Fatalf("Open should succeed, but got error: %v", err) - } + err := controller.Open(ctx) + require.NoError(t, err) + defer controller.Close() data, err := controller.Read(ctx) - if err != nil { - t.Fatalf("Read should succeed, but got error: %v", err) - } - if len(data) != 0 { - t.Fatalf("there is no schema change, Read should return empty data") - } + require.NoError(t, err) + require.Empty(t, data, "there is no schema change, Read should return empty data") } func TestLocalControllerOpen(t *testing.T) { controller := NewLocalController("") ctx := context.Background() - if err := controller.Open(ctx); err == nil || !strings.Contains(err.Error(), "no such file or directory") { - t.Fatalf("Open should fail, no such dir, but got: %v", err) - } + err := controller.Open(ctx) + require.ErrorContains(t, err, "no such file or directory", "Open should fail, no such dir") schemaChangeDir := t.TempDir() // create a file under schema change dir - _, err := os.Create(path.Join(schemaChangeDir, "create_test_table.sql")) - if err != nil { - t.Fatalf("failed to create sql file, error: %v", err) - } + _, err = os.Create(path.Join(schemaChangeDir, "create_test_table.sql")) + require.NoError(t, err, "failed to create sql file") controller = NewLocalController(schemaChangeDir) - if err := controller.Open(ctx); err != nil { - t.Fatalf("Open should succeed") - } + err = controller.Open(ctx) + require.NoError(t, err) + data, err := controller.Read(ctx) - if err != nil { - t.Fatalf("Read should succeed, but got error: %v", err) - } - if len(data) != 0 { - t.Fatalf("there is no schema change, Read should return empty data") - } + require.NoError(t, err) + require.Empty(t, data, "there is no schema change, Read should return empty data") + controller.Close() testKeyspaceDir := path.Join(schemaChangeDir, "test_keyspace") - if err := os.MkdirAll(testKeyspaceDir, os.ModePerm); err != nil { - t.Fatalf("failed to create test_keyspace dir, error: %v", err) - } + err = os.MkdirAll(testKeyspaceDir, os.ModePerm) + require.NoError(t, err, "failed to create test_keyspace dir") controller = NewLocalController(schemaChangeDir) - if err := controller.Open(ctx); err != nil { - t.Fatalf("Open should succeed") - } + err = controller.Open(ctx) + require.NoError(t, err) + data, err = controller.Read(ctx) - if err != nil { - t.Fatalf("Read should succeed, but got error: %v", err) - } - if len(data) != 0 { - t.Fatalf("there is no schema change, Read should return empty data") - } + require.NoError(t, err) + require.Empty(t, data, "there is no schema change, Read should return empty data") + controller.Close() } @@ -97,14 +84,11 @@ func TestLocalControllerSchemaChange(t *testing.T) { schemaChangeDir := t.TempDir() testKeyspaceInputDir := path.Join(schemaChangeDir, "test_keyspace/input") - if err := os.MkdirAll(testKeyspaceInputDir, os.ModePerm); err != nil { - t.Fatalf("failed to create test_keyspace dir, error: %v", err) - } + err := os.MkdirAll(testKeyspaceInputDir, os.ModePerm) + require.NoError(t, err, "failed to create test_keyspace dir") file, err := os.Create(path.Join(testKeyspaceInputDir, "create_test_table.sql")) - if err != nil { - t.Fatalf("failed to create sql file, error: %v", err) - } + require.NoError(t, err, "failed to create sql file") sqls := []string{ "create table test_table_01 (id int)", @@ -117,51 +101,36 @@ func TestLocalControllerSchemaChange(t *testing.T) { controller := NewLocalController(schemaChangeDir) ctx := context.Background() - if err := controller.Open(ctx); err != nil { - t.Fatalf("Open should succeed, but got error: %v", err) - } + err = controller.Open(ctx) + require.NoError(t, err) defer controller.Close() data, err := controller.Read(ctx) - if err != nil { - t.Fatalf("Read should succeed, but got error: %v", err) - } - - if !reflect.DeepEqual(sqls, data) { - t.Fatalf("expect to get sqls: %v, but got: %v", sqls, data) - } - - if controller.Keyspace() != "test_keyspace" { - t.Fatalf("expect to get keyspace: 'test_keyspace', but got: '%s'", - controller.Keyspace()) - } + require.NoError(t, err) + require.Equal(t, sqls, data) + require.Equal(t, "test_keyspace", controller.Keyspace()) // test various callbacks - if err := controller.OnReadSuccess(ctx); err != nil { - t.Fatalf("OnReadSuccess should succeed, but got error: %v", err) - } + err = controller.OnReadSuccess(ctx) + require.NoError(t, err) - if err := controller.OnReadFail(ctx, fmt.Errorf("read fail")); err != nil { - t.Fatalf("OnReadFail should succeed, but got error: %v", err) - } + err = controller.OnReadFail(ctx, fmt.Errorf("read fail")) + require.NoError(t, err) errorPath := path.Join(controller.errorDir, controller.sqlFilename) - if err := controller.OnValidationSuccess(ctx); err != nil { - t.Fatalf("OnReadSuccess should succeed, but got error: %v", err) - } + err = controller.OnValidationSuccess(ctx) + require.NoError(t, err) // move sql file from error dir to input dir for OnValidationFail test os.Rename(errorPath, controller.sqlPath) - if err := controller.OnValidationFail(ctx, fmt.Errorf("validation fail")); err != nil { - t.Fatalf("OnValidationFail should succeed, but got error: %v", err) - } + err = controller.OnValidationFail(ctx, fmt.Errorf("validation fail")) + require.NoError(t, err) - if _, err := os.Stat(errorPath); os.IsNotExist(err) { - t.Fatalf("sql file should be moved to error dir, error: %v", err) - } + _, err = os.Stat(errorPath) + require.Falsef(t, os.IsNotExist(err), "sql file should be moved to error dir, error: %v", err) // move sql file from error dir to input dir for OnExecutorComplete test os.Rename(errorPath, controller.sqlPath) @@ -175,16 +144,14 @@ func TestLocalControllerSchemaChange(t *testing.T) { } logPath := path.Join(controller.logDir, controller.sqlFilename) completePath := path.Join(controller.completeDir, controller.sqlFilename) - if err := controller.OnExecutorComplete(ctx, result); err != nil { - t.Fatalf("OnExecutorComplete should succeed, but got error: %v", err) - } - if _, err := os.Stat(completePath); os.IsNotExist(err) { - t.Fatalf("sql file should be moved to complete dir, error: %v", err) - } + err = controller.OnExecutorComplete(ctx, result) + require.NoError(t, err) - if _, err := os.Stat(logPath); os.IsNotExist(err) { - t.Fatalf("sql file should be moved to log dir, error: %v", err) - } + _, err = os.Stat(completePath) + require.Falsef(t, os.IsNotExist(err), "sql file should be moved to complete dir, error: %v", err) + + _, err = os.Stat(logPath) + require.Falsef(t, os.IsNotExist(err), "sql file should be moved to log dir, error: %v", err) // move sql file from error dir to input dir for OnExecutorComplete test os.Rename(completePath, controller.sqlPath) @@ -197,11 +164,9 @@ func TestLocalControllerSchemaChange(t *testing.T) { }}, } - if err := controller.OnExecutorComplete(ctx, result); err != nil { - t.Fatalf("OnExecutorComplete should succeed, but got error: %v", err) - } + err = controller.OnExecutorComplete(ctx, result) + require.NoError(t, err) - if _, err := os.Stat(errorPath); os.IsNotExist(err) { - t.Fatalf("sql file should be moved to error dir, error: %v", err) - } + _, err = os.Stat(errorPath) + require.Falsef(t, os.IsNotExist(err), "sql file should be moved to error dir, error: %v", err) } diff --git a/go/vt/schemamanager/plain_controller_test.go b/go/vt/schemamanager/plain_controller_test.go index ca3352cda82..ea6c845d433 100644 --- a/go/vt/schemamanager/plain_controller_test.go +++ b/go/vt/schemamanager/plain_controller_test.go @@ -21,6 +21,8 @@ import ( "testing" "context" + + "github.com/stretchr/testify/require" ) func TestPlainController(t *testing.T) { @@ -28,50 +30,31 @@ func TestPlainController(t *testing.T) { controller := NewPlainController([]string{sql}, "test_keyspace") ctx := context.Background() err := controller.Open(ctx) - if err != nil { - t.Fatalf("controller.Open should succeed, but got error: %v", err) - } + require.NoError(t, err) keyspace := controller.Keyspace() - if keyspace != "test_keyspace" { - t.Fatalf("expect to get keyspace: 'test_keyspace', but got keyspace: '%s'", keyspace) - } + require.Equal(t, "test_keyspace", keyspace) sqls, err := controller.Read(ctx) - if err != nil { - t.Fatalf("controller.Read should succeed, but got error: %v", err) - } - if len(sqls) != 1 { - t.Fatalf("controller should only get one sql, but got: %v", sqls) - } - if sqls[0] != sql { - t.Fatalf("expect to get sql: '%s', but got: '%s'", sql, sqls[0]) - } + require.NoError(t, err) + require.Len(t, sqls, 1, "controller should only get one sql") + require.Equal(t, sql, sqls[0]) + defer controller.Close() err = controller.OnReadSuccess(ctx) - if err != nil { - t.Fatalf("OnDataSourcerReadSuccess should succeed") - } + require.NoError(t, err) errReadFail := fmt.Errorf("read fail") err = controller.OnReadFail(ctx, errReadFail) - if err != errReadFail { - t.Fatalf("should get error:%v, but get: %v", errReadFail, err) - } + require.ErrorIs(t, err, errReadFail) err = controller.OnValidationSuccess(ctx) - if err != nil { - t.Fatalf("OnValidationSuccess should succeed") - } + require.NoError(t, err) errValidationFail := fmt.Errorf("validation fail") err = controller.OnValidationFail(ctx, errValidationFail) - if err != errValidationFail { - t.Fatalf("should get error:%v, but get: %v", errValidationFail, err) - } + require.ErrorIs(t, err, errValidationFail) err = controller.OnExecutorComplete(ctx, &ExecuteResult{}) - if err != nil { - t.Fatalf("OnExecutorComplete should succeed") - } + require.NoError(t, err) } diff --git a/go/vt/schemamanager/schemamanager_test.go b/go/vt/schemamanager/schemamanager_test.go index b4724241cd1..129600d0527 100644 --- a/go/vt/schemamanager/schemamanager_test.go +++ b/go/vt/schemamanager/schemamanager_test.go @@ -20,10 +20,10 @@ import ( "context" "errors" "fmt" - "strings" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "vitess.io/vitess/go/vt/sqlparser" @@ -61,10 +61,7 @@ func TestSchemaManagerControllerOpenFail(t *testing.T) { ctx := context.Background() _, err := Run(ctx, controller, newFakeExecutor(t)) - if err != errControllerOpen { - t.Fatalf("controller.Open fail, should get error: %v, but get error: %v", - errControllerOpen, err) - } + require.ErrorIs(t, err, errControllerOpen) } func TestSchemaManagerControllerReadFail(t *testing.T) { @@ -72,13 +69,8 @@ func TestSchemaManagerControllerReadFail(t *testing.T) { []string{"select * from test_db"}, false, true, false) ctx := context.Background() _, err := Run(ctx, controller, newFakeExecutor(t)) - if err != errControllerRead { - t.Fatalf("controller.Read fail, should get error: %v, but get error: %v", - errControllerRead, err) - } - if !controller.onReadFailTriggered { - t.Fatalf("OnReadFail should be called") - } + require.ErrorIs(t, err, errControllerRead) + require.True(t, controller.onReadFailTriggered, "OnReadFail should be called") } func TestSchemaManagerValidationFail(t *testing.T) { @@ -87,9 +79,7 @@ func TestSchemaManagerValidationFail(t *testing.T) { ctx := context.Background() _, err := Run(ctx, controller, newFakeExecutor(t)) - if err == nil || !strings.Contains(err.Error(), "failed to parse sql") { - t.Fatalf("run schema change should fail due to executor.Validate fail, but got: %v", err) - } + require.ErrorContains(t, err, "failed to parse sql", "run schema change should fail due to executor.Validate fail") } func TestSchemaManagerExecutorOpenFail(t *testing.T) { @@ -100,9 +90,7 @@ func TestSchemaManagerExecutorOpenFail(t *testing.T) { ctx := context.Background() _, err := Run(ctx, controller, executor) - if err == nil || !strings.Contains(err.Error(), "unknown_keyspace") { - t.Fatalf("run schema change should fail due to executor.Open fail, but got: %v", err) - } + require.ErrorContains(t, err, "unknown_keyspace", "run schema change should fail due to executor.Open fail") } func TestSchemaManagerRun(t *testing.T) { @@ -132,28 +120,14 @@ func TestSchemaManagerRun(t *testing.T) { ctx := context.Background() resp, err := Run(ctx, controller, executor) - if len(resp.UUIDs) > 0 { - t.Fatalf("response should contain an empty list of UUIDs, found %v", len(resp.UUIDs)) - } - - if err != nil { - t.Fatalf("schema change should success but get error: %v", err) - } - if !controller.onReadSuccessTriggered { - t.Fatalf("OnReadSuccess should be called") - } - if controller.onReadFailTriggered { - t.Fatalf("OnReadFail should not be called") - } - if !controller.onValidationSuccessTriggered { - t.Fatalf("OnValidateSuccess should be called") - } - if controller.onValidationFailTriggered { - t.Fatalf("OnValidationFail should not be called") - } - if !controller.onExecutorCompleteTriggered { - t.Fatalf("OnExecutorComplete should be called") - } + require.Lenf(t, resp.UUIDs, 0, "response should contain an empty list of UUIDs") + require.NoError(t, err) + + require.True(t, controller.onReadSuccessTriggered, "OnReadSuccess should be called") + require.False(t, controller.onReadFailTriggered, "OnReadFail should not be called") + require.True(t, controller.onValidationSuccessTriggered, "OnValidateSuccess should be called") + require.False(t, controller.onValidationFailTriggered, "OnValidationFail should not be called") + require.True(t, controller.onExecutorCompleteTriggered, "OnExecutorComplete should be called") }) } } @@ -182,13 +156,8 @@ func TestSchemaManagerExecutorFail(t *testing.T) { ctx := context.Background() resp, err := Run(ctx, controller, executor) - if len(resp.UUIDs) > 0 { - t.Fatalf("response should contain an empty list of UUIDs, found %v", len(resp.UUIDs)) - } - - if err == nil || !strings.Contains(err.Error(), "schema change failed") { - t.Fatalf("schema change should fail, but got err: %v", err) - } + require.Lenf(t, resp.UUIDs, 0, "response should contain an empty list of UUIDs") + require.ErrorContains(t, err, "schema change failed", "schema change should fail") } func TestSchemaManagerExecutorBatchVsStrategyFail(t *testing.T) { @@ -249,19 +218,15 @@ func TestSchemaManagerRegisterControllerFactory(t *testing.T) { }) _, err := GetControllerFactory("unknown") - if err == nil || !strings.Contains(err.Error(), "there is no data sourcer factory") { - t.Fatalf("controller factory is not registered, GetControllerFactory should return an error, but got: %v", err) - } + require.ErrorContains(t, err, "there is no data sourcer factory", "controller factory is not registered, GetControllerFactory should return an error") + _, err = GetControllerFactory("test_controller") - if err != nil { - t.Fatalf("GetControllerFactory should succeed, but get an error: %v", err) - } + require.NoError(t, err) + func() { defer func() { err := recover() - if err == nil { - t.Fatalf("RegisterControllerFactory should fail, it registers a registered ControllerFactory") - } + require.NotNil(t, err, "RegisterControllerFactory should fail, it registers a registered ControllerFactory") }() RegisterControllerFactory( "test_controller", @@ -336,13 +301,13 @@ func newFakeTopo(t *testing.T) *topo.Server { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ts := memorytopo.NewServer(ctx, "test_cell") - if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != nil { - t.Fatalf("CreateKeyspace failed: %v", err) - } + err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}) + require.NoError(t, err) + for i, shard := range []string{"0", "1", "2"} { - if err := ts.CreateShard(ctx, "test_keyspace", shard); err != nil { - t.Fatalf("CreateShard(%v) failed: %v", shard, err) - } + err = ts.CreateShard(ctx, "test_keyspace", shard) + require.NoError(t, err) + tablet := &topodatapb.Tablet{ Alias: &topodatapb.TabletAlias{ Cell: "test_cell", @@ -351,22 +316,23 @@ func newFakeTopo(t *testing.T) *topo.Server { Keyspace: "test_keyspace", Shard: shard, } - if err := ts.CreateTablet(ctx, tablet); err != nil { - t.Fatalf("CreateTablet failed: %v", err) - } - if _, err := ts.UpdateShardFields(ctx, "test_keyspace", shard, func(si *topo.ShardInfo) error { + + err = ts.CreateTablet(ctx, tablet) + require.NoError(t, err) + + _, err = ts.UpdateShardFields(ctx, "test_keyspace", shard, func(si *topo.ShardInfo) error { si.Shard.PrimaryAlias = tablet.Alias return nil - }); err != nil { - t.Fatalf("UpdateShardFields failed: %v", err) - } - } - if err := ts.CreateKeyspace(ctx, "unsharded_keyspace", &topodatapb.Keyspace{}); err != nil { - t.Fatalf("CreateKeyspace failed: %v", err) - } - if err := ts.CreateShard(ctx, "unsharded_keyspace", "0"); err != nil { - t.Fatalf("CreateShard(%v) failed: %v", "0", err) + }) + require.NoError(t, err) } + + err = ts.CreateKeyspace(ctx, "unsharded_keyspace", &topodatapb.Keyspace{}) + require.NoError(t, err) + + err = ts.CreateShard(ctx, "unsharded_keyspace", "0") + require.NoError(t, err) + tablet := &topodatapb.Tablet{ Alias: &topodatapb.TabletAlias{ Cell: "test_cell", @@ -375,15 +341,14 @@ func newFakeTopo(t *testing.T) *topo.Server { Keyspace: "test_keyspace", Shard: "0", } - if err := ts.CreateTablet(ctx, tablet); err != nil { - t.Fatalf("CreateTablet failed: %v", err) - } - if _, err := ts.UpdateShardFields(ctx, "unsharded_keyspace", "0", func(si *topo.ShardInfo) error { + err = ts.CreateTablet(ctx, tablet) + require.NoError(t, err) + + _, err = ts.UpdateShardFields(ctx, "unsharded_keyspace", "0", func(si *topo.ShardInfo) error { si.Shard.PrimaryAlias = tablet.Alias return nil - }); err != nil { - t.Fatalf("UpdateShardFields failed: %v", err) - } + }) + require.NoError(t, err) return ts } diff --git a/go/vt/schemamanager/tablet_executor_test.go b/go/vt/schemamanager/tablet_executor_test.go index 0ae960e6e9c..a683ef4d22e 100644 --- a/go/vt/schemamanager/tablet_executor_test.go +++ b/go/vt/schemamanager/tablet_executor_test.go @@ -19,11 +19,11 @@ package schemamanager import ( "context" "fmt" - "strings" "testing" "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" topodatapb "vitess.io/vitess/go/vt/proto/topodata" "vitess.io/vitess/go/vt/topo/memorytopo" @@ -43,15 +43,13 @@ func TestTabletExecutorOpen(t *testing.T) { executor := newFakeExecutor(t) ctx := context.Background() - if err := executor.Open(ctx, "test_keyspace"); err != nil { - t.Fatalf("executor.Open should succeed") - } + err := executor.Open(ctx, "test_keyspace") + require.NoError(t, err) defer executor.Close() - if err := executor.Open(ctx, "test_keyspace"); err != nil { - t.Fatalf("open an opened executor should also succeed") - } + err = executor.Open(ctx, "test_keyspace") + require.NoError(t, err, "open an opened executor should also succeed") } func TestTabletExecutorOpenWithEmptyPrimaryAlias(t *testing.T) { @@ -69,13 +67,12 @@ func TestTabletExecutorOpenWithEmptyPrimaryAlias(t *testing.T) { } // This will create the Keyspace, Shard and Tablet record. // Since this is a replica tablet, the Shard will have no primary. - if err := ts.InitTablet(ctx, tablet, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/); err != nil { - t.Fatalf("InitTablet failed: %v", err) - } + err := ts.InitTablet(ctx, tablet, false /*allowPrimaryOverride*/, true /*createShardAndKeyspace*/, false /*allowUpdate*/) + require.NoError(t, err) + executor := NewTabletExecutor("TestTabletExecutorOpenWithEmptyPrimaryAlias", ts, newFakeTabletManagerClient(), logutil.NewConsoleLogger(), testWaitReplicasTimeout, 0, sqlparser.NewTestParser()) - if err := executor.Open(ctx, "test_keyspace"); err == nil || !strings.Contains(err.Error(), "does not have a primary") { - t.Fatalf("executor.Open() = '%v', want error", err) - } + err = executor.Open(ctx, "test_keyspace") + require.ErrorContains(t, err, "does not have a primary") executor.Close() } @@ -115,42 +112,37 @@ func TestTabletExecutorValidate(t *testing.T) { "ALTER SCHEMA db_name CHARACTER SET = utf8mb4", } - if err := executor.Validate(ctx, sqls); err == nil { - t.Fatalf("validate should fail because executor is closed") - } + err := executor.Validate(ctx, sqls) + require.Error(t, err, "validate should fail because executor is closed") executor.Open(ctx, "test_keyspace") defer executor.Close() // schema changes with DMLs should fail - if err := executor.Validate(ctx, []string{ - "INSERT INTO test_table VALUES(1)"}); err == nil { - t.Fatalf("schema changes are for DDLs") - } + err = executor.Validate(ctx, []string{ + "INSERT INTO test_table VALUES(1)", + }) + require.Error(t, err, "schema changes are for DDLs") // validates valid ddls - if err := executor.Validate(ctx, sqls); err != nil { - t.Fatalf("executor.Validate should succeed, but got error: %v", err) - } + err = executor.Validate(ctx, sqls) + require.NoError(t, err) // alter a table with more than 100,000 rows - if err := executor.Validate(ctx, []string{ + err = executor.Validate(ctx, []string{ "ALTER TABLE test_table_03 ADD COLUMN new_id bigint(20)", - }); err != nil { - t.Fatalf("executor.Validate should not fail, even for a table with more than 100,000 rows") - } + }) + require.NoError(t, err, "executor.Validate should not fail, even for a table with more than 100,000 rows") - if err := executor.Validate(ctx, []string{ + err = executor.Validate(ctx, []string{ "TRUNCATE TABLE test_table_04", - }); err != nil { - t.Fatalf("executor.Validate should succeed, drop a table with more than 2,000,000 rows is allowed") - } + }) + require.NoError(t, err, "executor.Validate should succeed, truncate a table with more than 2,000,000 rows is allowed") - if err := executor.Validate(ctx, []string{ + err = executor.Validate(ctx, []string{ "DROP TABLE test_table_04", - }); err != nil { - t.Fatalf("executor.Validate should succeed, drop a table with more than 2,000,000 rows is allowed") - } + }) + require.NoError(t, err, "executor.Validate should succeed, drop a table with more than 2,000,000 rows is allowed") } func TestTabletExecutorDML(t *testing.T) { @@ -186,10 +178,10 @@ func TestTabletExecutorDML(t *testing.T) { defer executor.Close() // schema changes with DMLs should fail - if err := executor.Validate(ctx, []string{ - "INSERT INTO test_table VALUES(1)"}); err != nil { - t.Fatalf("executor.Validate should succeed, for DML to unsharded keyspace") - } + err := executor.Validate(ctx, []string{ + "INSERT INTO test_table VALUES(1)", + }) + require.NoError(t, err, "executor.Validate should succeed, for DML to unsharded keyspace") } func TestTabletExecutorExecute(t *testing.T) { @@ -199,9 +191,7 @@ func TestTabletExecutorExecute(t *testing.T) { sqls := []string{"DROP TABLE unknown_table"} result := executor.Execute(ctx, sqls) - if result.ExecutorErr == "" { - t.Fatalf("execute should fail, call execute.Open first") - } + require.NotEmpty(t, result.ExecutorErr, "execute should fail, call execute.Open first") } func TestIsOnlineSchemaDDL(t *testing.T) { diff --git a/go/vt/schemamanager/ui_controller_test.go b/go/vt/schemamanager/ui_controller_test.go index 1823717e0e3..de1b0bcda66 100644 --- a/go/vt/schemamanager/ui_controller_test.go +++ b/go/vt/schemamanager/ui_controller_test.go @@ -19,10 +19,11 @@ package schemamanager import ( "fmt" "net/http/httptest" - "strings" "testing" "context" + + "github.com/stretchr/testify/require" ) func TestUIController(t *testing.T) { @@ -32,68 +33,37 @@ func TestUIController(t *testing.T) { ctx := context.Background() err := controller.Open(ctx) - if err != nil { - t.Fatalf("controller.Open should succeed, but got error: %v", err) - } + require.NoError(t, err) keyspace := controller.Keyspace() - if keyspace != "test_keyspace" { - t.Fatalf("expect to get keyspace: 'test_keyspace', but got keyspace: '%s'", keyspace) - } + require.Equal(t, "test_keyspace", keyspace) sqls, err := controller.Read(ctx) - if err != nil { - t.Fatalf("controller.Read should succeed, but got error: %v", err) - } - if len(sqls) != 1 { - t.Fatalf("controller should only get one sql, but got: %v", sqls) - } - if sqls[0] != sql { - t.Fatalf("expect to get sql: '%s', but got: '%s'", sql, sqls[0]) - } + require.NoError(t, err) + require.Len(t, sqls, 1, "controller should only get one sql") + require.Equal(t, sql, sqls[0]) + defer controller.Close() + err = controller.OnReadSuccess(ctx) - if err != nil { - t.Fatalf("OnDataSourcerReadSuccess should succeed") - } - if !strings.Contains(response.Body.String(), "OnReadSuccess, sqls") { - t.Fatalf("controller.OnReadSuccess should write to http response") - } + require.NoError(t, err) + require.Contains(t, response.Body.String(), "OnReadSuccess, sqls", "controller.OnReadSuccess should write to http response") + errReadFail := fmt.Errorf("read fail") err = controller.OnReadFail(ctx, errReadFail) - if err != errReadFail { - t.Fatalf("should get error:%v, but get: %v", errReadFail, err) - } - - if !strings.Contains(response.Body.String(), "OnReadFail, error") { - t.Fatalf("controller.OnReadFail should write to http response") - } + require.ErrorIs(t, err, errReadFail) + require.Contains(t, response.Body.String(), "OnReadFail, error", "controller.OnReadFail should write to http response") err = controller.OnValidationSuccess(ctx) - if err != nil { - t.Fatalf("OnValidationSuccess should succeed") - } - - if !strings.Contains(response.Body.String(), "OnValidationSuccess, sqls") { - t.Fatalf("controller.OnValidationSuccess should write to http response") - } + require.NoError(t, err) + require.Contains(t, response.Body.String(), "OnValidationSuccess, sqls", "controller.OnValidationSuccess should write to http response") errValidationFail := fmt.Errorf("validation fail") err = controller.OnValidationFail(ctx, errValidationFail) - if err != errValidationFail { - t.Fatalf("should get error:%v, but get: %v", errValidationFail, err) - } - - if !strings.Contains(response.Body.String(), "OnValidationFail, error") { - t.Fatalf("controller.OnValidationFail should write to http response") - } + require.ErrorIs(t, err, errValidationFail) + require.Contains(t, response.Body.String(), "OnValidationFail, error", "controller.OnValidationFail should write to http response") err = controller.OnExecutorComplete(ctx, &ExecuteResult{}) - if err != nil { - t.Fatalf("OnExecutorComplete should succeed") - } - - if !strings.Contains(response.Body.String(), "Executor succeeds") { - t.Fatalf("controller.OnExecutorComplete should write to http response") - } + require.NoError(t, err) + require.Contains(t, response.Body.String(), "Executor succeeds", "controller.OnExecutorComplete should write to http response") }