forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresultref_test.go
755 lines (738 loc) · 21 KB
/
resultref_test.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1beta1_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/test/diff"
"k8s.io/apimachinery/pkg/selection"
)
func TestNewResultReference(t *testing.T) {
for _, tt := range []struct {
name string
param v1beta1.Param
want []*v1beta1.ResultRef
}{{
name: "Test valid expression",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTask.results.sumResult)"),
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "sumResult",
}},
}, {
name: "refer whole array result",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTask.results.sumResult[*])"),
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "sumResult",
}},
}, {
name: "Test valid expression with single object result property",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTask.results.sumResult.key1)"),
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "sumResult",
Property: "key1",
}},
}, {
name: "refer array indexing result",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTask.results.sumResult[1])"),
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "sumResult",
ResultsIndex: 1,
}},
}, {
name: "Test valid expression with multiple object result properties",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTask.results.imageresult.digest), and another one $(tasks.sumTask.results.imageresult.tag)"),
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "imageresult",
Property: "digest",
}, {
PipelineTask: "sumTask",
Result: "imageresult",
Property: "tag",
}},
}, {
name: "substitution within string",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("sum-will-go-here -> $(tasks.sumTask.results.sumResult)"),
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "sumResult",
}},
}, {
name: "multiple substitution",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTask1.results.sumResult) and another $(tasks.sumTask2.results.sumResult), last one $(tasks.sumTask3.results.sumResult.key1)"),
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask1",
Result: "sumResult",
}, {
PipelineTask: "sumTask2",
Result: "sumResult",
}, {
PipelineTask: "sumTask3",
Result: "sumResult",
Property: "key1",
}},
}, {
name: "multiple substitution with param",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(params.param) $(tasks.sumTask1.results.sumResult) and another $(tasks.sumTask2.results.sumResult), last one $(tasks.sumTask3.results.sumResult.key1)"),
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask1",
Result: "sumResult",
}, {
PipelineTask: "sumTask2",
Result: "sumResult",
}, {
PipelineTask: "sumTask3",
Result: "sumResult",
Property: "key1",
}},
}, {
name: "first separator typo",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(task.sumTasks.results.sumResult)"),
},
want: nil,
}, {
name: "third separator typo",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTasks.result.sumResult)"),
},
want: nil,
}, {
name: "more than 5 dot-separated components",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTasks.result.sumResult.key.extra)"),
},
want: nil,
}, {
name: "param substitution shouldn't be considered result ref",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(params.paramName)"),
},
want: nil,
}, {
name: "One bad and good result substitution",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("good -> $(tasks.sumTask1.results.sumResult) bad-> $(task.sumTask2.results.sumResult)"),
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask1",
Result: "sumResult",
}},
}} {
t.Run(tt.name, func(t *testing.T) {
expressions, ok := v1beta1.GetVarSubstitutionExpressionsForParam(tt.param)
if !ok && tt.want != nil {
t.Fatalf("expected to find expressions but didn't find any")
} else {
got := v1beta1.NewResultRefs(expressions)
if d := cmp.Diff(tt.want, got); d != "" {
t.Error(diff.PrintWantGot(d))
}
}
})
}
}
func TestHasResultReference(t *testing.T) {
for _, tt := range []struct {
name string
param v1beta1.Param
wantRef []*v1beta1.ResultRef
}{{
name: "Test valid expression",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTask.results.sumResult)"),
},
wantRef: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "sumResult",
}},
}, {
name: "Test valid expression with dashes",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sum-task.results.sum-result)"),
},
wantRef: []*v1beta1.ResultRef{{
PipelineTask: "sum-task",
Result: "sum-result",
}},
}, {
name: "Test valid expression with underscores",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sum-task.results.sum_result)"),
},
wantRef: []*v1beta1.ResultRef{{
PipelineTask: "sum-task",
Result: "sum_result",
}},
}, {
name: "Test invalid expression: param substitution shouldn't be considered result ref",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(params.paramName)"),
},
wantRef: nil,
}, {
name: "Test valid expression in array",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTask.results.sumResult)", "$(tasks.sumTask2.results.sumResult2)"),
},
wantRef: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "sumResult",
}, {
PipelineTask: "sumTask2",
Result: "sumResult2",
}},
}, {
name: "Test valid expression in array - no ref in first element",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("1", "$(tasks.sumTask2.results.sumResult2)"),
},
wantRef: []*v1beta1.ResultRef{{
PipelineTask: "sumTask2",
Result: "sumResult2",
}},
}, {
name: "Test valid expression in object",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewObject(map[string]string{
"key1": "$(tasks.sumTask1.results.sumResult1)",
"key2": "$(tasks.sumTask2.results.sumResult2) and another one $(tasks.sumTask3.results.sumResult3)",
"key3": "no ref here",
}),
},
wantRef: []*v1beta1.ResultRef{{
PipelineTask: "sumTask1",
Result: "sumResult1",
}, {
PipelineTask: "sumTask2",
Result: "sumResult2",
}, {
PipelineTask: "sumTask3",
Result: "sumResult3",
}},
}} {
t.Run(tt.name, func(t *testing.T) {
expressions, ok := v1beta1.GetVarSubstitutionExpressionsForParam(tt.param)
if !ok {
t.Fatalf("expected to find expressions but didn't find any")
}
got := v1beta1.NewResultRefs(expressions)
if d := cmp.Diff(tt.wantRef, got, cmpopts.SortSlices(func(i, j *v1beta1.ResultRef) bool {
if i.PipelineTask > j.PipelineTask {
return false
}
if i.Result > j.Result {
return false
}
return true
})); d != "" {
t.Error(diff.PrintWantGot(d))
}
})
}
}
func TestLooksLikeResultRef(t *testing.T) {
for _, tt := range []struct {
name string
param v1beta1.Param
want bool
}{{
name: "test expression that is a result ref",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTasks.results.sumResult)"),
},
want: true,
}, {
name: "test expression: invalid result ref, typo in 'task' separator",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(task.sumTasks.results.sumResult)"),
},
want: false,
}, {
name: "test expression: invalid result ref, typo in 'results' separator",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTasks.result.sumResult)"),
},
want: false,
}, {
name: "test expression: missing 'task' separator",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(sumTasks.results.sumResult)"),
},
want: false,
}, {
name: "test expression: missing variable substitution",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("tasks.sumTasks.results.sumResult"),
},
want: false,
}, {
name: "test expression: param substitution shouldn't be considered result ref",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(params.someParam)"),
},
want: false,
}, {
name: "test expression: one good ref, one bad one should return true",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTasks.results.sumResult) $(task.sumTasks.results.sumResult)"),
},
want: true,
}, {
name: "test expression: inside array parameter",
param: v1beta1.Param{
Name: "param",
Value: *v1beta1.NewStructuredValues("$(tasks.sumTask.results.sumResult)", "$(tasks.sumTask2.results.sumResult2)"),
},
want: true,
}} {
t.Run(tt.name, func(t *testing.T) {
expressions, ok := v1beta1.GetVarSubstitutionExpressionsForParam(tt.param)
if ok {
if got := v1beta1.LooksLikeContainsResultRefs(expressions); got != tt.want {
t.Errorf("LooksLikeContainsResultRefs() = %v, want %v", got, tt.want)
}
} else if tt.want {
t.Errorf("LooksLikeContainsResultRefs() = %v, want %v", false, tt.want)
}
})
}
}
func TestNewResultReferenceWhenExpressions(t *testing.T) {
for _, tt := range []struct {
name string
we v1beta1.WhenExpression
want []*v1beta1.ResultRef
}{{
name: "Test valid expression",
we: v1beta1.WhenExpression{
Input: "$(tasks.sumTask.results.sumResult)",
Operator: selection.In,
Values: []string{"foo"},
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "sumResult",
}},
}, {
name: "substitution within string",
we: v1beta1.WhenExpression{
Input: "sum-will-go-here -> $(tasks.sumTask.results.sumResult)",
Operator: selection.In,
Values: []string{"foo"},
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "sumResult",
}},
}, {
name: "multiple substitution",
we: v1beta1.WhenExpression{
Input: "$(tasks.sumTask1.results.sumResult) and another $(tasks.sumTask2.results.sumResult)",
Operator: selection.In,
Values: []string{"foo"},
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask1",
Result: "sumResult",
}, {
PipelineTask: "sumTask2",
Result: "sumResult",
}},
}, {
name: "multiple substitution with param",
we: v1beta1.WhenExpression{
Input: "$(params.param) $(tasks.sumTask1.results.sumResult) and another $(tasks.sumTask2.results.sumResult)",
Operator: selection.In,
Values: []string{"foo"},
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask1",
Result: "sumResult",
}, {
PipelineTask: "sumTask2",
Result: "sumResult",
}},
}, {
name: "first separator typo",
we: v1beta1.WhenExpression{
Input: "$(task.sumTasks.results.sumResult)",
Operator: selection.In,
Values: []string{"foo"},
},
want: nil,
}, {
name: "third separator typo",
we: v1beta1.WhenExpression{
Input: "$(tasks.sumTasks.result.sumResult)",
Operator: selection.In,
Values: []string{"foo"},
},
want: nil,
}, {
name: "param substitution shouldn't be considered result ref",
we: v1beta1.WhenExpression{
Input: "$(params.paramName)",
Operator: selection.In,
Values: []string{"foo"},
},
want: nil,
}, {
name: "One bad and good result substitution",
we: v1beta1.WhenExpression{
Input: "good -> $(tasks.sumTask1.results.sumResult) bad-> $(task.sumTask2.results.sumResult)",
Operator: selection.In,
Values: []string{"foo"},
},
want: []*v1beta1.ResultRef{{
PipelineTask: "sumTask1",
Result: "sumResult",
}},
}} {
t.Run(tt.name, func(t *testing.T) {
expressions, ok := tt.we.GetVarSubstitutionExpressions()
if !ok {
t.Fatalf("expected to find expressions but didn't find any")
} else {
got := v1beta1.NewResultRefs(expressions)
if d := cmp.Diff(tt.want, got); d != "" {
t.Error(diff.PrintWantGot(d))
}
}
})
}
}
func TestHasResultReferenceWhenExpression(t *testing.T) {
for _, tt := range []struct {
name string
we v1beta1.WhenExpression
wantRef []*v1beta1.ResultRef
}{{
name: "Test valid expression",
we: v1beta1.WhenExpression{
Input: "sumResult",
Operator: selection.In,
Values: []string{"$(tasks.sumTask.results.sumResult)"},
},
wantRef: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "sumResult",
}},
}, {
name: "Test valid expression with dashes",
we: v1beta1.WhenExpression{
Input: "$(tasks.sum-task.results.sum-result)",
Operator: selection.In,
Values: []string{"sum-result"},
},
wantRef: []*v1beta1.ResultRef{{
PipelineTask: "sum-task",
Result: "sum-result",
}},
}, {
name: "Test valid expression with underscores",
we: v1beta1.WhenExpression{
Input: "$(tasks.sum-task.results.sum_result)",
Operator: selection.In,
Values: []string{"sum-result"},
},
wantRef: []*v1beta1.ResultRef{{
PipelineTask: "sum-task",
Result: "sum_result",
}},
}, {
name: "Test invalid expression: param substitution shouldn't be considered result ref",
we: v1beta1.WhenExpression{
Input: "$(params.paramName)",
Operator: selection.In,
Values: []string{"sum-result"},
},
wantRef: nil,
}, {
name: "Test valid expression in array",
we: v1beta1.WhenExpression{
Input: "$sumResult",
Operator: selection.In,
Values: []string{"$(tasks.sumTask.results.sumResult)", "$(tasks.sumTask2.results.sumResult2)"},
},
wantRef: []*v1beta1.ResultRef{{
PipelineTask: "sumTask",
Result: "sumResult",
}, {
PipelineTask: "sumTask2",
Result: "sumResult2",
}},
}} {
t.Run(tt.name, func(t *testing.T) {
expressions, ok := tt.we.GetVarSubstitutionExpressions()
if !ok {
t.Fatalf("expected to find expressions but didn't find any")
}
got := v1beta1.NewResultRefs(expressions)
if d := cmp.Diff(tt.wantRef, got); d != "" {
t.Errorf(diff.PrintWantGot(d))
}
})
}
}
func TestLooksLikeResultRefWhenExpressionTrue(t *testing.T) {
tests := []struct {
name string
we v1beta1.WhenExpression
}{
{
name: "test expression that is a result ref",
we: v1beta1.WhenExpression{
Input: "$(tasks.sumTasks.results.sumResult)",
Operator: selection.In,
Values: []string{"foo"},
},
}, {
name: "test expression: one good ref, one bad one should return true",
we: v1beta1.WhenExpression{
Input: "$(tasks.sumTasks.results.sumResult) $(task.sumTasks.results.sumResult)",
Operator: selection.In,
Values: []string{"foo"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expressions, ok := tt.we.GetVarSubstitutionExpressions()
if !ok {
t.Fatalf("expected to find expressions but didn't find any")
}
if !v1beta1.LooksLikeContainsResultRefs(expressions) {
t.Errorf("expected expressions to look like they contain result refs")
}
})
}
}
func TestLooksLikeResultRefWhenExpressionFalse(t *testing.T) {
tests := []struct {
name string
we v1beta1.WhenExpression
}{
{
name: "test expression: missing 'task' separator",
we: v1beta1.WhenExpression{
Input: "$(sumTasks.results.sumResult)",
Operator: selection.In,
Values: []string{"foo"},
},
}, {
name: "test expression: missing 'results' separator",
we: v1beta1.WhenExpression{
Input: "$(tasks.sumTasks.sumResult)",
Operator: selection.In,
Values: []string{"foo"},
},
}, {
name: "test expression: param substitution shouldn't be considered result ref",
we: v1beta1.WhenExpression{
Input: "$(params.someParam)",
Operator: selection.In,
Values: []string{"foo"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expressions, ok := tt.we.GetVarSubstitutionExpressions()
if !ok {
t.Fatalf("expected to find expressions but didn't find any")
}
if v1beta1.LooksLikeContainsResultRefs(expressions) {
t.Errorf("expected expressions to not look like they contain results refs")
}
})
}
}
// TestPipelineTaskResultReferences tests that PipelineTaskResultRefs()
// parses all the result variables used in a PipelineTask correctly and
// returns them all in the expected order.
func TestPipelineTaskResultRefs(t *testing.T) {
pt := v1beta1.PipelineTask{
Params: []v1beta1.Param{{
Value: *v1beta1.NewStructuredValues("$(tasks.pt1.results.r1)"),
}, {
Value: *v1beta1.NewStructuredValues("$(tasks.pt2.results.r2)"),
}},
WhenExpressions: []v1beta1.WhenExpression{{
Input: "$(tasks.pt3.results.r3)",
Operator: selection.In,
Values: []string{
"$(tasks.pt4.results.r4)",
},
}},
Matrix: &v1beta1.Matrix{
Params: []v1beta1.Param{{
Value: *v1beta1.NewStructuredValues("$(tasks.pt5.results.r5)", "$(tasks.pt6.results.r6)"),
}, {
Value: *v1beta1.NewStructuredValues("$(tasks.pt7.results.r7)", "$(tasks.pt8.results.r8)"),
}}},
}
refs := v1beta1.PipelineTaskResultRefs(&pt)
expectedRefs := []*v1beta1.ResultRef{{
PipelineTask: "pt1",
Result: "r1",
}, {
PipelineTask: "pt2",
Result: "r2",
}, {
PipelineTask: "pt3",
Result: "r3",
}, {
PipelineTask: "pt4",
Result: "r4",
}, {
PipelineTask: "pt5",
Result: "r5",
}, {
PipelineTask: "pt6",
Result: "r6",
}, {
PipelineTask: "pt7",
Result: "r7",
}, {
PipelineTask: "pt8",
Result: "r8",
}}
if d := cmp.Diff(refs, expectedRefs, cmpopts.SortSlices(lessResultRef)); d != "" {
t.Errorf("%v", d)
}
}
func lessResultRef(i, j *v1beta1.ResultRef) bool {
return i.PipelineTask+i.Result < j.PipelineTask+i.Result
}
func TestParseResultName(t *testing.T) {
tests := []struct {
name string
input string
want []string
}{{
name: "array indexing",
input: "anArrayResult[1]",
want: []string{"anArrayResult", "1"},
},
{
name: "array star reference",
input: "anArrayResult[*]",
want: []string{"anArrayResult", "*"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resultName, idx := v1beta1.ParseResultName(tt.input)
if d := cmp.Diff(tt.want, []string{resultName, idx}); d != "" {
t.Error(diff.PrintWantGot(d))
}
})
}
}
func TestGetVarSubstitutionExpressionsForPipelineResult(t *testing.T) {
tests := []struct {
name string
result v1beta1.PipelineResult
want []string
}{{
name: "get string result expressions",
result: v1beta1.PipelineResult{
Name: "string result",
Type: v1beta1.ResultsTypeString,
Value: *v1beta1.NewStructuredValues("$(tasks.task1.results.result1) and $(tasks.task2.results.result2)"),
},
want: []string{"tasks.task1.results.result1", "tasks.task2.results.result2"},
}, {
name: "get array result expressions",
result: v1beta1.PipelineResult{
Name: "array result",
Type: v1beta1.ResultsTypeString,
Value: *v1beta1.NewStructuredValues("$(tasks.task1.results.result1)", "$(tasks.task2.results.result2)"),
},
want: []string{"tasks.task1.results.result1", "tasks.task2.results.result2"},
}, {
name: "get object result expressions",
result: v1beta1.PipelineResult{
Name: "object result",
Type: v1beta1.ResultsTypeString,
Value: *v1beta1.NewObject(map[string]string{
"key1": "$(tasks.task1.results.result1)",
"key2": "$(tasks.task2.results.result2) and another one $(tasks.task3.results.result3)",
"key3": "no ref here",
}),
},
want: []string{"tasks.task1.results.result1", "tasks.task2.results.result2", "tasks.task3.results.result3"},
},
}
var sortStrings = func(x, y string) bool {
return x < y
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
get, _ := v1beta1.GetVarSubstitutionExpressionsForPipelineResult(tt.result)
if d := cmp.Diff(tt.want, get, cmpopts.SortSlices(sortStrings)); d != "" {
t.Error(diff.PrintWantGot(d))
}
})
}
}