Skip to content

Commit

Permalink
refactor: store the entire mycnf
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <[email protected]>
  • Loading branch information
GuptaManan100 committed Apr 8, 2024
1 parent 1271cf7 commit 2d1f5a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
19 changes: 8 additions & 11 deletions go/test/endtoend/utils/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import (

const mysqlShutdownTimeout = 1 * time.Minute

var serverID uint32

// NewMySQL creates a new MySQL server using the local mysqld binary. The name of the database
// will be set to `dbName`. SQL queries that need to be executed on the new MySQL instance
// can be passed through the `schemaSQL` argument.
Expand All @@ -56,7 +54,7 @@ func NewMySQL(cluster *cluster.LocalProcessCluster, dbName string, schemaSQL ...
}
sqls = append(sqls, split...)
}
mysqlParam, _, closer, error := NewMySQLWithMysqld(cluster.GetAndReservePort(), cluster.Hostname, dbName, sqls...)
mysqlParam, _, _, closer, error := NewMySQLWithMysqld(cluster.GetAndReservePort(), cluster.Hostname, dbName, sqls...)
return mysqlParam, closer, error
}

Expand All @@ -67,7 +65,6 @@ func CreateMysqldAndMycnf(tabletUID uint32, mysqlSocket string, mysqlPort int) (
if err := mycnf.RandomizeMysqlServerID(); err != nil {
return nil, nil, fmt.Errorf("couldn't generate random MySQL server_id: %v", err)
}
serverID = mycnf.ServerID
if mysqlSocket != "" {
mycnf.SocketFile = mysqlSocket
}
Expand All @@ -78,24 +75,24 @@ func CreateMysqldAndMycnf(tabletUID uint32, mysqlSocket string, mysqlPort int) (
return mysqlctl.NewMysqld(&cfg), mycnf, nil
}

func NewMySQLWithMysqld(port int, hostname, dbName string, schemaSQL ...string) (mysql.ConnParams, *mysqlctl.Mysqld, func(), error) {
func NewMySQLWithMysqld(port int, hostname, dbName string, schemaSQL ...string) (mysql.ConnParams, *mysqlctl.Mysqld, *mysqlctl.Mycnf, func(), error) {
mysqlDir, err := createMySQLDir()
if err != nil {
return mysql.ConnParams{}, nil, nil, err
return mysql.ConnParams{}, nil, nil, nil, err
}
initMySQLFile, err := createInitSQLFile(mysqlDir, dbName)
if err != nil {
return mysql.ConnParams{}, nil, nil, err
return mysql.ConnParams{}, nil, nil, nil, err
}

mysqlPort := port
mysqld, mycnf, err := CreateMysqldAndMycnf(0, "", mysqlPort)
if err != nil {
return mysql.ConnParams{}, nil, nil, err
return mysql.ConnParams{}, nil, nil, nil, err
}
err = initMysqld(mysqld, mycnf, initMySQLFile)
if err != nil {
return mysql.ConnParams{}, nil, nil, err
return mysql.ConnParams{}, nil, nil, nil, err
}

params := mysql.ConnParams{
Expand All @@ -107,10 +104,10 @@ func NewMySQLWithMysqld(port int, hostname, dbName string, schemaSQL ...string)
for _, sql := range schemaSQL {
err = prepareMySQLWithSchema(params, sql)
if err != nil {
return mysql.ConnParams{}, nil, nil, err
return mysql.ConnParams{}, nil, nil, nil, err
}
}
return params, mysqld, func() {
return params, mysqld, mycnf, func() {
ctx := context.Background()
_ = mysqld.Teardown(ctx, mycnf, true, mysqlShutdownTimeout)
}, nil
Expand Down
5 changes: 3 additions & 2 deletions go/test/endtoend/utils/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
clusterInstance *cluster.LocalProcessCluster
mysqlParams mysql.ConnParams
mysqld *mysqlctl.Mysqld
mycnf *mysqlctl.Mycnf
keyspaceName = "ks"
cell = "test"
schemaSQL = `create table t1(
Expand All @@ -54,7 +55,7 @@ func TestMain(m *testing.M) {

var closer func()
var err error
mysqlParams, mysqld, closer, err = NewMySQLWithMysqld(clusterInstance.GetAndReservePort(), clusterInstance.Hostname, keyspaceName, schemaSQL)
mysqlParams, mysqld, mycnf, closer, err = NewMySQLWithMysqld(clusterInstance.GetAndReservePort(), clusterInstance.Hostname, keyspaceName, schemaSQL)
if err != nil {
fmt.Println(err)
return 1
Expand Down Expand Up @@ -142,7 +143,7 @@ func TestGetServerID(t *testing.T) {

sid, err := mysqld.GetServerID(context.Background())
assert.NoError(t, err)
assert.Equal(t, serverID, sid)
assert.Equal(t, mycnf.ServerID, sid)

suuid, err := mysqld.GetServerUUID(context.Background())
assert.NoError(t, err)
Expand Down

0 comments on commit 2d1f5a3

Please sign in to comment.