Skip to content

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

perf(tests): share containers and servers across the backend suite (#21433)

Danny Xu21da14fe83c5
diff --git a/.github/workflows/backend-tests.yml b/.github/workflows/backend-tests.yml
index 3e647abfd7..84b93fdd8b 100644
--- a/.github/workflows/backend-tests.yml
+++ b/.github/workflows/backend-tests.yml
@@ -36,7 +36,7 @@ jobs:
# go test runs packages in command-line order. backend/tests is the
# longest package by far, so it goes first instead of after the ~130
# packages that sort before it alphabetically.
- run: go test -p=8 -timeout 30m -ldflags "-w -s" -v ./backend/tests ./backend/api/v1 ./backend/... | tee test.log; exit ${PIPESTATUS[0]}
+ run: go test -p=8 -timeout 30m -ldflags "-w -s" -v ./backend/tests ./backend/api/v1 ./backend/... ./action/... | tee test.log; exit ${PIPESTATUS[0]}
- name: Pretty print tests running time
# grep: filter out lines like "--- PASS: Test (15.04s)"
# sed: remove unnecessary characters
diff --git a/AGENTS.md b/AGENTS.md
index e8a4a142ff..1d9b367f66 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -38,7 +38,7 @@ Test API and workflow behavior against PostgreSQL. Engine dialect and DDL fideli
- Test logic in its owning package. Use `backend/api/v1` for service behavior and `backend/store` for metadata queries, collision isolation, and transaction contention. CI runs every test with no `-short`, so never check `testing.Short()` or skip a test to park a known gap; gate a test on an environment variable only when it needs credentials or a server CI cannot provide.
- A backend test boots a Bytebase server only when it needs a background runner, real rollout, or audit trail; these tests live in `backend/tests`. Browser E2E tests use the separate frontend harness.
-- Packages needing metadata PostgreSQL use `testcontainer.Main` and `testcontainer.NewMetadataDB`. Target-engine tests use `testcontainer.SharedPgContainer` or its siblings, with `NewPgDatabase` for a database per test. Use these shared fixtures instead of package-owned or per-test containers.
+- Packages needing metadata PostgreSQL use `testcontainer.Main` and `testcontainer.NewMetadataDB`. Target-engine tests use `testcontainer.SharedPgContainer` or its siblings, with `NewPgDatabase` for a database per test. Use these shared fixtures instead of package-owned or per-test containers; a package with its own `TestMain` defers `testcontainer.CloseShared`.
- Prefer pure functions for handler decisions and conversions. When state reads are necessary, define a narrow interface beside the handler and fake it, as `backend/api/mcp` does with `serverStore`; avoid an interface over the entire store. Every fake requires a contract test against the real store too.
- New or modified composite-key methods require collision coverage. Prove colliding keys exist, assert the intended effect in the target scope, and assert the other scope is unchanged. Use the lowest test layer that exercises the behavior. Existing API collision tests remain regression coverage; fixture details are in the pre-PR checklist.
diff --git a/backend/common/testcontainer/shared.go b/backend/common/testcontainer/shared.go
index 8cef2393fe..42044c9102 100644
--- a/backend/common/testcontainer/shared.go
+++ b/backend/common/testcontainer/shared.go
@@ -11,21 +11,23 @@ import (
"github.com/stretchr/testify/require"
)
-// One container per test package, started on first use and stopped by Main.
-// A test binary is its own process, so package-level state here is per
-// package. Starting lazily is what keeps `go test -run` over the tests that
-// need no engine from paying for a container they never open.
+// One container per engine per test package, started on first use and stopped
+// by CloseShared. A test binary is its own process, so package-level state here
+// is per package. Starting lazily is what keeps `go test -run` over the tests
+// that need no engine from paying for a container they never open.
var (
- sharedPg = startOnce(GetPgContainer)
- sharedMySQL = startOnce(GetTestMySQLContainer)
- sharedOracle = startOnce(GetOracleContainer)
- sharedMSSQL = startOnce(GetMSSQLContainer)
- sharedTiDB = startOnce(GetTiDBContainer)
+ sharedPg = startOnce(GetPgContainer)
+ sharedTargetPg = startOnce(GetPgContainer)
+ sharedPg17 = startOnce(getPg17Container)
+ sharedTLSPg = startOnce(getTLSPgContainer)
+ sharedMSSQL = startOnce(getMSSQLContainer)
+ sharedTiDB = startOnce(getTiDBContainer)
+ sharedMongo = startOnce(getMongoDBContainer)
startedMu sync.Mutex
started []*Container
- pgDatabaseSeq atomic.Int64
+ databaseSeq atomic.Int64
)
func startOnce(start func(context.Context) (*Container, error)) func() (*Container, error) {
@@ -51,38 +53,73 @@ func shared(t testing.TB, start func() (*Container, error)) *Container {
//
// func TestMain(m *testing.M) { testcontainer.Main(m) }
func Main(m *testing.M) {
- defer func() {
- for _, c := range started {
- c.Close(context.Background())
- }
- }()
+ defer CloseShared()
m.Run()
}
+// CloseShared stops every container this package started. Main calls it; a
+// package whose TestMain does more than run the suite defers it there instead.
+func CloseShared() {
+ startedMu.Lock()
+ defer startedMu.Unlock()
+ for _, c := range started {
+ c.Close(context.Background())
+ }
+ started = nil
+}
+
+// StartSharedPg is SharedPgContainer for a TestMain, which has no testing.TB to
+// fail and so takes the error itself.
+func StartSharedPg() (*Container, error) { return sharedPg() }
+
// SharedPgContainer returns the package's PostgreSQL container, starting it on
// the first call. Tests isolate themselves with NewPgDatabase or NewMetadataDB.
func SharedPgContainer(t testing.TB) *Container { return shared(t, sharedPg) }
-// SharedMySQLContainer is SharedPgContainer for MySQL; tests take a database each.
-func SharedMySQLContainer(t testing.TB) *Container { return shared(t, sharedMySQL) }
+// SharedTargetPgContainer is a second PostgreSQL, for tests that point product
+// code at a server rather than store metadata in one. Keeping the two apart
+// keeps a package's target databases out of the metadata container's connection
+// budget, which is what caps parallelism.
+func SharedTargetPgContainer(t testing.TB) *Container { return shared(t, sharedTargetPg) }
+
+// SharedPg17Container is SharedPgContainer for the PostgreSQL 17 features absent
+// in 16, MERGE ... RETURNING being the one; tests take a database each with
+// NewPg17Database.
+func SharedPg17Container(t testing.TB) *Container { return shared(t, sharedPg17) }
-// SharedOracleContainer is SharedPgContainer for Oracle; tests take a user each.
-func SharedOracleContainer(t testing.TB) *Container { return shared(t, sharedOracle) }
+// SharedTLSPgContainer is SharedPgContainer for the TLS-enabled PostgreSQL that
+// tests reach with sslmode=verify-full and GetTLSCAPath.
+func SharedTLSPgContainer(t testing.TB) *Container { return shared(t, sharedTLSPg) }
// SharedMSSQLContainer is SharedPgContainer for SQL Server; tests take a database each.
func SharedMSSQLContainer(t testing.TB) *Container { return shared(t, sharedMSSQL) }
-// SharedTiDBContainer is SharedPgContainer for TiDB; tests take a database each.
+// SharedTiDBContainer is SharedPgContainer for TiDB; tests take a database each
+// with NewTiDBDatabase.
func SharedTiDBContainer(t testing.TB) *Container { return shared(t, sharedTiDB) }
+// SharedMongoDBContainer is SharedPgContainer for MongoDB; tests take a database
+// each with NewMongoDatabase.
+func SharedMongoDBContainer(t testing.TB) *Container { return shared(t, sharedMongo) }
+
// NewPgDatabase creates an empty database for the test on the shared
// PostgreSQL container and returns its name with a superuser handle to it.
// Anything cluster-wide the test creates, such as a role, should be named
// after the database so that parallel tests cannot collide.
func NewPgDatabase(t testing.TB) (string, *sql.DB) {
t.Helper()
- container := SharedPgContainer(t)
- name := fmt.Sprintf("test_%d", pgDatabaseSeq.Add(1))
+ return createPgDatabase(t, SharedPgContainer(t))
+}
+
+// NewPg17Database is NewPgDatabase on the shared PostgreSQL 17 container.
+func NewPg17Database(t testing.TB) (string, *sql.DB) {
+ t.Helper()
+ return createPgDatabase(t, SharedPg17Container(t))
+}
+
+func createPgDatabase(t testing.TB, container *Container) (string, *sql.DB) {
+ t.Helper()
+ name := newDatabaseName()
_, err := container.GetDB().Exec("CREATE DATABASE " + name)
require.NoError(t, err)
db, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password database=%s", container.GetHost(), container.GetPort(), name))
@@ -90,3 +127,28 @@ func NewPgDatabase(t testing.TB) (string, *sql.DB) {
t.Cleanup(func() { require.NoError(t, db.Close()) })
return name, db
}
+
+// NewTiDBDatabase creates an empty database for the test on the shared TiDB
+// container and returns the container with the database's name.
+func NewTiDBDatabase(t testing.TB) (*Container, string) {
+ t.Helper()
+ container := SharedTiDBContainer(t)
+ name := newDatabaseName()
+ _, err := container.GetDB().Exec("CREATE DATABASE " + name)
+ require.NoError(t, err)
+ return container, name
+}
+
+// NewMongoDatabase reserves a database for the test on the shared MongoDB
+// container and returns the container with the database's name. MongoDB creates
+// the database on the first write, so the name is all the test needs.
+func NewMongoDatabase(t testing.TB) (*Container, string) {
+ t.Helper()
+ return SharedMongoDBContainer(t), newDatabaseName()
+}
+
+// newDatabaseName is unique across the test binary, so tests that run in
+// parallel, on the same container or not, never share a database.
+func newDatabaseName() string {
+ return fmt.Sprintf("test_%d", databaseSeq.Add(1))
+}
diff --git a/backend/common/testcontainer/testcontainer.go b/backend/common/testcontainer/testcontainer.go
index 62a6d40bcf..52238fd47d 100644
--- a/backend/common/testcontainer/testcontainer.go
+++ b/backend/common/testcontainer/testcontainer.go
@@ -15,41 +15,39 @@ import (
"net"
"os"
"path/filepath"
- "testing"
"time"
- "github.com/moby/moby/api/types/container"
"github.com/pkg/errors"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
+// Container is a running engine and the admin handle its tests share. Tests
+// reach one through the accessors in shared.go; the two constructors exported
+// here are for backend/tests, whose containers are the Bytebase instances under
+// test and so cannot be shared.
type Container struct {
container testcontainers.Container
host string
port string
+ username string
+ password string
db *sql.DB
tlsDir string
tlsCAPath string
}
-func (c *Container) GetHost() string {
- return c.host
-}
-
-func (c *Container) GetPort() string {
- return c.port
-}
+func (c *Container) GetHost() string { return c.host }
+func (c *Container) GetPort() string { return c.port }
+func (c *Container) GetUsername() string { return c.username }
+func (c *Container) GetPassword() string { return c.password }
-func (c *Container) GetDB() *sql.DB {
- return c.db
-}
+// GetDB is the admin connection, nil for an engine with no database/sql driver.
+func (c *Container) GetDB() *sql.DB { return c.db }
// GetTLSCAPath returns the CA certificate path for a TLS-enabled PostgreSQL
// container.
-func (c *Container) GetTLSCAPath() string {
- return c.tlsCAPath
-}
+func (c *Container) GetTLSCAPath() string { return c.tlsCAPath }
func (c *Container) Close(ctx context.Context) {
if c == nil {
@@ -72,17 +70,24 @@ func (c *Container) Close(ctx context.Context) {
}
}
-// GetTestMySQLContainer creates a MySQL container for testing
-func GetTestMySQLContainer(ctx context.Context) (retc *Container, retErr error) {
- req := testcontainers.ContainerRequest{
- Image: "mysql:8.0.33",
- Env: map[string]string{
- "MYSQL_ROOT_PASSWORD": "root-password",
- },
- ExposedPorts: []string{"3306/tcp"},
- WaitingFor: wait.ForLog("ready for connections").WithOccurrence(2).WithStartupTimeout(5 * time.Minute),
- }
- c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
+// spec is what an engine needs beyond its container request: the port to map,
+// the credentials its tests connect with, and the driver and DSN for the admin
+// handle. An engine with no database/sql driver leaves driver empty, and its
+// Container carries the credentials instead of a handle.
+type spec struct {
+ port string
+ username string
+ password string
+ driver string
+ dsn func(*Container) string
+}
+
+// start runs the container and returns once the engine answers, so the first
+// statement against it does not race the boot. A container that fails any step
+// here is terminated rather than left behind.
+func start(ctx context.Context, req testcontainers.ContainerRequest, s spec) (retC *Container, retErr error) {
+ req.ExposedPorts = []string{s.port}
+ raw, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
@@ -90,182 +95,207 @@ func GetTestMySQLContainer(ctx context.Context) (retc *Container, retErr error)
return nil, err
}
- host, err := c.Host(ctx)
- if err != nil {
+ c := &Container{container: raw, username: s.username, password: s.password}
+ defer func() {
+ if retErr != nil {
+ c.Close(ctx)
+ }
+ }()
+
+ if c.host, err = raw.Host(ctx); err != nil {
return nil, err
}
- port, err := c.MappedPort(ctx, "3306/tcp")
+ port, err := raw.MappedPort(ctx, s.port)
if err != nil {
return nil, err
}
+ c.port = port.Port()
- dsn := fmt.Sprintf("root:root-password@tcp(%s:%s)/?multiStatements=true", host, port.Port())
- db, err := sql.Open("mysql", dsn)
- if err != nil {
+ if s.driver == "" {
+ return c, nil
+ }
+ if c.db, err = sql.Open(s.driver, s.dsn(c)); err != nil {
return nil, err
}
- defer func() {
- if retErr != nil {
- db.Close()
- }
- }()
-
- if err := waitDBPing(ctx, db); err != nil {
+ if err := waitDBPing(ctx, c.db); err != nil {
return nil, err
}
-
- return &Container{
- container: c,
- host: host,
- port: port.Port(),
- db: db,
- }, nil
+ return c, nil
}
-// GetPgContainer creates a PostgreSQL 16 container for testing.
-func GetPgContainer(ctx context.Context) (*Container, error) {
- return getPgContainerWithImage(ctx, "postgres:16-alpine")
-}
-
-// GetTLSPgContainer creates a TLS-enabled PostgreSQL 16 container. Clients
-// can connect with sslmode=verify-full and the CA returned by GetTLSCAPath.
-func GetTLSPgContainer(ctx context.Context) (*Container, error) {
- return getTLSPgContainerWithImage(ctx, "postgres:16-alpine")
+// waitDBPing returns once the database accepts a connection. The container's
+// log wait has already passed by the time this runs, so the first ping nearly
+// always succeeds; the poll only covers the gap between "ready" in the log and
+// the listener. A 3 s ticker here used to add 3 s to every container start.
+func waitDBPing(ctx context.Context, db *sql.DB) error {
+ ticker := time.NewTicker(100 * time.Millisecond)
+ defer ticker.Stop()
+ timeout := time.After(10 * time.Minute)
+ for {
+ if err := db.PingContext(ctx); err == nil {
+ return nil
+ }
+ select {
+ case <-ticker.C:
+ case <-timeout:
+ return errors.Errorf("start container timeout reached")
+ case <-ctx.Done():
+ return ctx.Err()
+ }
+ }
}
-// GetPg17Container creates a PostgreSQL 17 container for testing. PG17 is required
-// for features absent in 16 — notably MERGE ... RETURNING.
-func GetPg17Container(ctx context.Context) (*Container, error) {
- return getPgContainerWithImage(ctx, "postgres:17-alpine")
+var pgSpec = spec{
+ port: "5432/tcp",
+ username: "postgres",
+ password: "root-password",
+ driver: "pgx",
+ dsn: func(c *Container) string {
+ return fmt.Sprintf("host=%s port=%s user=%s password=%s database=postgres sslmode=disable",
+ c.host, c.port, c.username, c.password)
+ },
}
-func getPgContainerWithImage(ctx context.Context, image string) (retC *Container, retErr error) {
- req := testcontainers.ContainerRequest{
+func pgRequest(image string) testcontainers.ContainerRequest {
+ return testcontainers.ContainerRequest{
Image: image,
Env: map[string]string{
"LANG": "en_US.UTF-8",
- "POSTGRES_PASSWORD": "root-password",
+ "POSTGRES_PASSWORD": pgSpec.password,
},
- ExposedPorts: []string{"5432/tcp"},
- WaitingFor: wait.ForLog("database system is ready to accept connections").WithOccurrence(2).WithStartupTimeout(5 * time.Minute),
+ // A shared container carries a connection pool per server and per
+ // instance on it; the default 100 runs out at twenty parallel tests, as
+ // "server refused TLS connection".
+ Cmd: []string{"postgres", "-c", "max_connections=1000"},
+ WaitingFor: wait.ForLog("database system is ready to accept connections").
+ WithOccurrence(2).WithStartupTimeout(5 * time.Minute),
}
+}
- c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
- ContainerRequest: req,
- Started: true,
- })
- if err != nil {
- return nil, err
- }
+// GetPgContainer starts PostgreSQL 16. Tests share one through
+// SharedPgContainer; this is exported for the instances backend/tests
+// provisions per test.
+func GetPgContainer(ctx context.Context) (*Container, error) {
+ return start(ctx, pgRequest("postgres:16-alpine"), pgSpec)
+}
- host, err := c.Host(ctx)
- if err != nil {
- return nil, err
- }
- port, err := c.MappedPort(ctx, "5432/tcp")
- if err != nil {
- return nil, err
- }
+// getPg17Container starts PostgreSQL 17, required for the features absent in
+// 16 — MERGE ... RETURNING being the one.
+func getPg17Container(ctx context.Context) (*Container, error) {
+ return start(ctx, pgRequest("postgres:17-alpine"), pgSpec)
+}
- db, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password database=postgres", host, port.Port()))
+// getTLSPgContainer starts a PostgreSQL 16 that serves TLS, for clients
+// connecting with sslmode=verify-full and the CA at GetTLSCAPath. The admin
+// handle stays on plaintext.
+func getTLSPgContainer(ctx context.Context) (retC *Container, retErr error) {
+ tlsDir, ca, certificate, key, err := createPostgreSQLTLSMaterial()
if err != nil {
return nil, err
}
defer func() {
if retErr != nil {
- db.Close()
+ _ = os.RemoveAll(tlsDir)
}
}()
- if err := waitDBPing(ctx, db); err != nil {
- return nil, err
+ req := pgRequest("postgres:16-alpine")
+ req.Entrypoint = []string{
+ "/bin/sh",
+ "-c",
+ "chown postgres:postgres /tmp/server.key && exec /usr/local/bin/docker-entrypoint.sh \"$@\"",
+ "--",
+ }
+ req.Cmd = append(req.Cmd,
+ "-c", "ssl=on",
+ "-c", "ssl_cert_file=/tmp/server.crt",
+ "-c", "ssl_key_file=/tmp/server.key",
+ )
+ req.Files = []testcontainers.ContainerFile{
+ {Reader: bytes.NewReader(certificate), ContainerFilePath: "/tmp/server.crt", FileMode: 0o644},
+ {Reader: bytes.NewReader(key), ContainerFilePath: "/tmp/server.key", FileMode: 0o600},
}
- return &Container{
- container: c,
- host: host,
- port: port.Port(),
- db: db,
- }, nil
-}
-
-func getTLSPgContainerWithImage(ctx context.Context, image string) (retC *Container, retErr error) {
- tlsDir, ca, certificate, key, err := createPostgreSQLTLSMaterial()
+ c, err := start(ctx, req, pgSpec)
if err != nil {
return nil, err
}
- defer func() {
- if retErr != nil {
- _ = os.RemoveAll(tlsDir)
- }
- }()
+ c.tlsDir, c.tlsCAPath = tlsDir, ca
+ return c, nil
+}
- req := testcontainers.ContainerRequest{
- Image: image,
- Env: map[string]string{
- "LANG": "en_US.UTF-8",
- "POSTGRES_PASSWORD": "root-password",
+// GetMySQLContainer starts MySQL. Exported for the same reason as GetPgContainer.
+func GetMySQLContainer(ctx context.Context) (*Container, error) {
+ const user, password = "root", "root-password"
+ return start(ctx, testcontainers.ContainerRequest{
+ Image: "mysql:8.0.33",
+ Env: map[string]string{"MYSQL_ROOT_PASSWORD": password},
+ WaitingFor: wait.ForLog("ready for connections").WithOccurrence(2).WithStartupTimeout(5 * time.Minute),
+ }, spec{
+ port: "3306/tcp",
+ username: user,
+ password: password,
+ driver: "mysql",
+ dsn: func(c *Container) string {
+ return fmt.Sprintf("%s:%s@tcp(%s:%s)/?multiStatements=true", c.username, c.password, c.host, c.port)
},
- ExposedPorts: []string{"5432/tcp"},
- Entrypoint: []string{
- "/bin/sh",
- "-c",
- "chown postgres:postgres /tmp/server.key && exec /usr/local/bin/docker-entrypoint.sh \"$@\"",
- "--",
- },
- Cmd: []string{
- "postgres",
- "-c", "ssl=on",
- "-c", "ssl_cert_file=/tmp/server.crt",
- "-c", "ssl_key_file=/tmp/server.key",
+ })
+}
+
+func getMSSQLContainer(ctx context.Context) (*Container, error) {
+ const user, password = "sa", "Test123!"
+ return start(ctx, testcontainers.ContainerRequest{
+ Image: "mcr.microsoft.com/mssql/server:2022-latest",
+ Env: map[string]string{
+ "ACCEPT_EULA": "Y",
+ "SA_PASSWORD": password,
+ "MSSQL_PID": "Express",
},
- Files: []testcontainers.ContainerFile{
- {Reader: bytes.NewReader(certificate), ContainerFilePath: "/tmp/server.crt", FileMode: 0o644},
- {Reader: bytes.NewReader(key), ContainerFilePath: "/tmp/server.key", FileMode: 0o600},
+ WaitingFor: wait.ForLog("SQL Server is now ready for client connections").
+ WithStartupTimeout(3 * time.Minute),
+ }, spec{
+ port: "1433/tcp",
+ username: user,
+ password: password,
+ driver: "sqlserver",
+ dsn: func(c *Container) string {
+ return fmt.Sprintf("sqlserver://%s:%s@%s:%s?database=master", c.username, c.password, c.host, c.port)
},
- WaitingFor: wait.ForLog("database system is ready to accept connections").WithOccurrence(2).WithStartupTimeout(5 * time.Minute),
- }
- c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
- ContainerRequest: req,
- Started: true,
})
- if err != nil {
- return nil, err
- }
- var db *sql.DB
- defer func() {
- if retErr != nil {
- if db != nil {
- _ = db.Close()
- }
- _ = c.Terminate(ctx)
- }
- }()
+}
- host, err := c.Host(ctx)
- if err != nil {
- return nil, err
- }
- port, err := c.MappedPort(ctx, "5432/tcp")
- if err != nil {
- return nil, err
- }
- db, err = sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password database=postgres sslmode=disable", host, port.Port()))
- if err != nil {
- return nil, err
- }
- if err := waitDBPing(ctx, db); err != nil {
- return nil, err
- }
+// getTiDBContainer starts TiDB, which speaks the MySQL protocol with no
+// password on root.
+func getTiDBContainer(ctx context.Context) (*Container, error) {
+ return start(ctx, testcontainers.ContainerRequest{
+ Image: "pingcap/tidb:v8.5.0",
+ WaitingFor: wait.ForLog("server is running MySQL protocol").WithStartupTimeout(5 * time.Minute),
+ }, spec{
+ port: "4000/tcp",
+ username: "root",
+ driver: "mysql",
+ dsn: func(c *Container) string {
+ return fmt.Sprintf("%s@tcp(%s:%s)/?multiStatements=true&tls=false", c.username, c.host, c.port)
+ },
+ })
+}
- return &Container{
- container: c,
- host: host,
- port: port.Port(),
- db: db,
- tlsDir: tlsDir,
- tlsCAPath: ca,
- }, nil
+// getMongoDBContainer starts MongoDB, which has no database/sql driver: its
+// tests open the Mongo driver themselves from the container's credentials.
+func getMongoDBContainer(ctx context.Context) (*Container, error) {
+ const user, password = "testuser", "testpass"
+ return start(ctx, testcontainers.ContainerRequest{
+ Image: "mongo:5",
+ Env: map[string]string{
+ "MONGO_INITDB_ROOT_USERNAME": user,
+ "MONGO_INITDB_ROOT_PASSWORD": password,
+ },
+ WaitingFor: wait.ForLog("Waiting for connections").WithStartupTimeout(3 * time.Minute),
+ }, spec{
+ port: "27017/tcp",
+ username: user,
+ password: password,
+ })
}
func createPostgreSQLTLSMaterial() (string, string, []byte, []byte, error) {
@@ -335,339 +365,3 @@ func createPostgreSQLTLSMaterial() (string, string, []byte, []byte, error) {
}
return tlsDir, caPath, certificate, key, nil
}
-
-// waitDBPing returns once the database accepts a connection. The container's
-// log wait has already passed by the time this runs, so the first ping nearly
-// always succeeds; the poll only covers the gap between "ready" in the log and
-// the listener. A 3 s ticker here used to add 3 s to every container start.
-func waitDBPing(ctx context.Context, db *sql.DB) error {
- ticker := time.NewTicker(100 * time.Millisecond)
- defer ticker.Stop()
- timeout := time.After(10 * time.Minute)
- for {
- if err := db.PingContext(ctx); err == nil {
- return nil
- }
- select {
- case <-ticker.C:
- case <-timeout:
- return errors.Errorf("start container timeout reached")
- case <-ctx.Done():
- return ctx.Err()
- }
- }
-}
-
-// GetTestPgContainer is a helper function for tests that creates a PostgreSQL container
-// and handles the error by failing the test if container creation fails
-func GetTestPgContainer(ctx context.Context, t testing.TB) *Container {
- t.Helper()
- container, err := GetPgContainer(ctx)
- if err != nil {
- t.Fatalf("failed to create PostgreSQL container: %v", err)
- }
- return container
-}
-
-// GetTestTLSPgContainer creates a TLS-enabled PostgreSQL container, failing
-// the test when the container cannot be started.
-func GetTestTLSPgContainer(ctx context.Context, t testing.TB) *Container {
- t.Helper()
- container, err := GetTLSPgContainer(ctx)
- if err != nil {
- t.Fatalf("failed to create TLS PostgreSQL container: %v", err)
- }
- return container
-}
-
-// GetTestPg17Container creates a PostgreSQL 17 container for testing, failing the
-// test on error.
-func GetTestPg17Container(ctx context.Context, t testing.TB) *Container {
- t.Helper()
- container, err := GetPg17Container(ctx)
- if err != nil {
- t.Fatalf("failed to create PostgreSQL 17 container: %v", err)
- }
- return container
-}
-
-// GetOracleContainer creates an Oracle container for testing
-func GetOracleContainer(ctx context.Context) (retC *Container, retErr error) {
- req := testcontainers.ContainerRequest{
- Image: "gvenzl/oracle-free:slim",
- Env: map[string]string{
- "ORACLE_PASSWORD": "test123",
- "APP_USER": "testuser",
- "APP_USER_PASSWORD": "testpass",
- },
- ExposedPorts: []string{"1521/tcp"},
- WaitingFor: wait.ForLog("DATABASE IS READY TO USE!").
- WithStartupTimeout(10 * time.Minute),
- HostConfigModifier: func(hc *container.HostConfig) {
- hc.ShmSize = 1 * 1024 * 1024 * 1024 // 1GB shared memory
- },
- }
-
- 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, "1521/tcp")
- if err != nil {
- return nil, err
- }
-
- // Oracle connection string format: oracle://username:password@host:port/service_name
- // Use SYSTEM account for privileged operations (like creating users), similar to MySQL root
- dsn := fmt.Sprintf("oracle://system:test123@%s:%s/FREEPDB1", host, port.Port())
- db, err := sql.Open("oracle", dsn)
- if err != nil {
- return nil, err
- }
- defer func() {
- if retErr != nil {
- db.Close()
- }
- }()
-
- if err := waitDBPing(ctx, db); err != nil {
- return nil, err
- }
-
- return &Container{
- container: c,
- host: host,
- port: port.Port(),
- db: db,
- }, nil
-}
-
-// GetTestOracleContainer is a helper function for tests that creates an Oracle container
-// and handles the error by failing the test if container creation fails
-func GetTestOracleContainer(ctx context.Context, t testing.TB) *Container {
- t.Helper()
- container, err := GetOracleContainer(ctx)
- if err != nil {
- t.Fatalf("failed to create Oracle container: %v", err)
- }
- return container
-}
-
-// GetMSSQLContainer creates a Microsoft SQL Server container for testing
-func GetMSSQLContainer(ctx context.Context) (retC *Container, retErr error) {
- req := testcontainers.ContainerRequest{
- Image: "mcr.microsoft.com/mssql/server:2022-latest",
- Env: map[string]string{
- "ACCEPT_EULA": "Y",
- "SA_PASSWORD": "Test123!",
- "MSSQL_PID": "Express",
- },
- ExposedPorts: []string{"1433/tcp"},
- WaitingFor: wait.ForLog("SQL Server is now ready for client connections").
- WithStartupTimeout(3 * time.Minute),
- }
-
- 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, "1433/tcp")
- if err != nil {
- return nil, err
- }
-
- // MSSQL connection string format
- dsn := fmt.Sprintf("sqlserver://sa:Test123!@%s:%s?database=master", host, port.Port())
- db, err := sql.Open("sqlserver", dsn)
- if err != nil {
- return nil, err
- }
- defer func() {
- if retErr != nil {
- db.Close()
- }
- }()
-
- if err := waitDBPing(ctx, db); err != nil {
- return nil, err
- }
-
- return &Container{
- container: c,
- host: host,
- port: port.Port(),
- db: db,
- }, nil
-}
-
-// GetTestMSSQLContainer is a helper function for tests that creates a MSSQL container
-// and handles the error by failing the test if container creation fails
-func GetTestMSSQLContainer(ctx context.Context, t testing.TB) *Container {
- t.Helper()
- container, err := GetMSSQLContainer(ctx)
- if err != nil {
- t.Fatalf("failed to create MSSQL container: %v", err)
- }
- return container
-}
-
-// GetTiDBContainer creates a TiDB container for testing
-func GetTiDBContainer(ctx context.Context) (retC *Container, retErr error) {
- req := testcontainers.ContainerRequest{
- Image: "pingcap/tidb:v8.5.0",
- ExposedPorts: []string{"4000/tcp"},
- WaitingFor: wait.ForLog("server is running MySQL protocol").WithStartupTimeout(5 * time.Minute),
- }
-
- 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, "4000/tcp")
- if err != nil {
- return nil, err
- }
-
- // TiDB uses MySQL protocol, so we use MySQL driver
- dsn := fmt.Sprintf("root@tcp(%s:%s)/?multiStatements=true&tls=false", host, port.Port())
- db, err := sql.Open("mysql", dsn)
- if err != nil {
- return nil, err
- }
- defer func() {
- if retErr != nil {
- db.Close()
- }
- }()
-
- if err := waitDBPing(ctx, db); err != nil {
- return nil, err
- }
-
- return &Container{
- container: c,
- host: host,
- port: port.Port(),
- db: db,
- }, nil
-}
-
-// GetTestTiDBContainer is a helper function for tests that creates a TiDB container
-// and handles the error by failing the test if container creation fails
-func GetTestTiDBContainer(ctx context.Context, t testing.TB) *Container {
- t.Helper()
- container, err := GetTiDBContainer(ctx)
- if err != nil {
- t.Fatalf("failed to create TiDB container: %v", err)
- }
- return container
-}
-
-// MongoDBContainer represents a MongoDB container with its connection details
-type MongoDBContainer struct {
- container testcontainers.Container
- host string
- port string
- username string
- password string
-}
-
-func (c *MongoDBContainer) GetHost() string {
- return c.host
-}
-
-func (c *MongoDBContainer) GetPort() string {
- return c.port
-}
-
-func (c *MongoDBContainer) GetUsername() string {
- return c.username
-}
-
-func (c *MongoDBContainer) GetPassword() string {
- return c.password
-}
-
-func (c *MongoDBContainer) Close(ctx context.Context) {
- if c == nil {
- return
- }
- if c.container != nil {
- if err := c.container.Terminate(ctx, testcontainers.StopTimeout(1*time.Millisecond)); err != nil {
- slog.Error("close MongoDB container error")
- }
- }
-}
-
-// GetMongoDBContainer creates a MongoDB container for testing
-func GetMongoDBContainer(ctx context.Context) (*MongoDBContainer, error) {
- req := testcontainers.ContainerRequest{
- Image: "mongo:5",
- Env: map[string]string{
- "MONGO_INITDB_ROOT_USERNAME": "testuser",
- "MONGO_INITDB_ROOT_PASSWORD": "testpass",
- },
- ExposedPorts: []string{"27017/tcp"},
- WaitingFor: wait.ForLog("Waiting for connections").WithStartupTimeout(3 * time.Minute),
- }
-
- 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, "27017/tcp")
- if err != nil {
- return nil, err
- }
-
- return &MongoDBContainer{
- container: c,
- host: host,
- port: port.Port(),
- username: "testuser",
- password: "testpass",
- }, nil
-}
-
-// GetTestMongoDBContainer is a helper function for tests that creates a MongoDB container
-// and handles the error by failing the test if container creation fails
-func GetTestMongoDBContainer(ctx context.Context, t testing.TB) *MongoDBContainer {
- t.Helper()
- container, err := GetMongoDBContainer(ctx)
- if err != nil {
- t.Fatalf("failed to create MongoDB container: %v", err)
- }
- return container
-}
diff --git a/backend/component/review/evaluator.go b/backend/component/review/evaluator.go
index 170cb19865..5bb9df5ed8 100644
--- a/backend/component/review/evaluator.go
+++ b/backend/component/review/evaluator.go
@@ -149,7 +149,11 @@ func (r *Runner) processIssue(ctx context.Context, ref bus.IssueRef) {
return
}
if approved {
- r.bus.RolloutCreationChan <- bus.PlanRef{ProjectID: issue.ProjectID, PlanID: *issue.PlanUID}
+ // Give up on shutdown rather than blocking on a stopped consumer.
+ select {
+ case r.bus.RolloutCreationChan <- bus.PlanRef{ProjectID: issue.ProjectID, PlanID: *issue.PlanUID}:
+ case <-ctx.Done():
+ }
}
}
}
diff --git a/backend/plugin/db/mongodb/main_test.go b/backend/plugin/db/mongodb/main_test.go
new file mode 100644
index 0000000000..73ede54578
--- /dev/null
+++ b/backend/plugin/db/mongodb/main_test.go
@@ -0,0 +1,9 @@
+package mongodb
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+)
+
+func TestMain(m *testing.M) { testcontainer.Main(m) }
diff --git a/backend/plugin/db/mongodb/mongodb_test.go b/backend/plugin/db/mongodb/mongodb_test.go
index 9f935692fc..32520503bd 100644
--- a/backend/plugin/db/mongodb/mongodb_test.go
+++ b/backend/plugin/db/mongodb/mongodb_test.go
@@ -222,11 +222,9 @@ func TestExecuteLogsParseFailure(t *testing.T) {
// This ensures the fix for PR #17282 (which changed to single-quote bracket notation
// for special characters) works correctly.
func TestQueryWithBracketNotation(t *testing.T) {
+ t.Parallel()
ctx := context.Background()
-
- // Get MongoDB container from testcontainer utility
- container := testcontainer.GetTestMongoDBContainer(ctx, t)
- defer container.Close(ctx)
+ container, database := testcontainer.NewMongoDatabase(t)
// Create test database and collection with some data
driver := &Driver{}
@@ -237,14 +235,14 @@ func TestQueryWithBracketNotation(t *testing.T) {
Username: container.GetUsername(),
},
ConnectionContext: db.ConnectionContext{
- DatabaseName: "testdb",
+ DatabaseName: database,
},
Password: container.GetPassword(),
}
openedDriver, err := driver.Open(ctx, storepb.Engine_MONGODB, connConfig)
require.NoError(t, err)
- defer openedDriver.Close(ctx)
+ t.Cleanup(func() { openedDriver.Close(ctx) })
// Wait for MongoDB to be fully ready
err = openedDriver.Ping(ctx)
@@ -280,6 +278,7 @@ func TestQueryWithBracketNotation(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
results, err := openedDriver.QueryConn(ctx, nil, tc.statement, db.QueryContext{
Limit: 50,
MaximumSQLResultSize: 10 * 1024 * 1024, // 10MB
@@ -328,11 +327,9 @@ func TestQueryWithBracketNotation(t *testing.T) {
// TestQueryWithBracketNotationStructure tests the exact structure of query results
// using protocmp to ensure the result format is correct.
func TestQueryWithBracketNotationStructure(t *testing.T) {
+ t.Parallel()
ctx := context.Background()
-
- // Get MongoDB container from testcontainer utility
- container := testcontainer.GetTestMongoDBContainer(ctx, t)
- defer container.Close(ctx)
+ container, database := testcontainer.NewMongoDatabase(t)
// Create test database and collection with deterministic data
driver := &Driver{}
@@ -343,7 +340,7 @@ func TestQueryWithBracketNotationStructure(t *testing.T) {
Username: container.GetUsername(),
},
ConnectionContext: db.ConnectionContext{
- DatabaseName: "testdb",
+ DatabaseName: database,
},
Password: container.GetPassword(),
}
@@ -412,10 +409,9 @@ func TestQueryWithBracketNotationStructure(t *testing.T) {
// large integers must render in mongosh notation (plain decimal), not the Go
// driver's scientific notation (e.g. 1.779696815227E+12).
func TestQueryDoubleNotation(t *testing.T) {
+ t.Parallel()
ctx := context.Background()
-
- container := testcontainer.GetTestMongoDBContainer(ctx, t)
- defer container.Close(ctx)
+ container, database := testcontainer.NewMongoDatabase(t)
driver := &Driver{}
connConfig := db.ConnectionConfig{
@@ -425,7 +421,7 @@ func TestQueryDoubleNotation(t *testing.T) {
Username: container.GetUsername(),
},
ConnectionContext: db.ConnectionContext{
- DatabaseName: "testdb",
+ DatabaseName: database,
},
Password: container.GetPassword(),
}
@@ -441,7 +437,7 @@ func TestQueryDoubleNotation(t *testing.T) {
// BSON doubles, matching the issue's data shape.
mongoDriver, ok := openedDriver.(*Driver)
require.True(t, ok)
- _, err = mongoDriver.client.Database("testdb").Collection("users").InsertOne(ctx, bson.D{
+ _, err = mongoDriver.client.Database(database).Collection("users").InsertOne(ctx, bson.D{
{Key: "_id", Value: int32(1129063441)},
{Key: "name", Value: "repro user 1"},
{Key: "created_at", Value: float64(1779696815227)},
diff --git a/backend/plugin/db/pg/pg_test.go b/backend/plugin/db/pg/pg_test.go
index 743fa42429..73605be751 100644
--- a/backend/plugin/db/pg/pg_test.go
+++ b/backend/plugin/db/pg/pg_test.go
@@ -365,11 +365,10 @@ func TestQueryConnMergeCTEDoesNotLeakRows(t *testing.T) {
t.Parallel()
ctx := context.Background()
- pgContainer := testcontainer.GetTestPg17Container(ctx, t)
- defer pgContainer.Close(ctx)
+ pgContainer := testcontainer.SharedPg17Container(t)
+ dbName, rawDB := testcontainer.NewPg17Database(t)
const secret = "TOP-SECRET-SSN-123-45-6789"
- rawDB := pgContainer.GetDB()
require.NoError(t, rawDB.Ping())
_, err := rawDB.ExecContext(ctx, `CREATE TABLE secret_doc (id int PRIMARY KEY, secret text);`)
require.NoError(t, err)
@@ -383,7 +382,7 @@ func TestQueryConnMergeCTEDoesNotLeakRows(t *testing.T) {
Username: "postgres",
},
Password: "root-password",
- ConnectionContext: db.ConnectionContext{DatabaseName: "postgres"},
+ ConnectionContext: db.ConnectionContext{DatabaseName: dbName},
})
require.NoError(t, err)
defer driver.Close(ctx)
diff --git a/backend/plugin/db/tidb/main_test.go b/backend/plugin/db/tidb/main_test.go
new file mode 100644
index 0000000000..ff37c2c076
--- /dev/null
+++ b/backend/plugin/db/tidb/main_test.go
@@ -0,0 +1,9 @@
+package tidb
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+)
+
+func TestMain(m *testing.M) { testcontainer.Main(m) }
diff --git a/backend/plugin/db/tidb/tidb_test.go b/backend/plugin/db/tidb/tidb_test.go
index b1d907aa92..9d07428224 100644
--- a/backend/plugin/db/tidb/tidb_test.go
+++ b/backend/plugin/db/tidb/tidb_test.go
@@ -2,6 +2,7 @@ package tidb
import (
"context"
+ "fmt"
"strings"
"testing"
@@ -112,70 +113,66 @@ func openTestDriver(ctx context.Context, t *testing.T, container *testcontainer.
}
func TestExecuteCreateIndexInTransaction(t *testing.T) {
+ t.Parallel()
ctx := context.Background()
- container := testcontainer.GetTestTiDBContainer(ctx, t)
- defer container.Close(ctx)
+ container, database := testcontainer.NewTiDBDatabase(t)
tidbDriver := openTestDriver(ctx, t, container)
defer func() {
require.NoError(t, tidbDriver.Close(ctx))
}()
- _, err := tidbDriver.Execute(ctx, `
- CREATE DATABASE IF NOT EXISTS test;
- USE test;
- DROP TABLE IF EXISTS test.execute_create_index_in_transaction;
- CREATE TABLE test.execute_create_index_in_transaction (id INT);
+ _, err := tidbDriver.Execute(ctx, fmt.Sprintf(`
+ USE %[1]s;
+ CREATE TABLE %[1]s.execute_create_index_in_transaction (id INT);
BEGIN;
- CREATE INDEX idx_execute_create_index_in_transaction ON test.execute_create_index_in_transaction(id);
+ CREATE INDEX idx_execute_create_index_in_transaction ON %[1]s.execute_create_index_in_transaction(id);
COMMIT;
- `, db.ExecuteOptions{})
+ `, database), db.ExecuteOptions{})
require.NoError(t, err)
var count int
- query := `
+ query := fmt.Sprintf(`
SELECT COUNT(*)
FROM information_schema.tidb_indexes
- WHERE table_schema = 'test'
+ WHERE table_schema = '%s'
AND table_name = 'execute_create_index_in_transaction'
AND key_name = 'idx_execute_create_index_in_transaction'
- `
+ `, database)
err = tidbDriver.db.QueryRowContext(ctx, query).Scan(&count)
require.NoError(t, err)
require.Equal(t, 1, count)
}
func TestExecutePreparedStatementFlowWithCreateIndexString(t *testing.T) {
+ t.Parallel()
ctx := context.Background()
- container := testcontainer.GetTestTiDBContainer(ctx, t)
- defer container.Close(ctx)
+ container, database := testcontainer.NewTiDBDatabase(t)
tidbDriver := openTestDriver(ctx, t, container)
defer func() {
require.NoError(t, tidbDriver.Close(ctx))
}()
- statement := `
- CREATE DATABASE IF NOT EXISTS test;
- USE test;
- DROP TABLE IF EXISTS test.prepare_statement_flow;
- CREATE TABLE test.prepare_statement_flow (id INT);
- SET @sql := 'CREATE INDEX idx_prepare_statement_flow ON test.prepare_statement_flow(id)';
+ statement := fmt.Sprintf(`
+ USE %[1]s;
+ CREATE TABLE %[1]s.prepare_statement_flow (id INT);
+ SET @sql := 'CREATE INDEX idx_prepare_statement_flow ON %[1]s.prepare_statement_flow(id)';
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
- `
+ `, database)
_, err := tidbDriver.Execute(ctx, statement, db.ExecuteOptions{})
require.NoError(t, err)
var count int
- query := `
+ query := fmt.Sprintf(`
SELECT COUNT(*)
FROM information_schema.tidb_indexes
- WHERE table_schema = 'test'
+ WHERE table_schema = '%s'
AND table_name = 'prepare_statement_flow'
AND key_name = 'idx_prepare_statement_flow'
- `
+ `, database)
err = tidbDriver.db.QueryRowContext(ctx, query).Scan(&count)
require.NoError(t, err)
require.Equal(t, 1, count)
diff --git a/backend/runner/plancheck/scheduler.go b/backend/runner/plancheck/scheduler.go
index 34313c49a0..3ed83bff6b 100644
--- a/backend/runner/plancheck/scheduler.go
+++ b/backend/runner/plancheck/scheduler.go
@@ -40,6 +40,10 @@ type Scheduler struct {
executor *CombinedExecutor
licenseService *enterprise.LicenseService
productMetrics *productmetrics.ProductMetrics
+
+ // runs tracks the goroutines runOnce spawns. The server closes the store
+ // once every Run has returned, so Run has to wait for them.
+ runs sync.WaitGroup
}
// Run runs the scheduler.
@@ -47,6 +51,7 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
ticker := time.NewTicker(planCheckSchedulerInterval)
defer ticker.Stop()
defer wg.Done()
+ defer s.runs.Wait()
slog.Debug(fmt.Sprintf("Plan check scheduler started and will run every %v", planCheckSchedulerInterval))
for {
select {
@@ -91,7 +96,7 @@ func (s *Scheduler) runOnce(ctx context.Context) {
}
for _, c := range claimed {
- go s.runPlanCheckRun(ctx, c.ProjectID, c.UID, c.PlanUID, c.ApprovalInputVersion)
+ s.runs.Go(func() { s.runPlanCheckRun(ctx, c.ProjectID, c.UID, c.PlanUID, c.ApprovalInputVersion) })
}
result = productmetrics.ResultSuccess
}
@@ -208,8 +213,12 @@ func (s *Scheduler) markPlanCheckRunDone(ctx context.Context, projectID string,
return
}
if issue != nil && issue.PlanUID != nil && !issue.Payload.GetDraft() {
- // Trigger approval finding.
- s.bus.ApprovalCheckChan <- bus.IssueRef{ProjectID: projectID, UID: issue.UID}
+ // Trigger approval finding. Give up on shutdown: the consumer stops
+ // first, and a blocked send here would hold the runner open past it.
+ select {
+ case s.bus.ApprovalCheckChan <- bus.IssueRef{ProjectID: projectID, UID: issue.UID}:
+ case <-ctx.Done():
+ }
}
}
diff --git a/backend/runner/reviewrun/scheduler.go b/backend/runner/reviewrun/scheduler.go
index 990468d792..297b8500f3 100644
--- a/backend/runner/reviewrun/scheduler.go
+++ b/backend/runner/reviewrun/scheduler.go
@@ -59,6 +59,10 @@ type Scheduler struct {
licenseService *enterprise.LicenseService
productMetrics *productmetrics.ProductMetrics
executorMap map[string]Executor
+
+ // runs tracks the goroutines dispatchReviewRun spawns; Run waits for them,
+ // or they outlive the store.
+ runs sync.WaitGroup
}
// Register registers an executor for a reviewer type. Call before Run.
@@ -74,6 +78,7 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
ticker := time.NewTicker(reviewRunSchedulerInterval)
defer ticker.Stop()
defer wg.Done()
+ defer s.runs.Wait()
slog.Debug(fmt.Sprintf("Review run scheduler started and will run every %v", reviewRunSchedulerInterval))
for {
select {
@@ -136,7 +141,7 @@ func (s *Scheduler) dispatchReviewRun(ctx context.Context, claimed *store.Claime
s.completeReviewRun(ctx, claimed, errors.Errorf("no executor registered for reviewer type %q", claimed.Type))
return
}
- go s.runReviewRunOnce(ctx, claimed, executor)
+ s.runs.Go(func() { s.runReviewRunOnce(ctx, claimed, executor) })
}
func (s *Scheduler) runReviewRunOnce(ctx context.Context, claimed *store.ClaimedReviewRun, executor Executor) {
diff --git a/backend/runner/taskrun/running_scheduler.go b/backend/runner/taskrun/running_scheduler.go
index 15c7741411..51aaec48ba 100644
--- a/backend/runner/taskrun/running_scheduler.go
+++ b/backend/runner/taskrun/running_scheduler.go
@@ -27,6 +27,7 @@ func (s *Scheduler) runRunningTaskRunsScheduler(ctx context.Context, wg *sync.Wa
ticker := time.NewTicker(taskSchedulerInterval)
defer ticker.Stop()
defer wg.Done()
+ defer s.runs.Wait()
slog.Debug(fmt.Sprintf("Running task runs scheduler started and will run every %v", taskSchedulerInterval))
for {
select {
@@ -135,7 +136,7 @@ func (s *Scheduler) executeTaskRun(ctx context.Context, projectID string, taskRu
return errors.Wrapf(err, "failed to update task run start at")
}
- go s.runTaskRunOnce(ctx, taskRunUID, task, executor)
+ s.runs.Go(func() { s.runTaskRunOnce(ctx, taskRunUID, task, executor) })
return nil
}
@@ -257,8 +258,12 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int64, task *
return
}
- // Signal to check if plan is complete and successful (may send PIPELINE_COMPLETED)
- s.bus.PlanCompletionCheckChan <- bus.PlanRef{ProjectID: task.ProjectID, PlanID: task.PlanID}
+ // Signal to check if plan is complete and successful (may send PIPELINE_COMPLETED).
+ // Give up on shutdown rather than blocking on a stopped consumer.
+ select {
+ case s.bus.PlanCompletionCheckChan <- bus.PlanRef{ProjectID: task.ProjectID, PlanID: task.PlanID}:
+ case <-ctx.Done():
+ }
}
// validateTaskFreshness checks for state drift between task creation and execution time.
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.go
index d4996237b0..f9b0ed1146 100644
--- a/backend/runner/taskrun/scheduler.go
+++ b/backend/runner/taskrun/scheduler.go
@@ -40,6 +40,10 @@ type Scheduler struct {
// haFailSince is when CheckReplicaLimit first started failing.
// Zero means the check is currently passing.
haFailSince time.Time
+
+ // runs tracks the task goroutines; the scheduler that spawns them waits for
+ // them before it returns, or they outlive the store.
+ runs sync.WaitGroup
}
// NewScheduler will create a new scheduler.
@@ -159,7 +163,7 @@ func (s *Scheduler) failTaskRunsForHA(ctx context.Context, haErr error) {
func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
- go s.runTaskCompletionListener(ctx)
+ wg.Go(func() { s.runTaskCompletionListener(ctx) })
// Start rollout creator component
rolloutCreator := NewRolloutCreator(s.store, s.bus, s.webhookManager)
diff --git a/backend/server/server.go b/backend/server/server.go
index 0286f6637f..3f811ded15 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -367,6 +367,20 @@ func (s *Server) Run(ctx context.Context, port int) error {
}
// Shutdown will shut down the server.
+func waitRunners(ctx context.Context, wg *sync.WaitGroup) error {
+ stopped := make(chan struct{})
+ go func() {
+ wg.Wait()
+ close(stopped)
+ }()
+ select {
+ case <-stopped:
+ return nil
+ case <-ctx.Done():
+ return ctx.Err()
+ }
+}
+
func (s *Server) Shutdown(ctx context.Context) error {
slog.Info("Stopping Bytebase...")
slog.Info("Stopping web server...")
@@ -386,8 +400,12 @@ func (s *Server) Shutdown(ctx context.Context) error {
}
}
- // Wait for all runners to exit.
- s.runnerWG.Wait()
+ // Wait for all runners to exit, but not past the shutdown deadline: one
+ // blocked on a stopped consumer must not hold the process open. A runner
+ // that outlives this gets an error from the closed store, not a nil handle.
+ if err := waitRunners(ctx, &s.runnerWG); err != nil {
+ slog.Warn("Runners did not stop before the shutdown deadline", log.BBError(err))
+ }
// Close db connection
if s.store != nil {
diff --git a/backend/store/db_connection.go b/backend/store/db_connection.go
index 915c90da10..88d52c97c0 100644
--- a/backend/store/db_connection.go
+++ b/backend/store/db_connection.go
@@ -23,6 +23,7 @@ import (
type DBConnectionManager struct {
mu sync.Mutex
db *sql.DB
+ closed bool
cleanup func() error
pgURLOrFile string // Either a PostgreSQL URL or a file path
watcher *fsnotify.Watcher
@@ -73,8 +74,12 @@ func (m *DBConnectionManager) Initialize(ctx context.Context) error {
return nil
}
-// GetDB returns the current database connection.
+// GetDB returns the current database connection. After Close it returns the
+// same handle, closed, so a runner that outlives shutdown gets an error rather
+// than a nil pointer.
func (m *DBConnectionManager) GetDB() *sql.DB {
+ m.mu.Lock()
+ defer m.mu.Unlock()
return m.db
}
@@ -88,12 +93,12 @@ func (m *DBConnectionManager) Close() error {
m.mu.Lock()
defer m.mu.Unlock()
- if m.db == nil {
+ if m.db == nil || m.closed {
return nil
}
err := closeMetadataDBConnection(&metadataDBConnection{db: m.db, cleanup: m.cleanup})
- m.db = nil
+ m.closed = true
m.cleanup = nil
return err
}
@@ -164,6 +169,7 @@ func (m *DBConnectionManager) reloadConnection(ctx context.Context, filePath str
oldCleanup := m.cleanup
m.db = newConn.db
m.cleanup = newConn.cleanup
+ m.closed = false
m.mu.Unlock()
// Gracefully drain old connections and force close after 1 hour
diff --git a/backend/store/db_connection_test.go b/backend/store/db_connection_test.go
index abe70b13b8..a76c20e607 100644
--- a/backend/store/db_connection_test.go
+++ b/backend/store/db_connection_test.go
@@ -1,6 +1,8 @@
package store
import (
+ "context"
+ "database/sql"
"testing"
"github.com/stretchr/testify/require"
@@ -72,3 +74,19 @@ func TestIsFilePath(t *testing.T) {
})
}
}
+
+// TestDBConnectionManagerGetDBAfterClose pins what a runner that outlives the
+// server sees: a nil handle here panicked the process instead of erroring.
+func TestDBConnectionManagerGetDBAfterClose(t *testing.T) {
+ t.Parallel()
+ // Nothing connects: sql.Open is lazy, and the test never queries.
+ db, err := sql.Open("pgx", "postgresql://user:[email protected]:1/db")
+ require.NoError(t, err)
+ m := &DBConnectionManager{db: db, stopWatcher: make(chan struct{})}
+
+ require.NoError(t, m.Close())
+
+ require.Same(t, db, m.GetDB())
+ require.ErrorContains(t, m.GetDB().PingContext(context.Background()), "database is closed")
+ require.NoError(t, m.Close(), "Close is idempotent")
+}
diff --git a/backend/tests/account_email_validation_test.go b/backend/tests/account_email_validation_test.go
index 7bc3efce91..77f269d2e1 100644
--- a/backend/tests/account_email_validation_test.go
+++ b/backend/tests/account_email_validation_test.go
@@ -27,13 +27,10 @@ func TestServiceAccountEmailValidation(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
workspace := getCurrentWorkspace(ctx, t, ctl)
- _, err = ctl.serviceAccountServiceClient.CreateServiceAccount(ctx, connect.NewRequest(&v1pb.CreateServiceAccountRequest{
+ _, err := ctl.serviceAccountServiceClient.CreateServiceAccount(ctx, connect.NewRequest(&v1pb.CreateServiceAccountRequest{
Parent: workspace,
ServiceAccountId: "Bot",
ServiceAccount: &v1pb.ServiceAccount{
@@ -48,12 +45,9 @@ func TestServiceAccountEmailValidation(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- _, err = ctl.serviceAccountServiceClient.GetServiceAccount(ctx, connect.NewRequest(&v1pb.GetServiceAccountRequest{
+ _, err := ctl.serviceAccountServiceClient.GetServiceAccount(ctx, connect.NewRequest(&v1pb.GetServiceAccountRequest{
Name: "serviceAccounts/[email protected]",
}))
a.Error(err)
@@ -67,13 +61,10 @@ func TestWorkloadIdentityEmailValidation(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
workspace := getCurrentWorkspace(ctx, t, ctl)
- _, err = ctl.workloadIdentityServiceClient.CreateWorkloadIdentity(ctx, connect.NewRequest(&v1pb.CreateWorkloadIdentityRequest{
+ _, err := ctl.workloadIdentityServiceClient.CreateWorkloadIdentity(ctx, connect.NewRequest(&v1pb.CreateWorkloadIdentityRequest{
Parent: workspace,
WorkloadIdentityId: "Bot",
WorkloadIdentity: &v1pb.WorkloadIdentity{
@@ -88,12 +79,9 @@ func TestWorkloadIdentityEmailValidation(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- _, err = ctl.workloadIdentityServiceClient.GetWorkloadIdentity(ctx, connect.NewRequest(&v1pb.GetWorkloadIdentityRequest{
+ _, err := ctl.workloadIdentityServiceClient.GetWorkloadIdentity(ctx, connect.NewRequest(&v1pb.GetWorkloadIdentityRequest{
Name: "workloadIdentities/[email protected]",
}))
a.Error(err)
diff --git a/backend/tests/action_test.go b/backend/tests/action_test.go
index 4835ab688a..f3de4e168f 100644
--- a/backend/tests/action_test.go
+++ b/backend/tests/action_test.go
@@ -23,11 +23,7 @@ func TestActionCheckCommand_ValidMigrations(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create test instance and database
database := ctl.createTestDatabase(ctx, t)
@@ -35,7 +31,7 @@ func TestActionCheckCommand_ValidMigrations(t *testing.T) {
// Create test data directory
testDataDir := t.TempDir()
validMigrationsDir := filepath.Join(testDataDir, "valid-migrations")
- err = os.MkdirAll(validMigrationsDir, 0755)
+ err := os.MkdirAll(validMigrationsDir, 0755)
a.NoError(err)
// Create a valid migration file
@@ -85,11 +81,7 @@ func TestActionCheckCommand_ValidMigrationsForDatabaseGroup(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create test instance and database
database := ctl.createTestDatabase(ctx, t)
@@ -161,18 +153,14 @@ func TestActionCheckCommand_SyntaxErrors(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create test database
database := ctl.createTestDatabase(ctx, t)
// Create test data directory with syntax error migration
testDataDir := t.TempDir()
- err = os.MkdirAll(testDataDir, 0755)
+ err := os.MkdirAll(testDataDir, 0755)
a.NoError(err)
// Create a migration file with obvious syntax errors
@@ -214,11 +202,7 @@ func TestActionCheckCommand_MultipleTargets(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create multiple test databases
database1 := ctl.createTestDatabase(ctx, t)
@@ -226,7 +210,7 @@ func TestActionCheckCommand_MultipleTargets(t *testing.T) {
// Create test data directory
testDataDir := t.TempDir()
- err = os.MkdirAll(testDataDir, 0755)
+ err := os.MkdirAll(testDataDir, 0755)
a.NoError(err)
// Create a valid migration file
@@ -277,15 +261,10 @@ func TestActionCheckCommand_DeclarativeCheckValidSchema(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create PostgreSQL test database
- database, pgContainer := ctl.createTestPostgreSQLDatabase(ctx, t)
- defer pgContainer.Close(ctx)
+ database := ctl.createTestPostgreSQLDatabase(ctx, t)
// Create test data directory and schema.sql file
testDataDir := t.TempDir()
@@ -297,7 +276,7 @@ func TestActionCheckCommand_DeclarativeCheckValidSchema(t *testing.T) {
CONSTRAINT uk_users_username UNIQUE (username)
);`
schemaFile := filepath.Join(testDataDir, "schema.sql")
- err = os.WriteFile(schemaFile, []byte(schemaContent), 0644)
+ err := os.WriteFile(schemaFile, []byte(schemaContent), 0644)
a.NoError(err)
// Execute declarative check command
@@ -320,15 +299,10 @@ func TestActionCheckCommand_DeclarativeCheckMultipleFiles(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create PostgreSQL test database
- database, pgContainer := ctl.createTestPostgreSQLDatabase(ctx, t)
- defer pgContainer.Close(ctx)
+ database := ctl.createTestPostgreSQLDatabase(ctx, t)
// Create test data directory with multiple SQL files
testDataDir := t.TempDir()
@@ -342,7 +316,7 @@ func TestActionCheckCommand_DeclarativeCheckMultipleFiles(t *testing.T) {
CONSTRAINT uk_users_username UNIQUE (username)
);`
usersFile := filepath.Join(testDataDir, "users.sql")
- err = os.WriteFile(usersFile, []byte(usersContent), 0644)
+ err := os.WriteFile(usersFile, []byte(usersContent), 0644)
a.NoError(err)
// Create products.sql
@@ -376,11 +350,7 @@ func TestActionCheckCommand_DeclarativeCheckWithDatabaseGroup(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Keeps MySQL. Porting this to Postgres was tried and reverted: the
// declarative release check returns three errors against the same schema
@@ -431,15 +401,10 @@ func TestActionCheckCommand_DeclarativeCheckSyntaxErrors(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create PostgreSQL test database
- database, pgContainer := ctl.createTestPostgreSQLDatabase(ctx, t)
- defer pgContainer.Close(ctx)
+ database := ctl.createTestPostgreSQLDatabase(ctx, t)
// Create test data directory with invalid SQL
testDataDir := t.TempDir()
@@ -449,7 +414,7 @@ func TestActionCheckCommand_DeclarativeCheckSyntaxErrors(t *testing.T) {
email VARCHAR(255) -- Missing comma after previous column
);`
schemaFile := filepath.Join(testDataDir, "schema.sql")
- err = os.WriteFile(schemaFile, []byte(invalidSchemaContent), 0644)
+ err := os.WriteFile(schemaFile, []byte(invalidSchemaContent), 0644)
a.NoError(err)
// Execute declarative check command with syntax error
@@ -535,16 +500,16 @@ func (ctl *controller) createTestDatabase(ctx context.Context, t *testing.T) *v1
a := require.New(t)
// Create Postgres instance
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
- InstanceId: generateRandomString("inst")[:8],
+ InstanceId: shortInstanceID(),
Instance: &v1pb.Instance{
- Title: "Test Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "Test Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -568,12 +533,11 @@ func (ctl *controller) createTestMySQLDatabase(ctx context.Context, t *testing.T
a := require.New(t)
// Get MySQL container
- mysqlContainer, err := getMySQLContainer(ctx)
- a.NoError(err)
+ mysqlContainer := provisionMySQLInstance(t)
// Create MySQL instance
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
- InstanceId: generateRandomString("inst")[:8],
+ InstanceId: shortInstanceID(),
Instance: &v1pb.Instance{
Title: "Test MySQL Instance",
Engine: v1pb.Engine_MYSQL,
@@ -581,8 +545,8 @@ func (ctl *controller) createTestMySQLDatabase(ctx context.Context, t *testing.T
Activation: true,
DataSources: []*v1pb.DataSource{{
Type: v1pb.DataSourceType_ADMIN,
- Host: mysqlContainer.host,
- Port: mysqlContainer.port,
+ Host: mysqlContainer.GetHost(),
+ Port: mysqlContainer.GetPort(),
Username: "root",
Password: "root-password",
Id: "admin",
@@ -606,25 +570,25 @@ func (ctl *controller) createTestMySQLDatabase(ctx context.Context, t *testing.T
}
// createTestPostgreSQLDatabase creates a test PostgreSQL database instance and database
-func (ctl *controller) createTestPostgreSQLDatabase(ctx context.Context, t *testing.T) (*v1pb.Database, *Container) {
+func (ctl *controller) createTestPostgreSQLDatabase(ctx context.Context, t *testing.T) *v1pb.Database {
a := require.New(t)
// Get PostgreSQL container
- pgContainer, err := getPgContainer(ctx)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
// Create PostgreSQL instance
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
- InstanceId: generateRandomString("inst")[:8],
+ InstanceId: shortInstanceID(),
Instance: &v1pb.Instance{
- Title: "Test PostgreSQL Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "Test PostgreSQL Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
DataSources: []*v1pb.DataSource{{
Type: v1pb.DataSourceType_ADMIN,
- Host: pgContainer.host,
- Port: pgContainer.port,
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
Username: "postgres",
Password: "root-password",
Id: "admin",
@@ -644,18 +608,14 @@ func (ctl *controller) createTestPostgreSQLDatabase(ctx context.Context, t *test
}))
a.NoError(err)
- return databaseResp.Msg, pgContainer
+ return databaseResp.Msg
}
func TestActionRolloutCommand_BasicRollout(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create test database
database := ctl.createTestDatabase(ctx, t)
@@ -668,7 +628,7 @@ func TestActionRolloutCommand_BasicRollout(t *testing.T) {
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`
migrationFile := filepath.Join(testDataDir, "00001_create_users.sql")
- err = os.WriteFile(migrationFile, []byte(migrationContent), 0644)
+ err := os.WriteFile(migrationFile, []byte(migrationContent), 0644)
a.NoError(err)
// Create output file
@@ -785,11 +745,7 @@ func TestActionRolloutCommand_MultipleFiles(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create test database
database := ctl.createTestDatabase(ctx, t)
@@ -803,7 +759,7 @@ func TestActionRolloutCommand_MultipleFiles(t *testing.T) {
username TEXT NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`
- err = os.WriteFile(filepath.Join(testDataDir, "00001_create_users.sql"), []byte(migrationContent1), 0644)
+ err := os.WriteFile(filepath.Join(testDataDir, "00001_create_users.sql"), []byte(migrationContent1), 0644)
a.NoError(err)
// Create 00002_add_email.sql
@@ -989,11 +945,7 @@ func TestActionRolloutCommand_FileContentVersionMatch(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create test database
database := ctl.createTestDatabase(ctx, t)
@@ -1007,7 +959,7 @@ func TestActionRolloutCommand_FileContentVersionMatch(t *testing.T) {
migrationContent2 := `CREATE TABLE second_table (id INTEGER);`
migrationContent10 := `CREATE TABLE tenth_table (id INTEGER);`
- err = os.WriteFile(filepath.Join(testDataDir, "v1_first.sql"), []byte(migrationContent1), 0644)
+ err := os.WriteFile(filepath.Join(testDataDir, "v1_first.sql"), []byte(migrationContent1), 0644)
a.NoError(err)
err = os.WriteFile(filepath.Join(testDataDir, "v2_second.sql"), []byte(migrationContent2), 0644)
a.NoError(err)
@@ -1063,15 +1015,10 @@ func TestActionRolloutDeclarativeMode_BasicDeclarativeRollout(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create PostgreSQL test database
- database, pgContainer := ctl.createTestPostgreSQLDatabase(ctx, t)
- defer pgContainer.Close(ctx)
+ database := ctl.createTestPostgreSQLDatabase(ctx, t)
// Create test data directory and schema.sql file
testDataDir := t.TempDir()
@@ -1080,7 +1027,7 @@ func TestActionRolloutDeclarativeMode_BasicDeclarativeRollout(t *testing.T) {
username VARCHAR(255) NOT NULL UNIQUE
);`
schemaFile := filepath.Join(testDataDir, "schema.sql")
- err = os.WriteFile(schemaFile, []byte(migrationContent1), 0644)
+ err := os.WriteFile(schemaFile, []byte(migrationContent1), 0644)
a.NoError(err)
// Create output file
@@ -1133,15 +1080,10 @@ func TestActionRolloutDeclarativeMode_DeclarativeSchemaEvolution(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create PostgreSQL test database
- database, pgContainer := ctl.createTestPostgreSQLDatabase(ctx, t)
- defer pgContainer.Close(ctx)
+ database := ctl.createTestPostgreSQLDatabase(ctx, t)
// Create test data directory
testDataDir := t.TempDir()
@@ -1152,7 +1094,7 @@ func TestActionRolloutDeclarativeMode_DeclarativeSchemaEvolution(t *testing.T) {
id SERIAL,
username VARCHAR(255) NOT NULL UNIQUE
);`
- err = os.WriteFile(schemaFile, []byte(migrationContent1), 0644)
+ err := os.WriteFile(schemaFile, []byte(migrationContent1), 0644)
a.NoError(err)
// Execute first declarative rollout
@@ -1240,15 +1182,10 @@ func TestActionRolloutDeclarativeMode_DeclarativeIdempotency(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create PostgreSQL test database
- database, pgContainer := ctl.createTestPostgreSQLDatabase(ctx, t)
- defer pgContainer.Close(ctx)
+ database := ctl.createTestPostgreSQLDatabase(ctx, t)
// Create test data directory and schema.sql file
testDataDir := t.TempDir()
@@ -1258,7 +1195,7 @@ func TestActionRolloutDeclarativeMode_DeclarativeIdempotency(t *testing.T) {
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`
schemaFile := filepath.Join(testDataDir, "schema.sql")
- err = os.WriteFile(schemaFile, []byte(migrationContent), 0644)
+ err := os.WriteFile(schemaFile, []byte(migrationContent), 0644)
a.NoError(err)
// Create output files
@@ -1406,17 +1343,11 @@ func TestActionRolloutDeclarativeMode_DeclarativeMultipleDatabases(t *testing.T)
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create multiple PostgreSQL test databases
- database1, pgContainer1 := ctl.createTestPostgreSQLDatabase(ctx, t)
- defer pgContainer1.Close(ctx)
- database2, pgContainer2 := ctl.createTestPostgreSQLDatabase(ctx, t)
- defer pgContainer2.Close(ctx)
+ database1 := ctl.createTestPostgreSQLDatabase(ctx, t)
+ database2 := ctl.createTestPostgreSQLDatabase(ctx, t)
// Create test data directory and schema.sql file
testDataDir := t.TempDir()
@@ -1426,7 +1357,7 @@ func TestActionRolloutDeclarativeMode_DeclarativeMultipleDatabases(t *testing.T)
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`
schemaFile := filepath.Join(testDataDir, "schema.sql")
- err = os.WriteFile(schemaFile, []byte(migrationContent), 0644)
+ err := os.WriteFile(schemaFile, []byte(migrationContent), 0644)
a.NoError(err)
// Execute declarative rollout to multiple databases
@@ -1470,15 +1401,10 @@ func TestActionRolloutDeclarativeMode_DeclarativeWithDatabaseGroup(t *testing.T)
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create PostgreSQL test database
- database, pgContainer := ctl.createTestPostgreSQLDatabase(ctx, t)
- defer pgContainer.Close(ctx)
+ database := ctl.createTestPostgreSQLDatabase(ctx, t)
// Create database group
databaseGroup, err := ctl.databaseGroupServiceClient.CreateDatabaseGroup(ctx, connect.NewRequest(&v1pb.CreateDatabaseGroupRequest{
@@ -1541,15 +1467,10 @@ func TestActionRolloutDeclarativeMode_DeclarativeMultipleFilesMerged(t *testing.
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create PostgreSQL test database
- database, pgContainer := ctl.createTestPostgreSQLDatabase(ctx, t)
- defer pgContainer.Close(ctx)
+ database := ctl.createTestPostgreSQLDatabase(ctx, t)
// Create test data directory with multiple SQL files
testDataDir := t.TempDir()
@@ -1561,7 +1482,7 @@ func TestActionRolloutDeclarativeMode_DeclarativeMultipleFilesMerged(t *testing.
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`
usersFile := filepath.Join(testDataDir, "users.sql")
- err = os.WriteFile(usersFile, []byte(usersContent), 0644)
+ err := os.WriteFile(usersFile, []byte(usersContent), 0644)
a.NoError(err)
// Create names.sql
@@ -1646,11 +1567,7 @@ func TestActionErrorScenarios_InvalidServiceAccountSecret(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create test database
database := ctl.createTestDatabase(ctx, t)
@@ -1662,7 +1579,7 @@ func TestActionErrorScenarios_InvalidServiceAccountSecret(t *testing.T) {
username TEXT NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`
- err = os.WriteFile(filepath.Join(testDataDir, "00001_create_users.sql"), []byte(migrationContent), 0644)
+ err := os.WriteFile(filepath.Join(testDataDir, "00001_create_users.sql"), []byte(migrationContent), 0644)
a.NoError(err)
// Execute command with invalid credentials
@@ -1684,11 +1601,7 @@ func TestActionErrorScenarios_NonExistentDatabase(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create test data directory and migration file
testDataDir := t.TempDir()
@@ -1697,7 +1610,7 @@ func TestActionErrorScenarios_NonExistentDatabase(t *testing.T) {
username TEXT NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`
- err = os.WriteFile(filepath.Join(testDataDir, "00001_create_users.sql"), []byte(migrationContent), 0644)
+ err := os.WriteFile(filepath.Join(testDataDir, "00001_create_users.sql"), []byte(migrationContent), 0644)
a.NoError(err)
// Try to target a database that doesn't exist
@@ -1719,11 +1632,7 @@ func TestActionErrorScenarios_EmptyFilePattern(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
database := ctl.createTestDatabase(ctx, t)
@@ -1731,7 +1640,7 @@ func TestActionErrorScenarios_EmptyFilePattern(t *testing.T) {
testDataDir := t.TempDir()
// Use a pattern that matches no files
- _, err = executeActionCommand(ctx,
+ _, err := executeActionCommand(ctx,
"rollout",
"--url", ctl.rootURL,
"--service-account", "[email protected]",
diff --git a/backend/tests/approval_test.go b/backend/tests/approval_test.go
index 12d77625c0..a1eb58f4f1 100644
--- a/backend/tests/approval_test.go
+++ b/backend/tests/approval_test.go
@@ -27,23 +27,19 @@ func TestDirectApprovalRuleMatching(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Create instance in prod environment
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("inst"),
Instance: &v1pb.Instance{
- Title: "Prod Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "Prod Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -156,22 +152,18 @@ func TestApprovalFindingRerunsPlanCheckForStaleApprovalInputVersion(t *testing.T
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
+ ctl, ctx := startWorkspace(ctx, t)
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- t.Cleanup(func() { _ = ctl.Close(ctx) })
-
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("inst"),
Instance: &v1pb.Instance{
- Title: "Approval Input Version Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "Approval Input Version Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -307,11 +299,7 @@ func TestApprovalFindingRerunsPlanCheckForStaleApprovalInputVersion(t *testing.T
func TestApprovalActionRejectsStaleApprovalInputVersion(t *testing.T) {
t.Parallel()
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- require.NoError(t, err)
- t.Cleanup(func() { _ = ctl.Close(ctx) })
+ ctl, ctx := startWorkspace(ctx, t)
stores := getStore(t, ctl.server)
@@ -377,16 +365,16 @@ func createStaleApprovalInputVersionIssue(ctx context.Context, t *testing.T, ctl
t.Helper()
project := ctl.createTestProject(ctx, t, suffix)
- pgContainer, err := provisionPgInstance(ctx, t)
- require.NoError(t, err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("inst"),
Instance: &v1pb.Instance{
- Title: "Stale Approval Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "Stale Approval Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
require.NoError(t, err)
@@ -495,23 +483,19 @@ func TestApprovalRuleFirstMatchWins(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Create instance in prod environment
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("inst"),
Instance: &v1pb.Instance{
- Title: "Prod Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "Prod Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -637,23 +621,19 @@ func TestApprovalRuleNoMatch(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Create instance in test environment (not prod)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("inst"),
Instance: &v1pb.Instance{
- Title: "Test Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/test"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "Test Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/test"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -767,14 +747,10 @@ func TestFallbackRuleValidation(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Test 1: Valid fallback rule with project_id should succeed
- _, err = ctl.settingServiceClient.UpdateSetting(ctx, connect.NewRequest(&v1pb.UpdateSettingRequest{
+ _, err := ctl.settingServiceClient.UpdateSetting(ctx, connect.NewRequest(&v1pb.UpdateSettingRequest{
AllowMissing: true,
Setting: &v1pb.Setting{
Name: "settings/WORKSPACE_APPROVAL",
@@ -835,23 +811,19 @@ func TestFallbackRuleMatchesWhenSourceSpecificDoesNot(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Create instance in TEST environment (not prod)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("inst"),
Instance: &v1pb.Instance{
- Title: "Test Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/test"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "Test Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/test"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -971,23 +943,19 @@ func TestSourceSpecificRuleTakesPriorityOverFallback(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Create instance in prod environment
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("inst"),
Instance: &v1pb.Instance{
- Title: "Prod Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "Prod Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -1106,11 +1074,7 @@ func TestSelfApprovalBlocked(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Create a dedicated project (initially with self-approval enabled for database creation)
projectID := generateRandomString("self-approval-test")
@@ -1126,16 +1090,16 @@ func TestSelfApprovalBlocked(t *testing.T) {
project := projectResp.Msg
// Create instance
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("inst"),
Instance: &v1pb.Instance{
- Title: "Prod Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "Prod Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
diff --git a/backend/tests/collision_helper_test.go b/backend/tests/collision_helper_test.go
index a56f0cf07e..bb8b74c4c0 100644
--- a/backend/tests/collision_helper_test.go
+++ b/backend/tests/collision_helper_test.go
@@ -78,27 +78,27 @@ func setupCollidingProjects(
}))
a.NoError(err)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("col-inst"),
Instance: &v1pb.Instance{
- Title: "collision-instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "collision-instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
instance := instanceResp.Msg
- dbNameA := "collision_db_a"
+ dbNameA := uniqueDB("collision_db_a")
err = ctl.createDatabase(ctx, projectA.Msg, instance, nil, dbNameA, "")
a.NoError(err)
- dbNameB := "collision_db_b"
+ dbNameB := uniqueDB("collision_db_b")
err = ctl.createDatabase(ctx, projectB.Msg, instance, nil, dbNameB, "")
a.NoError(err)
@@ -238,14 +238,14 @@ func setupCollidingProjectsSeparateInstances(
instA := createPgInstance(ctx, t, ctl, "col-inst-a")
instB := createPgInstance(ctx, t, ctl, "col-inst-b")
- const dbNameA = "collision_db_a"
+ dbNameA := uniqueDB("collision_db_a")
a.NoError(ctl.createDatabase(ctx, projectA.Msg, instA, nil, dbNameA, ""))
dbA, err := ctl.databaseServiceClient.GetDatabase(ctx, connect.NewRequest(&v1pb.GetDatabaseRequest{
Name: fmt.Sprintf("%s/databases/%s", instA.Name, dbNameA),
}))
a.NoError(err)
- const dbNameB = "collision_db_b"
+ dbNameB := uniqueDB("collision_db_b")
a.NoError(ctl.createDatabase(ctx, projectB.Msg, instB, nil, dbNameB, ""))
dbB, err := ctl.databaseServiceClient.GetDatabase(ctx, connect.NewRequest(&v1pb.GetDatabaseRequest{
Name: fmt.Sprintf("%s/databases/%s", instB.Name, dbNameB),
@@ -464,16 +464,16 @@ func listTaskRunAndTaskUIDs(ctx context.Context, t *testing.T, ctl *controller,
func createPgInstance(ctx context.Context, t *testing.T, ctl *controller, titlePrefix string) *v1pb.Instance {
t.Helper()
a := require.New(t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
resp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString(titlePrefix),
Instance: &v1pb.Instance{
- Title: titlePrefix,
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: titlePrefix,
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
diff --git a/backend/tests/common.go b/backend/tests/common.go
index 58c1f3ff8b..7f84d7c38c 100644
--- a/backend/tests/common.go
+++ b/backend/tests/common.go
@@ -2,6 +2,7 @@ package tests
import (
"fmt"
+ "strings"
"github.com/google/uuid"
)
@@ -9,3 +10,15 @@ import (
func generateRandomString(prefix string) string {
return fmt.Sprintf("%s-%s", prefix, uuid.New().String()[:10])
}
+
+// shortInstanceID is an eight-character instance ID, for the tests that keep
+// names short. Instance IDs are unique per workspace, which the tests share.
+func shortInstanceID() string {
+ return "in" + strings.ReplaceAll(uuid.New().String(), "-", "")[:6]
+}
+
+// uniqueDB makes a database name unique to one test, which the shared target
+// Postgres needs: two tests asking for "history_db" would be one database.
+func uniqueDB(name string) string {
+ return fmt.Sprintf("%s_%s", name, strings.ReplaceAll(uuid.New().String(), "-", "")[:8])
+}
diff --git a/backend/tests/credential_change_test.go b/backend/tests/credential_change_test.go
index c04a76bcc0..9b3c1f68c9 100644
--- a/backend/tests/credential_change_test.go
+++ b/backend/tests/credential_change_test.go
@@ -40,10 +40,7 @@ func TestChangePasswordProofAndRevocation(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
metadataDB, err := sql.Open("pgx", ctl.profile.PgURL)
a.NoError(err)
@@ -153,16 +150,13 @@ func TestCredentialProofSharesLoginLockout(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
const email = "[email protected]"
const password = "victim-password-1"
adminToken := ctl.authInterceptor.token
ctl.authInterceptor.token = ""
- _, err = ctl.authServiceClient.Signup(ctx, connect.NewRequest(&v1pb.SignupRequest{
+ _, err := ctl.authServiceClient.Signup(ctx, connect.NewRequest(&v1pb.SignupRequest{
Email: email,
Title: "Lockout Victim",
Password: password,
@@ -201,10 +195,7 @@ func TestMFALifecycleFactorBoundProofs(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
const email = "[email protected]"
const password = "1024bytebase"
@@ -214,7 +205,7 @@ func TestMFALifecycleFactorBoundProofs(t *testing.T) {
// While a live factor exists, factor-touching methods refuse the password:
// ResetPassword mints one from mailbox possession alone, so accepting it
// here would let a stolen session plus mailbox strip the second factor.
- _, err = ctl.userServiceClient.DisableMFA(ctx, connect.NewRequest(&v1pb.DisableMFARequest{
+ _, err := ctl.userServiceClient.DisableMFA(ctx, connect.NewRequest(&v1pb.DisableMFARequest{
Name: userName,
Credential: passwordProofOf(password),
}))
@@ -278,10 +269,7 @@ func TestEmailCodeProofIsCloudOnly(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
metadataDB, err := sql.Open("pgx", ctl.profile.PgURL)
a.NoError(err)
@@ -366,10 +354,7 @@ func TestRecoveryCodeProofIsSpentOnce(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
const email = "[email protected]"
const password = "1024bytebase"
diff --git a/backend/tests/cross_project_claim_test.go b/backend/tests/cross_project_claim_test.go
index d830685808..f9c4cc9bcf 100644
--- a/backend/tests/cross_project_claim_test.go
+++ b/backend/tests/cross_project_claim_test.go
@@ -28,10 +28,7 @@ func TestClaimAvailableTaskRunsNoCrossProjectResurrection(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
@@ -58,10 +55,7 @@ func TestClaimAvailablePlanCheckRunsNoCrossProjectTransition(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
diff --git a/backend/tests/cross_project_delete_test.go b/backend/tests/cross_project_delete_test.go
index 162ef03c49..15608e68a4 100644
--- a/backend/tests/cross_project_delete_test.go
+++ b/backend/tests/cross_project_delete_test.go
@@ -25,10 +25,7 @@ func TestCollisionDeleteProjectCascade(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
@@ -40,7 +37,7 @@ func TestCollisionDeleteProjectCascade(t *testing.T) {
a.Greater(len(beforeB.Issues), 0, "project B should have issues")
// Project purge is an explicit archive-then-purge lifecycle.
- _, err = ctl.projectServiceClient.DeleteProject(ctx,
+ _, err := ctl.projectServiceClient.DeleteProject(ctx,
connect.NewRequest(&v1pb.DeleteProjectRequest{Name: fixture.ProjectA.Name}))
a.NoError(err)
_, err = ctl.projectServiceClient.DeleteProject(ctx,
@@ -96,10 +93,7 @@ func TestCollisionDeleteInstanceNoCrossProjectCorruption(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
@@ -140,10 +134,7 @@ func TestCollisionDeleteInstanceCrossProjectIsolation(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjectsSeparateInstances(ctx, t, ctl)
diff --git a/backend/tests/cross_project_read_test.go b/backend/tests/cross_project_read_test.go
index c5c78cee80..075cff9280 100644
--- a/backend/tests/cross_project_read_test.go
+++ b/backend/tests/cross_project_read_test.go
@@ -16,10 +16,7 @@ func TestCollisionListDatabasesProjectIsolation(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
diff --git a/backend/tests/cross_project_update_test.go b/backend/tests/cross_project_update_test.go
index bc534f74b6..fda12ee8d9 100644
--- a/backend/tests/cross_project_update_test.go
+++ b/backend/tests/cross_project_update_test.go
@@ -30,10 +30,7 @@ func TestCollisionUpdateIssueNoCrossProjectEffect(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
@@ -77,10 +74,7 @@ func TestCollisionCreateRolloutIsolation(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
a.Greater(len(fixture.BaselineA.PlanCheckRuns), 0, "project A should have plan_check_runs")
@@ -104,10 +98,7 @@ func TestCollisionBatchRunTasksNoCrossProjectEffect(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
a.Greater(len(fixture.BaselineA.TaskRuns), 0, "project A should have task_runs")
@@ -140,10 +131,7 @@ func TestCollisionBatchSkipTasksNoCrossProjectEffect(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
a.Greater(len(fixture.BaselineA.TaskRuns), 0, "project A should have task_runs")
@@ -190,10 +178,7 @@ func TestCollisionListPlansIsolation(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
@@ -219,10 +204,7 @@ func TestCollisionListTaskRunsIsolation(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
fixture.completeRolloutB(ctx, t, ctl)
@@ -252,10 +234,7 @@ func TestCollisionGetPlanCheckRunIsolation(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
fixture.completeRolloutB(ctx, t, ctl)
@@ -276,10 +255,7 @@ func TestCollisionGetIssueIsolation(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
@@ -299,10 +275,7 @@ func TestCollisionListIssuesIsolation(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
@@ -334,10 +307,7 @@ func TestSearchIssuesConcreteProjectAuthorization(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
ownerToken := ctl.authInterceptor.token
@@ -347,7 +317,7 @@ func TestSearchIssuesConcreteProjectAuthorization(t *testing.T) {
ctl.authInterceptor.token = memberToken
// Concrete parent the caller cannot read: denied, not silently served.
- _, err = ctl.issueServiceClient.SearchIssues(ctx,
+ _, err := ctl.issueServiceClient.SearchIssues(ctx,
connect.NewRequest(&v1pb.SearchIssuesRequest{
Parent: fixture.ProjectB.Name,
PageSize: 100,
diff --git a/backend/tests/data_source_test.go b/backend/tests/data_source_test.go
index 7029a91200..0382e328f2 100644
--- a/backend/tests/data_source_test.go
+++ b/backend/tests/data_source_test.go
@@ -15,13 +15,9 @@ func TestDataSource(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := provisionPgInstance(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
@@ -164,13 +160,9 @@ func TestDataSourceValidateOnly(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := provisionPgInstance(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
@@ -225,7 +217,7 @@ func TestDataSourceValidateOnly(t *testing.T) {
a.NoError(err)
// Create the role so the validate-only connection test with the updated username succeeds.
- _, err = pgContainer.db.Exec(`CREATE ROLE "updated-user" LOGIN PASSWORD 'root-password'`)
+ _, err = pgContainer.GetDB().Exec(`CREATE ROLE "updated-user" LOGIN PASSWORD 'root-password'`)
a.NoError(err)
updateResp, err = ctl.instanceServiceClient.UpdateDataSource(ctx, connect.NewRequest(&v1pb.UpdateDataSourceRequest{
diff --git a/backend/tests/database.go b/backend/tests/database.go
index b8702527c5..a2167bbb14 100644
--- a/backend/tests/database.go
+++ b/backend/tests/database.go
@@ -3,6 +3,7 @@ package tests
import (
"context"
"fmt"
+ "slices"
"connectrpc.com/connect"
"github.com/google/uuid"
@@ -11,7 +12,36 @@ import (
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
)
+// allowDatabaseSync names one more database an instance may sync. The syncer
+// imports only what the list names, so a database about to be created has to be
+// added first. An instance with a server to itself carries no list and skips this.
+func (ctl *controller) allowDatabaseSync(ctx context.Context, instance *v1pb.Instance, databaseName string) error {
+ if instance.GetSyncDatabases() == nil {
+ return nil
+ }
+ resp, err := ctl.instanceServiceClient.GetInstance(ctx, connect.NewRequest(&v1pb.GetInstanceRequest{Name: instance.Name}))
+ if err != nil {
+ return err
+ }
+ databases := resp.Msg.GetSyncDatabases().GetDatabases()
+ if slices.Contains(databases, databaseName) {
+ return nil
+ }
+ _, err = ctl.instanceServiceClient.UpdateInstance(ctx, connect.NewRequest(&v1pb.UpdateInstanceRequest{
+ Instance: &v1pb.Instance{
+ Name: instance.Name,
+ SyncDatabases: &v1pb.SyncDatabases{Databases: append(databases, databaseName)},
+ },
+ UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"sync_databases"}},
+ }))
+ return err
+}
+
func (ctl *controller) createDatabase(ctx context.Context, project *v1pb.Project, instance *v1pb.Instance, environment *v1pb.EnvironmentSetting_Environment, databaseName string, owner string) error {
+ if err := ctl.allowDatabaseSync(ctx, instance, databaseName); err != nil {
+ return err
+ }
+
// Database provisioning is test setup, and its plan is approved by the same
// test actor. Opt into that legacy behavior explicitly; production projects
// keep the restrictive default when the setting is absent.
diff --git a/backend/tests/diff_metadata_test.go b/backend/tests/diff_metadata_test.go
index 415cf5b076..b152c5f9ae 100644
--- a/backend/tests/diff_metadata_test.go
+++ b/backend/tests/diff_metadata_test.go
@@ -21,33 +21,27 @@ func TestDiffMetadata(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
ownerToken := ctl.authInterceptor.token
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "pgInstance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ Title: "pgInstance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ SyncDatabases: &v1pb.SyncDatabases{},
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
instance := instanceResp.Msg
- const databaseName = "diff_metadata_db"
+ databaseName := uniqueDB("diff_metadata_db")
err = ctl.createDatabase(ctx, ctl.project, instance, nil, databaseName, "postgres")
a.NoError(err)
diff --git a/backend/tests/environment_test.go b/backend/tests/environment_test.go
index 46a124175b..745faa2a24 100644
--- a/backend/tests/environment_test.go
+++ b/backend/tests/environment_test.go
@@ -16,13 +16,9 @@ func TestDatabaseEnvironment(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
prodEnvironment, err := ctl.getEnvironment(ctx, "prod")
a.NoError(err)
@@ -32,17 +28,18 @@ func TestDatabaseEnvironment(t *testing.T) {
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "test",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new(prodEnvironment.Name),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.dataSource(v1pb.DataSourceType_ADMIN, "admin-ds")},
+ Title: "test",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new(prodEnvironment.Name),
+ Activation: true,
+ SyncDatabases: &v1pb.SyncDatabases{},
+ DataSources: []*v1pb.DataSource{pgContainer.dataSource(v1pb.DataSourceType_ADMIN, "admin-ds")},
},
}))
a.NoError(err)
instance := instanceResp.Msg
- db0Name := "db0"
+ db0Name := uniqueDB("db0")
err = ctl.createDatabase(ctx, ctl.project, instance, testEnvironment /* environment */, db0Name, "")
a.NoError(err)
db0Resp, err := ctl.databaseServiceClient.GetDatabase(ctx, connect.NewRequest(&v1pb.GetDatabaseRequest{
@@ -55,7 +52,7 @@ func TestDatabaseEnvironment(t *testing.T) {
a.NotNil(db0.EffectiveEnvironment)
a.Equal(testEnvironment.Name, *db0.EffectiveEnvironment)
- db1Name := "db1"
+ db1Name := uniqueDB("db1")
err = ctl.createDatabase(ctx, ctl.project, instance, nil /* environment */, db1Name, "")
a.NoError(err)
db1Resp, err := ctl.databaseServiceClient.GetDatabase(ctx, connect.NewRequest(&v1pb.GetDatabaseRequest{
@@ -67,7 +64,7 @@ func TestDatabaseEnvironment(t *testing.T) {
a.NotNil(db1.EffectiveEnvironment)
a.Equal(prodEnvironment.Name, *db1.EffectiveEnvironment)
- db2Name := "db2"
+ db2Name := uniqueDB("db2")
err = ctl.createDatabase(ctx, ctl.project, instance, nil /* environment */, db2Name, "")
a.NoError(err)
db2Resp, err := ctl.databaseServiceClient.GetDatabase(ctx, connect.NewRequest(&v1pb.GetDatabaseRequest{
diff --git a/backend/tests/ghost_test.go b/backend/tests/ghost_test.go
index 46a41a47aa..9df7c99c6b 100644
--- a/backend/tests/ghost_test.go
+++ b/backend/tests/ghost_test.go
@@ -47,20 +47,13 @@ func TestGhostSchemaUpdate(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- mysqlContainer, err := getMySQLContainer(ctx)
- defer func() {
- mysqlContainer.Close(ctx)
- }()
- a.NoError(err)
+ mysqlContainer := provisionMySQLInstance(t)
- mysqlDB := mysqlContainer.db
+ mysqlDB := mysqlContainer.GetDB()
- _, err = mysqlDB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %v", databaseName))
+ _, err := mysqlDB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %v", databaseName))
a.NoError(err)
_, err = mysqlDB.Exec("DROP USER IF EXISTS bytebase")
@@ -78,7 +71,7 @@ func TestGhostSchemaUpdate(t *testing.T) {
Engine: v1pb.Engine_MYSQL,
Environment: new("environments/prod"),
Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: mysqlContainer.host, Port: mysqlContainer.port, Username: "bytebase", Password: "bytebase", Id: "admin"}},
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: mysqlContainer.GetHost(), Port: mysqlContainer.GetPort(), Username: "bytebase", Password: "bytebase", Id: "admin"}},
},
}))
a.NoError(err)
diff --git a/backend/tests/gitops_test.go b/backend/tests/gitops_test.go
index 03e09b7f49..1c05b866aa 100644
--- a/backend/tests/gitops_test.go
+++ b/backend/tests/gitops_test.go
@@ -25,10 +25,7 @@ func TestGitOpsCheck(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create a project for GitOps testing.
projectID := generateRandomString("gitops-check")
@@ -44,11 +41,9 @@ func TestGitOpsCheck(t *testing.T) {
project := projectResp.Msg
// Provision test and prod instances.
- testPgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ testPgContainer := provisionPgInstance(t)
- prodPgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ prodPgContainer := provisionPgInstance(t)
// Add the provisioned instances.
testInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
@@ -78,7 +73,7 @@ func TestGitOpsCheck(t *testing.T) {
prodInstance := prodInstanceResp.Msg
// Create databases.
- databaseName := "gitops_check_db"
+ databaseName := uniqueDB("gitops_check_db")
err = ctl.createDatabase(ctx, project, testInstance, nil, databaseName, "")
a.NoError(err)
err = ctl.createDatabase(ctx, project, prodInstance, nil, databaseName, "")
@@ -157,13 +152,9 @@ func TestGitOpsCheckReleaseRiskLevel(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- database, pgContainer := ctl.createTestPostgreSQLDatabase(ctx, t)
- defer pgContainer.Close(ctx)
+ database := ctl.createTestPostgreSQLDatabase(ctx, t)
setupSheet, err := ctl.sheetServiceClient.CreateSheet(ctx, connect.NewRequest(&v1pb.CreateSheetRequest{
Parent: ctl.project.Name,
@@ -200,10 +191,7 @@ func TestGitOpsCheckReleaseVCSUserTracking(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
project := createGitOpsVCSUserTestProject(ctx, t, ctl)
stores := getStore(t, ctl.server)
@@ -301,10 +289,7 @@ func TestGitOpsCheckReleaseVCSUserValidation(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
project := createGitOpsVCSUserTestProject(ctx, t, ctl)
stores := getStore(t, ctl.server)
@@ -421,15 +406,12 @@ func TestGitOpsCheckReleaseVCSUserMetadataTruncation(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
project := createGitOpsVCSUserTestProject(ctx, t, ctl)
target := createGitOpsVCSUserTestTarget(ctx, t, ctl, project)
- _, err = ctl.releaseServiceClient.CheckRelease(ctx, connect.NewRequest(&v1pb.CheckReleaseRequest{
+ _, err := ctl.releaseServiceClient.CheckRelease(ctx, connect.NewRequest(&v1pb.CheckReleaseRequest{
Parent: project.Name,
Release: gitOpsVCSUserTestRelease(),
Targets: []string{target},
@@ -457,10 +439,7 @@ func TestGitOpsCheckReleaseVCSUserEmptyDatabaseGroup(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
project := createGitOpsVCSUserTestProject(ctx, t, ctl)
databaseGroup, err := ctl.databaseGroupServiceClient.CreateDatabaseGroup(ctx, connect.NewRequest(&v1pb.CreateDatabaseGroupRequest{
@@ -498,10 +477,7 @@ func TestVCSProviderUserActuatorAndExport(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
stores := getStore(t, ctl.server)
workspaceID, err := stores.GetWorkspaceID(ctx)
@@ -550,10 +526,7 @@ func TestVCSProviderUserExportEscapesSpreadsheetFormulas(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
stores := getStore(t, ctl.server)
workspaceID, err := stores.GetWorkspaceID(ctx)
@@ -622,10 +595,7 @@ func TestGitOpsRollout(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create a project for GitOps testing.
projectID := generateRandomString("gitops-rollout")
@@ -641,25 +611,25 @@ func TestGitOpsRollout(t *testing.T) {
project := projectResp.Msg
// Provision test instance.
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
// Add the provisioned instance.
testInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "gitops-rollout-test",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/test"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "gitops-rollout-test",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/test"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
testInstance := testInstanceResp.Msg
// Create database.
- databaseName := "gitops_rollout_db"
+ databaseName := uniqueDB("gitops_rollout_db")
err = ctl.createDatabase(ctx, project, testInstance, nil, databaseName, "")
a.NoError(err)
@@ -805,20 +775,13 @@ func TestGitOpsRolloutGhostDirective(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- mysqlContainer, err := getMySQLContainer(ctx)
- a.NoError(err)
- defer func() {
- mysqlContainer.Close(ctx)
- }()
+ mysqlContainer := provisionMySQLInstance(t)
- const databaseName = "gitops_rollout_ghost_db"
- mysqlDB := mysqlContainer.db
- _, err = mysqlDB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %v", databaseName))
+ databaseName := uniqueDB("gitops_rollout_ghost_db")
+ mysqlDB := mysqlContainer.GetDB()
+ _, err := mysqlDB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %v", databaseName))
a.NoError(err)
_, err = mysqlDB.Exec("DROP USER IF EXISTS bytebase")
a.NoError(err)
@@ -846,7 +809,7 @@ func TestGitOpsRolloutGhostDirective(t *testing.T) {
Engine: v1pb.Engine_MYSQL,
Environment: new("environments/test"),
Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: mysqlContainer.host, Port: mysqlContainer.port, Username: "bytebase", Password: "bytebase", Id: "admin"}},
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: mysqlContainer.GetHost(), Port: mysqlContainer.GetPort(), Username: "bytebase", Password: "bytebase", Id: "admin"}},
},
}))
a.NoError(err)
@@ -947,10 +910,7 @@ func TestGitOpsRolloutMultiTarget(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create a project for GitOps testing.
projectID := generateRandomString("gitops-multi")
@@ -966,11 +926,9 @@ func TestGitOpsRolloutMultiTarget(t *testing.T) {
project := projectResp.Msg
// Provision test and prod instances.
- testPgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ testPgContainer := provisionPgInstance(t)
- prodPgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ prodPgContainer := provisionPgInstance(t)
// Add the provisioned instances.
testInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
@@ -1000,7 +958,7 @@ func TestGitOpsRolloutMultiTarget(t *testing.T) {
prodInstance := prodInstanceResp.Msg
// Create databases.
- databaseName := "gitops_multi_db"
+ databaseName := uniqueDB("gitops_multi_db")
err = ctl.createDatabase(ctx, project, testInstance, nil, databaseName, "")
a.NoError(err)
err = ctl.createDatabase(ctx, project, prodInstance, nil, databaseName, "")
@@ -1193,10 +1151,7 @@ func TestGitOpsCheckAppliedButChanged(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create a project for GitOps testing.
projectID := generateRandomString("gitops-changed")
@@ -1212,25 +1167,25 @@ func TestGitOpsCheckAppliedButChanged(t *testing.T) {
project := projectResp.Msg
// Provision test instance.
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
// Add the provisioned instance.
testInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "gitops-changed-test",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/test"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "gitops-changed-test",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/test"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
testInstance := testInstanceResp.Msg
// Create database.
- databaseName := "gitops_changed_db"
+ databaseName := uniqueDB("gitops_changed_db")
err = ctl.createDatabase(ctx, project, testInstance, nil, databaseName, "")
a.NoError(err)
@@ -1355,10 +1310,7 @@ func TestGitOpsCheckEmptyTargets(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create a project.
projectID := generateRandomString("gitops-empty")
@@ -1399,10 +1351,7 @@ func TestGitOpsCheckDeclarative(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create a project.
projectID := generateRandomString("gitops-decl")
@@ -1418,21 +1367,20 @@ func TestGitOpsCheckDeclarative(t *testing.T) {
project := projectResp.Msg
// Provision PostgreSQL instance.
- pgContainer, err := getPgContainer(ctx)
- a.NoError(err)
- defer pgContainer.Close(ctx)
+ pgContainer := sharedPgTarget(t)
pgInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "gitops-decl-pg",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/test"),
- Activation: true,
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "gitops-decl-pg",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/test"),
+ Activation: true,
DataSources: []*v1pb.DataSource{{
Type: v1pb.DataSourceType_ADMIN,
- Host: pgContainer.host,
- Port: pgContainer.port,
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
Username: "postgres",
Password: "root-password",
Id: "admin",
@@ -1443,7 +1391,7 @@ func TestGitOpsCheckDeclarative(t *testing.T) {
pgInstance := pgInstanceResp.Msg
// Create database.
- databaseName := "gitops_decl_db"
+ databaseName := uniqueDB("gitops_decl_db")
err = ctl.createDatabase(ctx, project, pgInstance, nil, databaseName, "postgres")
a.NoError(err)
@@ -1540,10 +1488,7 @@ ALTER TABLE public.users ADD COLUMN name VARCHAR(255);`,
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create a project.
projectID := generateRandomString("gitops-disallow")
@@ -1559,21 +1504,20 @@ ALTER TABLE public.users ADD COLUMN name VARCHAR(255);`,
project := projectResp.Msg
// Provision PostgreSQL instance.
- pgContainer, err := getPgContainer(ctx)
- a.NoError(err)
- defer pgContainer.Close(ctx)
+ pgContainer := sharedPgTarget(t)
pgInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "gitops-disallow-pg",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/test"),
- Activation: true,
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "gitops-disallow-pg",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/test"),
+ Activation: true,
DataSources: []*v1pb.DataSource{{
Type: v1pb.DataSourceType_ADMIN,
- Host: pgContainer.host,
- Port: pgContainer.port,
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
Username: "postgres",
Password: "root-password",
Id: "admin",
@@ -1584,7 +1528,7 @@ ALTER TABLE public.users ADD COLUMN name VARCHAR(255);`,
pgInstance := pgInstanceResp.Msg
// Create database.
- databaseName := "gitops_disallow_db"
+ databaseName := uniqueDB("gitops_disallow_db")
err = ctl.createDatabase(ctx, project, pgInstance, nil, databaseName, "postgres")
a.NoError(err)
@@ -1688,10 +1632,7 @@ func TestGitOpsCheckVersionedDependency(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Create a project.
projectID := generateRandomString("gitops-dep")
@@ -1741,21 +1682,20 @@ func TestGitOpsCheckVersionedDependency(t *testing.T) {
a.NoError(err)
// Provision PostgreSQL instance.
- pgContainer, err := getPgContainer(ctx)
- a.NoError(err)
- defer pgContainer.Close(ctx)
+ pgContainer := sharedPgTarget(t)
pgInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "gitops-dep-pg",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/test"),
- Activation: true,
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "gitops-dep-pg",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/test"),
+ Activation: true,
DataSources: []*v1pb.DataSource{{
Type: v1pb.DataSourceType_ADMIN,
- Host: pgContainer.host,
- Port: pgContainer.port,
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
Username: "postgres",
Password: "root-password",
Id: "admin",
@@ -1766,7 +1706,7 @@ func TestGitOpsCheckVersionedDependency(t *testing.T) {
pgInstance := pgInstanceResp.Msg
// Create database.
- databaseName := "gitops_dep_db"
+ databaseName := uniqueDB("gitops_dep_db")
err = ctl.createDatabase(ctx, project, pgInstance, nil, databaseName, "postgres")
a.NoError(err)
@@ -1820,10 +1760,7 @@ func TestGitOpsCheckDeclarativeMultipleFiles(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Create a project.
projectID := generateRandomString("gitops-multi-decl")
@@ -1839,21 +1776,20 @@ func TestGitOpsCheckDeclarativeMultipleFiles(t *testing.T) {
project := projectResp.Msg
// Provision PostgreSQL instance.
- pgContainer, err := getPgContainer(ctx)
- a.NoError(err)
- defer pgContainer.Close(ctx)
+ pgContainer := sharedPgTarget(t)
pgInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "gitops-multi-decl-pg",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/test"),
- Activation: true,
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "gitops-multi-decl-pg",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/test"),
+ Activation: true,
DataSources: []*v1pb.DataSource{{
Type: v1pb.DataSourceType_ADMIN,
- Host: pgContainer.host,
- Port: pgContainer.port,
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
Username: "postgres",
Password: "root-password",
Id: "admin",
@@ -1864,7 +1800,7 @@ func TestGitOpsCheckDeclarativeMultipleFiles(t *testing.T) {
pgInstance := pgInstanceResp.Msg
// Create database.
- databaseName := "gitops_multi_decl_db"
+ databaseName := uniqueDB("gitops_multi_decl_db")
err = ctl.createDatabase(ctx, project, pgInstance, nil, databaseName, "postgres")
a.NoError(err)
diff --git a/backend/tests/group_allow_missing_test.go b/backend/tests/group_allow_missing_test.go
index 6b2857d34e..b3f0f20866 100644
--- a/backend/tests/group_allow_missing_test.go
+++ b/backend/tests/group_allow_missing_test.go
@@ -19,10 +19,7 @@ func TestUpdateGroupAllowMissingRequiresCreatePermission(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
adminToken := ctl.authInterceptor.token
const password = "1024bytebase"
@@ -53,7 +50,7 @@ func TestUpdateGroupAllowMissingRequiresCreatePermission(t *testing.T) {
// Fixture check: if this ever passes, the role gained bb.groups.create and the
// denial below would prove nothing.
ctl.authInterceptor.token = dbaToken
- _, err = ctl.groupServiceClient.CreateGroup(ctx, connect.NewRequest(&v1pb.CreateGroupRequest{
+ _, err := ctl.groupServiceClient.CreateGroup(ctx, connect.NewRequest(&v1pb.CreateGroupRequest{
Group: &v1pb.Group{Title: "Front door"},
GroupEmail: fmt.Sprintf("front-%[email protected]", generateRandomString("g")),
}))
diff --git a/backend/tests/grouping_change_test.go b/backend/tests/grouping_change_test.go
index 9490c881a0..1b534c6f64 100644
--- a/backend/tests/grouping_change_test.go
+++ b/backend/tests/grouping_change_test.go
@@ -35,34 +35,34 @@ type createDatabaseGroupCase struct {
// the very end of the package.
func testCreateDatabaseGroup(t *testing.T, tc createDatabaseGroupCase) {
a := require.New(t)
- ctl := &controller{}
ctx := context.Background()
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer func() {
- _ = ctl.Close(ctx)
- }()
+ ctl, ctx := startProject(ctx, t)
instanceResourceID2InstanceTitle := make(map[string]string)
+ physicalNames := make(map[string][]string)
for _, prepareInstance := range tc.prepareInstances {
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResourceID := generateRandomString("instance")
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: instanceResourceID,
Instance: &v1pb.Instance{
- Title: prepareInstance.instanceTitle,
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
- Activation: true,
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: prepareInstance.instanceTitle,
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ Activation: true,
},
}))
a.NoError(err)
instance := instanceResp.Msg
instanceResourceID2InstanceTitle[instanceResourceID] = instance.Title
for preCreateDatabase := range prepareInstance.matchedDatabasesName {
- err = ctl.createDatabase(ctx, ctl.project, instance, nil, preCreateDatabase, "")
+ // The cases share one Postgres, so the physical name carries a
+ // suffix. The group expressions match on a prefix, which survives it.
+ physical := uniqueDB(preCreateDatabase)
+ physicalNames[prepareInstance.instanceTitle] = append(physicalNames[prepareInstance.instanceTitle], physical)
+ err = ctl.createDatabase(ctx, ctl.project, instance, nil, physical, "")
a.NoError(err)
}
}
@@ -99,7 +99,7 @@ func testCreateDatabaseGroup(t *testing.T, tc createDatabaseGroupCase) {
for _, prepareInstance := range tc.prepareInstances {
gotMatchedDatabases := gotInstanceTitleToMatchedDatabases[prepareInstance.instanceTitle]
a.Equal(len(gotMatchedDatabases), len(prepareInstance.matchedDatabasesName))
- for wantMatchedDatabase := range prepareInstance.matchedDatabasesName {
+ for _, wantMatchedDatabase := range physicalNames[prepareInstance.instanceTitle] {
a.Contains(gotMatchedDatabases, wantMatchedDatabase)
}
}
diff --git a/backend/tests/instance_create_project_test.go b/backend/tests/instance_create_project_test.go
index 9f91871047..3afc739ba8 100644
--- a/backend/tests/instance_create_project_test.go
+++ b/backend/tests/instance_create_project_test.go
@@ -16,24 +16,21 @@ func TestCreateInstanceWithoutProjectKeepsDefaultProject(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
- databaseName := "default_project_assignment"
+ pgContainer := sharedPgTarget(t)
+ databaseName := uniqueDB("default_project_assignment")
createPgDatabase(t, pgContainer, databaseName)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "default-project-sync",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{Databases: []string{databaseName}},
+ Title: "default-project-sync",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -51,14 +48,10 @@ func TestCreateInstanceWithEmptySyncDatabasesSkipsInitialDatabaseSync(t *testing
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
- databaseName := "unsynced_database"
+ pgContainer := sharedPgTarget(t)
+ databaseName := uniqueDB("unsynced_database")
createPgDatabase(t, pgContainer, databaseName)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
@@ -89,14 +82,10 @@ func TestListInstanceDatabaseBeforeCreate(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
- databaseName := "preview_database"
+ pgContainer := sharedPgTarget(t)
+ databaseName := uniqueDB("preview_database")
createPgDatabase(t, pgContainer, databaseName)
resp, err := ctl.instanceServiceClient.ListInstanceDatabase(ctx, connect.NewRequest(&v1pb.ListInstanceDatabaseRequest{
@@ -113,9 +102,12 @@ func TestListInstanceDatabaseBeforeCreate(t *testing.T) {
a.Contains(resp.Msg.Databases, databaseName)
}
+// createPgDatabase creates a database behind Bytebase's back, for the tests
+// about what a sync discovers. On the shared target the instance that should
+// discover it must name it in sync_databases.
func createPgDatabase(t *testing.T, pgContainer *Container, databaseName string) {
t.Helper()
- _, err := pgContainer.db.Exec(fmt.Sprintf("CREATE DATABASE %s", databaseName))
+ _, err := pgContainer.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", databaseName))
require.NoError(t, err)
}
diff --git a/backend/tests/instance_keytab_retention_test.go b/backend/tests/instance_keytab_retention_test.go
index 0a3696fa80..bdde4dd9dd 100644
--- a/backend/tests/instance_keytab_retention_test.go
+++ b/backend/tests/instance_keytab_retention_test.go
@@ -72,15 +72,8 @@ func createKeytabInstance(ctx context.Context, t *testing.T, ctl *controller, in
// the caller's host.
func TestKeytabIsNotInheritedByANewDestination(t *testing.T) {
t.Parallel()
- a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- // Not defer: the parallel subtests below outlive this function body, and a
- // Cleanup runs only once they are all done.
- t.Cleanup(func() { ctl.Close(ctx) })
+ ctl, ctx := startProject(ctx, t)
t.Run("UpdateDataSource", func(t *testing.T) {
t.Parallel()
diff --git a/backend/tests/login_attempt_collision_test.go b/backend/tests/login_attempt_collision_test.go
index 1fd7d3561d..1fbd88c040 100644
--- a/backend/tests/login_attempt_collision_test.go
+++ b/backend/tests/login_attempt_collision_test.go
@@ -2,7 +2,6 @@ package tests
import (
"context"
- "fmt"
"testing"
"time"
@@ -10,8 +9,6 @@ import (
"github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/migrator"
- "github.com/bytebase/bytebase/backend/store"
)
// TestCollision_LoginAttempt is the composite-PK collision gate for
@@ -24,14 +21,7 @@ import (
func TestCollision_LoginAttempt(t *testing.T) {
t.Parallel()
ctx := context.Background()
- container := testcontainer.GetTestPgContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
- require.NoError(t, migrator.MigrateSchema(ctx, container.GetDB()))
-
- pgURL := fmt.Sprintf("host=%s port=%s user=postgres password=root-password database=postgres", container.GetHost(), container.GetPort())
- s, err := store.New(ctx, pgURL, false)
- require.NoError(t, err)
- t.Cleanup(func() { require.NoError(t, s.Close()) })
+ metadataDB, s, _ := testcontainer.NewMetadataDB(t)
const window = 10 * time.Minute
const victim = "[email protected]"
@@ -59,7 +49,7 @@ func TestCollision_LoginAttempt(t *testing.T) {
attempts := func(identity string, kind storepb.LoginAttemptKind) int {
t.Helper()
var count int
- require.NoError(t, container.GetDB().QueryRowContext(ctx, `
+ require.NoError(t, metadataDB.QueryRowContext(ctx, `
SELECT COALESCE((SELECT attempts FROM login_attempt WHERE identity = $1 AND kind = $2), 0)
`, identity, kind.String()).Scan(&count))
return count
@@ -91,7 +81,7 @@ func TestCollision_LoginAttempt(t *testing.T) {
// The time-scoped purge must not use the lock as a shortcut: backdate only
// the neighbor and confirm the purge takes it and nothing else.
- _, err = container.GetDB().ExecContext(ctx, `
+ _, err = metadataDB.ExecContext(ctx, `
UPDATE login_attempt SET last_attempt_at = last_attempt_at - make_interval(secs => $3)
WHERE identity = $1 AND kind = $2
`, neighbor, storepb.LoginAttemptKind_PASSWORD.String(), (2 * time.Hour).Seconds())
diff --git a/backend/tests/login_audit_test.go b/backend/tests/login_audit_test.go
index c6e03a851c..e24859aa5b 100644
--- a/backend/tests/login_audit_test.go
+++ b/backend/tests/login_audit_test.go
@@ -27,10 +27,7 @@ func TestLoginFailureLockout(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspaceResp, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
@@ -313,10 +310,7 @@ func TestAuditLogFormat(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// --- Part 1: Login (workspace-scoped, allow_without_credential) ---
//
@@ -1030,10 +1024,7 @@ func TestLoginAuditsTheRequestedWorkspace(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspaceResp, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{Name: "workspaces/-"}))
a.NoError(err)
@@ -1093,10 +1084,7 @@ func TestLoginEnforcesWorkspaceDomains(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// The blocked admin exists before the domains are enforced, as an account
// created under a looser policy would.
diff --git a/backend/tests/main_test.go b/backend/tests/main_test.go
index 9e6a52772a..49f63b7640 100644
--- a/backend/tests/main_test.go
+++ b/backend/tests/main_test.go
@@ -4,13 +4,13 @@ import (
"context"
"testing"
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
webhookplugin "github.com/bytebase/bytebase/backend/plugin/webhook"
)
func TestMain(m *testing.M) {
- ctx := context.Background()
- code, err := startMain(ctx, m)
+ code, err := startMain(m)
if err != nil {
panic(err)
}
@@ -20,21 +20,22 @@ func TestMain(m *testing.M) {
}
}
-func startMain(ctx context.Context, m *testing.M) (int, error) {
- pgContainer, err := getPgContainer(ctx)
+func startMain(m *testing.M) (int, error) {
+ // The package's own Postgres: every server the tests start takes a metadata
+ // database on it, as does every test that builds a Store without a server.
+ pgContainer, err := testcontainer.StartSharedPg()
if err != nil {
return 0, err
}
+ defer testcontainer.CloseShared()
+ // Runs before CloseShared takes the Postgres out from under it.
defer func() {
- if pgContainer != nil {
- pgContainer.Close(ctx)
- }
- if sampleTargetContainer != nil {
- sampleTargetContainer.Close(ctx)
+ if sharedServerCtl != nil {
+ _ = sharedServerCtl.Close(context.Background())
}
}()
- externalPgHost = pgContainer.host
- externalPgPort = pgContainer.port
+ externalPgHost = pgContainer.GetHost()
+ externalPgPort = pgContainer.GetPort()
// Seeded once here, before any test starts, and never mutated afterwards:
// ValidateWebhookURL reads this map without synchronization, and the tests
diff --git a/backend/tests/maximum_request_expiration_test.go b/backend/tests/maximum_request_expiration_test.go
index 22ebaf6186..94b9705d24 100644
--- a/backend/tests/maximum_request_expiration_test.go
+++ b/backend/tests/maximum_request_expiration_test.go
@@ -66,11 +66,7 @@ func TestRoleGrantMaximumExpiration(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Cap request expiration to 7 days.
a.NoError(ctl.setMaximumRequestExpiration(ctx, durationpb.New(7*24*time.Hour)))
@@ -93,7 +89,7 @@ func TestRoleGrantMaximumExpiration(t *testing.T) {
// Request expiration does not cap role grants.
a.NoError(ctl.setMaximumRequestExpiration(ctx, durationpb.New(7*24*time.Hour)))
overCap := fmt.Sprintf(`request.time < timestamp("%s")`, time.Now().Add(30*24*time.Hour).Format(time.RFC3339))
- _, err = ctl.issueServiceClient.CreateIssue(ctx, newRoleGrantIssue(overCap))
+ _, err := ctl.issueServiceClient.CreateIssue(ctx, newRoleGrantIssue(overCap))
a.NoError(err, "role grant expiration should ignore the request expiration cap")
// Cap role expiration to 7 days.
@@ -128,11 +124,7 @@ func TestSetIamPolicyMaximumRoleExpiration(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
newProjectDeveloperBinding := func(days int) *v1pb.Binding {
return &v1pb.Binding{
@@ -182,23 +174,19 @@ func TestAccessGrantMaximumExpiration(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Create an instance and database to target.
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("inst"),
Instance: &v1pb.Instance{
- Title: "Test Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "Test Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
diff --git a/backend/tests/mcp_audit_provenance_test.go b/backend/tests/mcp_audit_provenance_test.go
index 086e701104..0ac69e64de 100644
--- a/backend/tests/mcp_audit_provenance_test.go
+++ b/backend/tests/mcp_audit_provenance_test.go
@@ -27,10 +27,7 @@ func TestMCPAuditProvenance(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// A plain workspace member drives the MCP session, so an IAM-gated admin
// action is genuinely denied by the ACL interceptor, not the handler.
diff --git a/backend/tests/mcp_capability_setting_test.go b/backend/tests/mcp_capability_setting_test.go
index 523872d9a5..b03acfc3e5 100644
--- a/backend/tests/mcp_capability_setting_test.go
+++ b/backend/tests/mcp_capability_setting_test.go
@@ -75,11 +75,7 @@ func TestMCPCapabilitySettingRoundTrip(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// A fresh workspace starts with the safe, immediately useful ceiling.
capability, err := ctl.getMCPCapability(ctx)
@@ -129,10 +125,7 @@ func TestMCPSettingExistsWithTheWorkspace(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
got, err := ctl.settingServiceClient.GetSetting(ctx, connect.NewRequest(&v1pb.GetSettingRequest{
Name: "settings/" + v1pb.Setting_MCP.String(),
@@ -177,10 +170,7 @@ func TestMCPUnrecognizedCeilingSurvivesAToggleOnlyUpdate(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspaceID := currentWorkspaceID(ctx, t, ctl)
db, err := sql.Open("pgx", ctl.profile.PgURL)
@@ -222,10 +212,7 @@ func TestMCPMissingRowUsesGenericUpdateSemantics(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspaceID := currentWorkspaceID(ctx, t, ctl)
db, err := sql.Open("pgx", ctl.profile.PgURL)
@@ -268,10 +255,7 @@ func TestMCPRepairKeepsTheMaskingToggle(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspaceID := currentWorkspaceID(ctx, t, ctl)
db, err := sql.Open("pgx", ctl.profile.PgURL)
@@ -306,10 +290,7 @@ func TestMCPMissingCapabilityRefusesPartialUpdate(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspaceID := currentWorkspaceID(ctx, t, ctl)
db, err := sql.Open("pgx", ctl.profile.PgURL)
diff --git a/backend/tests/mcp_denial_audit_test.go b/backend/tests/mcp_denial_audit_test.go
index fb1ef6d219..5512885663 100644
--- a/backend/tests/mcp_denial_audit_test.go
+++ b/backend/tests/mcp_denial_audit_test.go
@@ -25,10 +25,7 @@ func TestMCPPolicyDenialsReachTheAuditPage(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
diff --git a/backend/tests/mcp_forbidden_class_test.go b/backend/tests/mcp_forbidden_class_test.go
index 2f912192e7..f273d2fe70 100644
--- a/backend/tests/mcp_forbidden_class_test.go
+++ b/backend/tests/mcp_forbidden_class_test.go
@@ -32,11 +32,7 @@ func TestMCPCannotChangeOwnPasswordAndLogIn(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// A dedicated end user drives the session: the escape is about an MCP
// session rewriting its OWN credentials, and using the fixture's admin
@@ -162,11 +158,7 @@ func TestWebUserStillChangesPasswordAndLogsIn(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
const webEmail = "[email protected]"
const oldPassword = "1024bytebase"
diff --git a/backend/tests/mcp_forbidden_credential_mints_test.go b/backend/tests/mcp_forbidden_credential_mints_test.go
index 3008b2e5a7..8589856ed6 100644
--- a/backend/tests/mcp_forbidden_credential_mints_test.go
+++ b/backend/tests/mcp_forbidden_credential_mints_test.go
@@ -94,11 +94,7 @@ func TestMCPCannotMintCredentialsForOtherPrincipals(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
@@ -280,11 +276,7 @@ func TestMCPCannotWriteTrustAnchorsOrShipStoredSecrets(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
@@ -481,11 +473,7 @@ func TestMCPCannotRetargetADataSource(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
const storedDBPassword = "stored-db-secret"
const realHost = "db.internal.example.com"
@@ -603,11 +591,7 @@ func TestMCPCannotRewriteItsOwnCeiling(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
@@ -774,11 +758,7 @@ func TestMCPResetFlowDenialsAreAudited(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
@@ -853,11 +833,7 @@ func TestMCPCredentialMintsLeaveDiscovery(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
diff --git a/backend/tests/mcp_gate_test.go b/backend/tests/mcp_gate_test.go
index a029db6055..246d130601 100644
--- a/backend/tests/mcp_gate_test.go
+++ b/backend/tests/mcp_gate_test.go
@@ -27,11 +27,7 @@ func TestMCPGateServesAndRefusesByClass(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
@@ -92,11 +88,7 @@ func TestMCPGateRefusesGrantIssues(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
project, err := ctl.projectServiceClient.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{
ProjectId: "mcp-grant-issue",
@@ -199,27 +191,23 @@ func TestMCPCannotRunAnIssuelessRollout(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// CreateRollout is WRITE and the refusal under test is the
// issueless-rollout guard. Pinned so this test does not depend on the
// resolved default.
a.NoError(ctl.setMCPCapability(ctx, v1pb.MCPSetting_READ_WRITE))
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("inst"),
Instance: &v1pb.Instance{
- Title: "MCP rollout instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "MCP rollout instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -290,11 +278,7 @@ func TestMCPDefaultRowIsReadOnly(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
project, err := ctl.projectServiceClient.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{
ProjectId: "mcp-unset",
diff --git a/backend/tests/mcp_masking_test.go b/backend/tests/mcp_masking_test.go
index 9079a93e8f..a8a84bc378 100644
--- a/backend/tests/mcp_masking_test.go
+++ b/backend/tests/mcp_masking_test.go
@@ -114,10 +114,7 @@ type mcpMaskingFixture struct {
func setupMCPMaskingFixture(ctx context.Context, t *testing.T) *mcpMaskingFixture {
t.Helper()
a := require.New(t)
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- t.Cleanup(func() { ctl.Close(ctx) })
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
@@ -153,16 +150,16 @@ func setupMCPMaskingFixture(ctx context.Context, t *testing.T) *mcpMaskingFixtur
}))
a.NoError(err)
- container, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ container := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("mcp-masking"),
Instance: &v1pb.Instance{
- Title: "MCP masking",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{container.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "MCP masking",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{container.adminDataSource()},
},
}))
a.NoError(err)
@@ -900,10 +897,7 @@ func TestMCPSessionCannotFlipTheMaskingToggle(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
diff --git a/backend/tests/mcp_migration_matrix_test.go b/backend/tests/mcp_migration_matrix_test.go
index a48f4e33e8..8453f119e8 100644
--- a/backend/tests/mcp_migration_matrix_test.go
+++ b/backend/tests/mcp_migration_matrix_test.go
@@ -168,10 +168,7 @@ func TestMCPMigrationGrantStateMatrix(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// The probe is an audited mutation, so each session leaves exactly one
// provenance-carrying row to sort out below, and it is a WRITE method
@@ -283,10 +280,7 @@ func TestMCPMigrationCeilingLookupFailureFailsClosed(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
@@ -398,10 +392,7 @@ func TestMCPMigrationTightenedCeilingBitesLiveSession(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
mcpToken, _ := mintMCPOAuthToken(t, ctl, ctl.authInterceptor.token)
session := openMCPSession(ctx, t, ctl, mcpToken)
@@ -417,7 +408,7 @@ func TestMCPMigrationTightenedCeilingBitesLiveSession(t *testing.T) {
"the same bearer must be refused on its next request after the ceiling tightens; %s", body)
a.Contains(body, "turned MCP access off")
- _, err = session.CallTool(ctx, &mcp.CallToolParams{
+ _, err := session.CallTool(ctx, &mcp.CallToolParams{
Name: "call_api",
Arguments: map[string]any{"operationId": "WorkspaceService/ListWorkspaces"},
})
@@ -446,10 +437,7 @@ func TestMCPMembershipRevocationBitesLiveSession(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
const memberEmail = "[email protected]"
const memberPassword = "1024bytebase"
diff --git a/backend/tests/mcp_sql_clamp_test.go b/backend/tests/mcp_sql_clamp_test.go
index 86b74d3ae4..e65932a763 100644
--- a/backend/tests/mcp_sql_clamp_test.go
+++ b/backend/tests/mcp_sql_clamp_test.go
@@ -79,24 +79,22 @@ type mcpClampFixture struct {
token string
}
-func setupMCPClampFixture(ctx context.Context, t *testing.T) *mcpClampFixture {
+// setupMCPClampFixture builds the fixture on the caller's controller, so each
+// test decides whether a project of its own is enough or it needs a workspace.
+func setupMCPClampFixture(ctx context.Context, t *testing.T, ctl *controller) *mcpClampFixture {
t.Helper()
a := require.New(t)
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- t.Cleanup(func() { ctl.Close(ctx) })
- container, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ container := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("mcp-clamp"),
Instance: &v1pb.Instance{
- Title: "MCP clamp",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{container.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "MCP clamp",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{container.adminDataSource()},
},
}))
a.NoError(err)
@@ -157,7 +155,8 @@ func (f *mcpClampFixture) employeeCount(t *testing.T) int {
func TestMCPReadOnlyCeilingRefusesAWrite(t *testing.T) {
t.Parallel()
a := require.New(t)
- f := setupMCPClampFixture(context.Background(), t)
+ ctl, ctx := startWorkspace(context.Background(), t)
+ f := setupMCPClampFixture(ctx, t, ctl)
// A read is served, and the response says what held it to reads. Postgres
// gets the driver session too, so the strongest depth is what it reports.
@@ -266,7 +265,8 @@ func (f *mcpClampFixture) sequenceValue(t *testing.T) int64 {
func TestMCPReadOnlyCeilingRefusesASessionRewrite(t *testing.T) {
t.Parallel()
a := require.New(t)
- f := setupMCPClampFixture(context.Background(), t)
+ ctl, ctx := startWorkspace(context.Background(), t)
+ f := setupMCPClampFixture(ctx, t, ctl)
before := f.sequenceValue(t)
disarm := queryDatabaseOnSession(f.ctx, t, f.session, f.name,
@@ -291,7 +291,8 @@ func TestMCPReadOnlyCeilingRefusesASessionRewrite(t *testing.T) {
func TestMCPReadOnlyCeilingJudgesTheWholeBatch(t *testing.T) {
t.Parallel()
a := require.New(t)
- f := setupMCPClampFixture(context.Background(), t)
+ ctl, ctx := startWorkspace(context.Background(), t)
+ f := setupMCPClampFixture(ctx, t, ctl)
reads := queryDatabaseOnSession(f.ctx, t, f.session, f.name,
"SELECT id FROM employee; SELECT name FROM employee;")
@@ -314,7 +315,8 @@ func TestMCPReadOnlyCeilingJudgesTheWholeBatch(t *testing.T) {
func TestMCPReadOnlyCeilingLeavesTheHumanPathAlone(t *testing.T) {
t.Parallel()
a := require.New(t)
- f := setupMCPClampFixture(context.Background(), t)
+ ctl, ctx := startWorkspace(context.Background(), t)
+ f := setupMCPClampFixture(ctx, t, ctl)
// The agent's read runs first and is SERVED, which is what actually opens
// a read-only Postgres session; a refused statement never reaches a
@@ -356,7 +358,8 @@ func TestMCPReadOnlyCeilingLeavesTheHumanPathAlone(t *testing.T) {
func TestMCPReadOnlyTighteningBitesAnOpenSession(t *testing.T) {
t.Parallel()
a := require.New(t)
- f := setupMCPClampFixture(context.Background(), t)
+ ctl, ctx := startWorkspace(context.Background(), t)
+ f := setupMCPClampFixture(ctx, t, ctl)
// Start read-write, on a session opened under that ceiling.
a.NoError(f.ctl.setMCPCapability(f.ctx, v1pb.MCPSetting_READ_WRITE))
@@ -398,7 +401,8 @@ func TestMCPReadOnlyTighteningBitesAnOpenSession(t *testing.T) {
func TestMCPReadOnlyClampCoversAnExplainRequest(t *testing.T) {
t.Parallel()
a := require.New(t)
- f := setupMCPClampFixture(context.Background(), t)
+ ctl, ctx := startWorkspace(context.Background(), t)
+ f := setupMCPClampFixture(ctx, t, ctl)
explained := callAPIOnSession(f.ctx, t, f.session, "SQLService/Query", map[string]any{
"name": f.database,
@@ -426,10 +430,7 @@ func TestMCPCutoverAdmitsReadOnlyAndNothingElse(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
@@ -517,7 +518,8 @@ func TestMCPCutoverAdmitsReadOnlyAndNothingElse(t *testing.T) {
func TestMCPReadOnlyRoleDowngradeBitesTheNextRequest(t *testing.T) {
t.Parallel()
a := require.New(t)
- f := setupMCPClampFixture(context.Background(), t)
+ ctl, ctx := startWorkspace(context.Background(), t)
+ f := setupMCPClampFixture(ctx, t, ctl)
const readerEmail = "[email protected]"
const readerPassword = "1024bytebase"
diff --git a/backend/tests/mcp_token_boundary_test.go b/backend/tests/mcp_token_boundary_test.go
index 6496e48649..9b404bf47e 100644
--- a/backend/tests/mcp_token_boundary_test.go
+++ b/backend/tests/mcp_token_boundary_test.go
@@ -160,10 +160,7 @@ func TestMCPTokenIsRejectedOnGeneralAPI(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
mcpToken, _ := mintMCPOAuthToken(t, ctl, ctl.authInterceptor.token)
diff --git a/backend/tests/mcp_tool_parity_test.go b/backend/tests/mcp_tool_parity_test.go
index 3d6fcb48ff..5244897413 100644
--- a/backend/tests/mcp_tool_parity_test.go
+++ b/backend/tests/mcp_tool_parity_test.go
@@ -36,11 +36,7 @@ func TestMCPToolCallParity(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Parity baseline: the principal's direct v1 API view. The probe is
// ListWorkspaces, and any READ method whose result is scoped to the caller
@@ -113,11 +109,7 @@ func TestMCPCannotMintPlainUserToken(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
diff --git a/backend/tests/mcp_workspace_token_escape_test.go b/backend/tests/mcp_workspace_token_escape_test.go
index 69fd03dfe3..4f4c04cf15 100644
--- a/backend/tests/mcp_workspace_token_escape_test.go
+++ b/backend/tests/mcp_workspace_token_escape_test.go
@@ -124,11 +124,7 @@ func TestMCPCannotLeaveWorkspace(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
@@ -208,11 +204,7 @@ func TestMCPCannotDeleteWorkspace(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
@@ -258,11 +250,7 @@ func TestMCPOAuthSessionCannotLeaveOrDeleteWorkspace(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
-
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{
Name: "workspaces/-",
diff --git a/backend/tests/metrics_test.go b/backend/tests/metrics_test.go
index 7deeea8a27..efde879501 100644
--- a/backend/tests/metrics_test.go
+++ b/backend/tests/metrics_test.go
@@ -19,10 +19,7 @@ func TestCollisionMetricsLicenseSeats(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
actuator, err := ctl.actuatorServiceClient.GetActuatorInfo(ctx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))
a.NoError(err)
diff --git a/backend/tests/oauth2_grant_test.go b/backend/tests/oauth2_grant_test.go
index b4df7c2afe..b01a4a80bb 100644
--- a/backend/tests/oauth2_grant_test.go
+++ b/backend/tests/oauth2_grant_test.go
@@ -218,10 +218,7 @@ func TestOAuth2GrantLifecycle(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
db, err := sql.Open("pgx", ctl.profile.PgURL)
a.NoError(err)
@@ -420,10 +417,7 @@ func TestOAuth2IssuanceRechecksTheCeiling(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
db, err := sql.Open("pgx", ctl.profile.PgURL)
a.NoError(err)
diff --git a/backend/tests/pg_sdl_rollout_test.go b/backend/tests/pg_sdl_rollout_test.go
index 97198c6117..0a9d670718 100644
--- a/backend/tests/pg_sdl_rollout_test.go
+++ b/backend/tests/pg_sdl_rollout_test.go
@@ -31,28 +31,24 @@ func TestPgSDLRollout(t *testing.T) {
ctx := context.Background()
// Create a single shared PostgreSQL container for all subtests
- pgContainer, err := getPgContainer(ctx)
- a.NoError(err)
- defer pgContainer.Close(ctx)
+ pgContainer := sharedPgTarget(t)
- // Create a single shared Bytebase controller and instance for all subtests
- ctl := &controller{}
- ctx, err = ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ // One controller and instance for all the subtests.
+ ctl, ctx := startProject(ctx, t)
// Create shared instance in Bytebase
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
- InstanceId: generateRandomString("inst")[:8],
+ InstanceId: shortInstanceID(),
Instance: &v1pb.Instance{
- Title: "SDL Test Instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "SDL Test Instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
DataSources: []*v1pb.DataSource{{
Type: v1pb.DataSourceType_ADMIN,
- Host: pgContainer.host,
- Port: pgContainer.port,
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
Username: "postgres",
Password: "root-password",
Id: "admin",
@@ -69,7 +65,7 @@ func TestPgSDLRollout(t *testing.T) {
dbName := fmt.Sprintf("sdl_workflow_%s", strings.ReplaceAll(uuid.New().String()[:8], "-", ""))
// Create database directly in PostgreSQL
- _, err := pgContainer.db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
+ _, err := pgContainer.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
a.NoError(err)
// Create database in Bytebase
@@ -192,7 +188,7 @@ CREATE TABLE "public"."posts" (
dbName := fmt.Sprintf("sdl_empty_%s", strings.ReplaceAll(uuid.New().String()[:8], "-", ""))
// Create database directly in PostgreSQL
- _, err := pgContainer.db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
+ _, err := pgContainer.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
a.NoError(err)
// Create database in Bytebase
@@ -233,7 +229,7 @@ CREATE TABLE "public"."posts" (
dbName := fmt.Sprintf("sdl_nochange_%s", strings.ReplaceAll(uuid.New().String()[:8], "-", ""))
// Create database directly in PostgreSQL
- _, err := pgContainer.db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
+ _, err := pgContainer.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
a.NoError(err)
// Create database in Bytebase
diff --git a/backend/tests/plan_audit_collision_test.go b/backend/tests/plan_audit_collision_test.go
index 57475a2807..1f40e76b09 100644
--- a/backend/tests/plan_audit_collision_test.go
+++ b/backend/tests/plan_audit_collision_test.go
@@ -21,10 +21,7 @@ func TestCollision_PlanSpecAuditEmission(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
@@ -48,7 +45,7 @@ func TestCollision_PlanSpecAuditEmission(t *testing.T) {
cdc := originalSpec.GetChangeDatabaseConfig()
a.NotNil(cdc, "fresh plan A spec is expected to be a ChangeDatabaseConfig")
- _, err = ctl.planServiceClient.UpdatePlan(ctx, connect.NewRequest(&v1pb.UpdatePlanRequest{
+ _, err := ctl.planServiceClient.UpdatePlan(ctx, connect.NewRequest(&v1pb.UpdatePlanRequest{
Plan: &v1pb.Plan{
Name: planA2.Name,
Specs: []*v1pb.Plan_Spec{{
@@ -92,10 +89,7 @@ func TestCollision_PlanMetadataUpdate(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
beforeB := snapshotProject(ctx, t, ctl, fixture.ProjectB)
diff --git a/backend/tests/plan_draft_collision_test.go b/backend/tests/plan_draft_collision_test.go
index 860bb26f75..4a707cabac 100644
--- a/backend/tests/plan_draft_collision_test.go
+++ b/backend/tests/plan_draft_collision_test.go
@@ -18,10 +18,7 @@ func TestCollision_PlanDraftMetadataSync(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- require.NoError(t, err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
createDraft := func(project *v1pb.Project, database *v1pb.Database, title string) (*v1pb.Plan, *v1pb.Issue) {
@@ -61,11 +58,12 @@ func TestCollision_PlanDraftMetadataSync(t *testing.T) {
}
planA, issueA := createDraft(fixture.ProjectA, fixture.DatabaseA, "Draft A")
- _, issueB := createDraft(fixture.ProjectB, fixture.DatabaseB, "Draft B")
+ planB, issueB := createDraft(fixture.ProjectB, fixture.DatabaseB, "Draft B")
+ waitPlanCheckRunSettled(ctx, t, ctl, planB.Name)
beforeB := snapshotProject(ctx, t, ctl, fixture.ProjectB)
const updatedTitle = "Updated draft A"
- _, err = ctl.planServiceClient.UpdatePlan(ctx, connect.NewRequest(&v1pb.UpdatePlanRequest{
+ _, err := ctl.planServiceClient.UpdatePlan(ctx, connect.NewRequest(&v1pb.UpdatePlanRequest{
Plan: &v1pb.Plan{Name: planA.Name, Title: updatedTitle},
UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"title"}},
}))
diff --git a/backend/tests/plan_update_test.go b/backend/tests/plan_update_test.go
index 40e5219de0..f7b9f2ef05 100644
--- a/backend/tests/plan_update_test.go
+++ b/backend/tests/plan_update_test.go
@@ -34,14 +34,11 @@ func setupPlanUpdateFixture(t *testing.T, withIssue bool) *planUpdateFixture {
t.Helper()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- t.Cleanup(func() { _ = ctl.Close(ctx) })
+ ctl, ctx := startProject(ctx, t)
// The fixture creates a database through a bootstrap Plan owned and approved
// by the test user. Keep that setup flow independent of the feature under
// test; individual tests can set the project policy they need afterwards.
- _, err = ctl.projectServiceClient.UpdateProject(ctx, connect.NewRequest(&v1pb.UpdateProjectRequest{
+ _, err := ctl.projectServiceClient.UpdateProject(ctx, connect.NewRequest(&v1pb.UpdateProjectRequest{
Project: &v1pb.Project{
Name: ctl.project.Name,
AllowLastPlanEditorApproval: true,
@@ -51,16 +48,16 @@ func setupPlanUpdateFixture(t *testing.T, withIssue bool) *planUpdateFixture {
a.NoError(err)
instanceName := "planUpdateInstance_" + generateRandomString("inst")
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: instanceName,
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: instanceName,
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
diff --git a/backend/tests/project_instance_archive_task_run_test.go b/backend/tests/project_instance_archive_task_run_test.go
index 136540431d..53074ebb7f 100644
--- a/backend/tests/project_instance_archive_task_run_test.go
+++ b/backend/tests/project_instance_archive_task_run_test.go
@@ -17,13 +17,9 @@ func TestArchiveInstanceRejectsActiveTaskRuns(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pg, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pg := sharedPgTarget(t)
instance := createProjectInstanceTestInstance(
ctx,
t,
@@ -124,13 +120,9 @@ func TestForceArchiveInstanceRejectsActiveTaskRunsWithoutTransferringDatabases(t
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pg, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pg := sharedPgTarget(t)
instance := createProjectInstanceTestInstance(
ctx,
t,
diff --git a/backend/tests/project_instance_collision_extras_test.go b/backend/tests/project_instance_collision_extras_test.go
index 76babc260e..5e535aaae6 100644
--- a/backend/tests/project_instance_collision_extras_test.go
+++ b/backend/tests/project_instance_collision_extras_test.go
@@ -25,10 +25,7 @@ func TestCollision_PlanWebhookDeliveryWrite(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
@@ -148,10 +145,7 @@ func TestCollision_TaskRunLogWrite(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
fixture.completeRolloutB(ctx, t, ctl)
@@ -220,15 +214,12 @@ func TestCollision_DbGroupWrite(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
// Project B owns the baseline group with resource id "collide-group".
- _, err = ctl.databaseGroupServiceClient.CreateDatabaseGroup(ctx, connect.NewRequest(&v1pb.CreateDatabaseGroupRequest{
+ _, err := ctl.databaseGroupServiceClient.CreateDatabaseGroup(ctx, connect.NewRequest(&v1pb.CreateDatabaseGroupRequest{
Parent: fixture.ProjectB.Name,
DatabaseGroupId: "collide-group",
DatabaseGroup: &v1pb.DatabaseGroup{
@@ -286,10 +277,7 @@ func TestCollision_ReleaseWrite(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
diff --git a/backend/tests/project_instance_compat_extras_test.go b/backend/tests/project_instance_compat_extras_test.go
index 7272102089..8b95429daf 100644
--- a/backend/tests/project_instance_compat_extras_test.go
+++ b/backend/tests/project_instance_compat_extras_test.go
@@ -26,19 +26,15 @@ func TestProjectInstanceExport(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pg, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pg := sharedPgTarget(t)
const instanceID = "bot37-export-instance"
- const databaseID = "bot37_export_database"
+ databaseID := uniqueDB("bot37_export_database")
createPgDatabase(t, pg, databaseID)
- instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, instanceID, "export project instance", pg)
- _, err = ctl.instanceServiceClient.SyncInstance(ctx, connect.NewRequest(&v1pb.SyncInstanceRequest{Name: instance.Name}))
+ instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, instanceID, "export project instance", pg, databaseID)
+ _, err := ctl.instanceServiceClient.SyncInstance(ctx, connect.NewRequest(&v1pb.SyncInstanceRequest{Name: instance.Name}))
a.NoError(err)
databaseName := fmt.Sprintf("%s/databases/%s", instance.Name, databaseID)
database, err := ctl.databaseServiceClient.GetDatabase(ctx, connect.NewRequest(&v1pb.GetDatabaseRequest{Name: databaseName}))
@@ -107,19 +103,15 @@ func TestProjectInstanceSavedQuery(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pg, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pg := sharedPgTarget(t)
const instanceID = "bot37-saved-query-instance"
- const databaseID = "bot37_saved_query_database"
+ databaseID := uniqueDB("bot37_saved_query_database")
createPgDatabase(t, pg, databaseID)
- instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, instanceID, "saved query project instance", pg)
- _, err = ctl.instanceServiceClient.SyncInstance(ctx, connect.NewRequest(&v1pb.SyncInstanceRequest{Name: instance.Name}))
+ instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, instanceID, "saved query project instance", pg, databaseID)
+ _, err := ctl.instanceServiceClient.SyncInstance(ctx, connect.NewRequest(&v1pb.SyncInstanceRequest{Name: instance.Name}))
a.NoError(err)
databaseName := fmt.Sprintf("%s/databases/%s", instance.Name, databaseID)
_, err = ctl.databaseServiceClient.GetDatabase(ctx, connect.NewRequest(&v1pb.GetDatabaseRequest{Name: databaseName}))
@@ -195,19 +187,15 @@ func TestProjectInstanceAccessGrant(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pg, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pg := sharedPgTarget(t)
const instanceID = "bot37-grant-instance"
- const databaseID = "bot37_grant_database"
+ databaseID := uniqueDB("bot37_grant_database")
createPgDatabase(t, pg, databaseID)
- instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, instanceID, "grant project instance", pg)
- _, err = ctl.instanceServiceClient.SyncInstance(ctx, connect.NewRequest(&v1pb.SyncInstanceRequest{Name: instance.Name}))
+ instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, instanceID, "grant project instance", pg, databaseID)
+ _, err := ctl.instanceServiceClient.SyncInstance(ctx, connect.NewRequest(&v1pb.SyncInstanceRequest{Name: instance.Name}))
a.NoError(err)
databaseName := fmt.Sprintf("%s/databases/%s", instance.Name, databaseID)
_, err = ctl.databaseServiceClient.GetDatabase(ctx, connect.NewRequest(&v1pb.GetDatabaseRequest{Name: databaseName}))
@@ -273,13 +261,9 @@ func TestBatchSyncInstancesCompatibility(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pg, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pg := sharedPgTarget(t)
const (
workspaceInstanceID = "bot37-batch-workspace-instance"
projectInstanceID = "bot37-batch-project-instance"
@@ -288,14 +272,15 @@ func TestBatchSyncInstancesCompatibility(t *testing.T) {
projectDB = "bot37_batch_proj_db"
projectNewDB = "bot37_batch_proj_new_db"
)
+ databases := []string{workspaceDB, workspaceNewDB, projectDB, projectNewDB}
// Create both instances while the server has no physical databases yet:
// instance creation itself discovers databases, so the "zero side
// effects" probes below rely on databases created after both instances.
- workspaceInstance := createProjectInstanceTestInstance(ctx, t, ctl, nil, workspaceInstanceID, "batch workspace instance", pg)
+ workspaceInstance := createProjectInstanceTestInstance(ctx, t, ctl, nil, workspaceInstanceID, "batch workspace instance", pg, databases...)
a.Equal("instances/"+workspaceInstanceID, workspaceInstance.Name)
- projectInstance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, projectInstanceID, "batch project instance", pg)
+ projectInstance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, projectInstanceID, "batch project instance", pg, databases...)
a.Equal(fmt.Sprintf("%s/instances/%s", ctl.project.Name, projectInstanceID), projectInstance.Name)
- for _, database := range []string{workspaceDB, workspaceNewDB, projectDB, projectNewDB} {
+ for _, database := range databases {
createPgDatabase(t, pg, database)
}
@@ -322,7 +307,7 @@ func TestBatchSyncInstancesCompatibility(t *testing.T) {
// A project instance is not in the workspace collection: the mixed batch is
// rejected before any member is synced.
- err = batchSync(nil, workspaceInstance.Name, projectInstance.Name)
+ err := batchSync(nil, workspaceInstance.Name, projectInstance.Name)
a.Error(err)
a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err))
a.Contains(err.Error(), fmt.Sprintf("instance %q is not in its requested collection", projectInstance.Name))
diff --git a/backend/tests/project_instance_iam_collision_test.go b/backend/tests/project_instance_iam_collision_test.go
index a5564ae216..4431c3ff46 100644
--- a/backend/tests/project_instance_iam_collision_test.go
+++ b/backend/tests/project_instance_iam_collision_test.go
@@ -18,14 +18,10 @@ func TestProjectInstanceIAM(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
ownerToken := ctl.authInterceptor.token
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := provisionPgInstance(t)
otherProjectResp, err := ctl.projectServiceClient.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{
ProjectId: generateRandomString("instance-other-project"),
@@ -144,13 +140,9 @@ func TestProjectInstanceCollisionIsolation(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := provisionPgInstance(t)
projectBResp, err := ctl.projectServiceClient.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{
ProjectId: generateRandomString("instance-collision-project"),
Project: &v1pb.Project{Title: "Instance collision project", AllowSelfApproval: true},
diff --git a/backend/tests/project_instance_scheduler_test.go b/backend/tests/project_instance_scheduler_test.go
index 310fda400e..4ea0f0e833 100644
--- a/backend/tests/project_instance_scheduler_test.go
+++ b/backend/tests/project_instance_scheduler_test.go
@@ -19,18 +19,14 @@ func TestProjectInstancePlanCheckSchedulingFollowsProjectLifecycle(t *testing.T)
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pg, err := provisionPgInstance(ctx, t)
- a.NoError(err)
- const databaseID = "bot36_scheduler_database"
+ pg := sharedPgTarget(t)
+ databaseID := uniqueDB("bot36_scheduler_database")
createPgDatabase(t, pg, databaseID)
- instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, "bot36-scheduler-instance", "scheduler instance", pg)
- _, err = ctl.instanceServiceClient.SyncInstance(ctx, connect.NewRequest(&v1pb.SyncInstanceRequest{Name: instance.Name}))
+ instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, "bot36-scheduler-instance", "scheduler instance", pg, databaseID)
+ _, err := ctl.instanceServiceClient.SyncInstance(ctx, connect.NewRequest(&v1pb.SyncInstanceRequest{Name: instance.Name}))
a.NoError(err)
databaseName := fmt.Sprintf("%s/databases/%s", instance.Name, databaseID)
_, err = ctl.databaseServiceClient.SyncDatabase(ctx, connect.NewRequest(&v1pb.SyncDatabaseRequest{Name: databaseName}))
diff --git a/backend/tests/project_instance_test.go b/backend/tests/project_instance_test.go
index fcf4a8ad06..86d3fa785c 100644
--- a/backend/tests/project_instance_test.go
+++ b/backend/tests/project_instance_test.go
@@ -20,13 +20,9 @@ func TestProjectInstanceCoreBehavior(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
- pg, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pg := provisionPgInstance(t)
const databaseID = "bot35_project_database"
createPgDatabase(t, pg, databaseID)
@@ -296,7 +292,7 @@ func TestProjectInstanceCoreBehavior(t *testing.T) {
a.True(foundCanonicalAuditResource, "audit logs must retain the canonical instance name after purge")
physicalDatabaseCount := 0
- a.NoError(pg.db.QueryRowContext(ctx, "SELECT COUNT(*) FROM pg_database WHERE datname = $1", databaseID).Scan(&physicalDatabaseCount))
+ a.NoError(pg.GetDB().QueryRowContext(ctx, "SELECT COUNT(*) FROM pg_database WHERE datname = $1", databaseID).Scan(&physicalDatabaseCount))
a.Equal(1, physicalDatabaseCount, "Bytebase purge must not delete the physical database")
reused := createProjectInstanceTestInstance(ctx, t, ctl, &projectParent, projectID, "reused project instance", pg)
a.Equal(projectInstance.Name, reused.Name)
@@ -337,13 +333,9 @@ func TestBatchUpdateProjectInstanceAllowMissing(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
- pg, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pg := provisionPgInstance(t)
const instanceID = "bot35-batch-allow-missing"
parent := ctl.project.Name
@@ -371,10 +363,7 @@ func TestProjectInstanceValidation(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
for _, parent := range []string{"projects/missing-bot35", "projects/default"} {
parent := parent
@@ -395,7 +384,7 @@ func TestProjectInstanceValidation(t *testing.T) {
}
deletedProject := createProjectForProjectInstanceTest(ctx, t, ctl, "bot35-deleted-project")
- _, err = ctl.projectServiceClient.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: deletedProject.Name}))
+ _, err := ctl.projectServiceClient.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: deletedProject.Name}))
a.NoError(err)
_, err = ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
Parent: &deletedProject.Name,
@@ -416,25 +405,32 @@ func createProjectForProjectInstanceTest(ctx context.Context, t *testing.T, ctl
return project.Msg
}
-func createProjectInstanceTestInstance(ctx context.Context, t *testing.T, ctl *controller, parent *string, instanceID, title string, pg *Container) *v1pb.Instance {
+func createProjectInstanceTestInstance(ctx context.Context, t *testing.T, ctl *controller, parent *string, instanceID, title string, pg *Container, syncDatabases ...string) *v1pb.Instance {
t.Helper()
instance, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
Parent: parent,
InstanceId: instanceID,
- Instance: projectInstanceTestSpec(title, pg),
+ Instance: projectInstanceTestSpec(title, pg, syncDatabases...),
}))
require.NoError(t, err)
return instance.Msg
}
-func projectInstanceTestSpec(title string, pg *Container) *v1pb.Instance {
- return &v1pb.Instance{
+// projectInstanceTestSpec names the databases the instance may sync when it
+// sits on the shared target, and syncs everything when it has a container of
+// its own.
+func projectInstanceTestSpec(title string, pg *Container, syncDatabases ...string) *v1pb.Instance {
+ spec := &v1pb.Instance{
Title: title,
Engine: v1pb.Engine_POSTGRES,
Environment: new("environments/prod"),
Activation: true,
DataSources: []*v1pb.DataSource{pg.adminDataSource()},
}
+ if len(syncDatabases) > 0 {
+ spec.SyncDatabases = &v1pb.SyncDatabases{Databases: syncDatabases}
+ }
+ return spec
}
func projectInstanceTestInstanceNames(instances []*v1pb.Instance) []string {
@@ -453,17 +449,13 @@ func TestUndeleteProjectInstanceChecksActivationLimit(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
- pg, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pg := provisionPgInstance(t)
instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, "bot35-activation-limit", "activated project instance", pg)
a.True(instance.Activation)
- _, err = ctl.instanceServiceClient.DeleteInstance(ctx, connect.NewRequest(&v1pb.DeleteInstanceRequest{Name: instance.Name}))
+ _, err := ctl.instanceServiceClient.DeleteInstance(ctx, connect.NewRequest(&v1pb.DeleteInstanceRequest{Name: instance.Name}))
a.NoError(err)
a.NoError(ctl.removeLicense(ctx))
diff --git a/backend/tests/project_instance_workflow_test.go b/backend/tests/project_instance_workflow_test.go
index e7bf40b5e2..629467df37 100644
--- a/backend/tests/project_instance_workflow_test.go
+++ b/backend/tests/project_instance_workflow_test.go
@@ -18,19 +18,15 @@ func TestProjectInstanceWorkflowTargets(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pg, err := provisionPgInstance(ctx, t)
- a.NoError(err)
- const databaseID = "bot36_workflow_database"
+ pg := sharedPgTarget(t)
+ databaseID := uniqueDB("bot36_workflow_database")
createPgDatabase(t, pg, databaseID)
otherProject := createProjectForProjectInstanceTest(ctx, t, ctl, "bot36-other-project")
- instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, "bot36-project-instance", "project instance", pg)
- _, err = ctl.instanceServiceClient.SyncInstance(ctx, connect.NewRequest(&v1pb.SyncInstanceRequest{Name: instance.Name}))
+ instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, "bot36-project-instance", "project instance", pg, databaseID)
+ _, err := ctl.instanceServiceClient.SyncInstance(ctx, connect.NewRequest(&v1pb.SyncInstanceRequest{Name: instance.Name}))
a.NoError(err)
databaseName := fmt.Sprintf("%s/databases/%s", instance.Name, databaseID)
diff --git a/backend/tests/project_test.go b/backend/tests/project_test.go
index 3e1bbdf838..73d07d987b 100644
--- a/backend/tests/project_test.go
+++ b/backend/tests/project_test.go
@@ -16,30 +16,27 @@ func TestArchiveProject(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
// Add an instance.
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "test",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "test",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
instance := instanceResp.Msg
t.Run("ArchiveProjectWithDatabase", func(_ *testing.T) {
- databaseName := "db1"
+ databaseName := uniqueDB("db1")
err = ctl.createDatabase(ctx, ctl.project, instance, nil /* environment */, databaseName, "")
a.NoError(err)
diff --git a/backend/tests/query_history_test.go b/backend/tests/query_history_test.go
index 7161b99e74..6b196b6926 100644
--- a/backend/tests/query_history_test.go
+++ b/backend/tests/query_history_test.go
@@ -21,35 +21,29 @@ func TestListQueryHistories(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
ownerToken := ctl.authInterceptor.token
const ownerEmail = "[email protected]"
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
// 1. Create a Postgres instance + database as the owner.
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "pgInstance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ Title: "pgInstance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ SyncDatabases: &v1pb.SyncDatabases{},
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
instance := instanceResp.Msg
- const databaseName = "history_db"
+ databaseName := uniqueDB("history_db")
err = ctl.createDatabase(ctx, ctl.project, instance, nil, databaseName, "postgres")
a.NoError(err)
@@ -84,7 +78,7 @@ func TestListQueryHistories(t *testing.T) {
a.NoError(err)
otherProject := otherProjectResp.Msg
- const otherDatabaseName = "history_db_other"
+ otherDatabaseName := uniqueDB("history_db_other")
err = ctl.createDatabase(ctx, otherProject, instance, nil, otherDatabaseName, "postgres")
a.NoError(err)
diff --git a/backend/tests/read_path_secret_test.go b/backend/tests/read_path_secret_test.go
index 9344486f5c..54341639fa 100644
--- a/backend/tests/read_path_secret_test.go
+++ b/backend/tests/read_path_secret_test.go
@@ -31,10 +31,7 @@ func TestReadPathHidesTheWebhookURL(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
project := ctl.createTestProject(ctx, t, "webhook-redaction")
added, err := ctl.projectServiceClient.AddWebhook(ctx, connect.NewRequest(&v1pb.AddWebhookRequest{
@@ -111,10 +108,7 @@ func TestEnrollmentSecretsLiveOnlyInTheMintingResponses(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
userName := common.FormatUserEmail("[email protected]")
minted, err := ctl.userServiceClient.StartMFAEnrollment(ctx, connect.NewRequest(&v1pb.StartMFAEnrollmentRequest{
@@ -170,10 +164,7 @@ func TestMCPServesTheReadsTheRedactionFreed(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
project, err := ctl.projectServiceClient.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{
ProjectId: "mcp-freed-reads",
@@ -204,16 +195,16 @@ func TestMCPServesTheReadsTheRedactionFreed(t *testing.T) {
// mask: the credential it takes out is MariaDB's, and it is pinned against
// real server output in the converter's own test. What these two rows are
// for is the classification — the reads are served at all.
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instance, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: "mcp-freed-instance",
Instance: &v1pb.Instance{
- Title: "MCP freed instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "MCP freed instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -288,10 +279,7 @@ func TestMFAReplacementPromotesTheNewAuthenticator(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
userName := common.FormatUserEmail("[email protected]")
// liveSecret is empty for the account's first factor and holds the live
@@ -378,10 +366,7 @@ func TestSupersededPendingStateIsRefused(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
userName := common.FormatUserEmail("[email protected]")
enrolled, err := ctl.userServiceClient.StartMFAEnrollment(ctx, connect.NewRequest(&v1pb.StartMFAEnrollmentRequest{
@@ -465,10 +450,7 @@ func TestConfirmationCannotRevivePendingStateItRaced(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
userName := common.FormatUserEmail("[email protected]")
minted, err := ctl.userServiceClient.StartMFAEnrollment(ctx, connect.NewRequest(&v1pb.StartMFAEnrollmentRequest{
diff --git a/backend/tests/review_config_audit_test.go b/backend/tests/review_config_audit_test.go
index ee69319517..adc5e4f637 100644
--- a/backend/tests/review_config_audit_test.go
+++ b/backend/tests/review_config_audit_test.go
@@ -16,10 +16,7 @@ func TestReviewConfigAuditLog(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
loginResp, err := ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{
diff --git a/backend/tests/review_run_collision_test.go b/backend/tests/review_run_collision_test.go
index 8beba710a0..36a2dd8191 100644
--- a/backend/tests/review_run_collision_test.go
+++ b/backend/tests/review_run_collision_test.go
@@ -80,10 +80,7 @@ func TestCollision_ReviewRunProjectPurge(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
diff --git a/backend/tests/review_run_lifecycle_test.go b/backend/tests/review_run_lifecycle_test.go
index ee60b6e3ac..c8c60db63c 100644
--- a/backend/tests/review_run_lifecycle_test.go
+++ b/backend/tests/review_run_lifecycle_test.go
@@ -51,16 +51,13 @@ func TestCollision_RunReviewLifecycle(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
// The fixture issues carry rollouts; a rollout freezes the plan's SQL, so
// review is refused.
- _, err = runReview(ctx, ctl, fixture.IssueA.Name, "rule")
+ _, err := runReview(ctx, ctl, fixture.IssueA.Name, "rule")
a.Equal(connect.CodeFailedPrecondition, connect.CodeOf(err))
// Fresh colliding plan+issue pairs without rollouts.
diff --git a/backend/tests/sample_project_instance_test.go b/backend/tests/sample_project_instance_test.go
index d54d89eefd..6b5281a795 100644
--- a/backend/tests/sample_project_instance_test.go
+++ b/backend/tests/sample_project_instance_test.go
@@ -32,7 +32,6 @@ import (
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
"github.com/bytebase/bytebase/backend/generated-go/v1/v1connect"
- "github.com/bytebase/bytebase/backend/migrator"
samplerunner "github.com/bytebase/bytebase/backend/runner/sample"
"github.com/bytebase/bytebase/backend/runner/schemasync"
"github.com/bytebase/bytebase/backend/store"
@@ -45,58 +44,23 @@ import (
// server, so each gets its own migrated metadata database on the package's
// Postgres and they share one target, isolated by workspace.
-// newMetadataStore gives a test its own migrated metadata database on the
-// package's Postgres, for service-level tests that need a Store but no server.
-func newMetadataStore(ctx context.Context, t *testing.T) (*sql.DB, *store.Store) {
- t.Helper()
- admin, err := sql.Open("pgx", fmt.Sprintf("postgresql://postgres:root-password@%s:%s/postgres", externalPgHost, externalPgPort))
- require.NoError(t, err)
- defer admin.Close()
- name := getTestDatabaseString()
- _, err = admin.ExecContext(ctx, "CREATE DATABASE "+name)
- require.NoError(t, err)
-
- dsn := fmt.Sprintf("postgresql://postgres:root-password@%s:%s/%s", externalPgHost, externalPgPort, name)
- db, err := sql.Open("pgx", dsn)
- require.NoError(t, err)
- require.NoError(t, migrator.MigrateSchema(ctx, db))
- stores, err := store.New(ctx, dsn, false)
- require.NoError(t, err)
- t.Cleanup(func() {
- require.NoError(t, stores.Close())
- require.NoError(t, db.Close())
- })
- return db, stores
-}
-
var (
- sampleTargetOnce sync.Once
- sampleTargetContainer *testcontainer.Container
- sampleTargetErr error
+ sampleTargetOnce sync.Once
+ sampleTargetErr error
)
-// sharedSampleTarget starts the TLS Postgres the sample tests provision onto,
-// once for the package; startMain takes it down. The tests isolate by
-// workspace — every sample resource name derives from the workspace ID — so
-// one target serves them all.
+// sharedSampleTarget is the TLS Postgres the sample tests provision onto. The
+// tests isolate by workspace — every sample resource name derives from the
+// workspace ID — so one target serves them all, revoked down to its baseline
+// once.
func sharedSampleTarget(t *testing.T) *testcontainer.Container {
t.Helper()
+ container := testcontainer.SharedTLSPgContainer(t)
sampleTargetOnce.Do(func() {
- ctx := context.Background()
- container, err := testcontainer.GetTLSPgContainer(ctx)
- if err != nil {
- sampleTargetErr = err
- return
- }
- if err := prepareSampleTargetBaseline(ctx, container.GetDB()); err != nil {
- container.Close(ctx)
- sampleTargetErr = err
- return
- }
- sampleTargetContainer = container
+ sampleTargetErr = prepareSampleTargetBaseline(context.Background(), container.GetDB())
})
require.NoError(t, sampleTargetErr)
- return sampleTargetContainer
+ return container
}
func TestPrepareSampleProjectInstanceLifecycle(t *testing.T) {
@@ -432,7 +396,7 @@ func newSampleProjectInstanceFixture(t *testing.T, clock func() time.Time, works
t.Cleanup(cancel)
target := sharedSampleTarget(t)
- _, stores := newMetadataStore(ctx, t)
+ _, stores, _ := testcontainer.NewMetadataDB(t)
_, err := stores.GetDB().ExecContext(ctx, `INSERT INTO workspace (resource_id) VALUES ($1)`, workspaceID)
require.NoError(t, err)
_, err = stores.GetDB().ExecContext(ctx, `INSERT INTO project (resource_id, workspace, name) VALUES ('sample-project', $1, 'Sample Project')`, workspaceID)
diff --git a/backend/tests/saved_query_test.go b/backend/tests/saved_query_test.go
index 046225cedf..7a178c9c83 100644
--- a/backend/tests/saved_query_test.go
+++ b/backend/tests/saved_query_test.go
@@ -18,10 +18,7 @@ func TestListSavedQueries(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
ownerToken := ctl.authInterceptor.token
@@ -169,10 +166,7 @@ func TestListSavedQueriesOrdering(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
createSavedQuery := func(title string) *v1pb.SavedQuery {
return createTitledSavedQuery(ctx, a, ctl, title)
@@ -185,7 +179,7 @@ func TestListSavedQueriesOrdering(t *testing.T) {
// Editing a row must not move it in the default order; only an explicit
// "update_time desc" surfaces it first.
- _, err = ctl.savedQueryServiceClient.UpdateSavedQuery(ctx, connect.NewRequest(&v1pb.UpdateSavedQueryRequest{
+ _, err := ctl.savedQueryServiceClient.UpdateSavedQuery(ctx, connect.NewRequest(&v1pb.UpdateSavedQueryRequest{
SavedQuery: &v1pb.SavedQuery{Name: first.Name, Content: []byte("SELECT 2;")},
UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"content"}},
}))
@@ -267,10 +261,7 @@ func TestSearchSavedQueriesFilterByFolder(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
createSavedQuery := func(title string) *v1pb.SavedQuery {
return createTitledSavedQuery(ctx, a, ctl, title)
@@ -352,10 +343,7 @@ func TestSearchSavedQueriesFilterByTitle(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
createSavedQuery := func(title string) *v1pb.SavedQuery {
return createTitledSavedQuery(ctx, a, ctl, title)
@@ -411,10 +399,7 @@ func TestMoveSavedQueries(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
createSavedQuery := func(title, folder string) *v1pb.SavedQuery {
resp, err := ctl.savedQueryServiceClient.CreateSavedQuery(ctx, connect.NewRequest(&v1pb.CreateSavedQueryRequest{
@@ -506,10 +491,7 @@ func TestSearchSavedQueryFoldersReturnsCallerFolders(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
ownerToken := ctl.authInterceptor.token
@@ -634,10 +616,7 @@ func TestGetSavedQueryHidesUnreadableRows(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
ownerToken := ctl.authInterceptor.token
created, err := ctl.savedQueryServiceClient.CreateSavedQuery(ctx, connect.NewRequest(&v1pb.CreateSavedQueryRequest{
@@ -720,10 +699,7 @@ func TestListSavedQueriesReturnsWholeStatement(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
const ownerEmail = "[email protected]"
@@ -766,10 +742,7 @@ func TestCreateSavedQueryRejectsArchivedProject(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
projectID := generateRandomString("sq-archived")
projectResp, err := ctl.projectServiceClient.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{
@@ -803,12 +776,9 @@ func TestSearchSavedQueriesRejectsWildcardProject(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- _, err = ctl.savedQueryServiceClient.SearchSavedQueries(ctx, connect.NewRequest(&v1pb.SearchSavedQueriesRequest{
+ _, err := ctl.savedQueryServiceClient.SearchSavedQueries(ctx, connect.NewRequest(&v1pb.SearchSavedQueriesRequest{
Parent: "projects/-",
}))
a.Error(err)
@@ -854,10 +824,7 @@ func TestSavedQuerySharing(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
ownerToken := ctl.authInterceptor.token
@@ -1055,10 +1022,7 @@ func TestSavedQueryPerVerbRoleGrants(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
ownerToken := ctl.authInterceptor.token
diff --git a/backend/tests/schema_update_test.go b/backend/tests/schema_update_test.go
index 769f764b59..a822ffe80a 100644
--- a/backend/tests/schema_update_test.go
+++ b/backend/tests/schema_update_test.go
@@ -19,31 +19,28 @@ func TestSchemaAndDataUpdate(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Provision an instance.
instanceName := "testInstance1"
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: instanceName,
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: instanceName,
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
instance := instanceResp.Msg
// Create an issue that creates a database.
- databaseName := "testSchemaUpdate"
+ databaseName := uniqueDB("test_schema_update")
err = ctl.createDatabase(ctx, ctl.project, instance, nil /* environment */, databaseName, "")
a.NoError(err)
@@ -176,12 +173,7 @@ CREATE TABLE "public"."book" (
}
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer func() {
- _ = ctl.Close(ctx)
- }()
+ ctl, ctx := startWorkspace(ctx, t)
environmentName := strings.ToLower(t.Name())
environment, err := ctl.createEnvironment(ctx, environmentName, environmentName)
a.NoError(err)
@@ -189,19 +181,16 @@ CREATE TABLE "public"."book" (
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
a := require.New(t)
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: test.instanceID,
Instance: &v1pb.Instance{
- Title: test.name,
- Engine: v1pb.Engine_POSTGRES,
- Environment: new(environment.Name),
- Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: test.name,
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new(environment.Name),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
@@ -250,32 +239,29 @@ func TestMarkTaskAsDone(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Provision an instance.
instanceName := "testInstance1"
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
// Add an instance.
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: instanceName,
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: instanceName,
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
instance := instanceResp.Msg
// Create an issue that creates a database.
- databaseName := "testSchemaUpdate"
+ databaseName := uniqueDB("test_schema_update")
err = ctl.createDatabase(ctx, ctl.project, instance, nil, databaseName, "")
a.NoError(err)
diff --git a/backend/tests/sensitive_data_test.go b/backend/tests/sensitive_data_test.go
index ce7212546a..676664c698 100644
--- a/backend/tests/sensitive_data_test.go
+++ b/backend/tests/sensitive_data_test.go
@@ -82,10 +82,10 @@ var (
)
func TestSensitiveData(t *testing.T) {
+ databaseName := uniqueDB("sensitive_data")
const (
- databaseName = "sensitive_data"
- tableName = "tech_book"
- createTable = `
+ tableName = "tech_book"
+ createTable = `
CREATE TABLE tech_book(
id int primary key,
name varchar(220),
@@ -103,12 +103,9 @@ func TestSensitiveData(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
- _, err = ctl.settingServiceClient.UpdateSetting(ctx, connect.NewRequest(&v1pb.UpdateSettingRequest{
+ _, err := ctl.settingServiceClient.UpdateSetting(ctx, connect.NewRequest(&v1pb.UpdateSettingRequest{
Setting: &v1pb.Setting{
Name: "settings/" + v1pb.Setting_SEMANTIC_TYPES.String(),
Value: &v1pb.SettingValue{
@@ -131,20 +128,17 @@ func TestSensitiveData(t *testing.T) {
}))
a.NoError(err)
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "pgInstance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "pgInstance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
diff --git a/backend/tests/sheet_scope_test.go b/backend/tests/sheet_scope_test.go
index 7818a73ba1..a49ba3a76e 100644
--- a/backend/tests/sheet_scope_test.go
+++ b/backend/tests/sheet_scope_test.go
@@ -25,10 +25,7 @@ func TestSheetProjectScope(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
projectA := ctl.project
projectBResp, err := ctl.projectServiceClient.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{
@@ -160,10 +157,7 @@ func TestSheetHistoryOnDatabaseTransfer(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
projectA := ctl.project
projectBResp, err := ctl.projectServiceClient.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{
@@ -268,10 +262,7 @@ func TestSheetHistoryAfterOwnerPurge(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
purgedResp, err := ctl.projectServiceClient.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{
ProjectId: generateRandomString("sheet-purge"),
@@ -330,10 +321,7 @@ func TestCollision_SheetWrite(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
fixture := setupCollidingProjects(ctx, t, ctl)
@@ -354,7 +342,7 @@ func TestCollision_SheetWrite(t *testing.T) {
// Read A's shared-content sheet through the public API.
sharedHash := sha256.Sum256([]byte(sharedContent))
- _, err = ctl.sheetServiceClient.GetSheet(ctx, connect.NewRequest(&v1pb.GetSheetRequest{
+ _, err := ctl.sheetServiceClient.GetSheet(ctx, connect.NewRequest(&v1pb.GetSheetRequest{
Name: fmt.Sprintf("%s/sheets/%s", fixture.ProjectA.Name, hex.EncodeToString(sharedHash[:])),
}))
a.NoError(err)
diff --git a/backend/tests/sql_dml_table_scope_test.go b/backend/tests/sql_dml_table_scope_test.go
index a728c0ad03..b921ca72d9 100644
--- a/backend/tests/sql_dml_table_scope_test.go
+++ b/backend/tests/sql_dml_table_scope_test.go
@@ -34,35 +34,29 @@ func TestSQLEditorTableScopedDML(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Save the owner token so we can swap identities and swap back.
ownerToken := ctl.authInterceptor.token
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
// 1. Create a Postgres instance + database as the owner.
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "pgInstance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "pgInstance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
instance := instanceResp.Msg
- const databaseName = "sup222"
+ databaseName := uniqueDB("sup222")
err = ctl.createDatabase(ctx, ctl.project, instance, nil, databaseName, "postgres")
a.NoError(err)
@@ -202,34 +196,28 @@ func TestSQLEditorTableScopedDMLEdgeCases(t *testing.T) {
// with each other). The whole test still runs alongside other test binaries.
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
ownerToken := ctl.authInterceptor.token
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
// Single Postgres instance + database shared by every subtest.
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "pgInstance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "pgInstance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
instance := instanceResp.Msg
- const databaseName = "sup222edge"
+ databaseName := uniqueDB("sup222edge")
a.NoError(ctl.createDatabase(ctx, ctl.project, instance, nil, databaseName, "postgres"))
databaseResp, err := ctl.databaseServiceClient.GetDatabase(ctx, connect.NewRequest(&v1pb.GetDatabaseRequest{
@@ -870,7 +858,7 @@ func TestSQLEditorTableScopedDMLEdgeCases(t *testing.T) {
testEnv, err := ctl.getEnvironment(ctx, "test")
ra.NoError(err)
// A second database on the SAME instance, in a DIFFERENT (test) environment.
- const otherDBName = "sup222edge_other_env"
+ otherDBName := uniqueDB("sup222edge_other_env")
ra.NoError(ctl.createDatabase(ctx, ctl.project, instance, testEnv, otherDBName, "postgres"))
// Seed the granted table in the request (prod) database for the positive control.
@@ -915,7 +903,7 @@ func TestSQLEditorTableScopedDMLEdgeCases(t *testing.T) {
// catalog name, so the decision is observable.)
t.Run("MultiStatementCrossDatabaseNotBypassed", func(t *testing.T) {
ra := require.New(t)
- const otherDBName = "sup222edge_mstmt_other"
+ otherDBName := uniqueDB("sup222edge_mstmt_other")
ra.NoError(ctl.createDatabase(ctx, ctl.project, instance, nil /* environment */, otherDBName, "postgres"))
// Seed the granted table in the request database for the positive control.
@@ -986,7 +974,7 @@ func TestSQLEditorTableScopedDMLEdgeCases(t *testing.T) {
otherProject, err := ctl.projectServiceClient.GetProject(ctx, connect.NewRequest(&v1pb.GetProjectRequest{Name: fmt.Sprintf("projects/%s", projectID)}))
ra.NoError(err)
// A database in the OTHER project, on the SAME instance.
- const otherProjDB = "sup222edge_other_proj"
+ otherProjDB := uniqueDB("sup222edge_other_proj")
ra.NoError(ctl.createDatabase(ctx, otherProject.Msg, instance, nil /* environment */, otherProjDB, "postgres"))
// An ENVIRONMENT-scoped DML grant in the session's project (no database clause), so it
diff --git a/backend/tests/sql_editor_test.go b/backend/tests/sql_editor_test.go
index e093c60cea..a65457f9bd 100644
--- a/backend/tests/sql_editor_test.go
+++ b/backend/tests/sql_editor_test.go
@@ -103,25 +103,19 @@ func TestAdminQueryAffectedRows(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
pgInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "pgInstance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "pgInstance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
@@ -130,6 +124,7 @@ func TestAdminQueryAffectedRows(t *testing.T) {
for _, tt := range tests {
instance := pgInstance
databaseOwner := "postgres"
+ tt.databaseName = uniqueDB(tt.databaseName)
err = ctl.createDatabase(ctx, ctl.project, instance, nil /* environment */, tt.databaseName, databaseOwner)
a.NoError(err)
diff --git a/backend/tests/sql_export_data_source_test.go b/backend/tests/sql_export_data_source_test.go
index 699a63d18c..7557acb3c2 100644
--- a/backend/tests/sql_export_data_source_test.go
+++ b/backend/tests/sql_export_data_source_test.go
@@ -25,23 +25,16 @@ func TestSQLExportDataSourceResolution(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := provisionPgInstance(t)
// A login role for the read-only data source. Its grants have to wait until
// the table exists, further down: Postgres grants are per object, where
// MySQL's GRANT SELECT ON *.* covered tables created later.
- _, err = pgContainer.db.Exec("DROP ROLE IF EXISTS export_ro")
+ _, err := pgContainer.GetDB().Exec("DROP ROLE IF EXISTS export_ro")
a.NoError(err)
- _, err = pgContainer.db.Exec("CREATE ROLE export_ro LOGIN PASSWORD 'export_ro_password'")
+ _, err = pgContainer.GetDB().Exec("CREATE ROLE export_ro LOGIN PASSWORD 'export_ro_password'")
a.NoError(err)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
@@ -51,7 +44,7 @@ func TestSQLExportDataSourceResolution(t *testing.T) {
Engine: v1pb.Engine_POSTGRES,
Environment: new("environments/prod"),
Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
@@ -78,7 +71,7 @@ func TestSQLExportDataSourceResolution(t *testing.T) {
err = ctl.changeDatabase(ctx, ctl.project, database, setupSheetResp.Msg, false)
a.NoError(err)
- grantDB, err := sql.Open("pgx", fmt.Sprintf("postgresql://postgres:root-password@%s:%s/%s?sslmode=disable", pgContainer.host, pgContainer.port, databaseName))
+ grantDB, err := sql.Open("pgx", fmt.Sprintf("postgresql://postgres:root-password@%s:%s/%s?sslmode=disable", pgContainer.GetHost(), pgContainer.GetPort(), databaseName))
a.NoError(err)
defer grantDB.Close()
for _, stmt := range []string{
@@ -107,8 +100,8 @@ func TestSQLExportDataSourceResolution(t *testing.T) {
DataSource: &v1pb.DataSource{
Id: "readonly",
Type: v1pb.DataSourceType_READ_ONLY,
- Host: pgContainer.host,
- Port: pgContainer.port,
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
Username: "export_ro",
Password: "export_ro_password",
},
diff --git a/backend/tests/sql_export_test.go b/backend/tests/sql_export_test.go
index 2d9ce103ad..b980a7d0ac 100644
--- a/backend/tests/sql_export_test.go
+++ b/backend/tests/sql_export_test.go
@@ -96,25 +96,19 @@ func TestSQLExport(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
pgInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "pgInstance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "pgInstance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
diff --git a/backend/tests/sql_query_data_source_test.go b/backend/tests/sql_query_data_source_test.go
index 201076b59c..551da0643a 100644
--- a/backend/tests/sql_query_data_source_test.go
+++ b/backend/tests/sql_query_data_source_test.go
@@ -24,23 +24,16 @@ func TestSQLQueryDataSourceResolution(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := provisionPgInstance(t)
// A login role for the read-only data source. Its grants have to wait until
// the table exists, further down: Postgres grants are per object, where
// MySQL's GRANT SELECT ON *.* covered tables created later.
- _, err = pgContainer.db.Exec("DROP ROLE IF EXISTS query_ro")
+ _, err := pgContainer.GetDB().Exec("DROP ROLE IF EXISTS query_ro")
a.NoError(err)
- _, err = pgContainer.db.Exec("CREATE ROLE query_ro LOGIN PASSWORD 'query_ro_password'")
+ _, err = pgContainer.GetDB().Exec("CREATE ROLE query_ro LOGIN PASSWORD 'query_ro_password'")
a.NoError(err)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
@@ -50,7 +43,7 @@ func TestSQLQueryDataSourceResolution(t *testing.T) {
Engine: v1pb.Engine_POSTGRES,
Environment: new("environments/prod"),
Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
@@ -74,7 +67,7 @@ func TestSQLQueryDataSourceResolution(t *testing.T) {
err = ctl.changeDatabase(ctx, ctl.project, database, setupSheetResp.Msg, false)
a.NoError(err)
- grantDB, err := sql.Open("pgx", fmt.Sprintf("postgresql://postgres:root-password@%s:%s/%s?sslmode=disable", pgContainer.host, pgContainer.port, databaseName))
+ grantDB, err := sql.Open("pgx", fmt.Sprintf("postgresql://postgres:root-password@%s:%s/%s?sslmode=disable", pgContainer.GetHost(), pgContainer.GetPort(), databaseName))
a.NoError(err)
defer grantDB.Close()
for _, stmt := range []string{
@@ -99,8 +92,8 @@ func TestSQLQueryDataSourceResolution(t *testing.T) {
DataSource: &v1pb.DataSource{
Id: "readonly",
Type: v1pb.DataSourceType_READ_ONLY,
- Host: pgContainer.host,
- Port: pgContainer.port,
+ Host: pgContainer.GetHost(),
+ Port: pgContainer.GetPort(),
Username: "query_ro",
Password: "query_ro_password",
},
diff --git a/backend/tests/sql_query_test.go b/backend/tests/sql_query_test.go
index 0789f34561..df97b385e2 100644
--- a/backend/tests/sql_query_test.go
+++ b/backend/tests/sql_query_test.go
@@ -62,25 +62,19 @@ func TestSQLAdminQuery(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
pgInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "pgInstance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "pgInstance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
@@ -89,6 +83,7 @@ func TestSQLAdminQuery(t *testing.T) {
for _, tt := range tests {
instance := pgInstance
databaseOwner := "postgres"
+ tt.databaseName = uniqueDB(tt.databaseName)
err = ctl.createDatabase(ctx, ctl.project, instance, nil /* environment */, tt.databaseName, databaseOwner)
a.NoError(err)
diff --git a/backend/tests/sql_review_test.go b/backend/tests/sql_review_test.go
index 5575687c3a..da7c776df5 100644
--- a/backend/tests/sql_review_test.go
+++ b/backend/tests/sql_review_test.go
@@ -95,20 +95,13 @@ func TestSQLReviewForPostgreSQL(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
tests, err := readTestData(filepath)
a.NoError(err)
- ctx, err = ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := provisionPgInstance(t)
- pgDB := pgContainer.db
+ pgDB := pgContainer.GetDB()
err = pgDB.Ping()
a.NoError(err)
@@ -150,7 +143,7 @@ func TestSQLReviewForPostgreSQL(t *testing.T) {
Engine: v1pb.Engine_POSTGRES,
Environment: new("environments/prod"),
Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "bytebase", Password: "bytebase", Id: "admin"}},
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "bytebase", Password: "bytebase", Id: "admin"}},
},
}))
a.NoError(err)
diff --git a/backend/tests/sql_service_audit_test.go b/backend/tests/sql_service_audit_test.go
index 33ca536392..5a2ef083fc 100644
--- a/backend/tests/sql_service_audit_test.go
+++ b/backend/tests/sql_service_audit_test.go
@@ -14,26 +14,23 @@ func TestAdminExecuteAuditLog(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "admin execute audit",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "admin execute audit",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
- databaseName := "admin_execute_audit"
+ databaseName := uniqueDB("admin_execute_audit")
a.NoError(ctl.createDatabase(ctx, ctl.project, instanceResp.Msg, nil, databaseName, ""))
databaseResp, err := ctl.databaseServiceClient.GetDatabase(ctx, connect.NewRequest(&v1pb.GetDatabaseRequest{
Name: instanceResp.Msg.Name + "/databases/" + databaseName,
diff --git a/backend/tests/sql_stop_on_error_test.go b/backend/tests/sql_stop_on_error_test.go
index 5317cebcb5..6171129642 100644
--- a/backend/tests/sql_stop_on_error_test.go
+++ b/backend/tests/sql_stop_on_error_test.go
@@ -62,27 +62,19 @@ func TestSQLQueryStopOnError(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- t.Cleanup(func() {
- ctl.Close(ctx)
- })
-
- pgContainer, err := getPgContainer(ctx)
- t.Cleanup(func() {
- pgContainer.Close(ctx)
- })
- a.NoError(err)
+ ctl, ctx := startProject(ctx, t)
+
+ pgContainer := sharedPgTarget(t)
pgInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "pgInstance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "pgInstance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
@@ -188,27 +180,19 @@ func TestSQLAdminExecuteStopOnError(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- t.Cleanup(func() {
- ctl.Close(ctx)
- })
-
- pgContainer, err := getPgContainer(ctx)
- t.Cleanup(func() {
- pgContainer.Close(ctx)
- })
- a.NoError(err)
+ ctl, ctx := startProject(ctx, t)
+
+ pgContainer := sharedPgTarget(t)
pgInstanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "pgInstance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "postgres", Password: "root-password", Id: "admin"}},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "pgInstance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "postgres", Password: "root-password", Id: "admin"}},
},
}))
a.NoError(err)
diff --git a/backend/tests/subscription_test.go b/backend/tests/subscription_test.go
index b549b94e2f..28074a4efb 100644
--- a/backend/tests/subscription_test.go
+++ b/backend/tests/subscription_test.go
@@ -13,12 +13,9 @@ func TestSubscription(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
- err = ctl.removeLicense(ctx)
+ err := ctl.removeLicense(ctx)
a.NoError(err)
subscription, err := ctl.getSubscription(ctx)
a.NoError(err)
diff --git a/backend/tests/sync_schema_test.go b/backend/tests/sync_schema_test.go
index e31a7b8a03..07d289338d 100644
--- a/backend/tests/sync_schema_test.go
+++ b/backend/tests/sync_schema_test.go
@@ -29,19 +29,12 @@ func TestSyncSchema(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := provisionPgInstance(t)
- pgDB := pgContainer.db
- err = pgDB.Ping()
+ pgDB := pgContainer.GetDB()
+ err := pgDB.Ping()
a.NoError(err)
_, err = pgDB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %v", databaseName))
@@ -58,7 +51,7 @@ func TestSyncSchema(t *testing.T) {
Engine: v1pb.Engine_POSTGRES,
Environment: new("environments/prod"),
Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "bytebase", Password: "bytebase", Id: "admin"}},
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "bytebase", Password: "bytebase", Id: "admin"}},
},
}))
a.NoError(err)
@@ -130,19 +123,12 @@ func TestSyncSchemaWithTempSchema(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := provisionPgInstance(t)
- pgDB := pgContainer.db
- err = pgDB.Ping()
+ pgDB := pgContainer.GetDB()
+ err := pgDB.Ping()
a.NoError(err)
// Create database and user
@@ -160,7 +146,7 @@ func TestSyncSchemaWithTempSchema(t *testing.T) {
a.NoError(err)
// Connect to the test database to create temp tables and schemas
- testDB, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password dbname=%s sslmode=disable", pgContainer.host, pgContainer.port, databaseName))
+ testDB, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password dbname=%s sslmode=disable", pgContainer.GetHost(), pgContainer.GetPort(), databaseName))
a.NoError(err)
defer testDB.Close()
@@ -200,7 +186,7 @@ func TestSyncSchemaWithTempSchema(t *testing.T) {
Engine: v1pb.Engine_POSTGRES,
Environment: new("environments/prod"),
Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "testuser", Password: "testpass", Id: "admin"}},
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "testuser", Password: "testpass", Id: "admin"}},
},
}))
a.NoError(err)
diff --git a/backend/tests/syncer_test.go b/backend/tests/syncer_test.go
index f1157151c0..6e44692ead 100644
--- a/backend/tests/syncer_test.go
+++ b/backend/tests/syncer_test.go
@@ -181,19 +181,12 @@ func TestSyncerForPostgreSQL(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := getPgContainer(ctx)
- defer func() {
- pgContainer.Close(ctx)
- }()
- a.NoError(err)
+ pgContainer := provisionPgInstance(t)
- pgDB := pgContainer.db
- err = pgDB.Ping()
+ pgDB := pgContainer.GetDB()
+ err := pgDB.Ping()
a.NoError(err)
_, err = pgDB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %v", databaseName))
@@ -210,7 +203,7 @@ func TestSyncerForPostgreSQL(t *testing.T) {
Engine: v1pb.Engine_POSTGRES,
Environment: new("environments/prod"),
Activation: true,
- DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.host, Port: pgContainer.port, Username: "bytebase", Password: "bytebase", Id: "admin"}},
+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: pgContainer.GetHost(), Port: pgContainer.GetPort(), Username: "bytebase", Password: "bytebase", Id: "admin"}},
},
}))
a.NoError(err)
diff --git a/backend/tests/task_run_cancel_scope_test.go b/backend/tests/task_run_cancel_scope_test.go
index fcf70cd2d9..537c0fde98 100644
--- a/backend/tests/task_run_cancel_scope_test.go
+++ b/backend/tests/task_run_cancel_scope_test.go
@@ -27,24 +27,21 @@ func TestBatchCancelTaskRuns_RejectsTaskRunFromAnotherStage(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
testEnvironment, err := ctl.getEnvironment(ctx, "test")
a.NoError(err)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "testInstanceCancelScope",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "testInstanceCancelScope",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
diff --git a/backend/tests/task_run_idempotent_test.go b/backend/tests/task_run_idempotent_test.go
index 7ab1077507..5e09921fe7 100644
--- a/backend/tests/task_run_idempotent_test.go
+++ b/backend/tests/task_run_idempotent_test.go
@@ -21,24 +21,21 @@ func TestBatchRunTasks_Idempotent(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// Provision an instance
instanceName := "testInstanceIdempotent"
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: instanceName,
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: instanceName,
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -167,21 +164,18 @@ func TestBatchRunTasks_ConcurrentSkipCannotBothWin(t *testing.T) {
a := require.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "testInstanceConcurrentRunSkip",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "testInstanceConcurrentRunSkip",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -322,22 +316,19 @@ func TestBatchRunTasks_RejectsSkippedTasks(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "testInstanceSkippedTask",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "testInstanceSkippedTask",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
diff --git a/backend/tests/tenant_test.go b/backend/tests/tenant_test.go
index 646a26fab3..3f612fb189 100644
--- a/backend/tests/tenant_test.go
+++ b/backend/tests/tenant_test.go
@@ -35,10 +35,7 @@ func TestTenantBackfill(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
// ==================== TEST SETUP ====================
// Create baseline database, database group, and a pending rollout.
@@ -54,23 +51,23 @@ func TestTenantBackfill(t *testing.T) {
}))
a.NoError(err)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
prodInstance, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "prod-instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "prod-instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
// Create baseline database (source of truth for tenant schemas).
- baselineDBName := "tenant_baseline"
+ baselineDBName := uniqueDB("tenant_baseline")
err = ctl.createDatabase(ctx, project.Msg, prodInstance.Msg, nil, baselineDBName, "")
a.NoError(err)
@@ -162,7 +159,7 @@ func TestTenantBackfill(t *testing.T) {
a.NoError(err)
// Step 2: Create new tenant database.
- newTenantDBName := "tenant_new"
+ newTenantDBName := uniqueDB("tenant_new")
err = ctl.createDatabase(ctx, project.Msg, prodInstance.Msg, nil, newTenantDBName, "")
a.NoError(err)
@@ -307,21 +304,18 @@ func TestCreatePlanWithRepeatedDatabaseGroupTarget(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startProject(ctx, t)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instance, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "prod-instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "prod-instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
diff --git a/backend/tests/testcontainer.go b/backend/tests/testcontainer.go
index 328011cd8a..75296ac0d7 100644
--- a/backend/tests/testcontainer.go
+++ b/backend/tests/testcontainer.go
@@ -2,44 +2,27 @@ package tests
import (
"context"
- "database/sql"
"testing"
+ "github.com/stretchr/testify/require"
+
"github.com/bytebase/bytebase/backend/common/testcontainer"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
)
-// Container wraps testcontainer.Container and exposes fields for backward compatibility
+// Container is a test container with the data-source helpers the tests build
+// instances from.
type Container struct {
*testcontainer.Container
- host string
- port string
- db *sql.DB
}
-// getPgContainer creates a PostgreSQL container for testing
-func getPgContainer(ctx context.Context) (*Container, error) {
- tc, err := testcontainer.GetPgContainer(ctx)
- if err != nil {
- return nil, err
- }
- return &Container{
- Container: tc,
- host: tc.GetHost(),
- port: tc.GetPort(),
- db: tc.GetDB(),
- }, nil
-}
-
-// provisionPgInstance starts a dedicated Postgres container that backs a test
-// database instance. The container is closed when the test finishes.
-func provisionPgInstance(ctx context.Context, t *testing.T) (*Container, error) {
- c, err := getPgContainer(ctx)
- if err != nil {
- return nil, err
- }
- t.Cleanup(func() { c.Close(ctx) })
- return c, nil
+// provisionPgInstance starts a Postgres of the test's own, closed when the test
+// finishes. Use it for what sharedPgTarget cannot serve: a cluster-wide write
+// such as a role, two instances that must be two servers, or a connection the
+// test breaks on purpose.
+func provisionPgInstance(t *testing.T) *Container {
+ t.Helper()
+ return provision(t, testcontainer.GetPgContainer)
}
// dataSource returns a data source of the given type and ID pointing at the container.
@@ -47,8 +30,8 @@ func (c *Container) dataSource(dsType v1pb.DataSourceType, id string) *v1pb.Data
return &v1pb.DataSource{
Type: dsType,
Id: id,
- Host: c.host,
- Port: c.port,
+ Host: c.GetHost(),
+ Port: c.GetPort(),
Username: "postgres",
Password: "root-password",
}
@@ -59,16 +42,27 @@ func (c *Container) adminDataSource() *v1pb.DataSource {
return c.dataSource(v1pb.DataSourceType_ADMIN, "admin")
}
-// getMySQLContainer creates a MySQL container for testing
-func getMySQLContainer(ctx context.Context) (*Container, error) {
- tc, err := testcontainer.GetTestMySQLContainer(ctx)
- if err != nil {
- return nil, err
- }
- return &Container{
- Container: tc,
- host: tc.GetHost(),
- port: tc.GetPort(),
- db: tc.GetDB(),
- }, nil
+// sharedPgTarget is the Postgres the tests point instances at: one container
+// for the package, a database per test. An instance on it must carry
+// sync_databases, which createDatabase keeps current, or it syncs every other
+// test's databases too.
+func sharedPgTarget(t *testing.T) *Container {
+ t.Helper()
+ tc := testcontainer.SharedTargetPgContainer(t)
+ return &Container{tc}
+}
+
+// provisionMySQLInstance is provisionPgInstance for MySQL.
+func provisionMySQLInstance(t *testing.T) *Container {
+ t.Helper()
+ return provision(t, testcontainer.GetMySQLContainer)
+}
+
+func provision(t *testing.T, start func(context.Context) (*testcontainer.Container, error)) *Container {
+ t.Helper()
+ ctx := context.Background()
+ tc, err := start(ctx)
+ require.NoError(t, err)
+ t.Cleanup(func() { tc.Close(ctx) })
+ return &Container{tc}
}
diff --git a/backend/tests/tests.go b/backend/tests/tests.go
index f7909afeac..794626ab79 100644
--- a/backend/tests/tests.go
+++ b/backend/tests/tests.go
@@ -10,10 +10,12 @@ import (
"net"
"net/http"
"sync"
+ "testing"
"time"
"connectrpc.com/connect"
"github.com/pkg/errors"
+ "github.com/stretchr/testify/require"
"go.uber.org/multierr"
"golang.org/x/net/http2"
"google.golang.org/grpc/codes"
@@ -152,6 +154,73 @@ func getTestDatabaseString() string {
return fmt.Sprintf("bbtest%d", p)
}
+var (
+ sharedServerOnce sync.Once
+ sharedServerCtl *controller
+ sharedServerErr error
+)
+
+// sharedServer boots the package's one server, on the first test that asks for
+// it; startMain shuts it down.
+func sharedServer(t *testing.T) *controller {
+ t.Helper()
+ sharedServerOnce.Do(func() {
+ ctl := &controller{}
+ if _, err := ctl.StartServerWithExternalPg(context.Background()); err != nil {
+ sharedServerErr = err
+ return
+ }
+ sharedServerCtl = ctl
+ })
+ require.NoError(t, sharedServerErr)
+ return sharedServerCtl
+}
+
+// startWorkspace gives the test a server, and so a workspace, of its own. Use it
+// when the test writes what the workspace shares — a setting, a workspace or
+// environment policy, the license, the IAM policy, a workspace-scoped ID, the
+// demo principal's credentials — or asserts on a workspace-wide list.
+func startWorkspace(ctx context.Context, t *testing.T) (*controller, context.Context) {
+ t.Helper()
+ ctl := &controller{}
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = ctl.Close(ctx) })
+ return ctl, ctx
+}
+
+// startProject gives the test a project of its own on that server. Project is
+// Bytebase's tenancy boundary, so it is the test boundary here too: the server,
+// workspace, license and environments are built once, not once per test.
+func startProject(ctx context.Context, t *testing.T) (*controller, context.Context) {
+ t.Helper()
+ base := sharedServer(t)
+
+ ctl := &controller{
+ server: base.server,
+ profile: base.profile,
+ client: base.client,
+ rootURL: base.rootURL,
+ apiURL: base.apiURL,
+ v1APIURL: base.v1APIURL,
+ }
+ ctl.newClients()
+ ctl.authInterceptor.token = base.authInterceptor.token
+ ctl.principalName = base.principalName
+
+ projectID := generateRandomString("project")
+ resp, err := ctl.projectServiceClient.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{
+ Project: &v1pb.Project{
+ Title: projectID,
+ AllowSelfApproval: true,
+ },
+ ProjectId: projectID,
+ }))
+ require.NoError(t, err)
+ ctl.project = resp.Msg
+ return ctl, ctx
+}
+
// StartServerWithExternalPg starts the main server with external Postgres.
func (ctl *controller) StartServerWithExternalPg(ctx context.Context) (context.Context, error) {
pgMainURL := fmt.Sprintf("postgresql://postgres:root-password@%s:%s/postgres", externalPgHost, externalPgPort)
@@ -277,10 +346,28 @@ func (ctl *controller) start(ctx context.Context, port int) (context.Context, er
},
}
+ ctl.newClients()
+
+ if err := ctl.waitForHealthz(ctx); err != nil {
+ return nil, errors.Wrap(err, "failed to wait for healthz")
+ }
+ authToken, err := ctl.signupAndLogin(ctx)
+ if err != nil {
+ return nil, err
+ }
+ ctl.authInterceptor.token = authToken
+
+ return ctx, nil
+}
+
+// newClients builds the controller's service clients over an auth interceptor
+// of its own. A test that logs in as another principal moves only its own
+// token, which is what lets several controllers share one server.
+func (ctl *controller) newClients() {
ctl.authInterceptor = &authInterceptor{}
interceptors := connect.WithInterceptors(ctl.authInterceptor)
- baseURL := "http://localhost:" + fmt.Sprintf("%d", port)
+ baseURL := ctl.rootURL
ctl.issueServiceClient = v1connect.NewIssueServiceClient(ctl.client, baseURL, interceptors)
ctl.rolloutServiceClient = v1connect.NewRolloutServiceClient(ctl.client, baseURL, interceptors)
ctl.planServiceClient = v1connect.NewPlanServiceClient(ctl.client, baseURL, interceptors)
@@ -312,17 +399,6 @@ func (ctl *controller) start(ctx context.Context, port int) (context.Context, er
ctl.serviceAccountServiceClient = v1connect.NewServiceAccountServiceClient(ctl.client, baseURL, interceptors)
ctl.workloadIdentityServiceClient = v1connect.NewWorkloadIdentityServiceClient(ctl.client, baseURL, interceptors)
ctl.accessGrantServiceClient = v1connect.NewAccessGrantServiceClient(ctl.client, baseURL, interceptors)
-
- if err := ctl.waitForHealthz(ctx); err != nil {
- return nil, errors.Wrap(err, "failed to wait for healthz")
- }
- authToken, err := ctl.signupAndLogin(ctx)
- if err != nil {
- return nil, err
- }
- ctl.authInterceptor.token = authToken
-
- return ctx, nil
}
func (ctl *controller) waitForHealthz(ctx context.Context) error {
@@ -365,40 +441,52 @@ func (ctl *controller) Close(ctx context.Context) error {
}
// signupAndLogin will signup and login as user [email protected].
-// addMemberToWorkspaceIAM adds a member as workspace role to the current workspace.
+// addMemberToWorkspaceIAM adds a member as workspace role to the current
+// workspace. The policy is a single row behind an etag, so two tests granting
+// at once make one of them lose; re-read and re-apply, rather than making every
+// test that grants take a workspace of its own.
func (ctl *controller) addMemberToWorkspaceIAM(ctx context.Context, workspace, member, role string) (*v1pb.IamPolicy, error) {
- policyResp, err := ctl.workspaceServiceClient.GetIamPolicy(ctx, connect.NewRequest(&v1pb.GetIamPolicyRequest{
- Resource: workspace,
- }))
- if err != nil {
- return nil, err
- }
+ var conflict error
+ for range 20 {
+ policyResp, err := ctl.workspaceServiceClient.GetIamPolicy(ctx, connect.NewRequest(&v1pb.GetIamPolicyRequest{
+ Resource: workspace,
+ }))
+ if err != nil {
+ return nil, err
+ }
- policy := policyResp.Msg
- found := false
- for _, binding := range policy.Bindings {
- if binding.Role == role {
- binding.Members = append(binding.Members, member)
- found = true
- break
+ policy := policyResp.Msg
+ found := false
+ for _, binding := range policy.Bindings {
+ if binding.Role == role {
+ binding.Members = append(binding.Members, member)
+ found = true
+ break
+ }
+ }
+ if !found {
+ policy.Bindings = append(policy.Bindings, &v1pb.Binding{
+ Role: role,
+ Members: []string{member},
+ })
}
- }
- if !found {
- policy.Bindings = append(policy.Bindings, &v1pb.Binding{
- Role: role,
- Members: []string{member},
- })
- }
- updated, err := ctl.workspaceServiceClient.SetIamPolicy(ctx, connect.NewRequest(&v1pb.SetIamPolicyRequest{
- Etag: policy.Etag,
- Policy: policy,
- Resource: workspace,
- }))
- if err != nil {
- return nil, err
+ updated, err := ctl.workspaceServiceClient.SetIamPolicy(ctx, connect.NewRequest(&v1pb.SetIamPolicyRequest{
+ Etag: policy.Etag,
+ Policy: policy,
+ Resource: workspace,
+ }))
+ if err != nil {
+ if connect.CodeOf(err) != connect.CodeAborted {
+ return nil, err
+ }
+ conflict = err
+ time.Sleep(50 * time.Millisecond)
+ continue
+ }
+ return updated.Msg, nil
}
- return updated.Msg, nil
+ return nil, conflict
}
func (ctl *controller) signupAndLogin(ctx context.Context) (string, error) {
diff --git a/backend/tests/user_test.go b/backend/tests/user_test.go
index 8a274836ef..89aec81662 100644
--- a/backend/tests/user_test.go
+++ b/backend/tests/user_test.go
@@ -21,10 +21,7 @@ func TestDeleteUser(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
expectErrorMsg := "workspace must have at least one admin"
@@ -132,10 +129,7 @@ func TestBatchGetUsers(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
first, err := ctl.userServiceClient.CreateUser(ctx, connect.NewRequest(&v1pb.CreateUserRequest{
User: &v1pb.User{Title: "first", Email: "[email protected]", Password: "1024bytebase"},
@@ -169,10 +163,7 @@ func TestSeatCountExcludesDeletedPrincipal(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
workspaceInfo := func() int32 {
actuator, err := ctl.actuatorServiceClient.GetActuatorInfo(ctx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))
@@ -221,10 +212,7 @@ func TestSeatLimitAllowsServiceAccountWhenOverLimit(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
const freeSeatLimit = 20
@@ -315,10 +303,7 @@ func TestSeatLimitGuardsUndeleteOfBoundUser(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
const freeSeatLimit = 20
@@ -394,10 +379,7 @@ func TestUpdateUserEmail(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// 1. Create a user
originalEmail := "[email protected]"
@@ -483,16 +465,16 @@ func TestUpdateUserEmail(t *testing.T) {
}))
a.NoError(err)
- pgContainer, err := provisionPgInstance(ctx, t)
- a.NoError(err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("email-update"),
Instance: &v1pb.Instance{
- Title: "email-update",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "email-update",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
a.NoError(err)
@@ -660,13 +642,10 @@ func TestGetCurrentUser_ServiceAccount(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- a.NoError(err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Create a dummy user before creating the service account.
- _, err = ctl.userServiceClient.CreateUser(ctx, connect.NewRequest(&v1pb.CreateUserRequest{
+ _, err := ctl.userServiceClient.CreateUser(ctx, connect.NewRequest(&v1pb.CreateUserRequest{
User: &v1pb.User{
Title: "dummy",
diff --git a/backend/tests/webhook_helpers_test.go b/backend/tests/webhook_helpers_test.go
index 24f8512371..f15ca2f58e 100644
--- a/backend/tests/webhook_helpers_test.go
+++ b/backend/tests/webhook_helpers_test.go
@@ -198,7 +198,7 @@ func seedFailingSheet(ctx context.Context, t *testing.T, ctl *controller, projec
// succeed.
func unblockFailingTask(t *testing.T, pgContainer *Container, dbName string) {
t.Helper()
- db, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password database=%s", pgContainer.host, pgContainer.port, dbName))
+ db, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password database=%s", pgContainer.GetHost(), pgContainer.GetPort(), dbName))
require.NoError(t, err)
defer func() {
if cerr := db.Close(); cerr != nil {
diff --git a/backend/tests/webhook_test.go b/backend/tests/webhook_test.go
index fadc453d16..25177eefa3 100644
--- a/backend/tests/webhook_test.go
+++ b/backend/tests/webhook_test.go
@@ -141,10 +141,7 @@ func TestWebhookIntegration(t *testing.T) {
// unsynchronized from every parallel test that adds a webhook.
ctx := context.Background()
- ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx)
- require.NoError(t, err)
- defer ctl.Close(ctx)
+ ctl, ctx := startWorkspace(ctx, t)
// Test webhook server.
//
@@ -167,17 +164,17 @@ func TestWebhookIntegration(t *testing.T) {
defer webhookServer.Close()
// Create a single instance for all tests
- pgContainer, err := provisionPgInstance(ctx, t)
- require.NoError(t, err)
+ pgContainer := sharedPgTarget(t)
instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{
InstanceId: generateRandomString("instance"),
Instance: &v1pb.Instance{
- Title: "test instance",
- Engine: v1pb.Engine_POSTGRES,
- Environment: new("environments/prod"),
- Activation: true,
- DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
+ SyncDatabases: &v1pb.SyncDatabases{},
+ Title: "test instance",
+ Engine: v1pb.Engine_POSTGRES,
+ Environment: new("environments/prod"),
+ Activation: true,
+ DataSources: []*v1pb.DataSource{pgContainer.adminDataSource()},
},
}))
require.NoError(t, err)
diff --git a/docs/design/backend-test-execution-time.md b/docs/design/backend-test-execution-time.md
index 00cbae1d7b..bcfb327291 100644
--- a/docs/design/backend-test-execution-time.md
+++ b/docs/design/backend-test-execution-time.md
@@ -146,6 +146,46 @@ outgoing connections, and twenty servers' clients hold many of those. It shows
in none of the CI logs checked, but it scales with throughput; a runner that
starts seeing it should move one of the two ranges apart.
+### Third pass: 130 s, and every container in one place
+
+2026-09-12, same box, the command CI runs with `-count=1`: **130 s**, walls
+summing to 469 s and overlapping 3.6×, `backend/tests` 118 s of it.
+
+Every engine container now starts inside `backend/common/testcontainer` and is
+shared per package — `plugin/db/mongodb` 12.9 s to 6.3 s, three containers to
+one; `plugin/db/tidb` 5.1 s to 4.4 s; the pg17, TLS-Postgres and sample targets
+onto the same machinery. The Oracle container, unused since #21356, is deleted,
+and the per-test `GetTest*Container` helpers with it. Effort 4 then took the run
+to 120 s, the package to 107 s.
+
+**Then the target Postgres went the same way.** `backend/tests` was starting 162
+of them, one per test, because a container it registers as an instance is a
+whole server whose databases the syncer enumerates. `Instance.sync_databases` is
+what lets one server carry many instances — the syncer imports only the
+databases the list names, which is how Sample Project Instance already shares
+one target across workspaces — so a test now takes a database of its own on one
+shared target and the instance it registers names that database.
+**173 Postgres containers across the run became 31** and the run 1 m 56 s became
+**1 m 39 s**. Seventeen provisions stay private: the tests that create a
+cluster-wide role, the two that need separate servers to hold the same database
+name, and the one whose subject is two instances discovering the same physical
+`postgres` database. One server for twenty parallel tests also needs a raised
+`max_connections`; at the default 100 the failure reads "server refused TLS
+connection", which names neither the limit nor the cause.
+
+The two share a package, so they were measured as a 2×2 rather than in sequence
+— standalone `backend/tests`, one box, the same code with one toggle each:
+
+| | a server per test | one server |
+| --- | ---: | ---: |
+| **a container per test** | 87.9 s | 76.0 s |
+| **one target container** | 77.4 s | **55.3 s** |
+
+Each is worth about 11 s alone (effort 4 −11.9, the shared target −10.5) and
+32.6 s together, so the pair beats the sum of its parts by 10 s. Neither cost is
+the container or the boot itself: it is that a test spends that second holding a
+parallel slot, and removing one of the two just moves the queue to the other.
+
### The floor
`backend/plugin/...` cost 866 s across 69 packages at the start, and split by
@@ -193,11 +233,11 @@ The standing order, by what is left rather than by number:
| | Effort | Worth |
| --- | --- | --- |
-| 1 | 7, engine conformance to omni | 164 s, behind a large ownership blocker |
-| 2 | 6, seams instead of containers | seconds; do it for the tests, not the clock |
-| 3 | 4, isolate per project | a memory ceiling, not a speed play |
+| 1 | 6, seams instead of containers | seconds; do it for the tests, not the clock |
+| 2 | 7, engine conformance to omni | 14 s; do it for ownership, not the clock |
-Two entries have left this list. Effort 5 is done — and was worth less than the
+Three entries have left this list. Effort 4 is done, and beat its own
+re-sizing; see its section. Effort 5 is done — and was worth less than the
~320 s it was sized at, because a third of that number was
`TestSQLReviewForMySQL`, which was not the duplicate the sizing assumed; see its
section. `TestWebhookIntegration` was second at 148 s until #21355 fixed the
@@ -365,48 +405,30 @@ paragraph below described the state before that landed. At 148 s it was most of
173 s, and the rest of the package finishes around it. It is the one place left
in `backend/tests` where a single test is worth attacking on its own.
-### 4. Isolate per project, not per workspace
-
-**Re-sized: about 7 s of wall, not 362 s.** The 362 s below is real work — 207
-boots at 703 ms up and, after effort 1, ~1 ms down — but effort 2's step 2 made
-it overlap. Spread across twenty workers it is worth single-digit seconds of
-wall clock, and the run is no longer bound by this package anyway.
-
-What survives is a resource argument, not a speed one: twenty concurrent servers
-is what sets the memory ceiling on how far `-parallel` can go, and one server for
-the whole package would lift it. Do this when a runner cannot take the
-concurrency, not to buy time. The original sizing follows.
-
-**362 s of fixture time inside `backend/tests`.**
-
-Every test there gets its own server, and therefore its own workspace: a
-database, a full `LATEST.sql` migration, signup, login, a license upload, two
-rollout policies and a project. That is 703 ms up and 1047 ms down, 207 times
-over, almost all of it rebuilding scaffolding no test asserts on.
-
-Project is already Bytebase's tenancy boundary, so make it the test boundary:
-one server for the whole package, one shared workspace, and a project per test —
-plus its own principal where the test needs one. The collision tests already prove this
-works. `setupCollidingProjects` puts two projects in one workspace and asserts
-neither can see the other.
-
-**Workspace-scoped singletons are the exception.** These files call
-`UpdateSetting` on rows that exist once per workspace — the MCP capability and
-masking toggles, workspace approval, maximum request expiration, email, AI. Two
-parallel tests toggling one would race, so they keep a workspace of their own:
-
-```
-approval_test.go mcp_capability_setting_test.go mcp_masking_test.go
-login_audit_test.go review_run_lifecycle_test.go sensitive_data_test.go
-mcp_forbidden_credential_mints_test.go maximum_request_expiration_test.go
-webhook_test.go (via webhook_helpers_test.go)
-```
-
-Workspace IAM grants are additive, so `addMemberToWorkspaceIAM` is safe provided
-each test grants to its own principal.
-
-`TestWebhookIntegration` needs separate attention: 144 s, the slowest test in
-the repo, 24 subtests behind one boot with several fixed sleeps.
+### [✓] 4. Isolate per project, not per workspace
+
+**237 server boots down to 109, `backend/tests` 83–90 s to 71–77 s standalone,
+its summed test time 1954 s to 1765 s.** Project is Bytebase's tenancy boundary,
+so it is the test boundary: `startProject` takes a project on the package's one
+server (113 call sites), `startWorkspace` takes a server, and so a workspace, of
+its own (102).
+
+What forces a workspace is wider than the settings files this section first
+listed, and every item came back as a failing test rather than a prediction: the
+MCP capability ceiling and the license, the demo principal's password and MFA,
+**removals** from the workspace IAM policy — grants are additive, but
+`SetIamPolicy` writes the whole policy, so a grant that read before a removal and
+wrote after resurrects the binding — any workspace-wide list or count assertion,
+and workspace-scoped resource IDs, where instances collided on
+`generateRandomString("inst")[:8]`, a prefix plus three hex characters. One
+conflict was worth fixing in the helper instead: `addMemberToWorkspaceIAM`
+re-reads and re-applies on the etag conflict the server tells it to retry, which
+keeps all sixteen granting files on the shared server.
+
+The re-sizing to ~7 s of wall held for the run and understated the package,
+because the boots also set the memory ceiling on `-parallel`. What is left is the
+floor: `TestWebhookIntegration`, 48.7 s of a 73 s package, 24 sequential subtests
+behind one boot.
### [✓] 5. Postgres only for `backend/store` and `backend/tests`
@@ -623,20 +645,23 @@ the 27 `TestCollision*` and `TestClaim*` tests where they are —
### 7. Engine conformance to omni
-**Re-sized: 164 s of package wall time, no longer the largest change here.**
-#21344 replaced Oracle's migration round-trip with recorded goldens and #21351
-gave each engine package one shared container, taking `plugin/schema/*` from
-475 s to 164 s and Oracle from 315 s to 78 s. The ownership argument is
-unaffected — these tests still assert engine fidelity, which is omni's — but the
-port now buys 164 s, not 475 s, against the same large blocker described below.
+**Not done, and re-sized again: 14 s, so the clock argument is spent.** #21415
+took the testcontainer out of `plugin/schema` and #21429 moved every engine's
+catalog load onto omni's `LoadMetadata`, so the seven `plugin/schema/*` packages
+cost **14.2 s between them**, oracle **0.05 s** against the 315 s it opened this
+design at. Most of the rest is the ~3 s a package pays to start its test binary.
+`plugin/db/*` is 100 s and stays — it is our driver — with containers in four
+packages, each sharing one.
+
+Port it when omni owns the metadata model, because these tests assert engine
+fidelity and that is omni's to assert. Not to make the suite faster.
The blocker is that the round-trip is expressed in Bytebase's metadata proto
(`storepb`, `plugin/schema`, `store/model`), so omni must own the metadata model
first. The ownership split above is the plan: all of `plugin/schema/*` moves,
all of `plugin/db/*` stays, because that is our driver. The figures in the rest
-of this section — 475 s, 315 s for Oracle, 339 s for `plugin/db/*` — are the
-pre-#21351 ones the analysis was done against; the split they describe is
-unchanged, the totals are now 164 s and 145 s.
+of this section are the ones the analysis was done against; the split they
+describe is unchanged, the totals are in the opening above.
For most engines the metadata model is not the near blocker — omni has no engine
to move to. It ships a parser and AST for Oracle and for MSSQL and nothing more,
@@ -669,11 +694,12 @@ round-trip. That is engine fidelity, which belongs in omni, paid for on every PR
by every engineer. The two tests and the `GetStarRocksContainer` helpers were
deleted; the package now runs in under 2 s on its remaining unit tests.
-### One loose end
+### [✓] One loose end
-`action/**` is in the workflow's `paths` filter, but `go test ./backend/...`
-never runs the seven test files under `action/`. Add `./action/...` to the
-command. They are unit tests and cost about 2 s.
+`action/**` was in the workflow's `paths` filter while the test command never
+ran the seven test files under `action/`, so a PR touching only the action CLI
+ran the whole backend suite and none of its own tests. `./action/...` is now on
+the command: 48 tests, 0.15 s, in the tail behind `backend/tests`.
## Reproducing
@@ -692,5 +718,5 @@ subtest time inside a 315 s package.
Per-operation costs came from a temporary test in `backend/tests` that timed
`StartServerWithExternalPg` phase by phase, `GetPgContainer` and
-`getMySQLContainer` in a loop, plus four `slog` probes inside `server.Shutdown`.
+`provisionMySQLInstance` in a loop, plus four `slog` probes inside `server.Shutdown`.
All reverted.
diff --git a/go.mod b/go.mod
index 0bb16e5fe8..fc84eab6f3 100644
--- a/go.mod
+++ b/go.mod
@@ -66,7 +66,6 @@ require (
github.com/lib/pq v1.12.3
github.com/lor00x/goldap v0.0.0-20240304151906-8d785c64d1c8
github.com/microsoft/go-mssqldb v1.11.0
- github.com/moby/moby/api v1.55.0
github.com/modelcontextprotocol/go-sdk v1.7.0
github.com/nyaruka/phonenumbers/v2 v2.0.5
github.com/opensearch-project/opensearch-go/v4 v4.7.2
@@ -210,6 +209,7 @@ require (
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/go-archive v0.3.0 // indirect
+ github.com/moby/moby/api v1.55.0 // indirect
github.com/moby/moby/client v0.5.0 // indirect
github.com/moby/patternmatcher v0.6.1 // indirect
github.com/moby/sys/sequential v0.7.0 // indirect