-
Notifications
You must be signed in to change notification settings - Fork 144
/
types_executor.go
334 lines (291 loc) · 8 KB
/
types_executor.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
package venom
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/gosimple/slug"
"github.com/ovh/cds/sdk/interpolate"
"github.com/pkg/errors"
"github.com/rockbears/yaml"
)
// Executor execute a testStep.
type Executor interface {
// Run run a Test Step
Run(context.Context, TestStep) (interface{}, error)
}
type ExecutorRunner interface {
Executor
executorWithDefaultAssertions
executorWithZeroValueResult
ExecutorWithSetup
Name() string
Retry() int
RetryIf() []string
Delay() int
Timeout() int
Info() []string
Type() string
GetExecutor() Executor
}
var _ Executor = new(executor)
// ExecutorWrap contains an executor implementation and some attributes
type executor struct {
Executor
name string
retry int // nb retry a test case if it is in failure.
retryIf []string // retry conditions to check before performing any retries
delay int // delay between two retries
timeout int // timeout on executor
info []string // info to display after the run and before the assertion
stype string // builtin, plugin, user
}
func (e executor) Name() string {
return e.name
}
func (e executor) Type() string {
return e.stype
}
func (e executor) Retry() int {
return e.retry
}
func (e executor) RetryIf() []string {
return e.retryIf
}
func (e executor) Delay() int {
return e.delay
}
func (e executor) Timeout() int {
return e.timeout
}
func (e executor) Info() []string {
return e.info
}
func (e executor) GetExecutor() Executor {
return e.Executor
}
func (e executor) GetDefaultAssertions() *StepAssertions {
if e.Executor == nil {
return nil
}
x, ok := e.Executor.(executorWithDefaultAssertions)
if ok {
return x.GetDefaultAssertions()
}
return nil
}
func (e executor) ZeroValueResult() interface{} {
if e.Executor == nil {
return nil
}
x, ok := e.Executor.(executorWithZeroValueResult)
if ok {
return x.ZeroValueResult()
}
return nil
}
func (e executor) Setup(ctx context.Context, vars H) (context.Context, error) {
if e.Executor == nil {
return ctx, nil
}
x, ok := e.Executor.(ExecutorWithSetup)
if ok {
return x.Setup(ctx, vars)
}
return ctx, nil
}
func (e executor) TearDown(ctx context.Context) error {
if e.Executor == nil {
return nil
}
x, ok := e.Executor.(ExecutorWithSetup)
if ok {
return x.TearDown(ctx)
}
return nil
}
func (e executor) Run(ctx context.Context, step TestStep) (interface{}, error) {
if e.Executor == nil {
return nil, nil
}
return e.Executor.Run(ctx, step)
}
func newExecutorRunner(e Executor, name, stype string, retry int, retryIf []string, delay, timeout int, info []string) ExecutorRunner {
return &executor{
Executor: e,
name: name,
retry: retry,
retryIf: retryIf,
delay: delay,
timeout: timeout,
info: info,
stype: stype,
}
}
// executorWithDefaultAssertions execute a testStep.
type executorWithDefaultAssertions interface {
// GetDefaultAssertion returns default assertions
GetDefaultAssertions() *StepAssertions
}
type executorWithZeroValueResult interface {
ZeroValueResult() interface{}
}
type ExecutorWithSetup interface {
Setup(ctx context.Context, vars H) (context.Context, error)
TearDown(ctx context.Context) error
}
func GetExecutorResult(r interface{}) map[string]interface{} {
d, err := Dump(r)
if err != nil {
panic(err)
}
return d
}
type UserExecutor struct {
Executor string `json:"executor" yaml:"executor"`
Input H `json:"input" yaml:"input"`
RawTestSteps []json.RawMessage `json:"steps" yaml:"steps"`
Output json.RawMessage `json:"output" yaml:"output"`
Filename string `json:"-" yaml:"-"`
}
// Run is not implemented on user executor
func (ux UserExecutor) Run(ctx context.Context, step TestStep) (interface{}, error) {
return nil, errors.New("Run not implemented for user interface, use RunUserExecutor instead")
}
func (ux UserExecutor) ZeroValueResult() interface{} {
type Output struct {
Result interface{} `json:"result"`
}
output := &Output{
Result: ux.Output,
}
outputS, err := json.Marshal(output)
if err != nil {
return ""
}
result := make(map[string]interface{})
err = JSONUnmarshal(outputS, &result)
if err != nil {
return ""
}
return result
}
func (v *Venom) RunUserExecutor(ctx context.Context, runner ExecutorRunner, tcIn *TestCase, tsIn *TestStepResult, step TestStep) (interface{}, error) {
vrs := tcIn.TestSuiteVars.Clone()
uxIn := runner.GetExecutor().(UserExecutor)
for k, va := range uxIn.Input {
if strings.HasPrefix(k, "input.") {
// do not reinject input.vars from parent user executor if exists
continue
} else if !strings.HasPrefix(k, "venom") {
if vl, ok := step[k]; ok && vl != "" { // value from step
vrs.AddWithPrefix("input", k, vl)
} else { // default value from executor
vrs.AddWithPrefix("input", k, va)
}
} else {
vrs.Add(k, va)
}
}
// reload the user executor with the interpolated vars
_, exe, err := v.GetExecutorRunner(ctx, step, vrs)
if err != nil {
return nil, errors.Wrapf(err, "unable to reload executor")
}
ux := exe.GetExecutor().(UserExecutor)
tc := &TestCase{
TestCaseInput: TestCaseInput{
Name: ux.Executor,
RawTestSteps: ux.RawTestSteps,
Vars: vrs,
},
number: tcIn.number,
TestSuiteVars: tcIn.TestSuiteVars,
IsExecutor: true,
TestStepResults: make([]TestStepResult, 0),
}
tc.originalName = tc.Name
tc.Name = slug.Make(tc.Name)
tc.Vars.Add("venom.testcase", tc.Name)
tc.Vars.Add("venom.executor.filename", ux.Filename)
tc.Vars.Add("venom.executor.name", ux.Executor)
tc.computedVars = H{}
Debug(ctx, "running user executor %v", tc.Name)
Debug(ctx, "with vars: %v", vrs)
v.runTestSteps(ctx, tc, tsIn)
computedVars, err := DumpString(tc.computedVars)
if err != nil {
return nil, errors.Wrapf(err, "unable to dump testcase computedVars")
}
type Output struct {
Result json.RawMessage `json:"result"`
}
output := Output{
Result: ux.Output,
}
outputString, err := json.Marshal(output)
if err != nil {
return nil, err
}
// 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 user executor
for i := range computedVars {
computedVars[i] = strings.ReplaceAll(computedVars[i], "\"", "\\\"")
}
outputS, err := interpolate.Do(string(outputString), computedVars)
if err != nil {
return nil, err
}
var outputResult interface{}
if err := yaml.Unmarshal([]byte(outputS), &outputResult); err != nil {
return nil, errors.Wrapf(err, "unable to unmarshal")
}
if len(tsIn.Errors) > 0 {
return outputResult, fmt.Errorf("failed")
}
// here, we have the user executor results.
// and for each key in output, we try to add the json version
// this will allow user to use json version of output (map, etc...)
// because, it's not possible to to that:
// output:
// therawout: {{.result.systemout}}
//
// test is in file user_executor.yml
result, err := Dump(outputResult)
if err != nil {
return nil, errors.Wrapf(err, "unable to compute result")
}
for k, v := range result {
switch z := v.(type) {
case string:
var outJSON interface{}
if err := JSONUnmarshal([]byte(z), &outJSON); err == nil {
result[k+"json"] = outJSON
// Now we have to dump this object, but the key will change if this is a array or not
if reflect.ValueOf(outJSON).Kind() == reflect.Slice {
prefix := k + "json"
splitPrefix := strings.Split(prefix, ".")
prefix += "." + splitPrefix[len(splitPrefix)-1]
outJSONDump, err := Dump(outJSON)
if err != nil {
return nil, errors.Wrapf(err, "unable to compute result")
}
for ko, vo := range outJSONDump {
result[prefix+ko] = vo
}
} else {
outJSONDump, err := DumpWithPrefix(outJSON, k+"json")
if err != nil {
return nil, errors.Wrapf(err, "unable to compute result")
}
for ko, vo := range outJSONDump {
result[ko] = vo
}
}
}
}
}
return result, nil
}