Skip to content

Commit

Permalink
WiP4
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <[email protected]>
  • Loading branch information
beingnoble03 committed Mar 27, 2024
1 parent 07a03e8 commit c2eb9eb
Show file tree
Hide file tree
Showing 8 changed files with 995 additions and 64 deletions.
82 changes: 70 additions & 12 deletions go/vt/mysqlctl/mysqld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql/fakesqldb"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/dbconfigs"
)

Expand Down Expand Up @@ -216,19 +218,75 @@ func TestRunMysqlUpgrade(t *testing.T) {
err := testMysqld.RunMysqlUpgrade(ctx)
assert.NoError(t, err)

// TODO: Look for more tests
// TODO: Add more tests
}

// func TestMysqldInit(t *testing.T) {
// os.Remove(MycnfPath)
// testMysqld := NewMysqld(&dbconfigs.GlobalDBConfigs)
// defer testMysqld.Close()
func TestGetDbaConnection(t *testing.T) {
db := fakesqldb.New(t)
defer db.Close()

// ctx := context.Background()
// uid := uint32(11111)
// mycnf := NewMycnf(uid, 0)
// mycnf.Path = MycnfPath
// err := testMysqld.Init(ctx, mycnf, "")
params := db.ConnParams()
cp := *params
dbc := dbconfigs.NewTestDBConfigs(cp, cp, "fakesqldb")

// assert.NoError(t, err)
// }
testMysqld := NewMysqld(dbc)
defer testMysqld.Close()

ctx := context.Background()

conn, err := testMysqld.GetDbaConnection(ctx)
assert.NoError(t, err)
assert.NoError(t, conn.Ping())
defer conn.Close()
}

func TestGetVersionString(t *testing.T) {
db := fakesqldb.New(t)
defer db.Close()

params := db.ConnParams()
cp := *params
dbc := dbconfigs.NewTestDBConfigs(cp, cp, "fakesqldb")

testMysqld := NewMysqld(dbc)
defer testMysqld.Close()

ctx := context.Background()
str, err := testMysqld.GetVersionString(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, str)

ver := "test_version"
db.AddQuery("SELECT 1", &sqltypes.Result{})
db.AddQuery(versionSQLQuery, sqltypes.MakeTestResult(sqltypes.MakeTestFields("test_field", "varchar"), ver))

str, err = testMysqld.GetVersionString(ctx)
assert.Equal(t, ver, str)
assert.NoError(t, err)
}

func TestGetVersionComment(t *testing.T) {
db := fakesqldb.New(t)
defer db.Close()

params := db.ConnParams()
cp := *params
dbc := dbconfigs.NewTestDBConfigs(cp, cp, "fakesqldb")

db.AddQuery("SELECT 1", &sqltypes.Result{})
db.AddQuery("select @@global.version_comment", sqltypes.MakeTestResult(sqltypes.MakeTestFields("@@global.version_comment", "varchar"), "test_version1", "test_version2"))

testMysqld := NewMysqld(dbc)
defer testMysqld.Close()

ctx := context.Background()
_, err := testMysqld.GetVersionComment(ctx)
assert.ErrorContains(t, err, "unexpected result length")

ver := "test_version"
db.AddQuery("select @@global.version_comment", sqltypes.MakeTestResult(sqltypes.MakeTestFields("@@global.version_comment", "varchar"), ver))

str, err := testMysqld.GetVersionComment(ctx)
assert.NoError(t, err)
assert.Equal(t, ver, str)
}
44 changes: 44 additions & 0 deletions go/vt/mysqlctl/permissions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mysqlctl

import (
"testing"

"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/mysql/fakesqldb"
"vitess.io/vitess/go/sqltypes"
)

func TestGetPermissions(t *testing.T) {
db := fakesqldb.New(t)
defer db.Close()

testMysqld := NewFakeMysqlDaemon(db)
defer testMysqld.Close()

testMysqld.FetchSuperQueryMap = map[string]*sqltypes.Result{
"SELECT * FROM mysql.user ORDER BY host, user": sqltypes.MakeTestResult(sqltypes.MakeTestFields("host|user", "varchar|varchar"), "test_host1|test_user1", "test_host2|test_user2"),
"SELECT * FROM mysql.db ORDER BY host, db, user": sqltypes.MakeTestResult(sqltypes.MakeTestFields("host|user|db", "varchar|varchar|varchar"), "test_host1|test_user1|test_db1", "test_host2|test_user2|test_db2"),
}

per, err := GetPermissions(testMysqld)
assert.NoError(t, err)
assert.Len(t, per.DbPermissions, 2)
assert.Len(t, per.UserPermissions, 2)
}
52 changes: 52 additions & 0 deletions go/vt/mysqlctl/redo_log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mysqlctl

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/mysql/fakesqldb"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/dbconfigs"
)

