-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_testsuite.go
156 lines (139 loc) · 3.55 KB
/
process_testsuite.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
package venom
import (
"fmt"
"os"
"runtime/pprof"
"strings"
"time"
"github.com/fatih/color"
"github.com/fsamin/go-dump"
log "github.com/sirupsen/logrus"
)
func (v *Venom) runTestSuite(ts *TestSuite) {
if v.EnableProfiling {
var filename, filenameCPU, filenameMem string
if v.OutputDir != "" {
filename = v.OutputDir + "/"
}
filenameCPU = filename + "pprof_cpu_profile_" + ts.Filename + ".prof"
filenameMem = filename + "pprof_mem_profile_" + ts.Filename + ".prof"
fCPU, errCPU := os.Create(filenameCPU)
fMem, errMem := os.Create(filenameMem)
if errCPU != nil || errMem != nil {
log.Errorf("error while create profile file CPU:%v MEM:%v", errCPU, errMem)
} else {
pprof.StartCPUProfile(fCPU)
p := pprof.Lookup("heap")
defer p.WriteTo(fMem, 1)
defer pprof.StopCPUProfile()
}
}
l := log.WithField("v.testsuite", ts.Name)
start := time.Now()
d, err := dump.ToStringMap(ts.Vars)
if err != nil {
log.Errorf("err:%s", err)
}
ts.Templater.Add("", d)
ts.Templater.Add("", map[string]string{"venom.testsuite": ts.ShortName})
ts.Templater.Add("", map[string]string{"venom.testsuite.filename": ts.Filename})
// we apply templater on current vars only
for index := 0; index < 10; index++ {
var toApply bool
for k, v := range ts.Templater.Values {
if strings.Contains(v, "{{") {
toApply = true
_, s := ts.Templater.apply([]byte(v))
ts.Templater.Values[k] = string(s)
}
}
if !toApply {
break
}
}
totalSteps := 0
for _, tc := range ts.TestCases {
totalSteps += len(tc.TestSteps)
}
v.runTestCases(ts, l)
elapsed := time.Since(start)
var o string
if ts.Failures > 0 || ts.Errors > 0 {
red := color.New(color.FgRed).SprintFunc()
o = fmt.Sprintf("%s %s", red("FAILURE"), rightPad(ts.Package, " ", 47))
} else {
green := color.New(color.FgGreen).SprintFunc()
o = fmt.Sprintf("%s %s", green("SUCCESS"), rightPad(ts.Package, " ", 47))
}
o += fmt.Sprintf("%s", elapsed)
v.PrintFunc("%s\n", o)
}
func (v *Venom) runTestCases(ts *TestSuite, l Logger) {
for i := range ts.TestCases {
tc := &ts.TestCases[i]
if len(tc.Skipped) == 0 {
v.runTestCase(ts, tc, l)
}
if len(tc.Failures) > 0 {
ts.Failures += len(tc.Failures)
}
if len(tc.Errors) > 0 {
ts.Errors += len(tc.Errors)
}
if len(tc.Skipped) > 0 {
ts.Skipped += len(tc.Skipped)
}
if v.StopOnFailure && (len(tc.Failures) > 0 || len(tc.Errors) > 0) {
// break TestSuite
return
}
}
}
//Parse the suite to find unreplaced and extracted variables
func (v *Venom) parseTestSuite(ts *TestSuite) ([]string, []string, error) {
d, err := dump.ToStringMap(ts.Vars)
if err != nil {
log.Errorf("err:%s", err)
}
ts.Templater.Add("", d)
return v.parseTestCases(ts)
}
//Parse the testscases to find unreplaced and extracted variables
func (v *Venom) parseTestCases(ts *TestSuite) ([]string, []string, error) {
vars := []string{}
extractsVars := []string{}
for i := range ts.TestCases {
tc := &ts.TestCases[i]
if len(tc.Skipped) == 0 {
tvars, tExtractedVars, err := v.parseTestCase(ts, tc)
if err != nil {
return nil, nil, err
}
for _, k := range tvars {
var found bool
for i := 0; i < len(vars); i++ {
if vars[i] == k {
found = true
break
}
}
if !found {
vars = append(vars, k)
}
}
for _, k := range tExtractedVars {
var found bool
for i := 0; i < len(extractsVars); i++ {
if extractsVars[i] == k {
found = true
break
}
}
if !found {
extractsVars = append(extractsVars, k)
}
}
}
}
return vars, extractsVars, nil
}