Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: avoid using test cleanup (#9907)

Danny Xu9b3601cf5f35
diff --git a/backend/tests/ghost_test.go b/backend/tests/ghost_test.go
index 7c9313fac5..6379ab5c64 100644
--- a/backend/tests/ghost_test.go
+++ b/backend/tests/ghost_test.go
@@ -178,9 +178,11 @@ func TestGhostTenant(t *testing.T) {
// Provision instances.
var testInstances []*v1pb.Instance
var prodInstances []*v1pb.Instance
+ var stoppers []func()
for i := 0; i < testTenantNumber; i++ {
- port, err := getMySQLInstanceForGhostTest(t)
+ port, stopper, err := getMySQLInstanceForGhostTest(t)
a.NoError(err)
+ stoppers = append(stoppers, stopper)
// Add the provisioned instances.
instance, err := ctl.instanceServiceClient.CreateInstance(ctx, &v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance", 10),
@@ -196,8 +198,9 @@ func TestGhostTenant(t *testing.T) {
testInstances = append(testInstances, instance)
}
for i := 0; i < prodTenantNumber; i++ {
- port, err := getMySQLInstanceForGhostTest(t)
+ port, stopper, err := getMySQLInstanceForGhostTest(t)
a.NoError(err)
+ stoppers = append(stoppers, stopper)
// Add the provisioned instances.
instance, err := ctl.instanceServiceClient.CreateInstance(ctx, &v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance", 10),
@@ -212,6 +215,11 @@ func TestGhostTenant(t *testing.T) {
a.NoError(err)
prodInstances = append(prodInstances, instance)
}
+ defer func() {
+ for _, stopper := range stoppers {
+ stopper()
+ }
+ }()
// Create deployment configuration.
_, err = ctl.projectServiceClient.UpdateDeploymentConfig(ctx, &v1pb.UpdateDeploymentConfigRequest{
@@ -343,27 +351,26 @@ func TestGhostTenant(t *testing.T) {
}
}
-func getMySQLInstanceForGhostTest(t *testing.T) (int, error) {
+func getMySQLInstanceForGhostTest(t *testing.T) (int, func(), error) {
mysqlPort := getTestPort()
stopInstance := mysql.SetupTestInstance(t, mysqlPort, mysqlBinDir)
- t.Cleanup(stopInstance)
mysqlDB, err := connectTestMySQL(mysqlPort, "")
if err != nil {
- return 0, err
+ return 0, nil, err
}
defer mysqlDB.Close()
if _, err := mysqlDB.Exec("DROP USER IF EXISTS bytebase"); err != nil {
- return 0, err
+ return 0, nil, err
}
if _, err = mysqlDB.Exec("CREATE USER 'bytebase' IDENTIFIED WITH mysql_native_password BY 'bytebase'"); err != nil {
- return 0, err
+ return 0, nil, err
}
if _, err = mysqlDB.Exec("GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, PROCESS, REFERENCES, SELECT, SHOW DATABASES, SHOW VIEW, TRIGGER, UPDATE, USAGE, REPLICATION CLIENT, REPLICATION SLAVE, LOCK TABLES, RELOAD ON *.* to bytebase"); err != nil {
- return 0, err
+ return 0, nil, err
}
- return mysqlPort, nil
+ return mysqlPort, stopInstance, nil
}
diff --git a/backend/tests/tenant_test.go b/backend/tests/tenant_test.go
index dcfc856ec5..e7867a68c4 100644
--- a/backend/tests/tenant_test.go
+++ b/backend/tests/tenant_test.go
@@ -21,7 +21,7 @@ import (
var (
testTenantNumber = 1
- prodTenantNumber = 2
+ prodTenantNumber = 1
testInstanceName = "testInstanceTest"
prodInstanceName = "testInstanceProd"
)