Skip to content

Commit

Permalink
Add end-to-end testing for clean GitOps comment in the pull request
Browse files Browse the repository at this point in the history
Signed-off-by: Savita Ashture <[email protected]>
  • Loading branch information
savitaashture authored and chmouel committed Nov 17, 2023
1 parent 6c4db0c commit 0f662c2
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 44 deletions.
91 changes: 64 additions & 27 deletions test/github_pullrequest_retest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestGithubPullRequestRetest(t *testing.T) {
if os.Getenv("NIGHTLY_E2E_TEST") != "true" {
t.Skip("Skipping test since only enabled for nightly")
}
ctx := context.TODO()
ctx := context.Background()
runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, sha := tgithub.RunPullRequest(ctx, t,
"Github Retest comment", []string{"testdata/pipelinerun.yaml"}, false)
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)
Expand All @@ -48,41 +48,78 @@ func TestGithubPullRequestRetest(t *testing.T) {
runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded")
repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{})
assert.NilError(t, err)
assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue)
assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionTrue)
}

func TestGithubPullRequestRetestForParticularPipelineRun(t *testing.T) {
ctx := context.TODO()
func TestGithubPullRequestGitOpsComments(t *testing.T) {
ctx := context.Background()
runcnx, ghcnx, opts, targetNS, targetRefName, prNumber, sha := tgithub.RunPullRequest(ctx, t,
"Github Retest comment", []string{"testdata/pipelinerun.yaml", "testdata/pipelinerun-gitops.yaml"}, false)
defer tgithub.TearDown(ctx, t, runcnx, ghcnx, prNumber, targetRefName, targetNS, opts)

runcnx.Clients.Log.Infof("Creating /retest pipelinerun-name in PullRequest")
_, _, err := ghcnx.Client.Issues.CreateComment(ctx,
opts.Organization,
opts.Repo, prNumber,
&github.IssueComment{Body: github.String("/retest pr-gitops-comment")})
pruns, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{})
assert.NilError(t, err)
assert.Equal(t, len(pruns.Items), 2)

runcnx.Clients.Log.Infof("Wait for the repository update to be updated")
waitOpts := twait.Opts{
RepoName: targetNS,
Namespace: targetNS,
MinNumberStatus: 3,
PollTimeout: twait.DefaultTimeout,
TargetSHA: sha,
tests := []struct {
name, comment string
prNum int
}{
{
name: "Retest",
comment: "/retest pr-gitops-comment",
prNum: 3,
},
{
name: "Test and Cancel PipelineRun",
comment: "/cancel pr-gitops-comment",
prNum: 4,
},
}
err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts)
assert.NilError(t, err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
waitOpts := twait.Opts{
RepoName: targetNS,
Namespace: targetNS,
MinNumberStatus: tt.prNum,
PollTimeout: twait.DefaultTimeout,
TargetSHA: sha,
}
if tt.comment == "/cancel pr-gitops-comment" {
runcnx.Clients.Log.Info("/test pr-gitops-comment on Pull Request before canceling")
_, _, err := ghcnx.Client.Issues.CreateComment(ctx,
opts.Organization,
opts.Repo, prNumber,
&github.IssueComment{Body: github.String("/test pr-gitops-comment")})
assert.NilError(t, err)
err = twait.UntilPipelineRunCreated(ctx, runcnx.Clients, waitOpts)
assert.NilError(t, err)
}
runcnx.Clients.Log.Infof("%s on Pull Request", tt.comment)
_, _, err = ghcnx.Client.Issues.CreateComment(ctx,
opts.Organization,
opts.Repo, prNumber,
&github.IssueComment{Body: github.String(tt.comment)})
assert.NilError(t, err)

runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded")
repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{})
assert.NilError(t, err)
assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue)
runcnx.Clients.Log.Info("Waiting for Repository to be updated")
err = twait.UntilRepositoryUpdated(ctx, runcnx.Clients, waitOpts)
assert.NilError(t, err)

prs, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", keys.SHA, sha),
})
assert.NilError(t, err)
assert.Equal(t, len(prs.Items), 3)
runcnx.Clients.Log.Infof("Check if we have the repository set as succeeded")
repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{})
assert.NilError(t, err)
if tt.comment == "/cancel pr-gitops-comment" {
assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionFalse)
} else {
assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionTrue)
}

