Skip to content

Commit 755cbbd

Browse files
chmouelsavitaashture
authored andcommitted
Add e2e for pass-arguments-to-gitops-comments
The feature gets test on on-comment and when using /test explicitly. We test all different argument parsing, we had to refactor the tests to accept golden files to use that for comparison which is a bit better than regexp in some case. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent bf0a99c commit 755cbbd

10 files changed

+80
-55
lines changed

test/gitea_access_control_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,7 @@ func TestGiteaACLCommentsAllowing(t *testing.T) {
259259
tgitea.WaitForPullRequestCommentMatch(t, topts)
260260
tgitea.WaitForStatus(t, topts, topts.PullRequest.Head.Sha, "", false)
261261
// checking the pod log to make sure /test <prname> works
262-
err = twait.RegexpMatchingInPodLog(context.Background(), topts.ParamsRun, topts.TargetNS,
263-
"pipelinesascode.tekton.dev/event-type=pull_request",
264-
"step-task", *regexp.MustCompile(".*MOTO"), 2)
262+
err = twait.RegexpMatchingInPodLog(context.Background(), topts.ParamsRun, topts.TargetNS, "pipelinesascode.tekton.dev/event-type=pull_request", "step-task", *regexp.MustCompile(".*MOTO"), "", 2)
265263
assert.NilError(t, err, "Error while checking the logs of the pods")
266264
})
267265
}

test/gitops_commands_test.go renamed to test/gitea_gitops_commands_test.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build e2e
2-
// +build e2e
32

43
package test
54

