-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure E2E test setup on PAC for bitbucket server
configured e2e test for bitbucket server on PAC and wrote one test for pull request. https://issues.redhat.com/browse/SRVKP-6758 Signed-off-by: Zaki Shaikh <[email protected]>
- Loading branch information
Showing
57 changed files
with
63,951 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype" | ||
tbbs "github.com/openshift-pipelines/pipelines-as-code/test/pkg/bitbucketserver" | ||
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait" | ||
|
||
"github.com/tektoncd/pipeline/pkg/names" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestBitbucketServerPullRequest(t *testing.T) { | ||
targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns") | ||
ctx := context.Background() | ||
|
||
ctx, runcnx, opts, provider, restClient, err := tbbs.Setup(ctx) | ||
assert.NilError(t, err) | ||
|
||
repo := tbbs.CreateCRD(ctx, t, provider, runcnx, opts, targetNS) | ||
defer tbbs.TearDownNs(ctx, t, runcnx, targetNS) | ||
|
||
title := "TestPullRequest - " + targetNS | ||
numberOfFiles := 5 | ||
files := map[string]string{} | ||
for i := range numberOfFiles { | ||
files[fmt.Sprintf("pipelinerun-%d.yaml", i)] = "testdata/pipelinerun.yaml" | ||
} | ||
|
||
pr, commits := tbbs.CreatePR(ctx, t, restClient, *repo, runcnx, opts, files, title, targetNS) | ||
assert.Assert(t, numberOfFiles == len(commits)) | ||
runcnx.Clients.Log.Infof("Pull Request with title '%s' is created", pr.Title) | ||
defer tbbs.TearDownBranch(ctx, t, runcnx, provider, restClient, opts, pr.Id, targetNS) | ||
|
||
successOpts := wait.SuccessOpt{ | ||
TargetNS: targetNS, | ||
OnEvent: triggertype.PullRequest.String(), | ||
NumberofPRMatch: 5, | ||
Title: commits[0].Message, | ||
MinNumberStatus: 5, | ||
} | ||
wait.Succeeded(ctx, t, runcnx, opts, successOpts) | ||
} | ||
|
||
// Local Variables: | ||
// compile-command: "go test -tags=e2e -v -run TestBitbucketServerPullRequest$ ." | ||
// End: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package bitbucketserver | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
"mime/multipart" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func callAPI(ctx context.Context, endpointURL, method string, fields map[string]string) ([]byte, error) { | ||
req, err := createRequest(ctx, endpointURL, method, fields) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("error sending request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode > 300 { | ||
return nil, fmt.Errorf("error status code: %d", resp.StatusCode) | ||
} | ||
|
||
responseBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading response: %w", err) | ||
} | ||
|
||
return responseBody, nil | ||
} | ||
|
||
func createRequest(ctx context.Context, endpointURL, method string, fields map[string]string) (*http.Request, error) { | ||
var requestBody bytes.Buffer | ||
writer := multipart.NewWriter(&requestBody) | ||
|
||
if len(fields) > 0 { | ||
for field, value := range fields { | ||
err := writer.WriteField(field, value) | ||
if err != nil { | ||
return nil, fmt.Errorf("error writing field %s to multipart data: %w", field, err) | ||
} | ||
} | ||
|
||
err := writer.Close() | ||
if err != nil { | ||
return nil, fmt.Errorf("error closing writer: %w", err) | ||
} | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, method, endpointURL, &requestBody) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating request: %w", err) | ||
} | ||
|
||
if len(fields) > 0 { | ||
req.Header.Set("Content-Type", writer.FormDataContentType()) | ||
} | ||
|
||
bitbucketServerUser := os.Getenv("TEST_BITBUCKET_SERVER_USER") | ||
bitbucketServerToken := os.Getenv("TEST_BITBUCKET_SERVER_TOKEN") | ||
req.SetBasicAuth(bitbucketServerUser, bitbucketServerToken) | ||
|
||
return req, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package bitbucketserver | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1" | ||
"github.com/openshift-pipelines/pipelines-as-code/pkg/params" | ||
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketserver" | ||
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options" | ||
pacrepo "github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository" | ||
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/secret" | ||
|
||
bbv1 "github.com/gfleury/go-bitbucket-v1" | ||
"gotest.tools/v3/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func CreateCRD(ctx context.Context, t *testing.T, bprovider bitbucketserver.Provider, run *params.Run, opts options.E2E, targetNS string) *bbv1.Repository { | ||
resp, err := bprovider.Client.DefaultApi.GetRepository(opts.Organization, opts.Repo) | ||
assert.NilError(t, err) | ||
|
||
body, err := json.Marshal(resp.Values) | ||
assert.NilError(t, err) | ||
repo := &bbv1.Repository{} | ||
err = json.Unmarshal(body, repo) | ||
assert.NilError(t, err) | ||
|
||
url := strings.ReplaceAll(repo.Links.Self[0].Href, "/browse", "") | ||
repository := &v1alpha1.Repository{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: targetNS, | ||
}, | ||
Spec: v1alpha1.RepositorySpec{ | ||
URL: url, | ||
}, | ||
} | ||
|
||
err = pacrepo.CreateNS(ctx, targetNS, run) | ||
assert.NilError(t, err) | ||
run.Clients.Log.Infof("Namespace %s is created", targetNS) | ||
|
||
token, _ := os.LookupEnv("TEST_BITBUCKET_SERVER_TOKEN") | ||
apiURL, _ := os.LookupEnv("TEST_BITBUCKET_SERVER_API_URL") | ||
apiUser, _ := os.LookupEnv("TEST_BITBUCKET_SERVER_USER") | ||
webhookSecret := os.Getenv("TEST_BITBUCKET_SERVER_WEBHOOK_SECRET") | ||
secretName := "bitbucket-server-webhook-config" | ||
err = secret.Create(ctx, run, map[string]string{ | ||
"provider.token": token, | ||
"webhook.secret": webhookSecret, | ||
}, targetNS, secretName) | ||
assert.NilError(t, err) | ||
run.Clients.Log.Infof("PipelinesAsCode Secret %s is created", secretName) | ||
|
||
repository.Spec.GitProvider = &v1alpha1.GitProvider{ | ||
URL: apiURL, | ||
User: apiUser, | ||
Secret: &v1alpha1.Secret{Name: secretName}, | ||
WebhookSecret: &v1alpha1.Secret{Name: secretName}, | ||
} | ||
|
||
err = pacrepo.CreateRepo(ctx, targetNS, run, repository) | ||
assert.NilError(t, err) | ||
|
||
return repo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package bitbucketserver | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/params" | ||
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype" | ||
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options" | ||
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload" | ||
|
||
"github.com/antihax/optional" | ||
bbrest "github.com/gdasson/bitbucketv1go" | ||
bbv1 "github.com/gfleury/go-bitbucket-v1" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func CreatePR(ctx context.Context, t *testing.T, restClient *bbrest.APIClient, repo bbv1.Repository, runcnx *params.Run, opts options.E2E, files map[string]string, title, targetNS string) (bbrest.RestPullRequest, []*bbrest.RestCommit) { | ||
mainBranchRef := "refs/heads/main" | ||
branchCreateRequest := bbrest.RestBranchCreateRequest{Name: targetNS, StartPoint: mainBranchRef} | ||
branch, resp, err := restClient.RepositoryApi.CreateBranch(ctx, branchCreateRequest, opts.Organization, opts.Repo) | ||
assert.NilError(t, err) | ||
defer resp.Body.Close() | ||
runcnx.Clients.Log.Infof("Branch %s is created", branch.Id) | ||
|
||
files, err = payload.GetEntries(files, targetNS, options.MainBranch, triggertype.PullRequest.String(), map[string]string{}) | ||
assert.NilError(t, err) | ||
commits, err := pushFilesToBranch(ctx, runcnx, opts, targetNS, files) | ||
assert.NilError(t, err) | ||
|
||
prCreateOpts := &bbrest.PullRequestsApiCreateOpts{Body: optional.NewInterface(map[string]interface{}{ | ||
"title": title, | ||
"description": "Test PAC on bitbucket server", | ||
"fromRef": bbv1.PullRequestRef{ID: branch.Id, Repository: repo}, | ||
"toRef": bbv1.PullRequestRef{ID: mainBranchRef, Repository: repo}, | ||
"closed": false, | ||
})} | ||
pr, resp, err := restClient.PullRequestsApi.Create(ctx, opts.Organization, opts.Repo, prCreateOpts) | ||
assert.NilError(t, err) | ||
defer resp.Body.Close() | ||
|
||
return pr, commits | ||
} | ||
|
||
// pushFilesToBranch pushes multiple files to bitbucket server repo because | ||
// bitbucket server doesn't support uploading multiple files in an API call. | ||
// reference: https://community.developer.atlassian.com/t/rest-api-to-update-multiple-files/28731/2 | ||
func pushFilesToBranch(ctx context.Context, run *params.Run, opts options.E2E, branchName string, files map[string]string) ([]*bbrest.RestCommit, error) { | ||
if len(files) == 0 { | ||
return nil, fmt.Errorf("no file to commit") | ||
} | ||
|
||
commits := make([]*bbrest.RestCommit, 0, len(files)) | ||
apiURL := os.Getenv("TEST_BITBUCKET_SERVER_API_URL") | ||
for file, content := range files { | ||
endpointURL := fmt.Sprintf("%s/api/latest/projects/%s/repos/%s/browse/.tekton/%s", apiURL, opts.Organization, opts.Repo, file) | ||
fields := map[string]string{ | ||
"branch": branchName, | ||
"content": content, | ||
"message": "test commit Signed-off-by: Zaki Shaikh <[email protected]>", | ||
} | ||
|
||
response, err := callAPI(ctx, endpointURL, http.MethodPut, fields) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var tmpCommit bbrest.RestCommit | ||
err = json.Unmarshal(response, &tmpCommit) | ||
if err != nil { | ||
return nil, fmt.Errorf("error unmarshaling response: %w", err) | ||
} | ||
commits = append(commits, &tmpCommit) | ||
} | ||
run.Clients.Log.Infof("%d files committed to branch %s", len(files), branchName) | ||
|
||
return commits, nil | ||
} |
Oops, something went wrong.