Skip to content

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

chore: no embedded (#15542)

Danny Xudc46cc8fbafd
diff --git a/backend/resources/postgres/test_instance.go b/backend/resources/postgres/test_instance.go
deleted file mode 100644
index 43edf66de0..0000000000
--- a/backend/resources/postgres/test_instance.go
+++ /dev/null
@@ -1,60 +0,0 @@
-package postgres
-
-import (
- "fmt"
- "os"
- "os/exec"
- "path/filepath"
-
- "github.com/pkg/errors"
-
- "github.com/bytebase/bytebase/backend/common"
-)
-
-const (
- // Shared external PG server variables.
- TestPgUser = "root"
-)
-
-// SetupTestInstance installs and starts a postgresql instance for testing,
-// returns the stop function.
-func SetupTestInstance(pgBinDir, pgDataDir string, port int) func() {
- if err := initDB(pgBinDir, pgDataDir, TestPgUser); err != nil {
- panic(err)
- }
- if err := startForTest(port, pgBinDir, pgDataDir); err != nil {
- panic(err)
- }
-
- stopFn := func() {
- if err := stop(pgBinDir, pgDataDir); err != nil {
- panic(err)
- }
- if err := os.RemoveAll(pgDataDir); err != nil {
- panic(err)
- }
- }
-
- return stopFn
-}
-
-// startForTest starts a postgres instance on localhost given port, outputs to stdout and stderr.
-// If port is 0, then it will choose a random unused port.
-func startForTest(port int, pgBinDir, pgDataDir string) (err error) {
- pgbin := filepath.Join(pgBinDir, "pg_ctl")
-
- // See -p -k -h option definitions in the link below.
- // https://www.postgresql.org/docs/current/app-postgres.html
- p := exec.Command(pgbin, "start", "-w",
- "-D", pgDataDir,
- "-o", fmt.Sprintf(`-p %d -k %s`, port, common.GetPostgresSocketDir()))
-
- // Suppress log spam
- p.Stdout = nil
- p.Stderr = os.Stderr
- if err := p.Run(); err != nil {
- return errors.Wrapf(err, "failed to start postgres %q", p.String())
- }
-
- return nil
-}
diff --git a/backend/tests/change_history_test.go b/backend/tests/change_history_test.go
index e0f6f418be..68fce3c658 100644
--- a/backend/tests/change_history_test.go
+++ b/backend/tests/change_history_test.go
@@ -53,10 +53,7 @@ func TestFilterChangeHistoryByResources(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/data_source_test.go b/backend/tests/data_source_test.go
index 60323b51fb..aa4353a245 100644
--- a/backend/tests/data_source_test.go
+++ b/backend/tests/data_source_test.go
@@ -17,10 +17,7 @@ func TestDataSource(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
@@ -133,10 +130,7 @@ func TestExternalSecretManager(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/environment_test.go b/backend/tests/environment_test.go
index 6ee8f9b9e5..96b5421fed 100644
--- a/backend/tests/environment_test.go
+++ b/backend/tests/environment_test.go
@@ -15,10 +15,7 @@ func TestDatabaseEnvironment(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/ghost_test.go b/backend/tests/ghost_test.go
index 6f52fff486..fcfaa367be 100644
--- a/backend/tests/ghost_test.go
+++ b/backend/tests/ghost_test.go
@@ -39,10 +39,7 @@ func TestGhostSchemaUpdate(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/grouping_change_test.go b/backend/tests/grouping_change_test.go
index 951b91a832..0cea541604 100644
--- a/backend/tests/grouping_change_test.go
+++ b/backend/tests/grouping_change_test.go
@@ -127,9 +127,7 @@ func TestCreateDatabaseGroup(t *testing.T) {
t.Parallel()
ctl := &controller{}
ctx := context.Background()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: t.TempDir(),
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer func() {
_ = ctl.Close(ctx)
diff --git a/backend/tests/main_test.go b/backend/tests/main_test.go
new file mode 100644
index 0000000000..05fc22eeda
--- /dev/null
+++ b/backend/tests/main_test.go
@@ -0,0 +1,44 @@
+package tests
+
+import (
+ "context"
+ "log"
+ "os"
+ "testing"
+
+ "github.com/bytebase/bytebase/backend/resources/mongoutil"
+
+ "github.com/bytebase/bytebase/backend/resources/postgres"
+)
+
+func TestMain(m *testing.M) {
+ ctx := context.Background()
+ code, err := startMain(ctx, m)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ os.Exit(code)
+}
+
+func startMain(ctx context.Context, m *testing.M) (int, error) {
+ resourceDir = os.TempDir()
+ if _, err := postgres.Install(resourceDir); err != nil {
+ return 0, err
+ }
+ if _, err := mongoutil.Install(resourceDir); err != nil {
+ return 0, err
+ }
+
+ pgContainer, err := getPgContainer(ctx)
+ defer func() {
+ pgContainer.Close(ctx)
+ }()
+ if err != nil {
+ return 0, err
+ }
+ externalPgHost = pgContainer.host
+ externalPgPort = pgContainer.port
+
+ return m.Run(), nil
+}
diff --git a/backend/tests/project_test.go b/backend/tests/project_test.go
index 111c385d21..93eb4a2f56 100644
--- a/backend/tests/project_test.go
+++ b/backend/tests/project_test.go
@@ -14,10 +14,7 @@ func TestArchiveProject(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/schema_update_test.go b/backend/tests/schema_update_test.go
index 98ecd72674..3ee4e73b2b 100644
--- a/backend/tests/schema_update_test.go
+++ b/backend/tests/schema_update_test.go
@@ -20,10 +20,7 @@ func TestSchemaAndDataUpdate(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
@@ -252,9 +249,7 @@ CREATE TABLE "public"."book" (
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: t.TempDir(),
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer func() {
_ = ctl.Close(ctx)
@@ -361,10 +356,7 @@ func TestMarkTaskAsDone(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/sensitive_data_test.go b/backend/tests/sensitive_data_test.go
index 296e4261fb..ec02396bd0 100644
--- a/backend/tests/sensitive_data_test.go
+++ b/backend/tests/sensitive_data_test.go
@@ -99,10 +99,7 @@ func TestSensitiveData(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/sql_editor_test.go b/backend/tests/sql_editor_test.go
index a6aaef67ab..71145458f4 100644
--- a/backend/tests/sql_editor_test.go
+++ b/backend/tests/sql_editor_test.go
@@ -126,10 +126,7 @@ func TestAdminQueryAffectedRows(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/sql_export_test.go b/backend/tests/sql_export_test.go
index 53a4894332..3297988e9e 100644
--- a/backend/tests/sql_export_test.go
+++ b/backend/tests/sql_export_test.go
@@ -113,10 +113,7 @@ func TestSQLExport(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/sql_review_test.go b/backend/tests/sql_review_test.go
index 90173d2752..ce2e2551da 100644
--- a/backend/tests/sql_review_test.go
+++ b/backend/tests/sql_review_test.go
@@ -78,12 +78,9 @@ func TestSQLReviewForPostgreSQL(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
tests, err := readTestData(filepath)
a.NoError(err)
- ctx, err = ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err = ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
@@ -252,10 +249,7 @@ func TestSQLReviewForMySQL(t *testing.T) {
ctl := &controller{}
tests, err := readTestData(filepath)
a.NoError(err)
- dataDir := t.TempDir()
- ctx, err = ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err = ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/start_test.go b/backend/tests/start_test.go
deleted file mode 100644
index 245ebe03ce..0000000000
--- a/backend/tests/start_test.go
+++ /dev/null
@@ -1,78 +0,0 @@
-package tests
-
-import (
- "context"
- "log"
- "os"
- "testing"
-
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/resources/mongoutil"
-
- "github.com/bytebase/bytebase/backend/resources/postgres"
- v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
-)
-
-func startStopServer(ctx context.Context, a *require.Assertions, ctl *controller, dataDir string) {
- ctx, err := ctl.StartServer(ctx, &config{
- dataDir: dataDir,
- })
- a.NoError(err)
-
- resp, err := ctl.projectServiceClient.ListProjects(ctx, &v1pb.ListProjectsRequest{})
- a.NoError(err)
- projects := resp.Projects
-
- // Default.
- a.Equal(1, len(projects))
- a.Equal("Default", projects[0].Title)
-
- err = ctl.Close(ctx)
- a.NoError(err)
-}
-
-func TestServerRestart(t *testing.T) {
- t.Parallel()
- a := require.New(t)
- ctx := context.Background()
- ctl := &controller{}
- dataDir := t.TempDir()
- // Start server in non-readonly mode to init schema and register user.
- ctx, err := ctl.StartServer(ctx, &config{
- dataDir: dataDir,
- })
- a.NoError(err)
- err = ctl.Close(ctx)
- a.NoError(err)
-
- // Start server in readonly mode.
- startStopServer(ctx, a, ctl, dataDir)
-
- // Start server in non-readonly mode.
- startStopServer(ctx, a, ctl, dataDir)
-}
-
-func TestMain(m *testing.M) {
- resourceDir = os.TempDir()
- dir, err := postgres.Install(resourceDir)
- if err != nil {
- log.Fatal(err)
- }
- pgBinDir = dir
- if _, err := mongoutil.Install(resourceDir); err != nil {
- log.Fatal(err)
- }
-
- dir, err = os.MkdirTemp("", "bbtest-pgdata")
- if err != nil {
- log.Fatal(err)
- }
- stopInstance := postgres.SetupTestInstance(pgBinDir, dir, externalPgPort)
-
- code := m.Run()
-
- stopInstance()
-
- os.Exit(code)
-}
diff --git a/backend/tests/subscription_test.go b/backend/tests/subscription_test.go
index c3b259f4eb..4385db1797 100644
--- a/backend/tests/subscription_test.go
+++ b/backend/tests/subscription_test.go
@@ -13,10 +13,7 @@ func TestSubscription(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/sync_schema_test.go b/backend/tests/sync_schema_test.go
index d1144d408e..f11f03011a 100644
--- a/backend/tests/sync_schema_test.go
+++ b/backend/tests/sync_schema_test.go
@@ -40,10 +40,7 @@ DROP SCHEMA "schema_a";
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/syncer_test.go b/backend/tests/syncer_test.go
index a6c6a73417..47347073d7 100644
--- a/backend/tests/syncer_test.go
+++ b/backend/tests/syncer_test.go
@@ -178,10 +178,7 @@ func TestSyncerForPostgreSQL(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
@@ -461,10 +458,7 @@ func TestSyncerForMySQL(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/tenant_test.go b/backend/tests/tenant_test.go
index 9640b1007f..c0ac6083d1 100644
--- a/backend/tests/tenant_test.go
+++ b/backend/tests/tenant_test.go
@@ -26,10 +26,7 @@ func TestDatabaseGroup(t *testing.T) {
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- dataDir := t.TempDir()
- ctx, err := ctl.StartServerWithExternalPg(ctx, &config{
- dataDir: dataDir,
- })
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/backend/tests/tests.go b/backend/tests/tests.go
index 320f558706..038242e410 100644
--- a/backend/tests/tests.go
+++ b/backend/tests/tests.go
@@ -31,7 +31,6 @@ import (
"github.com/bytebase/bytebase/backend/common"
component "github.com/bytebase/bytebase/backend/component/config"
api "github.com/bytebase/bytebase/backend/legacyapi"
- "github.com/bytebase/bytebase/backend/resources/postgres"
"github.com/bytebase/bytebase/backend/server"
)
@@ -89,22 +88,17 @@ type controller struct {
v1APIURL string
}
-type config struct {
- dataDir string
- readOnly bool
-}
-
var (
mu sync.Mutex
nextPort = time.Now().Second()*200 + 5010
- externalPgPort = time.Now().Second()*200 + 5000
+ externalPgHost string
+ externalPgPort string
nextDatabaseNumber = 20210113
// resourceDir is the shared resource directory.
resourceDir string
- pgBinDir string
)
func getTestPort() int {
@@ -115,14 +109,6 @@ func getTestPort() int {
return p
}
-func getTestPortForEmbeddedPg() int {
- mu.Lock()
- defer mu.Unlock()
- p := nextPort
- nextPort += 3
- return p
-}
-
// getTestDatabaseString returns a unique database name in external pg database server.
func getTestDatabaseString() string {
mu.Lock()
@@ -133,8 +119,8 @@ func getTestDatabaseString() string {
}
// StartServerWithExternalPg starts the main server with external Postgres.
-func (ctl *controller) StartServerWithExternalPg(ctx context.Context, config *config) (context.Context, error) {
- pgMainURL := fmt.Sprintf("postgresql://%s@:%d/%s?host=%s", postgres.TestPgUser, externalPgPort, "postgres", common.GetPostgresSocketDir())
+func (ctl *controller) StartServerWithExternalPg(ctx context.Context) (context.Context, error) {
+ pgMainURL := fmt.Sprintf("postgresql://postgres:root-password@%s:%s/postgres", externalPgHost, externalPgPort)
db, err := sql.Open("pgx", pgMainURL)
if err != nil {
return nil, err
@@ -145,9 +131,9 @@ func (ctl *controller) StartServerWithExternalPg(ctx context.Context, config *co
return nil, err
}
- pgURL := fmt.Sprintf("postgresql://%s@:%d/%s?host=%s", postgres.TestPgUser, externalPgPort, databaseName, common.GetPostgresSocketDir())
+ pgURL := fmt.Sprintf("postgresql://postgres:root-password@%s:%s/%s", externalPgHost, externalPgPort, databaseName)
serverPort := getTestPort()
- profile := getTestProfileWithExternalPg(config.dataDir, resourceDir, serverPort, postgres.TestPgUser, pgURL)
+ profile := getTestProfileWithExternalPg(resourceDir, serverPort, pgURL)
server, err := server.NewServer(ctx, profile)
if err != nil {
return nil, err
@@ -196,20 +182,6 @@ func (ctl *controller) StartServerWithExternalPg(ctx context.Context, config *co
return metaCtx, nil
}
-// StartServer starts the main server with embed Postgres.
-func (ctl *controller) StartServer(ctx context.Context, config *config) (context.Context, error) {
- serverPort := getTestPortForEmbeddedPg()
- profile := getTestProfile(config.dataDir, resourceDir, serverPort, config.readOnly)
- server, err := server.NewServer(ctx, profile)
- if err != nil {
- return nil, err
- }
- ctl.server = server
- ctl.profile = profile
-
- return ctl.start(ctx, serverPort)
-}
-
func (ctl *controller) initWorkspaceProfile(ctx context.Context) error {
_, err := ctl.settingServiceClient.UpdateSetting(ctx, &v1pb.UpdateSettingRequest{
AllowMissing: true,
@@ -234,35 +206,15 @@ func (ctl *controller) initWorkspaceProfile(ctx context.Context) error {
return err
}
-// GetTestProfile will return a profile for testing.
-// We require port as an argument of GetTestProfile so that test can run in parallel in different ports.
-func getTestProfile(dataDir, resourceDir string, port int, readOnly bool) *component.Profile {
- return &component.Profile{
- Mode: common.ReleaseModeDev,
- ExternalURL: fmt.Sprintf("http://localhost:%d", port),
- Port: port,
- DatastorePort: port + 2,
- SampleDatabasePort: 0,
- PgUser: "bbtest",
- Readonly: readOnly,
- DataDir: dataDir,
- ResourceDir: resourceDir,
- AppRunnerInterval: 1 * time.Second,
- BackupRunnerInterval: 10 * time.Second,
- }
-}
-
// GetTestProfileWithExternalPg will return a profile for testing with external Postgres.
// We require port as an argument of GetTestProfile so that test can run in parallel in different ports,
// pgURL for connect to Postgres.
-func getTestProfileWithExternalPg(dataDir, resourceDir string, port int, pgUser string, pgURL string) *component.Profile {
+func getTestProfileWithExternalPg(resourceDir string, port int, pgURL string) *component.Profile {
return &component.Profile{
Mode: common.ReleaseModeDev,
ExternalURL: fmt.Sprintf("http://localhost:%d", port),
Port: port,
SampleDatabasePort: 0,
- PgUser: pgUser,
- DataDir: dataDir,
ResourceDir: resourceDir,
AppRunnerInterval: 1 * time.Second,
BackupRunnerInterval: 10 * time.Second,