-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathtest.go
More file actions
636 lines (582 loc) · 21.4 KB
/
test.go
File metadata and controls
636 lines (582 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package gitea
import (
"context"
"fmt"
"os"
"regexp"
"sort"
"strings"
"testing"
"time"
"code.gitea.io/sdk/gitea"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
pgitea "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitea"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/cctx"
tlogs "github.com/openshift-pipelines/pipelines-as-code/test/pkg/logs"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
pacrepo "github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/scm"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/names"
"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type TestOpts struct {
TargetRepoName string
StatusOnlyLatest bool
OnOrg bool
NoPullRequestCreation bool
SkipEventsCheck bool
TargetNS string
TargetEvent string
Settings *v1alpha1.Settings
Regexp *regexp.Regexp
YAMLFiles map[string]string
ExtraArgs map[string]string
RepoCRParams *[]v1alpha1.Params
GlobalRepoCRParams *[]v1alpha1.Params
CheckForStatus string
TargetRefName string
CheckForNumberStatus int
ConcurrencyLimit *int
ParamsRun *params.Run
GiteaCNX pgitea.Provider
Opts options.E2E
PullRequest *gitea.PullRequest
DefaultBranch string
GitCloneURL string
GitHTMLURL string
GiteaAPIURL string
GiteaPassword string
ExpectEvents bool
InternalGiteaURL string
Token string
SHA string
FileChanges []scm.FileChange
}
func PostCommentOnPullRequest(t *testing.T, topt *TestOpts, body string) {
_, _, err := topt.GiteaCNX.Client.CreateIssueComment(topt.Opts.Organization,
topt.Opts.Repo, topt.PullRequest.Index,
gitea.CreateIssueCommentOption{Body: body})
topt.ParamsRun.Clients.Log.Infof("Posted comment \"%s\" in %s", body, topt.PullRequest.HTMLURL)
assert.NilError(t, err)
}
func checkEvents(t *testing.T, events *corev1.EventList, topts *TestOpts) {
t.Helper()
newEvents := make([]corev1.Event, 0)
// filter out events that are not related to the test like checking for cancelled pipelineruns
for i := len(events.Items) - 1; i >= 0; i-- {
topts.ParamsRun.Clients.Log.Infof("Reason is %s", events.Items[i].Reason)
if events.Items[i].Reason == "CancelInProgress" {
continue
}
newEvents = append(newEvents, events.Items[i])
}
if len(newEvents) > 0 {
topts.ParamsRun.Clients.Log.Infof("0 events expected in case of failure but got %d", len(newEvents))
for _, em := range newEvents {
topts.ParamsRun.Clients.Log.Infof("Event: Reason: %s Type: %s ReportingInstance: %s Message: %s", em.Reason, em.Type, em.ReportingInstance, em.Message)
}
t.FailNow()
}
}
func AddLabelToIssue(t *testing.T, topt *TestOpts, label string) {
var targetID int64
allLabels, _, err := topt.GiteaCNX.Client.ListRepoLabels(topt.Opts.Organization, topt.Opts.Repo, gitea.ListLabelsOptions{})
assert.NilError(t, err)
for _, l := range allLabels {
if l.Name == label {
targetID = l.ID
}
}
opt := gitea.IssueLabelsOption{Labels: []int64{targetID}}
_, _, err = topt.GiteaCNX.Client.AddIssueLabels(topt.Opts.Organization, topt.Opts.Repo, topt.PullRequest.Index, opt)
assert.NilError(t, err)
topt.ParamsRun.Clients.Log.Infof("Added label \"%s\" to %s", label, topt.PullRequest.HTMLURL)
}
// TestPR will test the pull request event and grab comments from the PR.
func TestPR(t *testing.T, topts *TestOpts) (context.Context, func()) {
ctx := context.Background()
if topts.ParamsRun == nil {
runcnx, opts, giteacnx, err := Setup(ctx)
assert.NilError(t, err, fmt.Errorf("cannot do gitea setup: %w", err))
topts.GiteaCNX = giteacnx
topts.ParamsRun = runcnx
topts.Opts = opts
}
ctx, err := cctx.GetControllerCtxInfo(ctx, topts.ParamsRun)
assert.NilError(t, err)
giteaURL := os.Getenv("TEST_GITEA_API_URL")
giteaPassword := os.Getenv("TEST_GITEA_PASSWORD")
topts.GiteaAPIURL = giteaURL
topts.GiteaPassword = giteaPassword
hookURL := os.Getenv("TEST_GITEA_SMEEURL")
topts.InternalGiteaURL = os.Getenv("TEST_GITEA_INTERNAL_URL")
if topts.InternalGiteaURL == "" {
// default to internal forgejo as deployed by the helm
topts.InternalGiteaURL = "http://forgejo-http.forgejo.svc.cluster.local:3000"
}
if topts.ExtraArgs == nil {
topts.ExtraArgs = map[string]string{}
}
topts.ExtraArgs["ProviderURL"] = topts.InternalGiteaURL
if topts.TargetNS == "" {
topts.TargetNS = topts.TargetRefName
}
if topts.TargetRefName == "" {
topts.TargetRefName = names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test")
topts.TargetNS = topts.TargetRefName
}
if err := pacrepo.CreateNS(ctx, topts.TargetNS, topts.ParamsRun); err != nil {
t.Logf("error creating namespace %s: %v", topts.TargetNS, err)
}
if topts.TargetRepoName == "" {
topts.TargetRepoName = topts.TargetRefName
}
if topts.DefaultBranch == "" {
topts.DefaultBranch = options.MainBranch
}
repoInfo, err := CreateGiteaRepo(topts.GiteaCNX.Client, topts.Opts.Organization, topts.TargetRepoName, topts.DefaultBranch, hookURL, topts.OnOrg, topts.ParamsRun.Clients.Log)
assert.NilError(t, err)
topts.Opts.Repo = repoInfo.Name
topts.Opts.Organization = repoInfo.Owner.UserName
topts.DefaultBranch = repoInfo.DefaultBranch
topts.GitHTMLURL = repoInfo.HTMLURL
topts.Token, err = CreateToken(topts)
assert.NilError(t, err)
gp := &v1alpha1.GitProvider{
Type: "gitea",
// caveat this assume gitea running on the same cluster, which
// we do and need for e2e tests but that may be changed somehow
URL: topts.InternalGiteaURL,
Secret: &v1alpha1.Secret{Name: topts.TargetNS, Key: "token"},
}
spec := v1alpha1.RepositorySpec{
URL: topts.GitHTMLURL,
ConcurrencyLimit: topts.ConcurrencyLimit,
Params: topts.RepoCRParams,
Settings: topts.Settings,
}
if topts.GlobalRepoCRParams == nil {
spec.GitProvider = gp
} else {
spec.GitProvider = &v1alpha1.GitProvider{Type: "gitea"}
}
assert.NilError(t, CreateCRD(ctx, topts, spec, false))
// we only test params for global repo settings for now we may change that if we want
if topts.GlobalRepoCRParams != nil {
spec := v1alpha1.RepositorySpec{
Params: topts.GlobalRepoCRParams,
GitProvider: gp,
}
assert.NilError(t, CreateCRD(ctx, topts, spec, true))
}
cleanup := func() {
if os.Getenv("TEST_NOCLEANUP") != "true" {
defer TearDown(ctx, t, topts)
}
}
url, err := scm.MakeGitCloneURL(repoInfo.CloneURL, os.Getenv("TEST_GITEA_USERNAME"), os.Getenv("TEST_GITEA_PASSWORD"))
assert.NilError(t, err)
topts.GitCloneURL = url
if topts.NoPullRequestCreation {
return ctx, cleanup
}
entries, err := payload.GetEntries(topts.YAMLFiles,
topts.TargetNS,
repoInfo.DefaultBranch,
topts.TargetEvent,
topts.ExtraArgs)
assert.NilError(t, err)
scmOpts := &scm.Opts{
GitURL: topts.GitCloneURL,
Log: topts.ParamsRun.Clients.Log,
WebURL: topts.GitHTMLURL,
TargetRefName: topts.TargetRefName,
BaseRefName: topts.DefaultBranch,
}
topts.SHA = scm.PushFilesToRefGit(t, scmOpts, entries)
topts.ParamsRun.Clients.Log.Infof("Creating PullRequest")
for i := 0; i < 5; i++ {
if topts.PullRequest, _, err = topts.GiteaCNX.Client.CreatePullRequest(topts.Opts.Organization, repoInfo.Name, gitea.CreatePullRequestOption{
Title: "Test Pull Request - " + topts.TargetRefName,
Head: topts.TargetRefName,
Base: topts.DefaultBranch,
}); err == nil {
break
}
topts.ParamsRun.Clients.Log.Infof("Creating PullRequest has failed, retrying %d/%d, err", i, 5, err)
if i == 4 {
t.Fatalf("cannot create pull request: %v", err)
}
time.Sleep(5 * time.Second)
}
topts.ParamsRun.Clients.Log.Infof("PullRequest %s has been created", topts.PullRequest.HTMLURL)
if topts.CheckForStatus != "" {
WaitForStatus(t, topts, topts.TargetRefName, "", topts.StatusOnlyLatest)
}
if topts.Regexp != nil {
WaitForPullRequestCommentMatch(t, topts)
}
events, err := topts.ParamsRun.Clients.Kube.CoreV1().Events(topts.TargetNS).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", keys.Repository, formatting.CleanValueKubernetes(topts.TargetNS)),
})
assert.NilError(t, err)
if topts.ExpectEvents {
// in some cases event is expected but it takes time
// to emit and before that this check gets executed
// so adds a sleep for that case eg. TestGiteaBadYaml
if len(events.Items) == 0 {
// loop 30 times over a 5 second period and try to get any events
for i := 0; i < 30; i++ {
events, err = topts.ParamsRun.Clients.Kube.CoreV1().Events(topts.TargetNS).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", keys.Repository, formatting.CleanValueKubernetes(topts.TargetNS)),
})
assert.NilError(t, err)
if len(events.Items) > 0 {
break
}
time.Sleep(2 * time.Second)
}
}
assert.Assert(t, len(events.Items) != 0, "events expected in case of failure but got 0")
} else if !topts.SkipEventsCheck {
checkEvents(t, events, topts)
}
return ctx, cleanup
}
func NewPR(t *testing.T, topts *TestOpts) func() {
ctx := context.Background()
if topts.ParamsRun == nil {
runcnx, opts, giteacnx, err := Setup(ctx)
assert.NilError(t, err, fmt.Errorf("cannot do gitea setup: %w", err))
topts.GiteaCNX = giteacnx
topts.ParamsRun = runcnx
topts.Opts = opts
}
giteaURL := os.Getenv("TEST_GITEA_API_URL")
giteaPassword := os.Getenv("TEST_GITEA_PASSWORD")
topts.GiteaAPIURL = giteaURL
topts.GiteaPassword = giteaPassword
topts.InternalGiteaURL = os.Getenv("TEST_GITEA_INTERNAL_URL")
if topts.InternalGiteaURL == "" {
topts.InternalGiteaURL = "http://gitea.gitea:3000"
}
if topts.ExtraArgs == nil {
topts.ExtraArgs = map[string]string{}
}
topts.ExtraArgs["ProviderURL"] = topts.InternalGiteaURL
if topts.TargetNS == "" {
topts.TargetNS = topts.TargetRefName
}
if topts.TargetRefName == "" {
topts.TargetRefName = names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test")
topts.TargetNS = topts.TargetRefName
assert.NilError(t, pacrepo.CreateNS(ctx, topts.TargetNS, topts.ParamsRun))
}
if topts.TargetRepoName == "" {
topts.TargetRepoName = topts.TargetRefName
}
repoInfo, err := GetGiteaRepo(topts.GiteaCNX.Client, topts.Opts.Organization, topts.TargetRepoName, topts.ParamsRun.Clients.Log)
assert.NilError(t, err)
topts.Opts.Repo = repoInfo.Name
topts.Opts.Organization = repoInfo.Owner.UserName
topts.DefaultBranch = repoInfo.DefaultBranch
topts.GitHTMLURL = repoInfo.HTMLURL
cleanup := func() {
if os.Getenv("TEST_NOCLEANUP") != "true" {
defer TearDown(ctx, t, topts)
}
}
// topts.Token, err = CreateToken(topts)
// assert.NilError(t, err)
// assert.NilError(t, CreateCRD(ctx, topts))
url, err := scm.MakeGitCloneURL(repoInfo.CloneURL, os.Getenv("TEST_GITEA_USERNAME"), os.Getenv("TEST_GITEA_PASSWORD"))
assert.NilError(t, err)
topts.GitCloneURL = url
if topts.NoPullRequestCreation {
return cleanup
}
scmOpts := &scm.Opts{
GitURL: topts.GitCloneURL,
Log: topts.ParamsRun.Clients.Log,
WebURL: topts.GitHTMLURL,
TargetRefName: topts.TargetRefName,
BaseRefName: topts.DefaultBranch,
}
scm.ChangeFilesRefGit(t, scmOpts, topts.FileChanges)
topts.ParamsRun.Clients.Log.Infof("Creating PullRequest")
for i := 0; i < 5; i++ {
if topts.PullRequest, _, err = topts.GiteaCNX.Client.CreatePullRequest(topts.Opts.Organization, repoInfo.Name, gitea.CreatePullRequestOption{
Title: "Test Pull Request - " + topts.TargetRefName,
Head: topts.TargetRefName,
Base: options.MainBranch,
}); err == nil {
break
}
topts.ParamsRun.Clients.Log.Infof("Creating PullRequest has failed, retrying %d/%d, err", i, 5, err)
if i == 4 {
t.Fatalf("cannot create pull request: %v", err)
}
time.Sleep(5 * time.Second)
}
topts.ParamsRun.Clients.Log.Infof("PullRequest %s has been created", topts.PullRequest.HTMLURL)
if topts.CheckForStatus != "" {
WaitForStatus(t, topts, topts.TargetRefName, "", topts.StatusOnlyLatest)
}
if topts.Regexp != nil {
WaitForPullRequestCommentMatch(t, topts)
}
events, err := topts.ParamsRun.Clients.Kube.CoreV1().Events(topts.TargetNS).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", keys.Repository, formatting.CleanValueKubernetes(topts.TargetNS)),
})
assert.NilError(t, err)
if topts.ExpectEvents {
// in some cases event is expected but it takes time
// to emit and before that this check gets executed
// so adds a sleep for that case eg. TestGiteaBadYaml
if len(events.Items) == 0 {
// loop 30 times over a 5 second period and try to get any events
for i := 0; i < 30; i++ {
events, err = topts.ParamsRun.Clients.Kube.CoreV1().Events(topts.TargetNS).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", keys.Repository, formatting.CleanValueKubernetes(topts.TargetNS)),
})
assert.NilError(t, err)
if len(events.Items) > 0 {
break
}
time.Sleep(2 * time.Second)
}
}
assert.Assert(t, len(events.Items) != 0, "events expected in case of failure but got 0")
} else if !topts.SkipEventsCheck {
checkEvents(t, events, topts)
}
return cleanup
}
func WaitForStatus(t *testing.T, topts *TestOpts, ref, forcontext string, onlylatest bool) {
i := 0
if strings.HasPrefix(ref, "heads/") {
refo, _, err := topts.GiteaCNX.Client.GetRepoRefs(topts.Opts.Organization, topts.Opts.Repo, ref)
assert.NilError(t, err)
ref = refo[0].Object.SHA
}
checkNumberOfStatus := topts.CheckForNumberStatus
if checkNumberOfStatus == 0 {
checkNumberOfStatus = 1
}
for {
numstatus := 0
// get first sha of tree ref
statuses, _, err := topts.GiteaCNX.Client.ListStatuses(topts.Opts.Organization, topts.Opts.Repo, ref, gitea.ListStatusesOption{})
assert.NilError(t, err)
// sort statuses by id
sort.Slice(statuses, func(i, j int) bool {
return statuses[i].ID < statuses[j].ID
})
if onlylatest {
if len(statuses) > 1 {
statuses = statuses[len(statuses)-1:]
} else {
time.Sleep(5 * time.Second)
continue
}
}
for _, cstatus := range statuses {
if topts.CheckForStatus == "Skipped" {
if strings.HasSuffix(cstatus.Description, "Pending approval, waiting for an /ok-to-test") {
numstatus++
break
}
}
if cstatus.State == "pending" {
continue
}
if forcontext != "" && cstatus.Context != forcontext {
continue
}
statuscheck := topts.CheckForStatus
if statuscheck != "" && statuscheck != string(cstatus.State) {
if statuscheck != cstatus.Description {
t.Fatalf("Status on SHA: %s is %s from %s", ref, cstatus.State, cstatus.Context)
}
}
topts.ParamsRun.Clients.Log.Infof("Status on SHA: %s is %s from %s", ref, cstatus.State, cstatus.Context)
numstatus++
}
topts.ParamsRun.Clients.Log.Infof("Number of gitea status on PR: %d/%d", numstatus, checkNumberOfStatus)
if numstatus == checkNumberOfStatus {
return
}
if numstatus > checkNumberOfStatus {
t.Fatalf("Number of statuses is greater than expected, statuses: %d, expected: %d", numstatus, checkNumberOfStatus)
}
if i > 50 {
t.Fatalf("gitea status has not been updated")
}
time.Sleep(5 * time.Second)
i++
}
}
func WaitForSecretDeletion(t *testing.T, topts *TestOpts, _ string) {
i := 0
for {
// make sure pipelineRuns are deleted, before checking secrets
list, err := topts.ParamsRun.Clients.Tekton.TektonV1().PipelineRuns(topts.TargetNS).
List(context.Background(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("app.kubernetes.io/managed-by=%v", pipelinesascode.GroupName),
})
assert.NilError(t, err)
if i > 5 {
t.Fatalf("pipelineruns are not removed from the target namespace, something is fishy")
}
if len(list.Items) == 0 {
break
}
topts.ParamsRun.Clients.Log.Infof("deleting pipelineRuns in %v namespace", topts.TargetNS)
err = topts.ParamsRun.Clients.Tekton.TektonV1().PipelineRuns(topts.TargetNS).
DeleteCollection(context.Background(), metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: fmt.Sprintf("app.kubernetes.io/managed-by=%v", pipelinesascode.GroupName),
})
assert.NilError(t, err)
time.Sleep(5 * time.Second)
i++
}
topts.ParamsRun.Clients.Log.Infof("checking secrets in %v namespace", topts.TargetNS)
i = 0
for {
list, err := topts.ParamsRun.Clients.Kube.CoreV1().Secrets(topts.TargetNS).
List(context.Background(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s\n", keys.URLRepository, topts.TargetNS),
})
assert.NilError(t, err)
if len(list.Items) == 0 {
break
}
if i > 5 {
t.Fatalf("secret has not removed from the target namespace, something is fishy")
}
time.Sleep(5 * time.Second)
i++
}
}
func WaitForPullRequestCommentMatch(t *testing.T, topts *TestOpts) {
i := 0
topts.ParamsRun.Clients.Log.Infof("Looking for regexp \"%s\" in PR comments", topts.Regexp.String())
for {
comments, _, err := topts.GiteaCNX.Client.ListRepoIssueComments(topts.PullRequest.Base.Repository.Owner.UserName, topts.PullRequest.Base.Repository.Name, gitea.ListIssueCommentOptions{})
assert.NilError(t, err)
for _, v := range comments {
if topts.Regexp.MatchString(v.Body) {
topts.ParamsRun.Clients.Log.Infof("Found regexp in comment: %s", v.Body)
return
}
}
if i > 60 {
t.Fatalf("gitea driver has not been posted any comment")
}
time.Sleep(2 * time.Second)
i++
}
}
func CheckIfPipelineRunsCancelled(t *testing.T, topts *TestOpts) {
i := 0
for {
list, err := topts.ParamsRun.Clients.Tekton.TektonV1().PipelineRuns(topts.TargetNS).
List(context.Background(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("%v=%v", keys.Repository, formatting.CleanValueKubernetes((topts.TargetNS))),
})
assert.NilError(t, err)
if len(list.Items) == 0 {
t.Fatalf("pipelineruns not found, where are they???")
}
if list.Items[0].Spec.Status == v1.PipelineRunSpecStatusCancelledRunFinally {
topts.ParamsRun.Clients.Log.Info("PipelineRun is cancelled, yay!")
break
}
if i > 5 {
t.Fatalf("pipelineruns are not cancelled, something is fishy")
}
time.Sleep(5 * time.Second)
i++
}
}
func GetStandardParams(t *testing.T, topts *TestOpts, eventType string) (repoURL, sourceURL, sourceBranch, targetBranch string) {
t.Helper()
var err error
prs := &v1.PipelineRunList{}
for i := 0; i < 21; i++ {
prs, err = topts.ParamsRun.Clients.Tekton.TektonV1().PipelineRuns(topts.TargetNS).List(context.Background(), metav1.ListOptions{
LabelSelector: keys.EventType + "=" + eventType,
})
assert.NilError(t, err)
// get all pipelinerun names
names := []string{}
for _, pr := range prs.Items {
names = append(names, pr.Name)
}
assert.Equal(t, len(prs.Items), 1, "should have only one "+eventType+" pipelinerun", names)
if prs.Items[0].Status.Status.Conditions[0].Reason == "Succeeded" || prs.Items[0].Status.Status.Conditions[0].Reason == "Failed" {
break
}
time.Sleep(5 * time.Second)
if i == 20 {
t.Fatalf("pipelinerun has not finished, something is fishy")
}
}
numLines := int64(10)
out, err := tlogs.GetPodLog(context.Background(),
topts.ParamsRun.Clients.Kube.CoreV1(),
topts.TargetNS, fmt.Sprintf("tekton.dev/pipelineRun=%s",
prs.Items[0].Name), "step-test-standard-params-value",
&numLines)
assert.NilError(t, err)
assert.Assert(t, out != "")
out = strings.TrimSpace(out)
outputDataForPR := strings.Split(out, "--")
if len(outputDataForPR) != 5 {
t.Fatalf("expected 5 values in outputDataForPR, got %d: %v", len(outputDataForPR), outputDataForPR)
}
repoURL = outputDataForPR[0]
sourceURL = strings.TrimPrefix(outputDataForPR[1], "\n")
sourceBranch = strings.TrimPrefix(outputDataForPR[2], "\n")
targetBranch = strings.TrimPrefix(outputDataForPR[3], "\n")
return repoURL, sourceURL, sourceBranch, targetBranch
}
func VerifyConcurrency(t *testing.T, topts *TestOpts, globalRepoConcurrencyLimit *int) {
t.Helper()
ctx := context.Background()
topts.ParamsRun, topts.Opts, topts.GiteaCNX, _ = Setup(ctx)
assert.NilError(t, topts.ParamsRun.Clients.NewClients(ctx, &topts.ParamsRun.Info))
topts.TargetRefName = names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-test")
topts.TargetNS = topts.TargetRefName
ctx, err := cctx.GetControllerCtxInfo(ctx, topts.ParamsRun)
assert.NilError(t, err)
assert.NilError(t, pacrepo.CreateNS(ctx, topts.TargetNS, topts.ParamsRun))
globalNs, _, err := params.GetInstallLocation(ctx, topts.ParamsRun)
assert.NilError(t, err)
ctx = info.StoreNS(ctx, globalNs)
err = CreateCRD(ctx, topts,
v1alpha1.RepositorySpec{
ConcurrencyLimit: globalRepoConcurrencyLimit,
},
true)
assert.NilError(t, err)
defer (func() {
if os.Getenv("TEST_NOCLEANUP") != "true" {
topts.ParamsRun.Clients.Log.Infof("Cleaning up global repo %s in %s", info.DefaultGlobalRepoName, globalNs)
err = topts.ParamsRun.Clients.PipelineAsCode.PipelinesascodeV1alpha1().Repositories(globalNs).Delete(
context.Background(), info.DefaultGlobalRepoName, metav1.DeleteOptions{})
assert.NilError(t, err)
}
})()
_, f := TestPR(t, topts)
defer f()
}