Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(test): add test for creating repository and fake createProjectHook (#524)
Danny Xub119bd20d63c
diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.goindex aa6ec02455..adfc514034 100644--- a/plugin/vcs/gitlab/gitlab.go+++ b/plugin/vcs/gitlab/gitlab.go@@ -46,8 +46,8 @@ func (e WebhookType) String() string {return "UNKNOWN"}-// webhookInfo is the API message for webhook info.-type webhookInfo struct {+// WebhookInfo is the API message for webhook info.+type WebhookInfo struct {ID int `json:"id"`}@@ -351,7 +351,7 @@ func (provider *Provider) CreateWebhook(ctx context.Context, oauthCtx common.Oaureturn "", fmt.Errorf(reason)}- webhookInfo := &webhookInfo{}+ webhookInfo := &WebhookInfo{}if err := json.NewDecoder(resp.Body).Decode(webhookInfo); err != nil {return "", fmt.Errorf("failed to unmarshal create webhook response for repository %s from GitLab instance %s: %w", repositoryID, instanceURL, err)}diff --git a/tests/fake/gitlab.go b/tests/fake/gitlab.goindex aa097a5a0f..64f78fa350 100644--- a/tests/fake/gitlab.go+++ b/tests/fake/gitlab.go@@ -1,8 +1,11 @@package fakeimport (+ "encoding/json""fmt"+ "net/http"+ "github.com/bytebase/bytebase/plugin/vcs/gitlab""github.com/labstack/echo/v4""github.com/labstack/echo/v4/middleware")@@ -11,6 +14,8 @@ import (type GitLab struct {port intEcho *echo.Echo++ nextWebhookID int}// NewGitLab creates a fake GitLab.@@ -20,21 +25,39 @@ func NewGitLab(port int) *GitLab {e.Use(middleware.Logger())e.Use(middleware.Recover())+ gl := &GitLab{+ Echo: e,+ port: port,+ nextWebhookID: 20210113,+ }+// Routes- // TODO(d-bytebase): implement routes+ projectGroup := e.Group("/api/v4")+ projectGroup.POST("/projects/:id/hooks", gl.createProjectHook)- return &GitLab{- Echo: e,- port: port,- }+ return gl}// Run runs a GitLab server.-func (g *GitLab) Run() error {- return g.Echo.Start(fmt.Sprintf(":%d", g.port))+func (gl *GitLab) Run() error {+ return gl.Echo.Start(fmt.Sprintf(":%d", gl.port))}// Close close a GitLab server.-func (g *GitLab) Close() error {- return g.Echo.Close()+func (gl *GitLab) Close() error {+ return gl.Echo.Close()+}++// createProjectHook creates a project webhook.+func (gl *GitLab) createProjectHook(c echo.Context) error {+ c.Logger().Info("create webhook for project %q", c.Param("id"))++ if err := json.NewEncoder(c.Response().Writer).Encode(&gitlab.WebhookInfo{+ ID: gl.nextWebhookID,+ }); err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal WebhookInfo response").SetInternal(err)+ }+ gl.nextWebhookID++++ return nil}diff --git a/tests/schema_update_test.go b/tests/schema_update_test.goindex 4ea32e1417..1243ceb348 100644--- a/tests/schema_update_test.go+++ b/tests/schema_update_test.go@@ -8,6 +8,7 @@ import ("github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/plugin/db"+ "github.com/bytebase/bytebase/plugin/vcs""github.com/kr/pretty")@@ -193,3 +194,64 @@ func TestSchemaUpdate(t *testing.T) {t.Fatalf("want SQL result %q, got %q, diff %q", wantResult, sqlResultSet.Data, pretty.Diff(wantResult, sqlResultSet.Data))}}++func TestVCSSchemaUpdate(t *testing.T) {+ ctx := context.Background()+ ctl := &controller{}+ dataDir := t.TempDir()+ if err := ctl.StartMain(ctx, dataDir); err != nil {+ t.Fatal(err)+ }+ defer ctl.Close()++ if err := ctl.Login(); err != nil {+ t.Fatal(err)+ }++ // Create a VCS.+ applicationID := "testApplicationID"+ applicationSecret := "testApplicationSecret"+ vcs, err := ctl.createVCS(api.VCSCreate{+ Name: "TestVCS",+ Type: vcs.GitLabSelfHost,+ InstanceURL: gitURL,+ APIURL: gitAPIURL,+ ApplicationID: applicationID,+ Secret: applicationSecret,+ })+ if err != nil {+ t.Fatalf("failed to create VCS, error: %v", err)+ }++ // Create a project.+ project, err := ctl.createProject(api.ProjectCreate{+ Name: "Test VCS Project",+ Key: "TestVCSSchemaUpdate",+ })+ if err != nil {+ t.Fatalf("failed to create project, error: %v", err)+ }+ // Create a repository.+ repositoryPath := "test/schemaUpdate"+ accessToken := "accessToken1"+ refreshToken := "refreshToken1"+ externalID := "121"+ _, err = ctl.createRepository(api.RepositoryCreate{+ VCSID: vcs.ID,+ ProjectID: project.ID,+ Name: "Test Repository",+ FullPath: repositoryPath,+ WebURL: fmt.Sprintf("%s/%s", gitURL, repositoryPath),+ BranchFilter: "master",+ BaseDirectory: "bbtest",+ FilePathTemplate: "{{ENV_NAME}}/{{DB_NAME}}__{{VERSION}}__{{TYPE}}__{{DESCRIPTION}}.sql",+ SchemaPathTemplate: "{{ENV_NAME}}/.{{DB_NAME}}__LATEST.sql",+ ExternalID: externalID,+ AccessToken: accessToken,+ ExpiresTs: 0,+ RefreshToken: refreshToken,+ })+ if err != nil {+ t.Fatalf("failed to create repository, error: %v", err)+ }+}diff --git a/tests/tests.go b/tests/tests.goindex bb7d7ae975..b6cd42c2e5 100644--- a/tests/tests.go+++ b/tests/tests.go@@ -20,9 +20,11 @@ import ()var (- port = 1234- rootURL = fmt.Sprintf("http://localhost:%d/api", port)- gitPort = 1235+ 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))type controller struct {@@ -501,3 +503,41 @@ func (ctl *controller) executeSQL(sqlExecute api.SQLExecute) (*api.SQLResultSet,}return sqlResultSet, nil}++// createVCS creates a VCS.+func (ctl *controller) createVCS(vcsCreate api.VCSCreate) (*api.VCS, error) {+ buf := new(bytes.Buffer)+ if err := jsonapi.MarshalPayload(buf, &vcsCreate); err != nil {+ return nil, fmt.Errorf("failed to marshal vcsCreate, error: %w", err)+ }++ body, err := ctl.post("/vcs", buf)+ if err != nil {+ return nil, err+ }++ vcs := new(api.VCS)+ if err = jsonapi.UnmarshalPayload(body, vcs); err != nil {+ return nil, fmt.Errorf("fail to unmarshal vcs response, error: %w", err)+ }+ return vcs, nil+}++// createRepository creates a repository.+func (ctl *controller) createRepository(repositoryCreate api.RepositoryCreate) (*api.Repository, error) {+ buf := new(bytes.Buffer)+ if err := jsonapi.MarshalPayload(buf, &repositoryCreate); err != nil {+ return nil, fmt.Errorf("failed to marshal repositoryCreate, error: %w", err)+ }++ body, err := ctl.post(fmt.Sprintf("/project/%d/repository", repositoryCreate.ProjectID), buf)+ if err != nil {+ return nil, err+ }++ repository := new(api.Repository)+ if err = jsonapi.UnmarshalPayload(body, repository); err != nil {+ return nil, fmt.Errorf("fail to unmarshal repository response, error: %w", err)+ }+ return repository, nil+}