-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_testsuite.go
251 lines (227 loc) · 6.22 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
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
package venom
import (
"context"
"fmt"
"os"
"runtime/pprof"
"time"
"github.com/gosimple/slug"
"github.com/ovh/cds/sdk/interpolate"
"github.com/pkg/errors"
)
func (v *Venom) runTestSuite(ctx context.Context, ts *TestSuite) error {
if v.Verbose == 3 {
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 {
return fmt.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()
}
}
// Initialize the testsuite variables and compute a first interpolation over them
ts.Vars.AddAll(v.variables.Clone())
vars, _ := DumpStringPreserveCase(ts.Vars)
for k, v := range vars {
computedV, err := interpolate.Do(fmt.Sprintf("%v", v), vars)
if err != nil {
return errors.Wrapf(err, "error while computing variable %s=%q", k, v)
}
ts.Vars.Add(k, computedV)
}
exePath, err := os.Executable()
if err != nil {
return errors.Wrapf(err, "failed to get executable path")
} else {
ts.Vars.Add("venom.executable", exePath)
}
ts.Vars.Add("venom.outputdir", v.OutputDir)
ts.Vars.Add("venom.libdir", v.LibDir)
ts.Vars.Add("venom.testsuite", ts.Name)
ts.ComputedVars = H{}
ctx = context.WithValue(ctx, ContextKey("testsuite"), ts.Name)
Info(ctx, "Starting testsuite")
defer Info(ctx, "Ending testsuite")
totalSteps := 0
for _, tc := range ts.TestCases {
totalSteps += len(tc.testSteps)
}
ts.Status = StatusRun
Info(ctx, "With secrets in testsuite")
for _, v := range ts.Secrets {
Info(ctx, "secret %+v", v)
}
// ##### RUN Test Cases Here
v.runTestCases(ctx, ts)
var isFailed bool
var nSkip int
for _, tc := range ts.TestCases {
if tc.Status == StatusFail {
isFailed = true
ts.NbTestcasesFail++
} else if tc.Status == StatusSkip {
nSkip++
ts.NbTestcasesSkip++
} else if tc.Status == StatusPass {
ts.NbTestcasesPass++
}
}
if isFailed {
ts.Status = StatusFail
v.Tests.NbTestsuitesFail++
} else if nSkip > 0 && nSkip == len(ts.TestCases) {
ts.Status = StatusSkip
v.Tests.NbTestsuitesSkip++
} else {
ts.Status = StatusPass
v.Tests.NbTestsuitesPass++
}
return nil
}
func (v *Venom) runTestCases(ctx context.Context, ts *TestSuite) {
verboseReport := v.Verbose >= 1
v.Println(" • %s (%s)", ts.Name, ts.Filepath)
for i := range ts.TestCases {
tc := &ts.TestCases[i]
tc.IsEvaluated = true
v.Print(" \t• %s", tc.Name)
var hasFailure bool
var hasRanged bool
var hasSkipped = len(tc.Skipped) > 0
if !hasSkipped {
start := time.Now()
tc.Start = start
ts.Status = StatusRun
if verboseReport || hasRanged {
v.Print("\n")
}
// ##### RUN Test Case Here
v.runTestCase(ctx, ts, tc)
tc.End = time.Now()
tc.Duration = tc.End.Sub(tc.Start).Seconds()
}
skippedSteps := 0
for _, testStepResult := range tc.TestStepResults {
if testStepResult.RangedEnable {
hasRanged = true
}
if testStepResult.Status == StatusFail {
hasFailure = true
}
if testStepResult.Status == StatusSkip {
skippedSteps++
}
}
if hasFailure {
tc.Status = StatusFail
} else if skippedSteps == len(tc.TestStepResults) {
//If all test steps were skipped, consider the test case as skipped
tc.Status = StatusSkip
} else if tc.Status != StatusSkip {
tc.Status = StatusPass
}
// Verbose mode already reported tests status, so just print them when non-verbose
indent := ""
if verboseReport {
indent = "\t "
// If the testcase was entirely skipped, then the verbose mode will not have any output
// Print something to inform that the testcase was indeed processed although skipped
if len(tc.TestStepResults) == 0 {
v.Println("\t\t%s", Gray("• (all steps were skipped)"))
continue
}
} else {
if hasFailure {
v.Println(" %s", Red(StatusFail))
} else if tc.Status == StatusSkip {
v.Println(" %s", Gray(StatusSkip))
continue
} else {
v.Println(" %s", Green(StatusPass))
}
}
for _, i := range tc.computedVerbose {
v.PrintlnIndentedTrace(i, indent)
}
// Verbose mode already reported failures, so just print them when non-verbose
if !verboseReport && hasFailure {
for _, testStepResult := range tc.TestStepResults {
if len(testStepResult.ComputedInfo) > 0 || len(testStepResult.Errors) > 0 {
v.Println(" \t\t• %s", testStepResult.Name)
for _, f := range testStepResult.ComputedInfo {
v.Println(" \t\t %s", Cyan(f))
}
for _, f := range testStepResult.Errors {
v.Println(" \t\t %s", Yellow(f.Value))
}
}
}
}
if v.StopOnFailure {
for _, testStepResult := range tc.TestStepResults {
if len(testStepResult.Errors) > 0 {
// break TestSuite
return
}
}
}
ts.ComputedVars.AddAllWithPrefix(tc.Name, tc.computedVars)
}
}
// Parse the suite to find unreplaced and extracted variables
func (v *Venom) parseTestSuite(ts *TestSuite) ([]string, []string, error) {
return v.parseTestCases(ts)
}
// Parse the testscases to find unreplaced and extracted variables
func (v *Venom) parseTestCases(ts *TestSuite) ([]string, []string, error) {
var vars []string
var extractsVars []string
for i := range ts.TestCases {
tc := &ts.TestCases[i]
tc.originalName = tc.Name
tc.number = i
tc.Name = slug.Make(tc.Name)
tc.Vars = ts.Vars.Clone()
tc.Vars.Add("venom.testcase", tc.Name)
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
}