forked from ovh/venom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
247 lines (209 loc) · 8.16 KB
/
types.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
package venom
import (
"encoding/xml"
"github.com/fatih/color"
)
const (
// DetailsLow prints only summary results
DetailsLow = "low"
// DetailsMedium summary with lines in failure
DetailsMedium = "medium"
// DetailsHigh all
DetailsHigh = "high"
)
type H map[string]string
func (h H) Clone() H {
var h2 = make(H, len(h))
h2.AddAll(h)
return h2
}
func (h *H) Add(k, v string) {
(*h)[k] = v
}
func (h *H) AddWithPrefix(p, k, v string) {
(*h)[p+"."+k] = v
}
func (h *H) AddAll(h2 H) {
for k, v := range h2 {
h.Add(k, v)
}
}
func (h H) Get(k string) string {
return (h)[k]
}
func (h *H) AddAllWithPrefix(p string, h2 H) {
for k, v := range h2 {
h.AddWithPrefix(p, k, v)
}
}
// Aliases contains list of aliases
type Aliases map[string]string
// ExecutorResult represents an executor result on a test step
type ExecutorResult map[string]interface{}
// StepAssertions contains step assertions
type StepAssertions struct {
Assertions []string `json:"assertions,omitempty" yaml:"assertions,omitempty"`
}
// StepExtracts contains "step extracts"
type StepExtracts struct {
Extracts map[string]string `json:"extracts,omitempty" yaml:"extracts,omitempty"`
}
// Executor execute a testStep.
type Executor interface {
// Run run a Test Step
Run(TestCaseContext, Logger, TestStep, string) (ExecutorResult, error)
}
// TestCaseContext represents the context of a testcase
type TestCaseContext interface {
Init() error
Close() error
SetTestCase(tc TestCase)
GetName() string
}
// CommonTestCaseContext represents a Default TestCase Context
type CommonTestCaseContext struct {
TestCaseContext
TestCase TestCase
Name string
}
// SetTestCase set testcase in context
func (tcc *CommonTestCaseContext) SetTestCase(tc TestCase) {
tcc.TestCase = tc
}
// GetName Get the context name
func (tcc *CommonTestCaseContext) GetName() string {
return tcc.Name
}
// ExecutorWrap contains an executor implementation and some attributes
type ExecutorWrap struct {
executor Executor
retry int // nb retry a test case if it is in failure.
delay int // delay between two retries
timeout int // timeout on executor
}
// executorWithDefaultAssertions execute a testStep.
type executorWithDefaultAssertions interface {
// GetDefaultAssertion returns default assertions
GetDefaultAssertions() *StepAssertions
}
type executorWithZeroValueResult interface {
ZeroValueResult() ExecutorResult
}
// Tests contains all informations about tests in a pipeline build
type Tests struct {
XMLName xml.Name `xml:"testsuites" json:"-" yaml:"-"`
Total int `xml:"-" json:"total"`
TotalOK int `xml:"-" json:"ok"`
TotalKO int `xml:"-" json:"ko"`
TotalSkipped int `xml:"-" json:"skipped"`
TestSuites []TestSuite `xml:"testsuite" json:"test_suites"`
}
// TestSuite is a single JUnit test suite which may contain many
// testcases.
type TestSuite struct {
XMLName xml.Name `xml:"testsuite" json:"-" yaml:"-"`
Disabled int `xml:"disabled,attr,omitempty" json:"disabled" yaml:"-"`
Errors int `xml:"errors,attr,omitempty" json:"errors" yaml:"-"`
Failures int `xml:"failures,attr,omitempty" json:"failures" yaml:"-"`
Hostname string `xml:"hostname,attr,omitempty" json:"hostname" yaml:"-"`
ID string `xml:"id,attr,omitempty" json:"id" yaml:"-"`
Name string `xml:"name,attr" json:"name" yaml:"name"`
Filename string `xml:"-" json:"-" yaml:"-"`
ShortName string `xml:"-" json:"-" yaml:"-"`
Package string `xml:"package,attr,omitempty" json:"package" yaml:"-"`
Properties []Property `xml:"-" json:"properties" yaml:"-"`
Skipped int `xml:"skipped,attr,omitempty" json:"skipped" yaml:"skipped,omitempty"`
Total int `xml:"tests,attr" json:"total" yaml:"total,omitempty"`
TestCases []TestCase `xml:"testcase" hcl:"testcase" json:"tests" yaml:"testcases"`
Version string `xml:"version,omitempty" hcl:"version" json:"version" yaml:"version,omitempty"`
Time string `xml:"time,attr,omitempty" json:"time" yaml:"-"`
Timestamp string `xml:"timestamp,attr,omitempty" json:"timestamp" yaml:"-"`
Vars map[string]interface{} `xml:"-" json:"-" yaml:"vars"`
Templater *Templater `xml:"-" json:"-" yaml:"-"`
WorkDir string `xml:"-" json:"-" yaml:"-"`
}
// Property represents a key/value pair used to define properties.
type Property struct {
XMLName xml.Name `xml:"property" json:"-" yaml:"-"`
Name string `xml:"name,attr" json:"name" yaml:"-"`
Value string `xml:"value,attr" json:"value" yaml:"-"`
}
// TestCase is a single test case with its result.
type TestCase struct {
XMLName xml.Name `xml:"testcase" json:"-" yaml:"-"`
Classname string `xml:"classname,attr,omitempty" json:"classname" yaml:"-"`
Errors []Failure `xml:"error,omitempty" json:"errors" yaml:"errors,omitempty"`
Failures []Failure `xml:"failure,omitempty" json:"failures" yaml:"failures,omitempty"`
Name string `xml:"name,attr" json:"name" yaml:"name"`
Skipped []Skipped `xml:"skipped,omitempty" json:"skipped" yaml:"skipped,omitempty"`
Status string `xml:"status,attr,omitempty" json:"status" yaml:"status,omitempty"`
Systemout InnerResult `xml:"system-out,omitempty" json:"systemout" yaml:"systemout,omitempty"`
Systemerr InnerResult `xml:"system-err,omitempty" json:"systemerr" yaml:"systemerr,omitempty"`
Time string `xml:"time,attr,omitempty" json:"time" yaml:"time,omitempty"`
TestSteps []TestStep `xml:"-" hcl:"step" json:"steps" yaml:"steps"`
Context map[string]interface{} `xml:"-" json:"-" yaml:"context,omitempty"`
}
// TestStep represents a testStep
type TestStep map[string]interface{}
// Skipped contains data related to a skipped test.
type Skipped struct {
Value string `xml:",cdata" json:"value" yaml:"value,omitempty"`
}
// Failure contains data related to a failed test.
type Failure struct {
TestcaseClassname string `xml:"-" json:"-" yaml:"-"`
TestcaseName string `xml:"-" json:"-" yaml:"-"`
TestcaseLineNumber int `xml:"-" json:"-" yaml:"-"`
StepNumber int `xml:"-" json:"-" yaml:"-"`
Assertion string `xml:"-" json:"-" yaml:"-"`
Error error `xml:"-" json:"-" yaml:"-"`
Value string `xml:",cdata" json:"value" yaml:"value,omitempty"`
Result ExecutorResult `xml:"-" json:"-" yaml:"-"`
Type string `xml:"type,attr,omitempty" json:"type" yaml:"type,omitempty"`
Message string `xml:"message,attr,omitempty" json:"message" yaml:"message,omitempty"`
}
func newFailure(tc TestCase, stepNumber int, assertion string, err error, res ExecutorResult) *Failure {
var lineNumber int
lineNumber, _ = findLineNumber(tc.Classname, tc.Name, stepNumber, assertion)
value := color.YellowString(` Failure in %q:%d
Testcase %q, at step %d
Assertion %q failed. %v`,
tc.Classname,
lineNumber,
tc.Name,
stepNumber,
RemoveNotPrintableChar(assertion),
err,
)
var failure = Failure{
TestcaseClassname: tc.Classname,
TestcaseName: tc.Name,
TestcaseLineNumber: lineNumber,
StepNumber: stepNumber,
Assertion: assertion,
Result: res,
Error: err,
Value: value,
}
return &failure
}
// InnerResult is used by TestCase
type InnerResult struct {
Value string `xml:",cdata" json:"value" yaml:"value"`
}
//Logger is basically an interface for logrus.Entry
type Logger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Warningf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
}
type AssignStep struct {
Assignments map[string]Assignment `json:"vars" yaml:"vars" mapstructure:"vars"`
}
type Assignment struct {
From string `json:"from" yaml:"from"`
Regex string `json:"regex" yaml:"regex"`
}