func TestProcessCanDisableRedoLog(t *testing.T) {
db := fakesqldb.New(t)
defer db.Close()

params := db.ConnParams()
cp := *params
dbc := dbconfigs.NewTestDBConfigs(cp, cp, "fakesqldb")

db.AddQuery("SELECT 1", &sqltypes.Result{})
db.AddQuery("SELECT variable_value FROM performance_schema.global_status WHERE variable_name = 'innodb_redo_log_enabled'", sqltypes.MakeTestResult(sqltypes.MakeTestFields("field1", "varchar"), "val1"))

testMysqld := NewMysqld(dbc)
defer testMysqld.Close()

res, err := testMysqld.ProcessCanDisableRedoLog(context.Background())
assert.NoError(t, err)
assert.True(t, res)

db.AddQuery("SELECT variable_value FROM performance_schema.global_status WHERE variable_name = 'innodb_redo_log_enabled'", &sqltypes.Result{})
res, err = testMysqld.ProcessCanDisableRedoLog(context.Background())
assert.Error(t, err)
assert.False(t, res)
}
45 changes: 45 additions & 0 deletions go/vt/mysqlctl/reparent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ limitations under the License.
package mysqlctl

import (
"context"
"testing"

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

"vitess.io/vitess/go/mysql/fakesqldb"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/logutil"
)

Expand All @@ -48,3 +52,44 @@ func TestPopulateReparentJournal(t *testing.T) {
want := `INSERT INTO _vt.reparent_journal (time_created_ns, action_name, primary_alias, replication_position) VALUES (1, 'action', 'primaryAlias', 'MySQL56/145e508e-ae54-11e9-8ce6-46824dd1815e:1-3,1e51f8be-ae54-11e9-a7c6-4280a041109b:1-3,47b59de1-b368-11e9-b48b-624401d35560:1-152981,557def0a-b368-11e9-84ed-f6fffd91cc57:1-3,599ef589-ae55-11e9-9688-ca1f44501925:1-14857169,b9ce485d-b36b-11e9-9b17-2a6e0a6011f4:1-371262')`
assert.Equal(t, want, res)
}

func TestWaitForReparentJournal(t *testing.T) {
db := fakesqldb.New(t)
defer db.Close()

params := db.ConnParams()
cp := *params
dbc := dbconfigs.NewTestDBConfigs(cp, cp, "fakesqldb")

db.AddQuery("SELECT 1", &sqltypes.Result{})
db.AddQuery("SELECT action_name, primary_alias, replication_position FROM _vt.reparent_journal WHERE time_created_ns=5", sqltypes.MakeTestResult(sqltypes.MakeTestFields("test_field", "varchar"), "test_row"))

testMysqld := NewMysqld(dbc)
defer testMysqld.Close()

ctx := context.Background()
err := testMysqld.WaitForReparentJournal(ctx, 5)
assert.NoError(t, err)
}

func TestPromote(t *testing.T) {
db := fakesqldb.New(t)
defer db.Close()

params := db.ConnParams()
cp := *params
dbc := dbconfigs.NewTestDBConfigs(cp, cp, "fakesqldb")

db.AddQuery("SELECT 1", &sqltypes.Result{})
db.AddQuery("STOP SLAVE", &sqltypes.Result{})
db.AddQuery("RESET SLAVE ALL", &sqltypes.Result{})
db.AddQuery("FLUSH BINARY LOGS", &sqltypes.Result{})
db.AddQuery("SELECT @@global.gtid_executed", sqltypes.MakeTestResult(sqltypes.MakeTestFields("test_field", "varchar"), "8bc65c84-3fe4-11ed-a912-257f0fcdd6c9:1-8,8bc65c84-3fe4-11ed-a912-257f0fcdd6c9:12-17"))

testMysqld := NewMysqld(dbc)
defer testMysqld.Close()

pos, err := testMysqld.Promote(map[string]string{})
assert.NoError(t, err)
assert.Equal(t, "8bc65c84-3fe4-11ed-a912-257f0fcdd6c9:1-8:12-17", pos.String())
}
Loading

0 comments on commit c2eb9eb

Please sign in to comment.