Skip to content

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

chore: use testcontainers to spawn mysql instances (#15271)

p0ny04cbe4165a5a
diff --git a/backend/tests/ghost_test.go b/backend/tests/ghost_test.go
index 0d99bcba31..56e4e03b7a 100644
--- a/backend/tests/ghost_test.go
+++ b/backend/tests/ghost_test.go
@@ -6,10 +6,8 @@ import (
"context"
_ "embed"
"fmt"
- "strconv"
"testing"
- "github.com/bytebase/bytebase/backend/resources/mysql"
"github.com/bytebase/bytebase/backend/tests/fake"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
@@ -52,13 +50,15 @@ func TestGhostSchemaUpdate(t *testing.T) {
a.NoError(err)
defer ctl.Close(ctx)
- mysqlPort := getTestPort()
- stopInstance := mysql.SetupTestInstance(t, mysqlPort, mysqlBinDir)
- defer stopInstance()
-
- mysqlDB, err := connectTestMySQL(mysqlPort, "")
+ mysqlContainer, err := getMySQLContainer(ctx)
a.NoError(err)
- defer mysqlDB.Close()
+
+ defer func() {
+ mysqlContainer.db.Close()
+ err := mysqlContainer.container.Terminate(ctx)
+ a.NoError(err)
+ }()
+ mysqlDB := mysqlContainer.db
_, err = mysqlDB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %v", databaseName))
a.NoError(err)
@@ -78,7 +78,7 @@ func TestGhostSchemaUpdate(t *testing.T) {
Engine: v1pb.Engine_MYSQL,
Environment: "environments/prod",
Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: "127.0.0.1", Port: strconv.Itoa(mysqlPort), Username: "bytebase", Password: "bytebase", Id: "admin"}},
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: mysqlContainer.host, Port: mysqlContainer.port, Username: "bytebase", Password: "bytebase", Id: "admin"}},
},
})
a.NoError(err)
diff --git a/backend/tests/mysql.go b/backend/tests/mysql.go
deleted file mode 100644
index 0066c2d8f7..0000000000
--- a/backend/tests/mysql.go
+++ /dev/null
@@ -1,16 +0,0 @@
-//go:build mysql
-
-package tests
-
-import (
- "database/sql"
- "fmt"
-)
-
-// connectTestMySQL connects to the test mysql instance.
-func connectTestMySQL(port int, database string) (*sql.DB, error) {
- // If we connect using "localhost" on Unix, MySQL will use a socket file.
- // We don't want to be bothered by the socket file conflicts here.
- // ref: https://dev.mysql.com/doc/refman/8.0/en/connecting.html
- return sql.Open("mysql", fmt.Sprintf("root@tcp(127.0.0.1:%d)/%s?multiStatements=true", port, database))
-}
diff --git a/backend/tests/testcontainer.go b/backend/tests/testcontainer.go
new file mode 100644
index 0000000000..781ff2df15
--- /dev/null
+++ b/backend/tests/testcontainer.go
@@ -0,0 +1,59 @@
+package tests
+
+import (
+ "context"
+ "database/sql"
+ "fmt"
+
+ "github.com/testcontainers/testcontainers-go"
+ "github.com/testcontainers/testcontainers-go/wait"
+)
+
+type Container struct {
+ container testcontainers.Container
+ host string
+ port string
+ db *sql.DB
+}
+
+func getMySQLContainer(ctx context.Context) (*Container, error) {
+ req := testcontainers.ContainerRequest{
+ Image: "mysql:8.0.33",
+ Env: map[string]string{
+ "MYSQL_ROOT_PASSWORD": "root-password",
+ },
+ ExposedPorts: []string{"3306/tcp"},
+ WaitingFor: wait.ForListeningPort("3306/tcp"),
+ }
+
+ c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
+ ContainerRequest: req,
+ Started: true,
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ host, err := c.Host(ctx)
+ if err != nil {
+ return nil, err
+ }
+
+ port, err := c.MappedPort(ctx, "3306/tcp")
+ if err != nil {
+ return nil, err
+ }
+
+ dsn := fmt.Sprintf("root:root-password@tcp(%s:%s)/?multiStatements=true", host, port.Port())
+ db, err := sql.Open("mysql", dsn)
+ if err != nil {
+ return nil, err
+ }
+
+ return &Container{
+ container: c,
+ host: host,
+ port: port.Port(),
+ db: db,
+ }, nil
+}
diff --git a/go.mod b/go.mod
index 2428dbe130..a2807b6c74 100644
--- a/go.mod
+++ b/go.mod
@@ -82,6 +82,7 @@ require (
github.com/sourcegraph/jsonrpc2 v0.2.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.10.0
+ github.com/testcontainers/testcontainers-go v0.35.0
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75
github.com/vjeantet/ldapserver v1.0.1
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
@@ -219,7 +220,6 @@ require (
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
- github.com/testcontainers/testcontainers-go v0.34.0 // indirect
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
diff --git a/go.sum b/go.sum
index bd8cf04006..2322cbaa3c 100644
--- a/go.sum
+++ b/go.sum
@@ -1955,8 +1955,8 @@ github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
-github.com/testcontainers/testcontainers-go v0.34.0 h1:5fbgF0vIN5u+nD3IWabQwRybuB4GY8G2HHgCkbMzMHo=
-github.com/testcontainers/testcontainers-go v0.34.0/go.mod h1:6P/kMkQe8yqPHfPWNulFGdFHTD8HB2vLq/231xY2iPQ=
+github.com/testcontainers/testcontainers-go v0.35.0 h1:uADsZpTKFAtp8SLK+hMwSaa+X+JiERHtd4sQAFmXeMo=
+github.com/testcontainers/testcontainers-go v0.35.0/go.mod h1:oEVBj5zrfJTrgjwONs1SsRbnBtH9OKl+IGl3UMcr2B4=
github.com/the42/cartconvert v0.0.0-20131203171324-aae784c392b8 h1:I4DY8wLxJXCrMYzDM6lKCGc3IQwJX0PlTLsd3nQqI3c=
github.com/the42/cartconvert v0.0.0-20131203171324-aae784c392b8/go.mod h1:fWO/msnJVhHqN1yX6OBoxSyfj7TEj1hHiL8bJSQsK30=
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a h1:J/YdBZ46WKpXsxsW93SG+q0F8KI+yFrcIDT4c/RNoc4=