Skip to content

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

refactor(tests): make `controller` accept a constructor for creating VCS provider (#2051)

Joe Chen3d5756715910
diff --git a/tests/backup_restore_test.go b/tests/backup_restore_test.go
index 0241257afe..c90ee60157 100644
--- a/tests/backup_restore_test.go
+++ b/tests/backup_restore_test.go
@@ -19,6 +19,8 @@ import (
"github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/db"
resourcemysql "github.com/bytebase/bytebase/resources/mysql"
+ "github.com/bytebase/bytebase/tests/fake"
+
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
@@ -117,7 +119,7 @@ func TestPITR(t *testing.T) {
port := getTestPort(t.Name())
ctl := &controller{}
dataDir := t.TempDir()
- err := ctl.StartServer(ctx, dataDir, port)
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, port)
a.NoError(err)
defer ctl.Close(ctx)
err = ctl.Login()
diff --git a/tests/external_pg_test.go b/tests/external_pg_test.go
index bb20af6fa8..b968d7f95f 100644
--- a/tests/external_pg_test.go
+++ b/tests/external_pg_test.go
@@ -9,6 +9,8 @@ import (
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/resources/postgres"
+ "github.com/bytebase/bytebase/tests/fake"
+
"github.com/stretchr/testify/require"
)
@@ -64,7 +66,7 @@ func TestBootWithExternalPg(t *testing.T) {
ctl := &controller{}
dataTmpDir := t.TempDir()
- err = ctl.StartServerWithExternalPg(ctx, dataTmpDir, serverPort, externalPg.pgUser, externalPg.pgURL)
+ err = ctl.StartServerWithExternalPg(ctx, dataTmpDir, fake.NewGitLab, serverPort, externalPg.pgUser, externalPg.pgURL)
a.NoError(err)
defer ctl.Close(ctx)
}
diff --git a/tests/fake/gitlab.go b/tests/fake/gitlab.go
index 12e7937f80..e0b9b1220e 100644
--- a/tests/fake/gitlab.go
+++ b/tests/fake/gitlab.go
@@ -80,6 +80,11 @@ func (gl *GitLab) ListenerAddr() net.Addr {
return gl.echo.ListenerAddr()
}
+// APIURL returns the GitLab VCS provider API URL.
+func (*GitLab) APIURL(instanceURL string) string {
+ return fmt.Sprintf("%s/api/v4", instanceURL)
+}
+
// CreateRepository creates a GitLab project with given ID.
func (gl *GitLab) CreateRepository(id string) {
gl.projects[id] = &projectData{
diff --git a/tests/fake/provider.go b/tests/fake/provider.go
index 1b9b91b07c..ed2b391919 100644
--- a/tests/fake/provider.go
+++ b/tests/fake/provider.go
@@ -12,6 +12,8 @@ type VCSProvider interface {
Close() error
// ListenerAddr returns listener address of the server.
ListenerAddr() net.Addr
+ // APIURL returns the API URL of the VCS provider.
+ APIURL(instanceURL string) string
// CreateRepository creates a new repository with given ID.
CreateRepository(id string)
@@ -23,3 +25,6 @@ type VCSProvider interface {
// GetFiles returns files with given paths from the repository.
GetFiles(repositoryID string, filePaths ...string) (map[string]string, error)
}
+
+// VCSProviderCreator a function to create a new VCSProvider.
+type VCSProviderCreator func(port int) VCSProvider
diff --git a/tests/ghost_test.go b/tests/ghost_test.go
index 429057eabe..4f3f277f17 100644
--- a/tests/ghost_test.go
+++ b/tests/ghost_test.go
@@ -14,6 +14,8 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/resources/mysql"
+ "github.com/bytebase/bytebase/tests/fake"
+
ghostsql "github.com/github/gh-ost/go/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/require"
@@ -69,7 +71,7 @@ func TestGhostSchemaUpdate(t *testing.T) {
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- err := ctl.StartServer(ctx, dataDir, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, getTestPort(t.Name()))
a.NoError(err)
defer ctl.Close(ctx)
err = ctl.Login()
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 41818576f4..d35d2edf48 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -14,6 +14,7 @@ import (
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/vcs"
"github.com/bytebase/bytebase/plugin/vcs/gitlab"
+ "github.com/bytebase/bytebase/tests/fake"
)
func TestSchemaAndDataUpdate(t *testing.T) {
@@ -22,7 +23,7 @@ func TestSchemaAndDataUpdate(t *testing.T) {
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- err := ctl.StartServer(ctx, dataDir, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, getTestPort(t.Name()))
a.NoError(err)
defer ctl.Close(ctx)
err = ctl.Login()
@@ -279,7 +280,7 @@ func TestVCS_GitLab(t *testing.T) {
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- err := ctl.StartServer(ctx, dataDir, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, getTestPort(t.Name()))
a.NoError(err)
defer ctl.Close(ctx)
err = ctl.Login()
@@ -293,8 +294,8 @@ func TestVCS_GitLab(t *testing.T) {
vcs, err := ctl.createVCS(api.VCSCreate{
Name: "TestVCS",
Type: vcs.GitLabSelfHost,
- InstanceURL: ctl.gitURL,
- APIURL: ctl.gitAPIURL,
+ InstanceURL: ctl.vcsURL,
+ APIURL: ctl.vcsProvider.APIURL(ctl.vcsURL),
ApplicationID: applicationID,
Secret: applicationSecret,
})
@@ -313,14 +314,14 @@ func TestVCS_GitLab(t *testing.T) {
refreshToken := "refreshToken1"
gitlabProjectID := 121
gitlabProjectIDStr := fmt.Sprintf("%d", gitlabProjectID)
- // create a vcsProvider project.
+ // Create a GitLab project.
ctl.vcsProvider.CreateRepository(gitlabProjectIDStr)
_, err = ctl.createRepository(api.RepositoryCreate{
VCSID: vcs.ID,
ProjectID: project.ID,
Name: "Test Repository",
FullPath: repositoryPath,
- WebURL: fmt.Sprintf("%s/%s", ctl.gitURL, repositoryPath),
+ WebURL: fmt.Sprintf("%s/%s", ctl.vcsURL, repositoryPath),
BranchFilter: "feature/foo",
BaseDirectory: "bbtest",
FilePathTemplate: "{{ENV_NAME}}/{{DB_NAME}}__{{VERSION}}__{{TYPE}}__{{DESCRIPTION}}.sql",
diff --git a/tests/sheet_vcs_test.go b/tests/sheet_vcs_test.go
index 1b9bbfe888..585b5f714c 100644
--- a/tests/sheet_vcs_test.go
+++ b/tests/sheet_vcs_test.go
@@ -9,6 +9,7 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/plugin/vcs"
+ "github.com/bytebase/bytebase/tests/fake"
)
func TestSheetVCS_GitLab(t *testing.T) {
@@ -17,7 +18,7 @@ func TestSheetVCS_GitLab(t *testing.T) {
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- err := ctl.StartServer(ctx, dataDir, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, getTestPort(t.Name()))
a.NoError(err)
defer ctl.Close(ctx)
err = ctl.Login()
@@ -31,8 +32,8 @@ func TestSheetVCS_GitLab(t *testing.T) {
vcs, err := ctl.createVCS(api.VCSCreate{
Name: "TestVCS",
Type: vcs.GitLabSelfHost,
- InstanceURL: ctl.gitURL,
- APIURL: ctl.gitAPIURL,
+ InstanceURL: ctl.vcsURL,
+ APIURL: ctl.vcsProvider.APIURL(ctl.vcsURL),
ApplicationID: applicationID,
Secret: applicationSecret,
})
@@ -51,14 +52,14 @@ func TestSheetVCS_GitLab(t *testing.T) {
refreshToken := "refreshToken"
gitlabProjectID := 121
gitlabProjectIDStr := fmt.Sprintf("%d", gitlabProjectID)
- // create a vcsProvider project.
+ // Create a GitLab project.
ctl.vcsProvider.CreateRepository(gitlabProjectIDStr)
_, err = ctl.createRepository(api.RepositoryCreate{
VCSID: vcs.ID,
ProjectID: project.ID,
Name: "Test Repository",
FullPath: repositoryPath,
- WebURL: fmt.Sprintf("%s/%s", ctl.gitURL, repositoryPath),
+ WebURL: fmt.Sprintf("%s/%s", ctl.vcsURL, repositoryPath),
BranchFilter: "feature/foo",
BaseDirectory: "bbtest",
FilePathTemplate: "{{ENV_NAME}}/{{DB_NAME}}__{{VERSION}}__{{TYPE}}__{{DESCRIPTION}}.sql",
diff --git a/tests/sql_review_test.go b/tests/sql_review_test.go
index e2e1dc75fb..3fb14e8198 100644
--- a/tests/sql_review_test.go
+++ b/tests/sql_review_test.go
@@ -12,12 +12,14 @@ import (
"strconv"
"testing"
+ "github.com/stretchr/testify/require"
+
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/resources/mysql"
- "github.com/stretchr/testify/require"
+ "github.com/bytebase/bytebase/tests/fake"
)
func TestSQLReview(t *testing.T) {
@@ -256,7 +258,7 @@ func TestSQLReview(t *testing.T) {
ctl := &controller{}
dataDir := t.TempDir()
port := getTestPort(t.Name()) + 3
- err := ctl.StartServer(ctx, dataDir, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, getTestPort(t.Name()))
a.NoError(err)
defer ctl.Close(ctx)
err = ctl.Login()
diff --git a/tests/start_test.go b/tests/start_test.go
index dffc447c1b..b6de47e5ce 100644
--- a/tests/start_test.go
+++ b/tests/start_test.go
@@ -5,6 +5,8 @@ import (
"testing"
"github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/tests/fake"
)
func TestServiceRestart(t *testing.T) {
@@ -13,7 +15,7 @@ func TestServiceRestart(t *testing.T) {
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- err := ctl.StartServer(ctx, dataDir, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, getTestPort(t.Name()))
a.NoError(err)
err = ctl.Login()
@@ -29,7 +31,7 @@ func TestServiceRestart(t *testing.T) {
err = ctl.Close(ctx)
a.NoError(err)
- err = ctl.StartServer(ctx, dataDir, getTestPort(t.Name()))
+ err = ctl.StartServer(ctx, dataDir, fake.NewGitLab, getTestPort(t.Name()))
a.NoError(err)
defer ctl.Close(ctx)
diff --git a/tests/tenant_test.go b/tests/tenant_test.go
index afd0ffe9da..ec297115a7 100644
--- a/tests/tenant_test.go
+++ b/tests/tenant_test.go
@@ -12,6 +12,7 @@ import (
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/vcs"
"github.com/bytebase/bytebase/plugin/vcs/gitlab"
+ "github.com/bytebase/bytebase/tests/fake"
)
var (
@@ -29,7 +30,7 @@ func TestTenant(t *testing.T) {
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- err := ctl.StartServer(ctx, dataDir, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, getTestPort(t.Name()))
a.NoError(err)
defer ctl.Close(ctx)
err = ctl.Login()
@@ -209,7 +210,7 @@ func TestTenantVCS_GitLab(t *testing.T) {
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- err := ctl.StartServer(ctx, dataDir, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, getTestPort(t.Name()))
a.NoError(err)
defer ctl.Close(ctx)
err = ctl.Login()
@@ -223,8 +224,8 @@ func TestTenantVCS_GitLab(t *testing.T) {
vcs, err := ctl.createVCS(api.VCSCreate{
Name: "TestVCS",
Type: vcs.GitLabSelfHost,
- InstanceURL: ctl.gitURL,
- APIURL: ctl.gitAPIURL,
+ InstanceURL: ctl.vcsURL,
+ APIURL: ctl.vcsProvider.APIURL(ctl.vcsURL),
ApplicationID: applicationID,
Secret: applicationSecret,
})
@@ -244,14 +245,14 @@ func TestTenantVCS_GitLab(t *testing.T) {
refreshToken := "refreshToken1"
gitlabProjectID := 121
gitlabProjectIDStr := fmt.Sprintf("%d", gitlabProjectID)
- // create a vcsProvider project.
+ // Create a GitLab project.
ctl.vcsProvider.CreateRepository(gitlabProjectIDStr)
_, err = ctl.createRepository(api.RepositoryCreate{
VCSID: vcs.ID,
ProjectID: project.ID,
Name: "Test Repository",
FullPath: repositoryPath,
- WebURL: fmt.Sprintf("%s/%s", ctl.gitURL, repositoryPath),
+ WebURL: fmt.Sprintf("%s/%s", ctl.vcsURL, repositoryPath),
BranchFilter: "feature/foo",
BaseDirectory: baseDirectory,
FilePathTemplate: "{{DB_NAME}}__{{VERSION}}__{{TYPE}}__{{DESCRIPTION}}.sql",
@@ -437,7 +438,7 @@ func TestTenantDatabaseNameTemplate(t *testing.T) {
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- err := ctl.StartServer(ctx, dataDir, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, getTestPort(t.Name()))
a.NoError(err)
defer ctl.Close(ctx)
err = ctl.Login()
@@ -590,7 +591,7 @@ func TestTenantVCSDatabaseNameTemplate_GitLab(t *testing.T) {
ctx := context.Background()
ctl := &controller{}
dataDir := t.TempDir()
- err := ctl.StartServer(ctx, dataDir, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, getTestPort(t.Name()))
a.NoError(err)
defer ctl.Close(ctx)
err = ctl.Login()
@@ -604,8 +605,8 @@ func TestTenantVCSDatabaseNameTemplate_GitLab(t *testing.T) {
vcs, err := ctl.createVCS(api.VCSCreate{
Name: "TestVCS",
Type: vcs.GitLabSelfHost,
- InstanceURL: ctl.gitURL,
- APIURL: ctl.gitAPIURL,
+ InstanceURL: ctl.vcsURL,
+ APIURL: ctl.vcsProvider.APIURL(ctl.vcsURL),
ApplicationID: applicationID,
Secret: applicationSecret,
})
@@ -626,14 +627,14 @@ func TestTenantVCSDatabaseNameTemplate_GitLab(t *testing.T) {
refreshToken := "refreshToken1"
gitlabProjectID := 121
gitlabProjectIDStr := fmt.Sprintf("%d", gitlabProjectID)
- // create a vcsProvider project.
+ // Create a GitLab project.
ctl.vcsProvider.CreateRepository(gitlabProjectIDStr)
_, err = ctl.createRepository(api.RepositoryCreate{
VCSID: vcs.ID,
ProjectID: project.ID,
Name: "Test Repository",
FullPath: repositoryPath,
- WebURL: fmt.Sprintf("%s/%s", ctl.gitURL, repositoryPath),
+ WebURL: fmt.Sprintf("%s/%s", ctl.vcsURL, repositoryPath),
BranchFilter: "feature/foo",
BaseDirectory: "bbtest",
FilePathTemplate: "{{DB_NAME}}__{{VERSION}}__{{TYPE}}__{{DESCRIPTION}}.sql",
diff --git a/tests/tests.go b/tests/tests.go
index d49b3d69ff..0b2e5c297f 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -105,10 +105,9 @@ type controller struct {
cookie string
vcsProvider fake.VCSProvider
- rootURL string
- apiURL string
- gitURL string
- gitAPIURL string
+ rootURL string
+ apiURL string
+ vcsURL string
}
func getTestPort(testName string) int {
@@ -154,7 +153,7 @@ func getTestPort(testName string) int {
}
// StartServerWithExternalPg starts the main server with external Postgres.
-func (ctl *controller) StartServerWithExternalPg(ctx context.Context, dataDir string, port int, pgUser, pgURL string) error {
+func (ctl *controller) StartServerWithExternalPg(ctx context.Context, dataDir string, vcsProviderCreator fake.VCSProviderCreator, port int, pgUser, pgURL string) error {
log.SetLevel(zap.DebugLevel)
profile := cmd.GetTestProfileWithExternalPg(dataDir, port, pgUser, pgURL)
server, err := server.NewServer(ctx, profile)
@@ -163,11 +162,11 @@ func (ctl *controller) StartServerWithExternalPg(ctx context.Context, dataDir st
}
ctl.server = server
- return ctl.start(ctx, port)
+ return ctl.start(ctx, vcsProviderCreator, port)
}
// StartServer starts the main server with embed Postgres.
-func (ctl *controller) StartServer(ctx context.Context, dataDir string, port int) error {
+func (ctl *controller) StartServer(ctx context.Context, dataDir string, vcsProviderCreator fake.VCSProviderCreator, port int) error {
// start main server.
log.SetLevel(zap.DebugLevel)
profile := cmd.GetTestProfile(dataDir, port)
@@ -177,19 +176,18 @@ func (ctl *controller) StartServer(ctx context.Context, dataDir string, port int
}
ctl.server = server
- return ctl.start(ctx, port)
+ return ctl.start(ctx, vcsProviderCreator, port)
}
-// start only called by StartServer() and StartServerWithExternalPg.
-func (ctl *controller) start(ctx context.Context, port int) error {
+// start only called by StartServer() and StartServerWithExternalPg().
+func (ctl *controller) start(ctx context.Context, vcsProviderCreator fake.VCSProviderCreator, port int) error {
ctl.rootURL = fmt.Sprintf("http://localhost:%d", port)
ctl.apiURL = fmt.Sprintf("http://localhost:%d/api", port)
- // set up vcsProvider.
- gitlabPort := port + 2
- ctl.vcsProvider = fake.NewGitLab(gitlabPort)
- ctl.gitURL = fmt.Sprintf("http://localhost:%d", gitlabPort)
- ctl.gitAPIURL = fmt.Sprintf("%s/api/v4", ctl.gitURL)
+ // Set up VCS provider.
+ vcsPort := port + 2
+ ctl.vcsProvider = vcsProviderCreator(vcsPort)
+ ctl.vcsURL = fmt.Sprintf("http://localhost:%d", vcsPort)
errChan := make(chan error, 1)