-
Notifications
You must be signed in to change notification settings - Fork 784
/
test_helpers.go
698 lines (597 loc) · 18.2 KB
/
test_helpers.go
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
package syntax_helpers_test
import (
v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/v2/pkg/tekton/syntax"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/test/builder"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// PipelineStructureOp is an operation used in generating a PipelineStructure
type PipelineStructureOp func(structure *v1.PipelineStructure)
// PipelineStructureStageOp is an operation used in generating a PipelineStructureStage
type PipelineStructureStageOp func(stage *v1.PipelineStructureStage)
// PipelineStructure creates a PipelineStructure
func PipelineStructure(name string, ops ...PipelineStructureOp) *v1.PipelineStructure {
s := &v1.PipelineStructure{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
for _, op := range ops {
op(s)
}
return s
}
// StructurePipelineRunRef adds a run reference to the structure
func StructurePipelineRunRef(name string) PipelineStructureOp {
return func(structure *v1.PipelineStructure) {
structure.PipelineRunRef = &name
}
}
// StructureStage adds a stage to the structure
func StructureStage(name string, ops ...PipelineStructureStageOp) PipelineStructureOp {
return func(structure *v1.PipelineStructure) {
stage := v1.PipelineStructureStage{Name: name}
for _, op := range ops {
op(&stage)
}
structure.Stages = append(structure.Stages, stage)
}
}
// StructureStageTaskRef adds a task ref to the stage
func StructureStageTaskRef(name string) PipelineStructureStageOp {
return func(stage *v1.PipelineStructureStage) {
stage.TaskRef = &name
}
}
// StructureStageTaskRunRef adds a task run ref to the stage
func StructureStageTaskRunRef(name string) PipelineStructureStageOp {
return func(stage *v1.PipelineStructureStage) {
stage.TaskRunRef = &name
}
}
// StructureStageDepth sets the depth on the stage
func StructureStageDepth(depth int8) PipelineStructureStageOp {
return func(stage *v1.PipelineStructureStage) {
stage.Depth = depth
}
}
// StructureStageParent sets the parent stage for the stage
func StructureStageParent(parent string) PipelineStructureStageOp {
return func(stage *v1.PipelineStructureStage) {
stage.Parent = &parent
}
}
// StructureStagePrevious sets the previous stage for the stage
func StructureStagePrevious(previous string) PipelineStructureStageOp {
return func(stage *v1.PipelineStructureStage) {
stage.Previous = &previous
}
}
// StructureStageNext sets the next stage for the stage
func StructureStageNext(Next string) PipelineStructureStageOp {
return func(stage *v1.PipelineStructureStage) {
stage.Next = &Next
}
}
// StructureStageStages sets the nested sequential stages for the stage
func StructureStageStages(stages ...string) PipelineStructureStageOp {
return func(stage *v1.PipelineStructureStage) {
stage.Stages = append(stage.Stages, stages...)
}
}
// StructureStageParallel sets the nested parallel stages for the stage
func StructureStageParallel(stages ...string) PipelineStructureStageOp {
return func(stage *v1.PipelineStructureStage) {
stage.Parallel = append(stage.Parallel, stages...)
}
}
// PipelineOp is an operation on a ParsedPipeline
type PipelineOp func(*syntax.ParsedPipeline)
// PipelineOptionsOp is an operation on RootOptions
type PipelineOptionsOp func(*syntax.RootOptions)
// PipelinePostOp is an operation on Post
type PipelinePostOp func(*syntax.Post)
// StageOp is an operation on a Stage
type StageOp func(*syntax.Stage)
// StageOptionsOp is an operation on StageOptions
type StageOptionsOp func(*syntax.StageOptions)
// StepOp is an operation on a step
type StepOp func(*syntax.Step)
// LoopOp is an operation on a Loop
type LoopOp func(*syntax.Loop)
// ParsedPipeline creates a ParsedPipeline from the provided operations
func ParsedPipeline(ops ...PipelineOp) *syntax.ParsedPipeline {
s := &syntax.ParsedPipeline{}
for _, op := range ops {
op(s)
}
return s
}
// PipelineAgent sets the agent for the pipeline
func PipelineAgent(image string) PipelineOp {
return func(parsed *syntax.ParsedPipeline) {
parsed.Agent = &syntax.Agent{
Image: image,
}
}
}
// PipelineOptions sets the RootOptions for the pipeline
func PipelineOptions(ops ...PipelineOptionsOp) PipelineOp {
return func(parsed *syntax.ParsedPipeline) {
parsed.Options = &syntax.RootOptions{}
for _, op := range ops {
op(parsed.Options)
}
}
}
// PipelineSidecar adds a sidecar container to the RootOptions for the pipeline
func PipelineSidecar(sidecar *corev1.Container) PipelineOptionsOp {
return func(options *syntax.RootOptions) {
if options.Sidecars == nil {
options.Sidecars = []*corev1.Container{}
}
options.Sidecars = append(options.Sidecars, sidecar)
}
}
// PipelineVolume adds a volume to the RootOptions for the pipeline
func PipelineVolume(volume *corev1.Volume) PipelineOptionsOp {
return func(options *syntax.RootOptions) {
if options.Volumes == nil {
options.Volumes = []*corev1.Volume{}
}
options.Volumes = append(options.Volumes, volume)
}
}
// StageSidecar adds a sidecar to the StageOptions for the stage
func StageSidecar(sidecar *corev1.Container) StageOptionsOp {
return func(options *syntax.StageOptions) {
if options.RootOptions == nil {
options.RootOptions = &syntax.RootOptions{}
}
if options.Sidecars == nil {
options.Sidecars = []*corev1.Container{}
}
options.Sidecars = append(options.Sidecars, sidecar)
}
}
// StageVolume adds a volume to the StageOptions for the stage
func StageVolume(volume *corev1.Volume) StageOptionsOp {
return func(options *syntax.StageOptions) {
if options.RootOptions == nil {
options.RootOptions = &syntax.RootOptions{}
}
if options.Volumes == nil {
options.Volumes = []*corev1.Volume{}
}
options.Volumes = append(options.Volumes, volume)
}
}
// PipelineContainerOptions sets the containerOptions for the pipeline
func PipelineContainerOptions(ops ...builder.ContainerOp) PipelineOptionsOp {
return func(options *syntax.RootOptions) {
options.ContainerOptions = &corev1.Container{}
for _, op := range ops {
op(options.ContainerOptions)
}
}
}
// PipelineTolerations sets the tolerations for the pipeline
func PipelineTolerations(tolerations []corev1.Toleration) PipelineOptionsOp {
return func(options *syntax.RootOptions) {
options.Tolerations = append(options.Tolerations, tolerations...)
}
}
// PipelinePodLabels sets the optional pod labels for the pipeline
func PipelinePodLabels(labels map[string]string) PipelineOptionsOp {
return func(options *syntax.RootOptions) {
options.PodLabels = util.MergeMaps(options.PodLabels, labels)
}
}
// StageContainerOptions sets the containerOptions for a stage
func StageContainerOptions(ops ...builder.ContainerOp) StageOptionsOp {
return func(options *syntax.StageOptions) {
if options.RootOptions == nil {
options.RootOptions = &syntax.RootOptions{}
}
options.ContainerOptions = &corev1.Container{}
for _, op := range ops {
op(options.ContainerOptions)
}
}
}
// PipelineDir sets the default working directory for the pipeline
func PipelineDir(dir string) PipelineOp {
return func(pipeline *syntax.ParsedPipeline) {
pipeline.WorkingDir = &dir
}
}
// StageDir sets the default working directory for the stage
func StageDir(dir string) StageOp {
return func(stage *syntax.Stage) {
stage.WorkingDir = &dir
}
}
// ContainerResourceLimits sets the resource limits for container options
func ContainerResourceLimits(cpus, memory string) builder.ContainerOp {
return func(container *corev1.Container) {
cpuQuantity, _ := resource.ParseQuantity(cpus)
memoryQuantity, _ := resource.ParseQuantity(memory)
container.Resources.Limits = corev1.ResourceList{
"cpu": cpuQuantity,
"memory": memoryQuantity,
}
}
}
// ContainerResourceRequests sets the resource requests for container options
func ContainerResourceRequests(cpus, memory string) builder.ContainerOp {
return func(container *corev1.Container) {
cpuQuantity, _ := resource.ParseQuantity(cpus)
memoryQuantity, _ := resource.ParseQuantity(memory)
container.Resources.Requests = corev1.ResourceList{
"cpu": cpuQuantity,
"memory": memoryQuantity,
}
}
}
// ContainerSecurityContext sets the security context for container options
func ContainerSecurityContext(privileged bool) builder.ContainerOp {
return func(container *corev1.Container) {
container.SecurityContext = &corev1.SecurityContext{
Privileged: &privileged,
}
}
}
// ContainerVolumeMount adds a VolumeMount to the container options
func ContainerVolumeMount(name string, mountPath string) builder.ContainerOp {
return func(container *corev1.Container) {
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: name,
MountPath: mountPath,
})
}
}
// StepVolumeMount adds a VolumeMount to the container options on a step
func StepVolumeMount(name string, mountPath string) builder.StepOp {
return func(step *v1alpha1.Step) {
step.VolumeMounts = append(step.VolumeMounts, corev1.VolumeMount{
Name: name,
MountPath: mountPath,
})
}
}
// StepResourceLimits sets the resource limits for container options on a step
func StepResourceLimits(cpus, memory string) builder.StepOp {
return func(step *v1alpha1.Step) {
cpuQuantity, _ := resource.ParseQuantity(cpus)
memoryQuantity, _ := resource.ParseQuantity(memory)
step.Resources.Limits = corev1.ResourceList{
"cpu": cpuQuantity,
"memory": memoryQuantity,
}
}
}
// StepResourceRequests sets the resource requests for container options on a step
func StepResourceRequests(cpus, memory string) builder.StepOp {
return func(step *v1alpha1.Step) {
cpuQuantity, _ := resource.ParseQuantity(cpus)
memoryQuantity, _ := resource.ParseQuantity(memory)
step.Resources.Requests = corev1.ResourceList{
"cpu": cpuQuantity,
"memory": memoryQuantity,
}
}
}
// StepSecurityContext sets the security context for container options on a step
func StepSecurityContext(privileged bool) builder.StepOp {
return func(step *v1alpha1.Step) {
step.SecurityContext = &corev1.SecurityContext{
Privileged: &privileged,
}
}
}
// EnvVarFrom adds an environment variable using EnvVarSource to the container options
func EnvVarFrom(name string, source *corev1.EnvVarSource) builder.ContainerOp {
return func(container *corev1.Container) {
container.Env = append(container.Env, corev1.EnvVar{
Name: name,
ValueFrom: source,
})
}
}
// EnvVar adds an environment variable with a value to the container options
func EnvVar(name string, value string) builder.ContainerOp {
return func(container *corev1.Container) {
container.Env = append(container.Env, corev1.EnvVar{
Name: name,
Value: value,
})
}
}
// PipelineOptionsTimeout sets the timeout for the pipeline
func PipelineOptionsTimeout(time int64, unit syntax.TimeoutUnit) PipelineOptionsOp {
return func(options *syntax.RootOptions) {
options.Timeout = &syntax.Timeout{
Time: time,
Unit: unit,
}
}
}
// PipelineOptionsRetry sets the retry count for the pipeline
func PipelineOptionsRetry(count int8) PipelineOptionsOp {
return func(options *syntax.RootOptions) {
options.Retry = count
}
}
// PipelineOptionsDistributeParallelAcrossNodes sets the value for distributeParallelAcrossNodes
func PipelineOptionsDistributeParallelAcrossNodes(b bool) PipelineOptionsOp {
return func(options *syntax.RootOptions) {
options.DistributeParallelAcrossNodes = b
}
}
// PipelineEnvVar add an environment variable, with specified name and value, to the pipeline.
func PipelineEnvVar(name, value string) PipelineOp {
return func(parsed *syntax.ParsedPipeline) {
parsed.Env = append(parsed.GetEnv(), corev1.EnvVar{
Name: name,
Value: value,
})
}
}
// PipelinePost adds a post condition to the pipeline
func PipelinePost(condition syntax.PostCondition, ops ...PipelinePostOp) PipelineOp {
return func(parsed *syntax.ParsedPipeline) {
post := syntax.Post{
Condition: condition,
}
for _, op := range ops {
op(&post)
}
parsed.Post = append(parsed.Post, post)
}
}
// PipelineStage adds a stage to the pipeline
func PipelineStage(name string, ops ...StageOp) PipelineOp {
return func(parsed *syntax.ParsedPipeline) {
s := syntax.Stage{
Name: name,
}
for _, op := range ops {
op(&s)
}
parsed.Stages = append(parsed.Stages, s)
}
}
// PostAction adds a post action to a post condition
func PostAction(name string, options map[string]string) PipelinePostOp {
return func(post *syntax.Post) {
post.Actions = append(post.Actions, syntax.PostAction{
Name: name,
Options: options,
})
}
}
// StageAgent sets the image/agent for a stage
func StageAgent(image string) StageOp {
return func(stage *syntax.Stage) {
stage.Agent = &syntax.Agent{
Image: image,
}
}
}
// StageOptions sets the StageOptions for a stage
func StageOptions(ops ...StageOptionsOp) StageOp {
return func(stage *syntax.Stage) {
stage.Options = &syntax.StageOptions{}
for _, op := range ops {
op(stage.Options)
}
}
}
// StageOptionsTimeout sets the timeout for a stage
func StageOptionsTimeout(time int64, unit syntax.TimeoutUnit) StageOptionsOp {
return func(options *syntax.StageOptions) {
if options.RootOptions == nil {
options.RootOptions = &syntax.RootOptions{}
}
options.Timeout = &syntax.Timeout{
Time: time,
Unit: unit,
}
}
}
// StageOptionsRetry sets the retry count for a stage
func StageOptionsRetry(count int8) StageOptionsOp {
return func(options *syntax.StageOptions) {
if options.RootOptions == nil {
options.RootOptions = &syntax.RootOptions{}
}
options.Retry = count
}
}
// StageOptionsWorkspace sets the workspace for a stage
func StageOptionsWorkspace(ws string) StageOptionsOp {
return func(options *syntax.StageOptions) {
options.Workspace = &ws
}
}
// StageOptionsStash adds a stash to the stage
func StageOptionsStash(name, files string) StageOptionsOp {
return func(options *syntax.StageOptions) {
options.Stash = &syntax.Stash{
Name: name,
Files: files,
}
}
}
// StageOptionsUnstash adds an unstash to the stage
func StageOptionsUnstash(name, dir string) StageOptionsOp {
return func(options *syntax.StageOptions) {
options.Unstash = &syntax.Unstash{
Name: name,
}
if dir != "" {
options.Unstash.Dir = dir
}
}
}
// StageEnvVar add an environment variable, with specified name and value, to the stage.
func StageEnvVar(name, value string) StageOp {
return func(stage *syntax.Stage) {
stage.Env = append(stage.GetEnv(), corev1.EnvVar{
Name: name,
Value: value,
})
}
}
// StagePost adds a post condition to the stage
func StagePost(condition syntax.PostCondition, ops ...PipelinePostOp) StageOp {
return func(stage *syntax.Stage) {
post := syntax.Post{
Condition: condition,
}
for _, op := range ops {
op(&post)
}
stage.Post = append(stage.Post, post)
}
}
// StepAgent sets the agent for a step
func StepAgent(image string) StepOp {
return func(step *syntax.Step) {
step.Agent = &syntax.Agent{
Image: image,
}
}
}
// StepImage sets the image for a step
func StepImage(image string) StepOp {
return func(step *syntax.Step) {
step.Image = image
}
}
// StepCmd sets the command for a step
func StepCmd(cmd string) StepOp {
return func(step *syntax.Step) {
step.Command = cmd
}
}
// StepName sets the name for a step
func StepName(name string) StepOp {
return func(step *syntax.Step) {
step.Name = name
}
}
// StepArg sets the arguments for a step
func StepArg(arg string) StepOp {
return func(step *syntax.Step) {
step.Arguments = append(step.Arguments, arg)
}
}
// StepStep sets the alias step for a step
func StepStep(s string) StepOp {
return func(step *syntax.Step) {
step.Step = s
}
}
// StepOptions sets the alias step options for a step
func StepOptions(options map[string]string) StepOp {
return func(step *syntax.Step) {
step.Options = options
}
}
// StepDir sets the working dir for a step
func StepDir(dir string) StepOp {
return func(step *syntax.Step) {
step.Dir = dir
}
}
// StepLoop adds a loop to the step
func StepLoop(variable string, values []string, ops ...LoopOp) StepOp {
return func(step *syntax.Step) {
loop := &syntax.Loop{
Variable: variable,
Values: values,
}
for _, op := range ops {
op(loop)
}
step.Loop = loop
}
}
// StepEnvVar add an environment variable, with specified name and value, to the step.
func StepEnvVar(name, value string) StepOp {
return func(step *syntax.Step) {
step.Env = append(step.Env, corev1.EnvVar{
Name: name,
Value: value,
})
}
}
// LoopStep adds a step to the loop
func LoopStep(ops ...StepOp) LoopOp {
return func(loop *syntax.Loop) {
step := syntax.Step{}
for _, op := range ops {
op(&step)
}
loop.Steps = append(loop.Steps, step)
}
}
// StageStep adds a step to the stage
func StageStep(ops ...StepOp) StageOp {
return func(stage *syntax.Stage) {
step := syntax.Step{}
for _, op := range ops {
op(&step)
}
stage.Steps = append(stage.Steps, step)
}
}
// StageParallel adds a nested parallel stage to the stage
func StageParallel(name string, ops ...StageOp) StageOp {
return func(stage *syntax.Stage) {
n := syntax.Stage{Name: name}
for _, op := range ops {
op(&n)
}
stage.Parallel = append(stage.Parallel, n)
}
}
// StageSequential adds a nested sequential stage to the stage
func StageSequential(name string, ops ...StageOp) StageOp {
return func(stage *syntax.Stage) {
n := syntax.Stage{Name: name}
for _, op := range ops {
op(&n)
}
stage.Stages = append(stage.Stages, n)
}
}
// TaskStageLabel sets the stage label on the task
func TaskStageLabel(value string) builder.TaskOp {
return func(t *v1alpha1.Task) {
if t.ObjectMeta.Labels == nil {
t.ObjectMeta.Labels = map[string]string{}
}
t.ObjectMeta.Labels[syntax.LabelStageName] = syntax.MangleToRfc1035Label(value, "")
}
}
// OutputsResource adds a resource, with specified name and type, to the Outputs.
// Any number of TaskResource modifier can be passed to transform it.
func OutputsResource(name string, resourceType v1alpha1.PipelineResourceType, ops ...builder.TaskResourceOp) builder.OutputsOp {
return func(i *v1alpha1.Outputs) {
r := &v1alpha1.TaskResource{
ResourceDeclaration: v1alpha1.ResourceDeclaration{
Name: name,
Type: resourceType,
}}
for _, op := range ops {
op(r)
}
i.Resources = append(i.Resources, *r)
}
}