pruns, err = runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", keys.SHA, sha),
})
assert.NilError(t, err)
assert.Equal(t, len(pruns.Items), tt.prNum)
})
}
}
21 changes: 6 additions & 15 deletions test/github_push_retest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ package test

import (
"context"
"fmt"
"testing"

"github.com/google/go-github/v56/github"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
tgithub "github.com/openshift-pipelines/pipelines-as-code/test/pkg/github"
twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
"gotest.tools/v3/assert"
Expand All @@ -25,7 +23,7 @@ func TestGithubPushRequestGitOpsComments(t *testing.T) {

pruns, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{})
assert.NilError(t, err)
assert.Assert(t, len(pruns.Items) == 2)
assert.Equal(t, len(pruns.Items), 2)

tests := []struct {
name, comment string
Expand Down Expand Up @@ -58,15 +56,8 @@ func TestGithubPushRequestGitOpsComments(t *testing.T) {
opts.Repo, sha,
&github.RepositoryComment{Body: github.String("/test pipelinerun-on-push branch:" + targetNS)})
assert.NilError(t, err)
for {
prs, err := runcnx.Clients.Tekton.TektonV1().PipelineRuns(waitOpts.Namespace).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", keys.SHA, waitOpts.TargetSHA),
})
assert.NilError(t, err)
if len(prs.Items) == tt.prNum {
break
}
}
err = twait.UntilPipelineRunCreated(ctx, runcnx.Clients, waitOpts)
assert.NilError(t, err)
}
runcnx.Clients.Log.Infof("%s on Push Request", tt.comment)
_, _, err = ghcnx.Client.Repositories.CreateComment(ctx,
Expand All @@ -83,14 +74,14 @@ func TestGithubPushRequestGitOpsComments(t *testing.T) {
repo, err := runcnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(targetNS).Get(ctx, targetNS, metav1.GetOptions{})
assert.NilError(t, err)
if tt.comment == "/cancel pipelinerun-on-push branch:"+targetNS {
assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionFalse)
assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionFalse)
} else {
assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue)
assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionTrue)
}

pruns, err = runcnx.Clients.Tekton.TektonV1().PipelineRuns(targetNS).List(ctx, metav1.ListOptions{})
assert.NilError(t, err)
assert.Assert(t, len(pruns.Items) == tt.prNum)
assert.Equal(t, len(pruns.Items), tt.prNum)
})
}
}
17 changes: 17 additions & 0 deletions test/pkg/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,20 @@ func UntilRepositoryUpdated(ctx context.Context, clients clients.Clients, opts O
return len(r.Status) >= opts.MinNumberStatus, nil
})
}

func UntilPipelineRunCreated(ctx context.Context, clients clients.Clients, opts Opts) error {
ctx, cancel := context.WithTimeout(ctx, opts.PollTimeout)
defer cancel()
return kubeinteraction.PollImmediateWithContext(ctx, opts.PollTimeout, func() (bool, error) {
clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(opts.Namespace)
prs, err := clients.Tekton.TektonV1().PipelineRuns(opts.Namespace).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", keys.SHA, opts.TargetSHA),
})
if err != nil {
return true, err
}

clients.Log.Info("still waiting for pipelinerun to be created")
return len(prs.Items) == opts.MinNumberStatus, nil
})
}
5 changes: 4 additions & 1 deletion test/testdata/pipelinerun-gitops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ spec:
steps:
- name: task
image: registry.access.redhat.com/ubi9/ubi-micro
command: ["/bin/echo", "HELLOMOTO"]
script: |
echo "HELLOMOTO"
sleep 10
exit 0
5 changes: 4 additions & 1 deletion test/testdata/pipelinerun-on-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ spec:
steps:
- name: task
image: registry.access.redhat.com/ubi9/ubi-micro
command: ["/bin/echo", "HELLOMOTO"]
script: |
echo "HELLOMOTO"
sleep 10
exit 0

0 comments on commit 0f662c2

Please sign in to comment.