-
Notifications
You must be signed in to change notification settings - Fork 89
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 <zashaikh@redhat.com>
- Loading branch information
Showing
50 changed files
with
6,388 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"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() | ||
bitbucketWSOwner := os.Getenv("TEST_BITBUCKET_SERVER_E2E_REPOSITORY") | ||
|
||
ctx, runcnx, opts, client, err := tbbs.Setup(ctx) | ||
assert.NilError(t, err) | ||
|
||
repo := tbbs.CreateCRD(ctx, t, client, runcnx, bitbucketWSOwner, targetNS) | ||
runcnx.Clients.Log.Infof("Repository %s has been created", repo.Name) | ||
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 := tbbs.CreatePR(ctx, t, client, runcnx, bitbucketWSOwner, files, title, targetNS) | ||
runcnx.Clients.Log.Infof("Pull Request with title '%s' is created", pr.Title) | ||
defer tbbs.TearDownBranch(ctx, t, runcnx, client, pr.Number, bitbucketWSOwner, targetNS) | ||
|
||
successOpts := wait.SuccessOpt{ | ||
TargetNS: targetNS, | ||
OnEvent: triggertype.PullRequest.String(), | ||
NumberofPRMatch: 5, | ||
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,60 @@ | ||
package bitbucketserver | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1" | ||
"github.com/openshift-pipelines/pipelines-as-code/pkg/params" | ||
pacrepo "github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository" | ||
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/secret" | ||
|
||
"github.com/jenkins-x/go-scm/scm" | ||
"gotest.tools/v3/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func CreateCRD(ctx context.Context, t *testing.T, client *scm.Client, run *params.Run, orgAndRepo, targetNS string) *scm.Repository { | ||
repo, _, err := client.Repositories.Find(ctx, orgAndRepo) | ||
assert.NilError(t, err) | ||
|
||
url := strings.ReplaceAll(repo.Link, "/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,63 @@ | ||
package bitbucketserver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"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/jenkins-x/go-scm/scm" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func CreatePR(ctx context.Context, t *testing.T, client *scm.Client, runcnx *params.Run, orgAndRepo string, files map[string]string, title, targetNS string) *scm.PullRequest { | ||
mainBranchRef := "refs/heads/main" | ||
branch, _, err := client.Git.CreateRef(ctx, orgAndRepo, targetNS, mainBranchRef) | ||
assert.NilError(t, err) | ||
runcnx.Clients.Log.Infof("Branch %s has been created", branch.Name) | ||
|
||
files, err = payload.GetEntries(files, targetNS, options.MainBranch, triggertype.PullRequest.String(), map[string]string{}) | ||
assert.NilError(t, err) | ||
err = pushFilesToBranch(ctx, runcnx, client, orgAndRepo, targetNS, files) | ||
assert.NilError(t, err) | ||
|
||
prOpts := &scm.PullRequestInput{ | ||
Title: title, | ||
Body: "Test PAC on bitbucket server", | ||
Head: targetNS, | ||
Base: "main", | ||
} | ||
pr, _, err := client.PullRequests.Create(ctx, orgAndRepo, prOpts) | ||
assert.NilError(t, err) | ||
return pr | ||
} | ||
|
||
// 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, client *scm.Client, repoAndOrg, branchName string, files map[string]string) error { | ||
if len(files) == 0 { | ||
return fmt.Errorf("no file to commit") | ||
} | ||
|
||
for file, content := range files { | ||
param := &scm.ContentParams{ | ||
Branch: branchName, | ||
Message: "test commit", | ||
Data: []byte(content), | ||
Signature: scm.Signature{Name: "Zaki Shaikh", Email: "zaki@example.com"}, | ||
} | ||
path := fmt.Sprintf(".tekton/%s", file) | ||
_, err := client.Contents.Create(ctx, repoAndOrg, path, param) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
run.Clients.Log.Infof("%d files committed to branch %s", len(files), branchName) | ||
|
||
return 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,98 @@ | ||
package bitbucketserver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/params" | ||
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info" | ||
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options" | ||
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository" | ||
|
||
"github.com/jenkins-x/go-scm/scm" | ||
"github.com/jenkins-x/go-scm/scm/driver/stash" | ||
"github.com/jenkins-x/go-scm/scm/transport/oauth2" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func Setup(ctx context.Context) (context.Context, *params.Run, options.E2E, *scm.Client, error) { | ||
bitbucketServerUser := os.Getenv("TEST_BITBUCKET_SERVER_USER") | ||
bitbucketServerToken := os.Getenv("TEST_BITBUCKET_SERVER_TOKEN") | ||
bitbucketWSOwner := os.Getenv("TEST_BITBUCKET_SERVER_E2E_REPOSITORY") | ||
bitbucketServerAPIURL := os.Getenv("TEST_BITBUCKET_SERVER_API_URL") | ||
|
||
for _, value := range []string{ | ||
"BITBUCKET_SERVER_USER", "BITBUCKET_SERVER_TOKEN", "BITBUCKET_SERVER_E2E_REPOSITORY", "BITBUCKET_SERVER_API_URL", "BITBUCKET_SERVER_WEBHOOK_SECRET", | ||
} { | ||
if env := os.Getenv("TEST_" + value); env == "" { | ||
return ctx, nil, options.E2E{}, nil, fmt.Errorf("\"TEST_%s\" env variable is required, skipping", value) | ||
} | ||
} | ||
|
||
split := strings.Split(bitbucketWSOwner, "/") | ||
|
||
run := params.New() | ||
if err := run.Clients.NewClients(ctx, &run.Info); err != nil { | ||
return ctx, nil, options.E2E{}, nil, err | ||
} | ||
e2eoptions := options.E2E{ | ||
Organization: split[0], | ||
Repo: split[1], | ||
} | ||
|
||
event := info.NewEvent() | ||
event.Provider = &info.Provider{ | ||
Token: bitbucketServerToken, | ||
URL: bitbucketServerAPIURL, | ||
User: bitbucketServerUser, | ||
} | ||
|
||
client, err := stash.New(bitbucketServerAPIURL) | ||
if err != nil { | ||
return ctx, nil, options.E2E{}, nil, err | ||
} | ||
|
||
client.Client = &http.Client{ | ||
Transport: &oauth2.Transport{ | ||
Source: oauth2.StaticTokenSource( | ||
&scm.Token{ | ||
Token: bitbucketServerToken, | ||
}, | ||
), | ||
}, | ||
} | ||
|
||
return ctx, run, e2eoptions, client, nil | ||
} | ||
|
||
func TearDownNs(ctx context.Context, t *testing.T, runcnx *params.Run, targetNS string) { | ||
if os.Getenv("TEST_NOCLEANUP") == "true" { | ||
runcnx.Clients.Log.Infof("Not cleaning up and closing PR since TEST_NOCLEANUP is set") | ||
return | ||
} | ||
|
||
repository.NSTearDown(ctx, t, runcnx, targetNS) | ||
} | ||
|
||
func TearDownBranch(ctx context.Context, t *testing.T, runcnx *params.Run, client *scm.Client, prID int, orgAndRepo, ref string) { | ||
if os.Getenv("TEST_NOCLEANUP") == "true" { | ||
runcnx.Clients.Log.Infof("Not cleaning up and closing PR since TEST_NOCLEANUP is set") | ||
return | ||
} | ||
|
||
if prID != -1 { | ||
runcnx.Clients.Log.Infof("Deleting PR #%d", prID) | ||
_, err := client.PullRequests.DeletePullRequest(ctx, orgAndRepo, prID) | ||
assert.NilError(t, err) | ||
} | ||
|
||
if ref != "" { | ||
runcnx.Clients.Log.Infof("Deleting Branch %s", ref) | ||
_, err := client.Git.DeleteRef(ctx, orgAndRepo, ref) | ||
assert.NilError(t, err) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.