Skip to content

Commit

Permalink
fix(tests) improve pipeline run cancellation test
Browse files Browse the repository at this point in the history
- Add logging for pipeline run creation and cancellation
- Update wait options for pipeline run creation
- Replace check run status check with pipeline run status check
- Ensure proper handling of pipeline run cancellation status
  • Loading branch information
chmouel committed Dec 18, 2024
1 parent a0cd25b commit a701846
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions test/github_pullrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import (
"time"

"github.com/google/go-github/v66/github"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
"github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
tgithub "github.com/openshift-pipelines/pipelines-as-code/test/pkg/github"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

tgithub "github.com/openshift-pipelines/pipelines-as-code/test/pkg/github"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"

twait "github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
Expand Down Expand Up @@ -224,13 +227,25 @@ func TestGithubSecondCancelInProgress(t *testing.T) {
g.RunPullRequest(ctx, t)
defer g.TearDown(ctx, t)

g.Cnx.Clients.Log.Infof("Waiting for one pipelinerun to be created")
waitOpts := twait.Opts{
RepoName: g.TargetNamespace,
Namespace: g.TargetNamespace,
MinNumberStatus: 1,
PollTimeout: twait.DefaultTimeout,
TargetSHA: g.SHA,
}
err := twait.UntilPipelineRunCreated(ctx, g.Cnx.Clients, waitOpts)
assert.NilError(t, err)
time.Sleep(10 * time.Second)

g.Cnx.Clients.Log.Infof("Creating /retest on PullRequest")
_, _, err := g.Provider.Client.Issues.CreateComment(ctx, g.Options.Organization, g.Options.Repo, g.PRNumber,
_, _, err = g.Provider.Client.Issues.CreateComment(ctx, g.Options.Organization, g.Options.Repo, g.PRNumber,
&github.IssueComment{Body: github.String("/retest")})
assert.NilError(t, err)

g.Cnx.Clients.Log.Infof("Waiting for the two pipelinerun to be created")
waitOpts := twait.Opts{
waitOpts = twait.Opts{
RepoName: g.TargetNamespace,
Namespace: g.TargetNamespace,
MinNumberStatus: 2,
Expand All @@ -241,18 +256,31 @@ func TestGithubSecondCancelInProgress(t *testing.T) {
assert.NilError(t, err)

g.Cnx.Clients.Log.Infof("Sleeping for 10 seconds to let the pipelinerun to be canceled")
time.Sleep(10 * time.Second)

res, resp, err := g.Provider.Client.Checks.ListCheckRunsForRef(ctx, g.Options.Organization, g.Options.Repo, g.SHA, &github.ListCheckRunsOptions{
AppID: g.Provider.ApplicationID,
ListOptions: github.ListOptions{},
})
assert.NilError(t, err)
assert.Equal(t, resp.StatusCode, 200)
i := 0
foundCancelled := false
for i < 10 {
prs, err := g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", keys.SHA, g.SHA),
})
assert.NilError(t, err)

assert.Equal(t, res.CheckRuns[0].GetConclusion(), "cancelled")
}
for _, pr := range prs.Items {
if pr.GetStatusCondition() == nil {
continue
}
if pr.Status.Conditions[0].Reason == "Cancelled" {
g.Cnx.Clients.Log.Infof("PipelineRun %s has been canceled", pr.Name)
foundCancelled = true
break
}
}
if foundCancelled {
break
}

// Local Variables:
// compile-command: "go test -tags=e2e -v -info TestGithubPullRequest$ ."
// End:
time.Sleep(5 * time.Second)
i++
}
assert.Assert(t, foundCancelled, "No Pipelines has been found cancedl in NS %s", g.TargetNamespace)
}

0 comments on commit a701846

Please sign in to comment.