-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_testcase.go
626 lines (557 loc) · 17.9 KB
/
process_testcase.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
package venom
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/ovh/cds/sdk/interpolate"
"github.com/pkg/errors"
"github.com/rockbears/yaml"
)
var varRegEx = regexp.MustCompile("{{.*}}")
// Parse the testcase to find unreplaced and extracted variables
func (v *Venom) parseTestCase(ts *TestSuite, tc *TestCase) ([]string, []string, error) {
dvars, err := DumpStringPreserveCase(tc.Vars)
if err != nil {
return nil, nil, err
}
vars := []string{}
extractedVars := []string{}
// the value of each var can contain a double-quote -> "
// if the value is not escaped, it will be used as is, and the json sent to unmarshall will be incorrect.
// This also avoids injections into the json structure of a step
for i := range dvars {
dvars[i] = strings.ReplaceAll(dvars[i], "\"", "\\\"")
}
for _, rawStep := range tc.RawTestSteps {
content, err := interpolate.Do(string(rawStep), dvars)
if err != nil {
return nil, nil, err
}
var step TestStep
if err := yaml.Unmarshal([]byte(content), &step); err != nil {
return nil, nil, errors.Wrapf(err, "unable to unmarshal teststep")
}
_, exec, err := v.GetExecutorRunner(context.Background(), step, tc.Vars)
if err != nil {
return nil, nil, err
}
defaultResult := exec.ZeroValueResult()
if defaultResult != nil {
dumpE, err := DumpString(defaultResult)
if err != nil {
return nil, nil, err
}
for k := range dumpE {
var found bool
for i := 0; i < len(vars); i++ {
if vars[i] == k {
found = true
break
}
}
if !found {
extractedVars = append(extractedVars, k)
}
extractedVars = append(extractedVars, tc.Name+"."+k)
if strings.HasSuffix(k, "__type__") && dumpE[k] == "Map" {
// go-dump doesn't dump the map name, here is a workaround
k = strings.TrimSuffix(k, "__type__")
extractedVars = append(extractedVars, tc.Name+"."+k)
}
}
}
dumpE, err := DumpStringPreserveCase(step)
if err != nil {
return nil, nil, err
}
for k, v := range dumpE {
if strings.HasPrefix(k, "vars.") {
s := tc.Name + "." + strings.Split(k[5:], ".")[0]
extractedVars = append(extractedVars, s)
continue
}
if strings.HasPrefix(k, "range.") {
continue
}
if strings.HasPrefix(k, "extracts.") {
s := tc.Name + "." + strings.Split(k[9:], ".")[0]
extractedVars = append(extractedVars, s)
continue
}
if strings.HasPrefix(k, "info") {
continue
}
if varRegEx.MatchString(v) {
var found bool
for i := 0; i < len(vars); i++ {
if vars[i] == k {
found = true
break
}
}
submatches := varRegEx.FindStringSubmatch(v)
for submatcheIndex, s := range submatches {
if submatcheIndex == 0 {
continue
}
for i := 0; i < len(extractedVars); i++ {
prefix := "{{." + extractedVars[i]
if strings.HasPrefix(s, prefix) {
found = true
break
}
}
if !found {
vars = append(vars, s)
s = strings.ReplaceAll(s, "{{ .", "")
s = strings.ReplaceAll(s, "{{.", "")
s = strings.ReplaceAll(s, "}}", "")
vars = append(vars, s)
}
}
}
}
}
return vars, extractedVars, nil
}
func (v *Venom) runTestCase(ctx context.Context, ts *TestSuite, tc *TestCase) {
ctx = context.WithValue(ctx, ContextKey("testcase"), tc.Name)
tc.TestSuiteVars = ts.Vars.Clone()
tc.Vars = ts.Vars.Clone()
tc.Vars.Add("venom.testcase", tc.Name)
tc.Vars.AddAll(ts.ComputedVars)
tc.computedVars = H{}
ctx = v.processSecrets(ctx, ts, tc)
Info(ctx, "Starting testcase")
defer Info(ctx, "Ending testcase")
// ##### RUN Test Steps Here
v.runTestSteps(ctx, tc, nil)
}
func (v *Venom) processSecrets(ctx context.Context, ts *TestSuite, tc *TestCase) context.Context {
computedSecrets := []string{}
for k, v := range tc.Vars {
for _, s := range ts.Secrets {
if strings.Compare(k, s) == 0 {
computedSecrets = append(computedSecrets, fmt.Sprint(v))
}
}
}
return context.WithValue(ctx, ContextKey("secrets"), computedSecrets)
}
func (v *Venom) runTestSteps(ctx context.Context, tc *TestCase, tsIn *TestStepResult) {
results, err := testConditionalStatement(ctx, tc, tc.Skip, tc.Vars, "skipping testcase %q: %v")
if err != nil {
Error(ctx, "unable to evaluate \"skip\" assertions: %v", err)
testStepResult := TestStepResult{}
testStepResult.appendError(err)
tc.TestStepResults = append(tc.TestStepResults, testStepResult)
return
}
if len(results) > 0 {
tc.Status = StatusSkip
for _, s := range results {
tc.Skipped = append(tc.Skipped, Skipped{Value: s})
Warn(ctx, s)
}
return
}
var knowExecutors = map[string]struct{}{}
var previousStepVars = H{}
fromUserExecutor := tsIn != nil
loopRawTestSteps:
for stepNumber, rawStep := range tc.RawTestSteps {
stepVars := tc.Vars.Clone()
stepVars.AddAll(previousStepVars)
stepVars.AddAllWithPrefix(tc.Name, tc.computedVars)
stepVars.Add("venom.teststep.number", stepNumber)
ranged, err := parseRanged(ctx, rawStep, stepVars)
if err != nil {
Error(ctx, "unable to parse \"range\" attribute: %v", err)
testStepResult := TestStepResult{}
testStepResult.appendError(err)
tc.TestStepResults = append(tc.TestStepResults, testStepResult)
return
}
for rangedIndex, rangedData := range ranged.Items {
tc.TestStepResults = append(tc.TestStepResults, TestStepResult{})
tsResult := &tc.TestStepResults[len(tc.TestStepResults)-1]
if ranged.Enabled {
Debug(ctx, "processing range index: %d", rangedIndex)
stepVars.Add("index", rangedIndex)
stepVars.Add("key", rangedData.Key)
stepVars.Add("value", rangedData.Value)
}
vars, err := DumpStringPreserveCase(stepVars)
if err != nil {
Error(ctx, "unable to dump testcase vars: %v", err)
tsResult.appendError(err)
return
}
for k, v := range vars {
content, err := interpolate.Do(v, vars)
if err != nil {
tsResult.appendError(err)
Error(ctx, "unable to interpolate variable %q: %v", k, err)
return
}
vars[k] = content
}
// the value of each var can contains a double-quote -> "
// if the value is not escaped, it will be used as is, and the json sent to unmarshall will be incorrect.
// This also avoids injections into the json structure of a step
for i := range vars {
if strings.Contains(vars[i], `"`) {
x := strconv.Quote(vars[i])
x = strings.TrimPrefix(x, `"`)
x = strings.TrimSuffix(x, `"`)
vars[i] = x
}
}
var content string
for i := 0; i < 10; i++ {
content, err = interpolate.Do(string(rawStep), vars)
if err != nil {
tsResult.appendError(err)
Error(ctx, "unable to interpolate step: %v", err)
return
}
if !strings.Contains(content, "{{") {
break
}
}
if ranged.Enabled {
Info(ctx, "Step #%d-%d content is: %s", stepNumber, rangedIndex, HideSensitive(ctx, content))
} else {
Info(ctx, "Step #%d content is: %s", stepNumber, HideSensitive(ctx, content))
}
data, err := yaml.Marshal(rawStep)
if err != nil {
tsResult.appendError(err)
Error(ctx, "unable to marshal raw: %v", err)
}
tsResult.Raw = data
var step TestStep
if err := yaml.Unmarshal([]byte(content), &step); err != nil {
tsResult.appendError(err)
Error(ctx, "unable to parse step #%d: %v", stepNumber, err)
Error(ctx, content)
v.printTestStepResult(tc, tsResult, tsIn, stepNumber, false)
break loopRawTestSteps
}
data2, err := yaml.JSONToYAML([]byte(content))
if err != nil {
tsResult.appendError(err)
Error(ctx, "unable to marshal step #%d to json: %v", stepNumber, err)
}
tsResult.Interpolated = data2
tsResult.Number = stepNumber
tsResult.RangedIndex = rangedIndex
tsResult.RangedEnable = ranged.Enabled
tsResult.InputVars = vars
tc.testSteps = append(tc.testSteps, step)
var e ExecutorRunner
ctx, e, err = v.GetExecutorRunner(ctx, step, stepVars)
if err != nil {
tsResult.appendError(err)
Error(ctx, "unable to get executor: %v", err)
v.printTestStepResult(tc, tsResult, tsIn, stepNumber, false)
break loopRawTestSteps
}
if e != nil {
_, known := knowExecutors[e.Name()]
if !known {
ctx, err = e.Setup(ctx, tc.Vars)
if err != nil {
tsResult.appendError(err)
Error(ctx, "unable to setup executor: %v", err)
break
}
knowExecutors[e.Name()] = struct{}{}
defer func(ctx context.Context) {
if err := e.TearDown(ctx); err != nil {
tsResult.appendError(err)
Error(ctx, "unable to teardown executor: %v", err)
}
}(ctx)
}
}
v.setTestStepName(tsResult, e, step, &ranged, &rangedData, rangedIndex)
if v.Verbose >= 1 && !fromUserExecutor {
v.Print(" \t\t• %s", tsResult.Name)
}
// ##### RUN Test Step Here
skip, err := parseSkip(ctx, tc, tsResult, rawStep, stepNumber)
if err != nil {
tsResult.appendError(err)
tsResult.Status = StatusFail
} else if skip {
tsResult.Status = StatusSkip
} else {
tsResult.Start = time.Now()
tsResult.Status = StatusRun
v.RunTestStep(ctx, e, tc, tsResult, stepNumber, rangedIndex, step)
if len(tsResult.Errors) > 0 || !tsResult.AssertionsApplied.OK {
tsResult.Status = StatusFail
} else {
tsResult.Status = StatusPass
}
tsResult.End = time.Now()
tsResult.Duration = tsResult.End.Sub(tsResult.Start).Seconds()
tc.testSteps = append(tc.testSteps, step)
}
var isRequired bool
if tsResult.Status != StatusFail {
Warn(ctx, "Step %q result is %q", tsResult.Name, tsResult.Status)
}
if tsResult.Status == StatusFail {
Error(ctx, "Step %q result is %q", tsResult.Name, tsResult.Status)
Error(ctx, "Errors: ")
for _, e := range tsResult.Errors {
Error(ctx, "%v", e)
isRequired = isRequired || e.AssertionRequired || v.StopOnFailure
}
if isRequired {
failure := newFailure(ctx, *tc, stepNumber, rangedIndex, "", errors.New("At least one required assertion failed, skipping remaining steps"))
tsResult.appendFailure(*failure)
v.printTestStepResult(tc, tsResult, tsIn, stepNumber, true)
return
}
v.printTestStepResult(tc, tsResult, tsIn, stepNumber, false)
continue
}
allVars := tc.Vars.Clone()
allVars.AddAll(tsResult.ComputedVars.Clone())
assign, _, errAssignment := processVariableAssignments(ctx, tc.Name, allVars, rawStep)
if errAssignment != nil {
tsResult.appendError(errAssignment)
Error(ctx, "unable to process variable assignments: %v", errAssignment)
}
v.printTestStepResult(tc, tsResult, tsIn, stepNumber, false)
if errAssignment != nil {
break loopRawTestSteps
}
tc.computedVars.AddAll(assign)
previousStepVars.AddAll(assign)
}
}
}
// Set test step name (defaults to executor name, excepted if it got a "name" attribute. in range, also print key)
func (v *Venom) setTestStepName(ts *TestStepResult, e ExecutorRunner, step TestStep, ranged *Range, rangedData *RangeData, rangedIndex int) {
name := e.Name()
if value, ok := step["name"]; ok {
switch value := value.(type) {
case string:
name = value
}
}
if ranged.Enabled {
name = fmt.Sprintf("%s (range=%s)", name, rangedData.Key)
}
ts.Name = name
}
// Print a single step result (if verbosity is enabled)
func (v *Venom) printTestStepResult(tc *TestCase, ts *TestStepResult, tsIn *TestStepResult, stepNumber int, mustAssertionFailed bool) {
if tsIn != nil {
tsIn.appendFailure(ts.Errors...)
} else if v.Verbose >= 1 {
if len(ts.Errors) > 0 {
v.Println(" %s", Red(StatusFail))
for _, i := range ts.ComputedInfo {
v.Println(" \t\t %s %s", Cyan("[info]"), Cyan(i))
}
for _, f := range ts.Errors {
v.Println(" \t\t %s", Yellow(f.Value))
}
if mustAssertionFailed {
skipped := len(tc.RawTestSteps) - stepNumber - 1
if skipped == 1 {
v.Println(" \t\t %s", Gray(fmt.Sprintf("%d other step was skipped", skipped)))
} else {
v.Println(" \t\t %s", Gray(fmt.Sprintf("%d other steps were skipped", skipped)))
}
}
} else if ts.Status == StatusSkip {
v.Println(" %s", Gray(StatusSkip))
} else {
if ts.Retries == 0 {
v.Println(" %s", Green(StatusPass))
} else {
v.Println(" %s (after %d attempts)", Green(StatusPass), ts.Retries)
}
for _, i := range ts.ComputedInfo {
v.Println(" \t\t %s %s", Cyan("[info]"), Cyan(i))
}
}
}
}
// Parse and format skip conditional
func parseSkip(ctx context.Context, tc *TestCase, ts *TestStepResult, rawStep []byte, stepNumber int) (bool, error) {
// Load "skip" attribute from step
var assertions struct {
Skip []string `yaml:"skip"`
}
if err := yaml.Unmarshal(rawStep, &assertions); err != nil {
return false, fmt.Errorf("unable to parse \"skip\" assertions: %v", err)
}
// Evaluate skip assertions
if len(assertions.Skip) > 0 {
results, err := testConditionalStatement(ctx, tc, assertions.Skip, tc.Vars, fmt.Sprintf("skipping testcase %%q step #%d: %%v", stepNumber))
if err != nil {
Error(ctx, "unable to evaluate \"skip\" assertions: %v", err)
return false, err
}
if len(results) > 0 {
for _, s := range results {
ts.Skipped = append(ts.Skipped, Skipped{Value: s})
Warn(ctx, s)
}
return true, nil
}
}
return false, nil
}
// Parse and format range data to allow iterations over user data
func parseRanged(ctx context.Context, rawStep []byte, stepVars H) (Range, error) {
//Load "range" attribute and perform actions depending on its typing
var ranged Range
if err := json.Unmarshal(rawStep, &ranged); err != nil {
return ranged, fmt.Errorf("unable to parse range expression: %v", err)
}
switch ranged.RawContent.(type) {
//Nil means this is not a ranged data, append an empty item to force at least one iteration and exit
case nil:
ranged.Items = append(ranged.Items, RangeData{})
return ranged, nil
//String needs to be parsed and possibly templated
case string:
Debug(ctx, "attempting to parse range expression")
rawString := ranged.RawContent.(string)
if len(rawString) == 0 {
return ranged, fmt.Errorf("range expression has been specified without any data")
}
// Try parsing already templated data
err := json.Unmarshal([]byte("{\"range\":"+rawString+"}"), &ranged)
// ... or fallback
if err != nil {
//Try templating and escaping data
Debug(ctx, "attempting to template range expression and parse it again")
vars, err := DumpStringPreserveCase(stepVars)
if err != nil {
Warn(ctx, "failed to parse range expression when loading step variables: %v", err)
break
}
for i := range vars {
vars[i] = strings.ReplaceAll(vars[i], "\"", "\\\"")
}
content, err := interpolate.Do(string(rawStep), vars)
if err != nil {
Warn(ctx, "failed to parse range expression when templating variables: %v", err)
break
}
//Try parsing data
err = json.Unmarshal([]byte(content), &ranged)
if err != nil {
Warn(ctx, "failed to parse range expression when parsing data into raw string: %v", err)
break
}
switch ranged.RawContent.(type) {
case string:
rawString = ranged.RawContent.(string)
err := json.Unmarshal([]byte("{\"range\":"+rawString+"}"), &ranged)
if err != nil {
Warn(ctx, "failed to parse range expression when parsing raw string into data: %v", err)
return ranged, fmt.Errorf("unable to parse range expression: unable to transform string data into a supported range expression type")
}
}
}
}
//Format data
switch t := ranged.RawContent.(type) {
//Array-like data
case []interface{}:
Debug(ctx, "\"range\" data is array-like")
for index, value := range ranged.RawContent.([]interface{}) {
key := strconv.Itoa(index)
ranged.Items = append(ranged.Items, RangeData{key, value})
}
//Number data
case float64:
Debug(ctx, "\"range\" data is number-like")
upperBound := int(ranged.RawContent.(float64))
for i := 0; i < upperBound; i++ {
key := strconv.Itoa(i)
ranged.Items = append(ranged.Items, RangeData{key, i})
}
//Map-like data
case map[string]interface{}:
Debug(ctx, "\"range\" data is map-like")
for key, value := range ranged.RawContent.(map[string]interface{}) {
ranged.Items = append(ranged.Items, RangeData{key, value})
}
//Unsupported data format
default:
return ranged, fmt.Errorf("\"range\" was provided an unsupported type %T", t)
}
ranged.Enabled = true
ranged.RawContent = nil
return ranged, nil
}
func processVariableAssignments(ctx context.Context, tcName string, tcVars H, rawStep json.RawMessage) (H, bool, error) {
var stepAssignment AssignStep
var result = make(H)
if err := yaml.Unmarshal(rawStep, &stepAssignment); err != nil {
Error(ctx, "unable to parse assignments (%s): %v", string(rawStep), err)
return nil, false, err
}
if len(stepAssignment.Assignments) == 0 {
return nil, false, nil
}
var tcVarsKeys []string
for k := range tcVars {
tcVarsKeys = append(tcVarsKeys, k)
}
for varname, assignment := range stepAssignment.Assignments {
Debug(ctx, "Processing %s assignment", varname)
varValue, has := tcVars[assignment.From]
if !has {
varValue, has = tcVars[tcName+"."+assignment.From]
if !has {
if assignment.Default == nil {
err := fmt.Errorf("%s reference not found", assignment.From)
Error(ctx, "%v", err)
return nil, true, err
}
varValue = assignment.Default
}
}
if assignment.Regex == "" {
Info(ctx, "Assign '%s' value '%s'", varname, varValue)
result.Add(varname, varValue)
} else {
regex, err := regexp.Compile(assignment.Regex)
if err != nil {
Warn(ctx, "unable to compile regexp %q", assignment.Regex)
return nil, true, err
}
varValueS, ok := varValue.(string)
if !ok {
Warn(ctx, "%q is not a string value", varname)
result.Add(varname, "")
continue
}
submatches := regex.FindStringSubmatch(varValueS)
if len(submatches) == 0 {
Warn(ctx, "%s: %q doesn't match anything in %q", varname, regex, varValue)
result.Add(varname, "")
continue
}
Info(ctx, "Assign %q from regexp %q, values %q", varname, regex, submatches)
result.Add(varname, submatches[len(submatches)-1])
}
}
return result, true, nil
}