From 731e65019486a21b79c96d0a6be1a0c892382d66 Mon Sep 17 00:00:00 2001 From: Michael Sauter Date: Tue, 5 Oct 2021 14:50:24 +0200 Subject: [PATCH] Check Bitbucket build status in E2E test Closes #187. --- pkg/bitbucket/branches.go | 10 +++++----- pkg/bitbucket/build_status.go | 23 ++++++++++++----------- pkg/bitbucket/commits.go | 14 +++++++------- pkg/bitbucket/tags.go | 10 +++++----- pkg/tasktesting/bitbucket.go | 15 +++++++++++++++ test/e2e/e2e_test.go | 4 ++++ test/tasks/common_test.go | 12 ------------ test/tasks/ods-finish_test.go | 6 +++--- test/tasks/ods-start_test.go | 4 ++-- 9 files changed, 53 insertions(+), 45 deletions(-) diff --git a/pkg/bitbucket/branches.go b/pkg/bitbucket/branches.go index 09103376..a5dd48e9 100644 --- a/pkg/bitbucket/branches.go +++ b/pkg/bitbucket/branches.go @@ -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 { diff --git a/pkg/bitbucket/build_status.go b/pkg/bitbucket/build_status.go index 31ab87eb..778e3ca0 100644 --- a/pkg/bitbucket/build_status.go +++ b/pkg/bitbucket/build_status.go @@ -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 { @@ -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: diff --git a/pkg/bitbucket/commits.go b/pkg/bitbucket/commits.go index d67fc941..dda8dc0a 100644 --- a/pkg/bitbucket/commits.go +++ b/pkg/bitbucket/commits.go @@ -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 { diff --git a/pkg/bitbucket/tags.go b/pkg/bitbucket/tags.go index a2882c74..b3007b7e 100644 --- a/pkg/bitbucket/tags.go +++ b/pkg/bitbucket/tags.go @@ -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 { diff --git a/pkg/tasktesting/bitbucket.go b/pkg/tasktesting/bitbucket.go index 23c8d6e5..1881c829 100644 --- a/pkg/tasktesting/bitbucket.go +++ b/pkg/tasktesting/bitbucket.go @@ -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) + } + +} diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index 9d76381d..ca0d8a41 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -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 { diff --git a/test/tasks/common_test.go b/test/tasks/common_test.go index 5fdea392..a3c42895 100644 --- a/test/tasks/common_test.go +++ b/test/tasks/common_test.go @@ -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" @@ -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) - } - -} diff --git a/test/tasks/ods-finish_test.go b/test/tasks/ods-finish_test.go index e6499014..8dfd109a 100644 --- a/test/tasks/ods-finish_test.go +++ b/test/tasks/ods-finish_test.go @@ -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": { @@ -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" @@ -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) }, }, diff --git a/test/tasks/ods-start_test.go b/test/tasks/ods-start_test.go index dea6f1d6..ba3c1dd7 100644 --- a/test/tasks/ods-start_test.go +++ b/test/tasks/ods-start_test.go @@ -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)) @@ -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) }, },