-
Notifications
You must be signed in to change notification settings - Fork 144
/
process_teststep.go
177 lines (154 loc) · 5.35 KB
/
process_teststep.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
package venom
import (
"context"
"encoding/json"
"fmt"
"os"
"path"
"time"
"github.com/gosimple/slug"
"github.com/ovh/cds/sdk/interpolate"
)
type dumpFile struct {
Variables H `json:"variables"`
TestStep TestStep `json:"step"`
Result interface{} `json:"result"`
}
// RunTestStep executes a venom testcase is a venom context
func (v *Venom) RunTestStep(ctx context.Context, e ExecutorRunner, tc *TestCase, tsResult *TestStepResult, stepNumber int, rangedIndex int, step TestStep) {
ctx = context.WithValue(ctx, ContextKey("executor"), e.Name())
var assertRes AssertionsApplied
var result interface{}
for tsResult.Retries = 0; tsResult.Retries <= e.Retry() && !assertRes.OK; tsResult.Retries++ {
if tsResult.Retries > 1 && !assertRes.OK {
Debug(ctx, "Sleep %d, it's %d attempt", e.Delay(), tsResult.Retries)
time.Sleep(time.Duration(e.Delay()) * time.Second)
}
var err error
result, err = v.runTestStepExecutor(ctx, e, tc, tsResult, step)
if err != nil {
// we save the failure only if it's the last attempt
if tsResult.Retries == e.Retry() {
failure := newFailure(ctx, *tc, stepNumber, rangedIndex, "", err)
tsResult.appendFailure(*failure)
}
continue
}
Debug(ctx, "result of executor: %s", HideSensitive(ctx, result))
mapResult := GetExecutorResult(result)
mapResultString, _ := DumpString(result)
if v.Verbose >= 2 {
fdump := dumpFile{
Result: result,
TestStep: step,
Variables: AllVarsFromCtx(ctx),
}
output, err := json.MarshalIndent(fdump, "", " ")
if err != nil {
Error(ctx, "unable to marshal result: %v", err)
}
oDir := v.OutputDir
if oDir == "" {
oDir = "."
}
filename := path.Join(oDir, fmt.Sprintf("%s.%s.testcase.%d.step.%d.%d.dump.json", slug.Make(StringVarFromCtx(ctx, "venom.testsuite.shortName")), slug.Make(tc.Name), tc.number, stepNumber, rangedIndex))
if err := os.WriteFile(filename, []byte(HideSensitive(ctx, string(output))), 0644); err != nil {
Error(ctx, "Error while creating file %s: %v", filename, err)
return
}
tc.computedVerbose = append(tc.computedVerbose, fmt.Sprintf("writing %s", filename))
}
for ninfo, i := range e.Info() {
info, err := interpolate.Do(i, mapResultString)
if err != nil {
Error(ctx, "unable to parse %q: %v", i, err)
continue
}
if info == "" {
continue
}
filename := StringVarFromCtx(ctx, "venom.testsuite.filename")
lineNumber := findLineNumber(filename, tc.originalName, stepNumber, i, ninfo+1)
if lineNumber > 0 {
info += fmt.Sprintf(" (%s:%d)", filename, lineNumber)
} else if tc.IsExecutor {
filename = StringVarFromCtx(ctx, "venom.executor.filename")
originalName := StringVarFromCtx(ctx, "venom.executor.name")
lineNumber = findLineNumber(filename, originalName, stepNumber, i, ninfo+1)
if lineNumber > 0 {
info += fmt.Sprintf(" (%s:%d)", filename, lineNumber)
}
}
Info(ctx, info)
tsResult.ComputedInfo = append(tsResult.ComputedInfo, info)
}
if result == nil {
Debug(ctx, "empty testcase, applying assertions on variables: %v", AllVarsFromCtx(ctx))
assertRes = applyAssertions(ctx, AllVarsFromCtx(ctx), *tc, stepNumber, rangedIndex, step, nil)
} else {
if h, ok := e.(executorWithDefaultAssertions); ok {
assertRes = applyAssertions(ctx, result, *tc, stepNumber, rangedIndex, step, h.GetDefaultAssertions())
} else {
assertRes = applyAssertions(ctx, result, *tc, stepNumber, rangedIndex, step, nil)
}
}
tsResult.AssertionsApplied = assertRes
tsResult.ComputedVars.AddAll(H(mapResult))
if assertRes.OK {
break
}
failures, err := testConditionalStatement(ctx, tc, e.RetryIf(), tsResult.ComputedVars, "")
if err != nil {
tsResult.appendError(fmt.Errorf("Error while evaluating retry condition: %v", err))
break
}
if len(failures) > 0 {
failure := newFailure(ctx, *tc, stepNumber, rangedIndex, "", fmt.Errorf("retry conditions not fulfilled, skipping %d remaining retries", e.Retry()-tsResult.Retries))
tsResult.Errors = append(tsResult.Errors, *failure)
break
}
}
if tsResult.Retries > 1 && len(assertRes.errors) > 0 {
tsResult.appendFailure(Failure{Value: fmt.Sprintf("It's a failure after %d attempts", tsResult.Retries)})
}
if len(assertRes.errors) > 0 {
tsResult.appendFailure(assertRes.errors...)
}
tsResult.Systemerr += assertRes.systemerr + "\n"
tsResult.Systemout += assertRes.systemout + "\n"
}
func (v *Venom) runTestStepExecutor(ctx context.Context, e ExecutorRunner, tc *TestCase, ts *TestStepResult, step TestStep) (interface{}, error) {
ctx = context.WithValue(ctx, ContextKey("executor"), e.Name())
if e.Timeout() == 0 {
if e.Type() == "user" {
return v.RunUserExecutor(ctx, e, tc, ts, step)
}
return e.Run(ctx, step)
}
ctxTimeout, cancel := context.WithTimeout(ctx, time.Duration(e.Timeout())*time.Second)
defer cancel()
ch := make(chan interface{})
cherr := make(chan error)
go func(e ExecutorRunner, step TestStep) {
var err error
var result interface{}
if e.Type() == "user" {
result, err = v.RunUserExecutor(ctx, e, tc, ts, step)
} else {
result, err = e.Run(ctx, step)
}
if err != nil {
cherr <- err
} else {
ch <- result
}
}(e, step)
select {
case err := <-cherr:
return nil, err
case result := <-ch:
return result, nil
case <-ctxTimeout.Done():
return nil, fmt.Errorf("Timeout after %d second(s)", e.Timeout())
}
}