-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_teststep.go
111 lines (94 loc) · 2.98 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
package venom
import (
"context"
"fmt"
"time"
dump "github.com/fsamin/go-dump"
log "github.com/sirupsen/logrus"
)
//RunTestStep executes a venom testcase is a venom context
func (v *Venom) RunTestStep(tcc TestCaseContext, e *ExecutorWrap, ts *TestSuite, tc *TestCase, stepNumber int, step TestStep, l Logger) ExecutorResult {
var assertRes assertionsApplied
var retry int
var result ExecutorResult
for retry = 0; retry <= e.retry && !assertRes.ok; retry++ {
if retry > 1 && !assertRes.ok {
l.Debugf("Sleep %d, it's %d attempt", e.delay, retry)
time.Sleep(time.Duration(e.delay) * time.Second)
}
var err error
result, err = runTestStepExecutor(tcc, e, ts, step, l)
if err != nil {
// we save the failure only if it's the last attempt
if retry == e.retry {
tc.Failures = append(tc.Failures, Failure{Value: RemoveNotPrintableChar(err.Error())})
}
continue
}
// add result in templater
ts.Templater.Add(tc.Name, stringifyExecutorResult(result))
if h, ok := e.executor.(executorWithDefaultAssertions); ok {
assertRes = applyChecks(&result, *tc, stepNumber, step, h.GetDefaultAssertions())
} else {
assertRes = applyChecks(&result, *tc, stepNumber, step, nil)
}
// add result again for extracts values
ts.Templater.Add(tc.Name, stringifyExecutorResult(result))
// then template the TestSuite vars if needed
var applied bool
applied, ts.Vars, err = ts.Templater.ApplyOnMap(ts.Vars)
if err != nil {
log.Errorf("err:%s", err)
}
if applied {
d, err := dump.ToStringMap(ts.Vars)
if err != nil {
log.Errorf("err:%s", err)
}
ts.Templater.Add("", d)
}
if assertRes.ok {
break
}
}
tc.Errors = append(tc.Errors, assertRes.errors...)
tc.Failures = append(tc.Failures, assertRes.failures...)
if retry > 1 && (len(assertRes.failures) > 0 || len(assertRes.errors) > 0) {
tc.Failures = append(tc.Failures, Failure{Value: fmt.Sprintf("It's a failure after %d attempts", retry)})
}
tc.Systemout.Value += assertRes.systemout
tc.Systemerr.Value += assertRes.systemerr
return result
}
func stringifyExecutorResult(e ExecutorResult) map[string]string {
out := make(map[string]string)
for k, v := range e {
out[k] = fmt.Sprintf("%v", v)
}
return out
}
func runTestStepExecutor(tcc TestCaseContext, e *ExecutorWrap, ts *TestSuite, step TestStep, l Logger) (ExecutorResult, error) {
if e.timeout == 0 {
return e.executor.Run(tcc, l, step, ts.WorkDir)
}
ctxTimeout, cancel := context.WithTimeout(context.Background(), time.Duration(e.timeout)*time.Second)
defer cancel()
ch := make(chan ExecutorResult)
cherr := make(chan error)
go func(tcc TestCaseContext, e *ExecutorWrap, step TestStep, l Logger) {
result, err := e.executor.Run(tcc, l, step, ts.WorkDir)
if err != nil {
cherr <- err
} else {
ch <- result
}
}(tcc, e, step, l)
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)
}
}