Skip to content

Commit

Permalink
Check Bitbucket build status in E2E test
Browse files Browse the repository at this point in the history
Closes #187.
  • Loading branch information
michaelsauter committed Nov 2, 2021
1 parent 7f96ec9 commit 731e650
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 45 deletions.
10 changes: 5 additions & 5 deletions pkg/bitbucket/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type Branch struct {
}

type BranchPage struct {
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Branch
Start int `json:"start"`
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Branch `json:"values"`
Start int `json:"start"`
}

type BranchListParams struct {
Expand Down
23 changes: 12 additions & 11 deletions pkg/bitbucket/build_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,26 @@ func (c *Client) BuildStatusCreate(gitCommit string, payload BuildStatusCreatePa
return nil
}

type BuildStatusPage struct {
Size int `json:"size"`
Limit int `json:"limit"`
Islastpage bool `json:"isLastPage"`
BuildStatus []BuildStatus `json:"values"`
Start int `json:"start"`
}
type BuildStatus struct {
State string `json:"state"`
Key string `json:"key"`
Name string `json:"name"`
URL string `json:"url"`
Description string `json:"description"`
Dateadded int64 `json:"dateAdded"`
DateAdded int64 `json:"dateAdded"`
}

type BuildStatusPage struct {
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []BuildStatus `json:"values"` // newest build status appears first
Start int `json:"start"`
}

// BuildStatusGet gets the build statuses associated with a commit.
// BuildStatusList gets the build statuses associated with a commit.
// https://docs.atlassian.com/bitbucket-server/rest/7.13.0/bitbucket-build-rest.html#idp8
func (c *Client) BuildStatusGet(gitCommit string) (*BuildStatus, error) {
func (c *Client) BuildStatusList(gitCommit string) (*BuildStatusPage, error) {
urlPath := fmt.Sprintf("/rest/build-status/1.0/commits/%s", gitCommit)
statusCode, response, err := c.get(urlPath)
if err != nil {
Expand All @@ -75,7 +76,7 @@ func (c *Client) BuildStatusGet(gitCommit string) (*BuildStatus, error) {
"could not unmarshal response: %w. status code: %d, body: %s", err, statusCode, string(response),
)
}
return &BuildStatusPage.BuildStatus[0], nil // return the newest by default
return &BuildStatusPage, nil
case 401:
return nil, fmt.Errorf("you are not permitted to get the build status of git commit %s", gitCommit)
default:
Expand Down
14 changes: 7 additions & 7 deletions pkg/bitbucket/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ type Commit struct {
}

type CommitPage struct {
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Commit
Start int `json:"start"`
AuthorCount int `json:"authorCount"`
TotalCount int `json:"totalCount"`
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Commit `json:"values"`
Start int `json:"start"`
AuthorCount int `json:"authorCount"`
TotalCount int `json:"totalCount"`
}

type PullRequestPage struct {
Expand Down
10 changes: 5 additions & 5 deletions pkg/bitbucket/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type Tag struct {
}

type TagPage struct {
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Tag
Start int `json:"start"`
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Tag `json:"values"`
Start int `json:"start"`
}

type TagCreatePayload struct {
Expand Down
15 changes: 15 additions & 0 deletions pkg/tasktesting/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,18 @@ func BitbucketClientOrFatal(t *testing.T, c *kclient.Clientset, namespace string
})
return bitbucketClient
}

func CheckBitbucketBuildStatus(t *testing.T, c *bitbucket.Client, gitCommit, wantBuildStatus string) {
buildStatusPage, err := c.BuildStatusList(gitCommit)
if err != nil {
t.Fatal(err)
}
if buildStatusPage == nil || len(buildStatusPage.Values) == 0 {
t.Fatal("no build status found")
}
buildStatus := buildStatusPage.Values[0]
if buildStatus.State != wantBuildStatus {
t.Fatalf("Got: %s, want: %s", buildStatus.State, wantBuildStatus)
}

}
4 changes: 4 additions & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ func TestWebhookInterceptor(t *testing.T) {
t.Log(logs)
t.Fatal()
}
t.Log("Pipeline run succeeded.")
t.Log("Sleeping for 2s to make it work - unsure why needed ...")
time.Sleep(2 * time.Second)
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusSuccessful)
}

func waitForServiceToBeReady(t *testing.T, clientset *k8s.Clientset, ns, name string, timeout time.Duration) error {
Expand Down
12 changes: 0 additions & 12 deletions test/tasks/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/opendevstack/pipeline/internal/kubernetes"
"github.com/opendevstack/pipeline/pkg/bitbucket"
"github.com/opendevstack/pipeline/pkg/config"
"github.com/opendevstack/pipeline/pkg/pipelinectxt"
"github.com/opendevstack/pipeline/pkg/sonar"
Expand Down Expand Up @@ -184,14 +183,3 @@ func createODSYML(wsDir string, o *config.ODS) error {
filename := filepath.Join(wsDir, "ods.yaml")
return ioutil.WriteFile(filename, y, 0644)
}

func checkBuildStatus(t *testing.T, c *bitbucket.Client, gitCommit, wantBuildStatus string) {
buildStatus, err := c.BuildStatusGet(gitCommit)
if err != nil {
t.Fatal(err)
}
if buildStatus.State != wantBuildStatus {
t.Fatalf("Got: %s, want: %s", buildStatus.State, wantBuildStatus)
}

}
6 changes: 3 additions & 3 deletions test/tasks/ods-finish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestTaskODSFinish(t *testing.T) {
WantRunSuccess: true,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusFailed)
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusFailed)
},
},
"set bitbucket build status to successful and upload artifacts to temporary Nexus repository": {
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestTaskODSFinish(t *testing.T) {
WantRunSuccess: true,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
checkArtifactsAreInNexus(t, ctxt, nexus.TemporaryRepositoryDefault)

wantLogMsg := "Artifact coverage.out is already present in Nexus repository"
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestTaskODSFinish(t *testing.T) {
WantRunSuccess: true,
PostRunFunc: func(t *testing.T, ctxt *tasktesting.TaskRunContext) {
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusSuccessful)
checkArtifactsAreInNexus(t, ctxt, nexus.PermanentRepositoryDefault)
},
},
Expand Down
4 changes: 2 additions & 2 deletions test/tasks/ods-start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestTaskODSStart(t *testing.T) {
checkODSContext(t, wsDir, ctxt.ODS)

bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusInProgress)
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusInProgress)

checkFileExists(t, filepath.Join(wsDir, pipelinectxt.PipelineRunsPath, "foo-zh9gt0.json"))
checkFileExists(t, filepath.Join(wsDir, pipelinectxt.ArtifactsPath, pipelinectxt.ArtifactsManifestFilename))
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestTaskODSStart(t *testing.T) {
checkFileExists(t, filepath.Join(destinationArtifactsBaseDir, pipelinectxt.ArtifactsManifestFilename))

bitbucketClient := tasktesting.BitbucketClientOrFatal(t, ctxt.Clients.KubernetesClientSet, ctxt.Namespace)
checkBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusInProgress)
tasktesting.CheckBitbucketBuildStatus(t, bitbucketClient, ctxt.ODS.GitCommitSHA, bitbucket.BuildStatusInProgress)

},
},
Expand Down

0 comments on commit 731e650

Please sign in to comment.