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: Add missing tests for go/vt/mysqlctl #15585

Merged
merged 9 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 5 additions & 5 deletions go/test/endtoend/mysqlctl/mysqlctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,21 @@ func initCluster(shardNames []string, totalTabletsRequired int) {
func TestRestart(t *testing.T) {
defer cluster.PanicHandler(t)
err := primaryTablet.MysqlctlProcess.Stop()
require.Nil(t, err)
require.NoError(t, err)
primaryTablet.MysqlctlProcess.CleanupFiles(primaryTablet.TabletUID)
err = primaryTablet.MysqlctlProcess.Start()
require.Nil(t, err)
require.NoError(t, err)
}

func TestAutoDetect(t *testing.T) {
defer cluster.PanicHandler(t)

err := clusterInstance.Keyspaces[0].Shards[0].Vttablets[0].VttabletProcess.Setup()
require.Nil(t, err, "error should be nil")
require.NoError(t, err)
err = clusterInstance.Keyspaces[0].Shards[0].Vttablets[1].VttabletProcess.Setup()
require.Nil(t, err, "error should be nil")
require.NoError(t, err)

// Reparent tablets, which requires flavor detection
err = clusterInstance.VtctldClientProcess.InitializeShard(keyspaceName, shardName, cell, primaryTablet.TabletUID)
require.Nil(t, err, "error should be nil")
require.NoError(t, err)
}
54 changes: 51 additions & 3 deletions go/vt/mysqlctl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import (
"testing"
"time"

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

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/test/utils"

"vitess.io/vitess/go/mysql/replication"
Expand Down Expand Up @@ -148,9 +150,8 @@ func TestFindFilesToBackupWithoutRedoLog(t *testing.T) {
rocksdbDir := path.Join(dataDir, ".rocksdb")
sdiOnlyDir := path.Join(dataDir, "sdi_dir")
for _, s := range []string{innodbDataDir, innodbLogDir, dataDbDir, extraDir, outsideDbDir, rocksdbDir, sdiOnlyDir} {
if err := os.MkdirAll(s, os.ModePerm); err != nil {
t.Fatalf("failed to create directory %v: %v", s, err)
}
err := os.MkdirAll(s, os.ModePerm)
require.NoErrorf(t, err, "failed to create directory %v: %v", s, err)
}

innodbLogFile := "innodb_log_1"
Expand Down Expand Up @@ -678,3 +679,50 @@ func (fbe *fakeBackupRestoreEnv) setStats(stats *backupstats.FakeStats) {
fbe.restoreParams.Stats = nil
fbe.stats = nil
}

func TestParseBackupName(t *testing.T) {
// backup name doesn't contain 3 parts
_, _, err := ParseBackupName("dir", "asd.saddsa")
assert.ErrorContains(t, err, "cannot backup name")

// Invalid time
bt, al, err := ParseBackupName("dir", "2024-03-18.123.tablet_id")
assert.Nil(t, bt)
assert.Nil(t, al)
assert.NoError(t, err)

// Valid case
bt, al, err = ParseBackupName("dir", "2024-03-18.180911.cell1-42")
assert.NotNil(t, *bt, time.Date(2024, 03, 18, 18, 9, 11, 0, time.UTC))
assert.Equal(t, "cell1", al.Cell)
assert.Equal(t, uint32(42), al.Uid)
assert.NoError(t, err)
}

func TestShouldRestore(t *testing.T) {
env := createFakeBackupRestoreEnv(t)

b, err := ShouldRestore(env.ctx, env.restoreParams)
assert.False(t, b)
assert.Error(t, err)

env.restoreParams.DeleteBeforeRestore = true
b, err = ShouldRestore(env.ctx, env.restoreParams)
assert.True(t, b)
assert.NoError(t, err)
env.restoreParams.DeleteBeforeRestore = false

env.mysqld.FetchSuperQueryMap = map[string]*sqltypes.Result{
"SHOW DATABASES": {Rows: [][]sqltypes.Value{{sqltypes.NewVarBinary("any_db")}}},
}
b, err = ShouldRestore(env.ctx, env.restoreParams)
assert.NoError(t, err)
assert.True(t, b)

env.mysqld.FetchSuperQueryMap = map[string]*sqltypes.Result{
"SHOW DATABASES": {Rows: [][]sqltypes.Value{{sqltypes.NewVarBinary("test")}}},
}
b, err = ShouldRestore(env.ctx, env.restoreParams)
assert.False(t, b)
assert.NoError(t, err)
}
92 changes: 26 additions & 66 deletions go/vt/mysqlctl/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ package mysqlctl
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"reflect"
"strings"
"testing"
"time"

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

"vitess.io/vitess/go/vt/logutil"
Expand All @@ -47,15 +46,8 @@ func TestGetExtensionFromEngine(t *testing.T) {
for _, tt := range tests {
t.Run(tt.engine, func(t *testing.T) {
ext, err := getExtensionFromEngine(tt.engine)
// if err != tt.err {
if !errors.Is(err, tt.err) {
t.Errorf("got err: %v; expected: %v", err, tt.err)
}
// }

if ext != tt.extension {
t.Errorf("got err: %v; expected: %v", ext, tt.extension)
}
assert.ErrorIs(t, err, tt.err)
assert.Equal(t, tt.extension, ext)
})
}
}
Expand All @@ -69,33 +61,20 @@ func TestBuiltinCompressors(t *testing.T) {
var compressed, decompressed bytes.Buffer
reader := bytes.NewReader(data)
compressor, err := newBuiltinCompressor(engine, &compressed, logger)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

_, err = io.Copy(compressor, reader)
if err != nil {
t.Error(err)
return
}
require.NoError(t, err)

compressor.Close()
decompressor, err := newBuiltinDecompressor(engine, &compressed, logger)
if err != nil {
t.Error(err)
return
}
require.NoError(t, err)

_, err = io.Copy(&decompressed, decompressor)
if err != nil {
t.Error(err)
return
}
decompressor.Close()
if len(data) != len(decompressed.Bytes()) {
t.Errorf("Different size of original (%d bytes) and uncompressed (%d bytes) data", len(data), len(decompressed.Bytes()))
}
require.NoError(t, err)

if !reflect.DeepEqual(data, decompressed.Bytes()) {
t.Error("decompressed content differs from the original")
}
decompressor.Close()
assert.Equal(t, data, decompressed.Bytes())
})
}
}
Expand Down Expand Up @@ -142,33 +121,20 @@ func TestExternalCompressors(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
compressor, err := newExternalCompressor(ctx, tt.compress, &compressed, logger)
if err != nil {
t.Error(err)
return
}
require.NoError(t, err)

_, err = io.Copy(compressor, reader)
if err != nil {
t.Error(err)
return
}
require.NoError(t, err)

compressor.Close()
decompressor, err := newExternalDecompressor(ctx, tt.decompress, &compressed, logger)
if err != nil {
t.Error(err)
return
}
require.NoError(t, err)

_, err = io.Copy(&decompressed, decompressor)
if err != nil {
t.Error(err)
return
}
require.NoError(t, err)

decompressor.Close()
if len(data) != len(decompressed.Bytes()) {
t.Errorf("Different size of original (%d bytes) and uncompressed (%d bytes) data", len(data), len(decompressed.Bytes()))
}
if !reflect.DeepEqual(data, decompressed.Bytes()) {
t.Error("decompressed content differs from the original")
}
assert.Equal(t, data, decompressed.Bytes())

})
}
Expand All @@ -190,19 +156,13 @@ func TestValidateExternalCmd(t *testing.T) {
t.Run(fmt.Sprintf("Test #%d", i+1), func(t *testing.T) {
CmdName := tt.cmdName
path, err := validateExternalCmd(CmdName)
if tt.path != "" {
if !strings.HasSuffix(path, tt.path) {
t.Errorf("Expected path \"%s\" to include \"%s\"", path, tt.path)
}
}

assert.Contains(t, path, tt.path)

if tt.errStr == "" {
if err != nil {
t.Errorf("Expected result \"%v\", got \"%v\"", "<nil>", err)
}
assert.NoError(t, err)
} else {
if !strings.Contains(fmt.Sprintf("%v", err), tt.errStr) {
t.Errorf("Expected result \"%v\", got \"%v\"", tt.errStr, err)
}
assert.ErrorContains(t, err, tt.errStr)
}
})
}
Expand Down
51 changes: 16 additions & 35 deletions go/vt/mysqlctl/mycnf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package mysqlctl
import (
"bytes"
"os"
"strings"
"sync"
"testing"
"time"

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

"vitess.io/vitess/go/mysql/collations"
Expand Down Expand Up @@ -120,14 +120,11 @@ func NoTestMycnfHook(t *testing.T) {
servenv.OnClose(mysqld.Close)

err := mysqld.InitConfig(cnf)
if err != nil {
t.Errorf("err: %v", err)
}
require.NoError(t, err)

_, err = os.ReadFile(cnf.Path)
if err != nil {
t.Errorf("failed reading, err %v", err)
return
}
require.NoError(t, err)

mycnf := NewMycnf(uid, 0)
mycnf.Path = cnf.Path
mycnf, err = ReadMycnf(mycnf, 0)
Expand All @@ -137,33 +134,17 @@ func NoTestMycnfHook(t *testing.T) {
t.Logf("socket file %v", mycnf.SocketFile)
}
// Tablet UID should be 11111, which determines tablet/data dir.
if got, want := mycnf.DataDir, "/vt_0000011111/"; !strings.Contains(got, want) {
t.Errorf("mycnf.DataDir = %v, want *%v*", got, want)
}
assert.Contains(t, mycnf.DataDir, "/vt_0000011111/")

// MySQL server-id should be 22222, different from Tablet UID.
if got, want := mycnf.ServerID, uint32(22222); got != want {
t.Errorf("mycnf.ServerID = %v, want %v", got, want)
}
assert.Equal(t, uint32(22222), mycnf.ServerID)

// check that the env variables we set were passed correctly to the hook
if got, want := mycnf.lookup("KEYSPACE"), "test-messagedb"; got != want {
t.Errorf("Error passing env %v, got %v, want %v", "KEYSPACE", got, want)
}
if got, want := mycnf.lookup("SHARD"), "0"; got != want {
t.Errorf("Error passing env %v, got %v, want %v", "SHARD", got, want)
}
if got, want := mycnf.lookup("TABLET_TYPE"), "PRIMARY"; got != want {
t.Errorf("Error passing env %v, got %v, want %v", "TABLET_TYPE", got, want)
}
if got, want := mycnf.lookup("TABLET_ID"), "11111"; got != want {
t.Errorf("Error passing env %v, got %v, want %v", "TABLET_ID", got, want)
}
if got, want := mycnf.lookup("TABLET_DIR"), "/vt_0000011111"; !strings.Contains(got, want) {
t.Errorf("Error passing env %v, got %v, want %v", "TABLET_DIR", got, want)
}
if got, want := mycnf.lookup("MYSQL_PORT"), "15306"; got != want {
t.Errorf("Error passing env %v, got %v, want %v", "MYSQL_PORT", got, want)
}
if got := mycnf.lookup("MY_VAR"); got != "" {
t.Errorf("Unexpected env %v set to %v", "MY_VAR", got)
}
assert.Equal(t, "test-messagedb", mycnf.lookup("KEYSPACE"))
assert.Equal(t, "test-0", mycnf.lookup("SHARD"))
assert.Equal(t, "PRIMARY", mycnf.lookup("TABLET_TYPE"))
assert.Equal(t, "11111", mycnf.lookup("TABLET_ID"))
assert.Equal(t, "/vt_0000011111", mycnf.lookup("TABLET_DIR"))
assert.Equal(t, "15306", mycnf.lookup("MYSQL_PORT"))
assert.Equal(t, "", mycnf.lookup("MY_VAR"))
}
Loading
Loading