Skip to content

Commit

Permalink
Configure E2E test setup on PAC for bitbucket server
Browse files Browse the repository at this point in the history
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
zakisk committed Dec 31, 2024
1 parent e0d0ad7 commit cbb5d04
Show file tree
Hide file tree
Showing 50 changed files with 6,388 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/google/go-github/v64 v64.0.0
github.com/google/go-github/v66 v66.0.0
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b
github.com/jenkins-x/go-scm v1.14.52
github.com/jonboulle/clockwork v0.4.0
github.com/juju/ansiterm v1.0.0
github.com/ktrysmt/go-bitbucket v0.9.81
Expand Down
14 changes: 12 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWS
github.com/grpc-ecosystem/grpc-gateway v1.14.6/go.mod h1:zdiPV4Yse/1gnckTHtghG4GkDEdKCRJduHpTxT3/jcw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 h1:ad0vkEBuk23VJzZR9nkLVG0YAoN9coASF1GusYX6AlU=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0/go.mod h1:igFoXX2ELCW06bol23DWPB5BEWfZISOzSP5K2sbLea0=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b h1:wDUNC2eKiL35DbLvsDhiblTUXHxcOPwQSCzi7xpQUN4=
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b/go.mod h1:VzxiSdG6j1pi7rwGm/xYI5RbtpBgM8sARDXlvEvxlu0=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand All @@ -289,6 +291,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jenkins-x/go-scm v1.14.52 h1:y2tbEFwyQLpFke0uKOq60Fa3wv95yIyxWorFiGhY6OI=
github.com/jenkins-x/go-scm v1.14.52/go.mod h1:1RPxLZndnvu31XhFZ+RTvXiHmMX70HkQ17bRupTQxGs=
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand Down Expand Up @@ -437,9 +441,15 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/githubv4 v0.0.0-20190718010115-4ba037080260 h1:xKXiRdBUtMVp64NaxACcyX4kvfmHJ9KrLU+JvyB1mdM=
github.com/shurcooL/githubv4 v0.0.0-20190718010115-4ba037080260/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f h1:tygelZueB1EtXkPI6mQ4o9DQ0+FKW41hTbunoXZCTqk=
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down Expand Up @@ -592,8 +602,6 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -860,6 +868,8 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/evanphx/json-patch.v4 v4.12.0 h1:n6jtcsulIzXPJaxegRbvFNNrZDjbij7ny3gmSPG+6V4=
gopkg.in/evanphx/json-patch.v4 v4.12.0/go.mod h1:p8EYWUEYMpynmqDbY58zCKCFZw8pRWMG4EsWvDvM72M=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
Expand Down
54 changes: 54 additions & 0 deletions test/bitbucket_server_pull_request_test.go
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:
60 changes: 60 additions & 0 deletions test/pkg/bitbucketserver/crd.go
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
}
63 changes: 63 additions & 0 deletions test/pkg/bitbucketserver/pr.go
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
}
98 changes: 98 additions & 0 deletions test/pkg/bitbucketserver/setup.go
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)
}
}
3 changes: 3 additions & 0 deletions vendor/github.com/jenkins-x/go-scm/COPYRIGHT

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions vendor/github.com/jenkins-x/go-scm/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cbb5d04

Please sign in to comment.