Skip to content

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

refactor: enable parallel test so that testing can scale (#550)

Danny Xua8b914b16bda
diff --git a/bin/server/cmd/profile_dev.go b/bin/server/cmd/profile_dev.go
index 9e71e9e07a..b9f0fe185f 100644
--- a/bin/server/cmd/profile_dev.go
+++ b/bin/server/cmd/profile_dev.go
@@ -25,10 +25,10 @@ func activeProfile(dataDir string, port int, isDemo bool) Profile {
}
// GetTestProfile will return a profile for testing.
-func GetTestProfile(dataDir string) Profile {
+func GetTestProfile(dataDir string, port int) Profile {
return Profile{
mode: "dev",
- port: 1234,
+ port: port,
dataDir: dataDir,
dsn: fmt.Sprintf("file:%s/bytebase_test.db", dataDir),
seedDir: "seed/test",
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 46e5bb1fa4..aff300cf54 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -23,10 +23,11 @@ var (
)
func TestSchemaUpdate(t *testing.T) {
+ t.Parallel()
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- if err := ctl.StartMain(ctx, dataDir); err != nil {
+ if err := ctl.StartMain(ctx, dataDir, getTestPort(t.Name())); err != nil {
t.Fatal(err)
}
defer ctl.Close()
@@ -160,10 +161,11 @@ func TestSchemaUpdate(t *testing.T) {
}
func TestVCSSchemaUpdate(t *testing.T) {
+ t.Parallel()
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- if err := ctl.StartMain(ctx, dataDir); err != nil {
+ if err := ctl.StartMain(ctx, dataDir, getTestPort(t.Name())); err != nil {
t.Fatal(err)
}
defer ctl.Close()
@@ -180,8 +182,8 @@ func TestVCSSchemaUpdate(t *testing.T) {
vcs, err := ctl.createVCS(api.VCSCreate{
Name: "TestVCS",
Type: vcs.GitLabSelfHost,
- InstanceURL: gitURL,
- APIURL: gitAPIURL,
+ InstanceURL: ctl.gitURL,
+ APIURL: ctl.gitAPIURL,
ApplicationID: applicationID,
Secret: applicationSecret,
})
@@ -211,7 +213,7 @@ func TestVCSSchemaUpdate(t *testing.T) {
ProjectID: project.ID,
Name: "Test Repository",
FullPath: repositoryPath,
- WebURL: fmt.Sprintf("%s/%s", gitURL, repositoryPath),
+ WebURL: fmt.Sprintf("%s/%s", ctl.gitURL, repositoryPath),
BranchFilter: "feature/foo",
BaseDirectory: "bbtest",
FilePathTemplate: "{{ENV_NAME}}/{{DB_NAME}}__{{VERSION}}__{{TYPE}}__{{DESCRIPTION}}.sql",
diff --git a/tests/start_test.go b/tests/start_test.go
index e3a53e347b..b70969f2a4 100644
--- a/tests/start_test.go
+++ b/tests/start_test.go
@@ -6,10 +6,11 @@ import (
)
func TestServiceStart(t *testing.T) {
+ t.Parallel()
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- if err := ctl.StartMain(ctx, dataDir); err != nil {
+ if err := ctl.StartMain(ctx, dataDir, getTestPort(t.Name())); err != nil {
t.Fatal(err)
}
defer ctl.Close()
diff --git a/tests/tenant_test.go b/tests/tenant_test.go
index 7b2d7f922a..f6b3b28b69 100644
--- a/tests/tenant_test.go
+++ b/tests/tenant_test.go
@@ -21,10 +21,11 @@ var (
)
func TestTenant(t *testing.T) {
+ t.Parallel()
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- if err := ctl.StartMain(ctx, dataDir); err != nil {
+ if err := ctl.StartMain(ctx, dataDir, getTestPort(t.Name())); err != nil {
t.Fatal(err)
}
defer ctl.Close()
@@ -226,10 +227,11 @@ func TestTenant(t *testing.T) {
}
func TestTenantVCS(t *testing.T) {
+ t.Parallel()
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- if err := ctl.StartMain(ctx, dataDir); err != nil {
+ if err := ctl.StartMain(ctx, dataDir, getTestPort(t.Name())); err != nil {
t.Fatal(err)
}
defer ctl.Close()
@@ -246,8 +248,8 @@ func TestTenantVCS(t *testing.T) {
vcs, err := ctl.createVCS(api.VCSCreate{
Name: "TestVCS",
Type: vcs.GitLabSelfHost,
- InstanceURL: gitURL,
- APIURL: gitAPIURL,
+ InstanceURL: ctl.gitURL,
+ APIURL: ctl.gitAPIURL,
ApplicationID: applicationID,
Secret: applicationSecret,
})
@@ -278,7 +280,7 @@ func TestTenantVCS(t *testing.T) {
ProjectID: project.ID,
Name: "Test Repository",
FullPath: repositoryPath,
- WebURL: fmt.Sprintf("%s/%s", gitURL, repositoryPath),
+ WebURL: fmt.Sprintf("%s/%s", ctl.gitURL, repositoryPath),
BranchFilter: "feature/foo",
BaseDirectory: "bbtest",
FilePathTemplate: "{{DB_NAME}}__{{VERSION}}__{{TYPE}}__{{DESCRIPTION}}.sql",
@@ -482,10 +484,11 @@ func TestTenantVCS(t *testing.T) {
}
func TestTenantDatabaseNameTemplate(t *testing.T) {
+ t.Parallel()
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- if err := ctl.StartMain(ctx, dataDir); err != nil {
+ if err := ctl.StartMain(ctx, dataDir, getTestPort(t.Name())); err != nil {
t.Fatal(err)
}
defer ctl.Close()
diff --git a/tests/tests.go b/tests/tests.go
index ec81c49f84..9eeacdb48f 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -26,14 +26,6 @@ import (
//go:embed fake
var fakeFS embed.FS
-var (
- port = 1234
- rootURL = fmt.Sprintf("http://localhost:%d/api", port)
- gitPort = 1235
- gitURL = fmt.Sprintf("http://localhost:%d", gitPort)
- gitAPIURL = fmt.Sprintf("%s/api/v4", gitURL)
-)
-
var (
deploymentSchdule = api.DeploymentSchedule{
Deployments: []*api.Deployment{
@@ -78,23 +70,51 @@ var (
)
type controller struct {
- main *cmd.Main
- client *http.Client
- cookie string
- gitlab *fake.GitLab
+ main *cmd.Main
+ client *http.Client
+ cookie string
+ gitlab *fake.GitLab
+ rootURL string
+ gitURL string
+ gitAPIURL string
+}
+
+func getTestPort(testName string) int {
+ // The port should be incremented by 2. One port for bytebase server, and the other for gitlab server.
+ switch testName {
+ case "TestServiceStart":
+ return 1234
+ case "TestSchemaUpdate":
+ return 1236
+ case "TestVCSSchemaUpdate":
+ return 1238
+ case "TestTenant":
+ return 1240
+ case "TestTenantVCS":
+ return 1242
+ case "TestTenantDatabaseNameTemplate":
+ return 1244
+ }
+ panic(fmt.Sprintf("test %q doesn't have assigned port, please set it in getTestPort()", testName))
}
// StartMain starts the main server.
-func (ctl *controller) StartMain(ctx context.Context, dataDir string) error {
+func (ctl *controller) StartMain(ctx context.Context, dataDir string, port int) error {
// start main server.
logger, err := cmd.GetLogger()
if err != nil {
return fmt.Errorf("failed to get logger, error: %w", err)
}
defer logger.Sync()
- profile := cmd.GetTestProfile(dataDir)
+ profile := cmd.GetTestProfile(dataDir, port)
ctl.main = cmd.NewMain(profile, logger)
- ctl.gitlab = fake.NewGitLab(gitPort)
+ ctl.rootURL = fmt.Sprintf("http://localhost:%d/api", port)
+
+ // set up gitlab.
+ gitlabPort := port + 1
+ ctl.gitlab = fake.NewGitLab(gitlabPort)
+ ctl.gitURL = fmt.Sprintf("http://localhost:%d", gitlabPort)
+ ctl.gitAPIURL = fmt.Sprintf("%s/api/v4", ctl.gitURL)
errChan := make(chan error, 1)
go func() {
@@ -187,7 +207,7 @@ func (ctl *controller) Close() error {
// Login will login as user [email protected] and caches its cookie.
func (ctl *controller) Login() error {
resp, err := ctl.client.Post(
- fmt.Sprintf("%s/auth/login/BYTEBASE", rootURL),
+ fmt.Sprintf("%s/auth/login/BYTEBASE", ctl.rootURL),
"",
strings.NewReader(`{"data":{"type":"loginInfo","attributes":{"email":"[email protected]","password":"1024"}}}`))
if err != nil {
@@ -223,7 +243,7 @@ func (ctl *controller) provisionSQLiteInstance(rootDir, name string) (string, er
// get sends a GET client request.
func (ctl *controller) get(shortURL string, params map[string]string) (io.ReadCloser, error) {
- gURL := fmt.Sprintf("%s%s", rootURL, shortURL)
+ gURL := fmt.Sprintf("%s%s", ctl.rootURL, shortURL)
req, err := http.NewRequest("GET", gURL, nil)
if err != nil {
return nil, fmt.Errorf("fail to create a new GET request(%q), error: %w", gURL, err)
@@ -250,7 +270,7 @@ func (ctl *controller) get(shortURL string, params map[string]string) (io.ReadCl
// post sends a POST client request.
func (ctl *controller) post(shortURL string, body io.Reader) (io.ReadCloser, error) {
- url := fmt.Sprintf("%s%s", rootURL, shortURL)
+ url := fmt.Sprintf("%s%s", ctl.rootURL, shortURL)
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, fmt.Errorf("fail to create a new POST request(%q), error: %w", url, err)
@@ -272,7 +292,7 @@ func (ctl *controller) post(shortURL string, body io.Reader) (io.ReadCloser, err
// patch sends a PATCH client request.
func (ctl *controller) patch(shortURL string, body io.Reader) (io.ReadCloser, error) {
- url := fmt.Sprintf("%s%s", rootURL, shortURL)
+ url := fmt.Sprintf("%s%s", ctl.rootURL, shortURL)
req, err := http.NewRequest("PATCH", url, body)
if err != nil {
return nil, fmt.Errorf("fail to create a new PATCH request(%q), error: %w", url, err)