Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Replace t.fatalf with testify require in go/vt/schemamanager #15600

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 54 additions & 89 deletions go/vt/schemamanager/local_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,90 +21,74 @@ import (
"fmt"
"os"
"path"
"reflect"
"strings"
"testing"

"github.com/stretchr/testify/require"

querypb "vitess.io/vitess/go/vt/proto/query"
)

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()
}

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)",
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
43 changes: 13 additions & 30 deletions go/vt/schemamanager/plain_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,40 @@ import (
"testing"

"context"

"github.com/stretchr/testify/require"
)

func TestPlainController(t *testing.T) {
sql := "CREATE TABLE test_table (pk int)"
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)
}
Loading
Loading