@@ -11,6 +10,7 @@ import (
1110
"testing"
1211
"time"
1312

13+
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
1414
"github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
1515
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
1616
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/cctx"
@@ -55,6 +55,20 @@ func TestGiteaOnCommentAnnotation(t *testing.T) {
5555
ctx := context.Background()
5656
topts := &tgitea.TestOpts{
5757
TargetRefName: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test"),
58+
RepoCRParams: &[]v1alpha1.Params{
59+
{
60+
Name: "custom1",
61+
Value: "foo",
62+
},
63+
{
64+
Name: "custom2",
65+
Value: "bar",
66+
},
67+
{
68+
Name: "custom3",
69+
Value: "moto",
70+
},
71+
},
5872
}
5973
triggerComment := "/hello-world"
6074
topts.TargetNS = topts.TargetRefName
@@ -94,10 +108,19 @@ func TestGiteaOnCommentAnnotation(t *testing.T) {
94108
assert.Equal(t, *repo.Status[len(repo.Status)-1].EventType, opscomments.OnCommentEventType.String(), "should have a on comment event type")
95109

96110
last := repo.Status[len(repo.Status)-1]
97-
err = twait.RegexpMatchingInPodLog(context.Background(),
98-
topts.ParamsRun,
99-
topts.TargetNS,
100-
fmt.Sprintf("tekton.dev/pipelineRun=%s", last.PipelineRunName), "step-task", *regexp.MustCompile(triggerComment), 2)
111+
err = twait.RegexpMatchingInPodLog(context.Background(), topts.ParamsRun, topts.TargetNS, fmt.Sprintf("tekton.dev/pipelineRun=%s", last.PipelineRunName), "step-task", *regexp.MustCompile(triggerComment), "", 2)
112+
assert.NilError(t, err)
113+
114+
tgitea.PostCommentOnPullRequest(t, topts, fmt.Sprintf(`%s revision=main custom1=thisone custom2="another one" custom3="a \"quote\""`, triggerComment))
115+
waitOpts.MinNumberStatus = 4
116+
repo, err = twait.UntilRepositoryUpdated(context.Background(), topts.ParamsRun.Clients, waitOpts)
117+
assert.NilError(t, err)
118+
assert.Equal(t, len(repo.Status), waitOpts.MinNumberStatus, fmt.Sprintf("should have only %d status", waitOpts.MinNumberStatus))
119+
assert.Equal(t, *repo.Status[len(repo.Status)-1].EventType, opscomments.OnCommentEventType.String(), "should have a on comment event type")
120+
// now we should have only 3 status, the last one is the on comment match with an argument redefining the revision which is a standard parameter
121+
122+
last = repo.Status[len(repo.Status)-1]
123+
err = twait.RegexpMatchingInPodLog(context.Background(), topts.ParamsRun, topts.TargetNS, fmt.Sprintf("tekton.dev/pipelineRun=%s", last.PipelineRunName), "step-task", regexp.Regexp{}, t.Name(), 2)
101124
assert.NilError(t, err)
102125
}
103126

@@ -108,6 +131,12 @@ func TestGiteaTestPipelineRunExplicitelyWithTestComment(t *testing.T) {
108131
ctx := context.Background()
109132
topts := &tgitea.TestOpts{
110133
TargetRefName: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test"),
134+
RepoCRParams: &[]v1alpha1.Params{
135+
{
136+
Name: "custom",
137+
Value: "foo",
138+
},
139+
},
111140
}
112141
topts.TargetNS = topts.TargetRefName
113142
topts.ParamsRun, topts.Opts, topts.GiteaCNX, err = tgitea.Setup(ctx)
@@ -124,7 +153,7 @@ func TestGiteaTestPipelineRunExplicitelyWithTestComment(t *testing.T) {
124153
_, f := tgitea.TestPR(t, topts)
125154
defer f()
126155
targetPrName := "no-match"
127-
tgitea.PostCommentOnPullRequest(t, topts, "/test "+targetPrName)
156+
tgitea.PostCommentOnPullRequest(t, topts, fmt.Sprintf("/test %s custom=awesome", targetPrName))
128157
tgitea.WaitForStatus(t, topts, "heads/"+topts.TargetRefName, "", false)
129158
waitOpts := twait.Opts{
130159
RepoName: topts.TargetNS,
@@ -139,6 +168,10 @@ func TestGiteaTestPipelineRunExplicitelyWithTestComment(t *testing.T) {
139168
assert.Equal(t, *repo.Status[0].EventType, opscomments.TestSingleCommentEventType.String(), "should have a test comment event in status")
140169
assert.Assert(t, strings.HasPrefix(repo.Status[0].PipelineRunName, targetPrName+"-"),
141170
"we didn't target the proper pipelinerun, we tested: %s", repo.Status[0].PipelineRunName)
171+
172+
last := repo.Status[len(repo.Status)-1]
173+
err = twait.RegexpMatchingInPodLog(context.Background(), topts.ParamsRun, topts.TargetNS, fmt.Sprintf("tekton.dev/pipelineRun=%s", last.PipelineRunName), "step-task", *regexp.MustCompile("custom is awesome"), "", 2)
174+
assert.NilError(t, err)
142175
}
143176

144177
func TestGiteaRetestAll(t *testing.T) {

test/gitea_params_test.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,9 @@ func TestGiteaParamsOnRepoCR(t *testing.T) {
187187
assert.NilError(t, err)
188188
assert.Assert(t, len(repo.Status) != 0)
189189
assert.NilError(t,
190-
twait.RegexpMatchingInPodLog(context.Background(),
191-
topts.ParamsRun,
192-
topts.TargetNS,
193-
fmt.Sprintf("tekton.dev/pipelineRun=%s,tekton.dev/pipelineTask=params",
194-
repo.Status[0].PipelineRunName),
195-
"step-test-params-value", *regexp.MustCompile(
196-
"I am the most Kawaī params\nSHHHHHHH\nFollow me on my ig #nofilter\n{{ no_match }}\nHey I show up from a payload match\n{{ secret_nothere }}"), 2))
190+
twait.RegexpMatchingInPodLog(context.Background(), topts.ParamsRun, topts.TargetNS, fmt.Sprintf("tekton.dev/pipelineRun=%s,tekton.dev/pipelineTask=params",
191+
repo.Status[0].PipelineRunName), "step-test-params-value", *regexp.MustCompile(
192+
"I am the most Kawaī params\nSHHHHHHH\nFollow me on my ig #nofilter\n{{ no_match }}\nHey I show up from a payload match\n{{ secret_nothere }}"), "", 2))
197193
}
198194

199195
// TestGiteaParamsBodyHeadersCEL Test that we can access the pull request body and headers in params
@@ -223,12 +219,8 @@ func TestGiteaParamsBodyHeadersCEL(t *testing.T) {
223219
// check the output logs if the CEL body headers has expanded properly
224220
output := `Look mum I know that we are acting on a pull_request
225221
my email is a true beauty and like groot, I AM pac`
226-
err = twait.RegexpMatchingInPodLog(context.Background(),
227-
topts.ParamsRun,
228-
topts.TargetNS,
229-
fmt.Sprintf("tekton.dev/pipelineRun=%s,tekton.dev/pipelineTask=cel-pullrequest-params",
230-
repo.Status[0].PipelineRunName),
231-
"step-test-cel-params-value", *regexp.MustCompile(output), 2)
222+
err = twait.RegexpMatchingInPodLog(context.Background(), topts.ParamsRun, topts.TargetNS, fmt.Sprintf("tekton.dev/pipelineRun=%s,tekton.dev/pipelineTask=cel-pullrequest-params",
223+
repo.Status[0].PipelineRunName), "step-test-cel-params-value", *regexp.MustCompile(output), "", 2)
232224
assert.NilError(t, err)
233225

234226
// Merge the pull request so we can generate a push event and wait that it is updated
@@ -266,12 +258,7 @@ my email is a true beauty and like groot, I AM pac`
266258
// push matching the expanded CEL body and headers values
267259
output = `Look mum I know that we are acting on a push
268260
my email is a true beauty and you can call me pacman`
269-
err = twait.RegexpMatchingInPodLog(context.Background(),
270-
topts.ParamsRun,
271-
topts.TargetNS,
272-
fmt.Sprintf("tekton.dev/pipelineRun=%s,tekton.dev/pipelineTask=cel-push-params",
273-
sortedstatus[0].PipelineRunName),
274-
"step-test-cel-params-value", *regexp.MustCompile(output), 2)
261+
err = twait.RegexpMatchingInPodLog(context.Background(), topts.ParamsRun, topts.TargetNS, fmt.Sprintf("tekton.dev/pipelineRun=%s,tekton.dev/pipelineTask=cel-push-params", sortedstatus[0].PipelineRunName), "step-test-cel-params-value", *regexp.MustCompile(output), "", 2)
275262
assert.NilError(t, err)
276263
}
277264

test/github_incoming_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,7 @@ func verifyIncomingWebhook(t *testing.T, randomedString string, entries map[stri
169169
}
170170
assert.Assert(t, prName == "pipelinerun-incoming")
171171

172-
err = wait.RegexpMatchingInPodLog(context.Background(),
173-
runcnx, randomedString,
174-
"pipelinesascode.tekton.dev/event-type=incoming",
175-
"step-task", *regexp.MustCompile(".*It's a Bird... It's a Plane... It's Superman"), 2)
172+
err = wait.RegexpMatchingInPodLog(context.Background(), runcnx, randomedString, "pipelinesascode.tekton.dev/event-type=incoming", "step-task", *regexp.MustCompile(".*It's a Bird... It's a Plane... It's Superman"), "", 2)
176173
assert.NilError(t, err, "Error while checking the logs of the pods")
177174
}
178175

test/github_pullrequest_test_comment_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,6 @@ func TestGithubSecondOnCommentAnnotation(t *testing.T) {
9090
assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue)
9191
lastPrName := repo.Status[len(repo.Status)-1].PipelineRunName
9292

93-
err = twait.RegexpMatchingInPodLog(
94-
context.Background(),
95-
g.Cnx,
96-
g.TargetNamespace,
97-
fmt.Sprintf("tekton.dev/pipelineRun=%s", lastPrName),
98-
"step-task",
99-
*regexp.MustCompile(triggerComment),
100-
2)
93+
err = twait.RegexpMatchingInPodLog(context.Background(), g.Cnx, g.TargetNamespace, fmt.Sprintf("tekton.dev/pipelineRun=%s", lastPrName), "step-task", *regexp.MustCompile(triggerComment), "", 2)
10194
assert.NilError(t, err)
10295
}

test/gitlab_merge_request_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,7 @@ func TestGitlabOnComment(t *testing.T) {
206206
assert.Assert(t, repo.Status[len(repo.Status)-1].Conditions[0].Status == corev1.ConditionTrue)
207207
lastPrName := repo.Status[len(repo.Status)-1].PipelineRunName
208208

209-
err = twait.RegexpMatchingInPodLog(
210-
context.Background(),
211-
runcnx,
212-
targetNS,
213-
fmt.Sprintf("tekton.dev/pipelineRun=%s", lastPrName),
214-
"step-task",
215-
*regexp.MustCompile(triggerComment),
216-
2)
209+
err = twait.RegexpMatchingInPodLog(context.Background(), runcnx, targetNS, fmt.Sprintf("tekton.dev/pipelineRun=%s", lastPrName), "step-task", *regexp.MustCompile(triggerComment), "", 2)
217210
assert.NilError(t, err)
218211
}
219212

test/pkg/wait/logs.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,31 @@ func RegexpMatchingInControllerLog(ctx context.Context, clients *params.Run, reg
3636
return fmt.Errorf("could not find a match using the labelSelector: %s in container %s for regexp: %s", labelselector, containerName, reg.String())
3737
}
3838

39-
func RegexpMatchingInPodLog(ctx context.Context, clients *params.Run, ns, labelselector, containerName string, reg regexp.Regexp, maxNumberOfLoop int) error {
39+
func RegexpMatchingInPodLog(ctx context.Context, clients *params.Run, ns, labelselector, containerName string, reg regexp.Regexp, goldenFile string, maxNumberOfLoop int) error {
4040
var err error
4141
output := ""
42-
clients.Clients.Log.Infof("looking for regexp %s in namespace: %s for label %s and container %s", reg.String(), ns, labelselector, containerName)
42+
if goldenFile != "" {
43+
goldenFile = strings.ReplaceAll(fmt.Sprintf("%s.golden", goldenFile), "/", "-")
44+
}
45+
if reg.String() != "" {
46+
clients.Clients.Log.Infof("looking for regexp %s in namespace: %s for label %s and container %s", reg.String(), ns, labelselector, containerName)
47+
} else {
48+
clients.Clients.Log.Infof("looking for matching content of file %s in namespace: %s for label %s and container %s", goldenFile, ns, labelselector, containerName)
49+
}
50+
4351
for i := 0; i <= maxNumberOfLoop; i++ {
4452
output, err = tlogs.GetPodLog(ctx, clients.Clients.Kube.CoreV1(), ns, labelselector, containerName, github.Int64(10))
4553
if err != nil {
4654
return err
4755
}
48-
49-
if reg.MatchString(output) {
50-
clients.Clients.Log.Infof("matched regexp in labelSelector/container %s:%s",
51-
labelselector, containerName)
56+
if goldenFile != "" {
57+
if golden.String(output, goldenFile)().Success() {
58+
clients.Clients.Log.Infof("matched file content %s in labelSelector/container %s:%s", goldenFile, labelselector, containerName)
59+
return nil
60+
}
61+
} else if reg.MatchString(output) {
62+
clients.Clients.Log.Infof("matched regexp %s in labelSelector/container %s:%s",
63+
reg.String(), labelselector, containerName)
5264
return nil
5365
}
5466
time.Sleep(5 * time.Second)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The comment is:
2+
/hello-world revision=main custom1=thisone custom2="another one" custom3="a \"quote\""
3+
The revision is main
4+
The custom1 value is thisone
5+
The custom2 value is another one
6+
The custom3 value is a quote

test/testdata/pipelinerun-nomatch.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ spec:
1414
steps:
1515
- name: task
1616
image: registry.access.redhat.com/ubi9/ubi-micro
17-
command: ["/bin/echo", "NO MATCH"]
17+
script: |
18+
echo "custom is {{ custom }}"
19+
echo "NO MATCH"

test/testdata/pipelinerun-on-comment-annotation.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ spec:
2020
cat <<EOF
2121
{{ trigger_comment }}
2222
EOF
23+
echo "The revision is {{ revision }}"
24+
echo "The custom1 value is {{ custom1 }}"
25+
echo "The custom2 value is {{ custom2 }}"
26+
echo "The custom3 value is {{ custom3 }}"

0 commit comments

Comments
 (0)