Skip to content

Commit 6d3cec1

Browse files
committed
Separate TestGithubPullRequestGitOpsComments
separate in two different test not using testcase since it really not needed to do this. Jira: https://issues.redhat.com/browse/SRVKP-7296 Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
1 parent b7279f9 commit 6d3cec1

File tree

2 files changed

+66
-62
lines changed

2 files changed

+66
-62
lines changed

test/github_pullrequest_retest_test.go

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1919
)
2020

21-
func TestGithubSecondPullRequestRetest(t *testing.T) {
21+
func TestGithubSecondPullRequestGitopsCommentRetest(t *testing.T) {
2222
if os.Getenv("NIGHTLY_E2E_TEST") != "true" {
2323
t.Skip("Skipping test since only enabled for nightly")
2424
}
@@ -56,13 +56,15 @@ func TestGithubSecondPullRequestRetest(t *testing.T) {
5656
assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionTrue)
5757
}
5858

59-
// TestGithubPullRequestGitOpsComments tests GitOps comments /test, /retest and /cancel commands.
60-
func TestGithubPullRequestGitOpsComments(t *testing.T) {
59+
// TestGithubSecondPullRequestRetest tests the retest functionality of a GitHub pull request.
60+
// It sets up a pull request, triggers a retest comment, waits for the repository to be updated,
61+
// and verifies that the repository status is set to succeeded and the correct number of PipelineRuns are created.
62+
func TestGithubSecondPullRequestGitopsCommentCancel(t *testing.T) {
6163
ctx := context.Background()
6264
g := &tgithub.PRTest{
63-
Label: "Github PullRequest GitOps Comments test",
65+
Label: "Github PullRequest Cancel",
6466
YamlFiles: []string{"testdata/pipelinerun.yaml", "testdata/pipelinerun-gitops.yaml"},
65-
SecondController: false,
67+
SecondController: true,
6668
}
6769
g.RunPullRequest(ctx, t)
6870
defer g.TearDown(ctx, t)
@@ -71,65 +73,67 @@ func TestGithubPullRequestGitOpsComments(t *testing.T) {
7173
assert.NilError(t, err)
7274
assert.Equal(t, len(pruns.Items), 2)
7375

74-
tests := []struct {
75-
name, comment string
76-
prNum int
77-
}{
78-
{
79-
name: "Retest",
80-
comment: "/retest pr-gitops-comment",
81-
prNum: 3,
82-
},
83-
{
84-
name: "Test and Cancel PipelineRun",
85-
comment: "/cancel pr-gitops-comment",
86-
prNum: 4,
87-
},
76+
g.Cnx.Clients.Log.Info("/test pr-gitops-comment on Pull Request before canceling")
77+
_, _, err = g.Provider.Client.Issues.CreateComment(ctx,
78+
g.Options.Organization,
79+
g.Options.Repo,
80+
g.PRNumber,
81+
&github.IssueComment{Body: github.Ptr("/test pr-gitops-comment")},
82+
)
83+
waitOpts := twait.Opts{
84+
RepoName: g.TargetNamespace,
85+
Namespace: g.TargetNamespace,
86+
MinNumberStatus: 3,
87+
PollTimeout: twait.DefaultTimeout,
88+
TargetSHA: g.SHA,
8889
}
89-
for _, tt := range tests {
90-
t.Run(tt.name, func(t *testing.T) {
91-
waitOpts := twait.Opts{
92-
RepoName: g.TargetNamespace,
93-
Namespace: g.TargetNamespace,
94-
MinNumberStatus: tt.prNum,
95-
PollTimeout: twait.DefaultTimeout,
96-
TargetSHA: g.SHA,
97-
}
98-
if tt.comment == "/cancel pr-gitops-comment" {
99-
g.Cnx.Clients.Log.Info("/test pr-gitops-comment on Pull Request before canceling")
100-
_, _, err := g.Provider.Client.Issues.CreateComment(ctx,
101-
g.Options.Organization,
102-
g.Options.Repo, g.PRNumber,
103-
&github.IssueComment{Body: github.Ptr("/test pr-gitops-comment")})
104-
assert.NilError(t, err)
105-
err = twait.UntilPipelineRunCreated(ctx, g.Cnx.Clients, waitOpts)
106-
assert.NilError(t, err)
107-
}
108-
g.Cnx.Clients.Log.Infof("%s on Pull Request", tt.comment)
109-
_, _, err = g.Provider.Client.Issues.CreateComment(ctx,
110-
g.Options.Organization,
111-
g.Options.Repo, g.PRNumber,
112-
&github.IssueComment{Body: github.Ptr(tt.comment)})
113-
assert.NilError(t, err)
90+
assert.NilError(t, err)
91+
err = twait.UntilPipelineRunCreated(ctx, g.Cnx.Clients, waitOpts)
92+
assert.NilError(t, err)
11493

115-
g.Cnx.Clients.Log.Info("Waiting for Repository to be updated")
116-
_, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, waitOpts)
117-
assert.NilError(t, err)
94+
g.Cnx.Clients.Log.Infof("/cancel pr-gitops-comment on Pull Request")
95+
_, _, err = g.Provider.Client.Issues.CreateComment(ctx,
96+
g.Options.Organization,
97+
g.Options.Repo, g.PRNumber,
98+
&github.IssueComment{Body: github.Ptr("/cancel pr-gitops-comment")})
99+
assert.NilError(t, err)
118100

119-
g.Cnx.Clients.Log.Infof("Check if we have the repository set as succeeded")
120-
repo, err := g.Cnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(g.TargetNamespace).Get(ctx, g.TargetNamespace, metav1.GetOptions{})
121-
assert.NilError(t, err)
122-
if tt.comment == "/cancel pr-gitops-comment" {
123-
assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionFalse)
124-
} else {
125-
assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionTrue)
126-
}
101+
waitOpts = twait.Opts{
102+
RepoName: g.TargetNamespace,
103+
Namespace: g.TargetNamespace,
104+
MinNumberStatus: 3,
105+
PollTimeout: twait.DefaultTimeout,
106+
TargetSHA: g.SHA,
107+
}
108+
g.Cnx.Clients.Log.Info("Waiting for Repository to be updated")
109+
_, err = twait.UntilRepositoryUpdated(ctx, g.Cnx.Clients, waitOpts)
110+
assert.NilError(t, err)
127111

128-
pruns, err = g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{
129-
LabelSelector: fmt.Sprintf("%s=%s", keys.SHA, g.SHA),
130-
})
131-
assert.NilError(t, err)
132-
assert.Equal(t, len(pruns.Items), tt.prNum)
133-
})
112+
g.Cnx.Clients.Log.Infof("Check if we have the repository set as succeeded")
113+
repo, err := g.Cnx.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(g.TargetNamespace).Get(ctx, g.TargetNamespace, metav1.GetOptions{})
114+
assert.NilError(t, err)
115+
assert.Equal(t, repo.Status[len(repo.Status)-1].Conditions[0].Status, corev1.ConditionFalse)
116+
117+
pruns, err = g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{
118+
LabelSelector: fmt.Sprintf("%s=%s", keys.SHA, g.SHA),
119+
})
120+
assert.NilError(t, err)
121+
assert.Equal(t, len(pruns.Items), 3)
122+
123+
// go over all pruns check that at least one is canceled and the other two are succeeded
124+
canceledCount := 0
125+
succeededCount := 0
126+
for _, prun := range pruns.Items {
127+
for _, condition := range prun.Status.Conditions {
128+
if condition.Type == "Succeeded" {
129+
if condition.Status == corev1.ConditionFalse {
130+
canceledCount++
131+
} else if condition.Status == corev1.ConditionTrue {
132+
succeededCount++
133+
}
134+
}
135+
}
134136
}
137+
assert.Equal(t, canceledCount, 1, "should have one canceled PipelineRun")
138+
assert.Equal(t, succeededCount, 2, "should have two succeeded PipelineRuns")
135139
}

test/pkg/wait/wait.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func UntilPipelineRunCreated(ctx context.Context, clients clients.Clients, opts
8181
return true, err
8282
}
8383

84-
clients.Log.Info("still waiting for pipelinerun to be created")
84+
clients.Log.Infof("waiting for pipelinerun to be created: selector %s=%s, MinNumberStatus=%d pr.Items=%d", keys.SHA, opts.TargetSHA, opts.MinNumberStatus, len(prs.Items))
8585
return len(prs.Items) == opts.MinNumberStatus, nil
8686
})
8787
}

0 commit comments

Comments
 (0)