/
lib.go
217 lines (189 loc) · 5.06 KB
/
lib.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
package venom
import (
"testing"
"encoding/json"
"github.com/ovh/venom"
"github.com/ovh/venom/context/default"
"github.com/ovh/venom/context/webctx"
"github.com/ovh/venom/executors/dbfixtures"
"github.com/ovh/venom/executors/exec"
"github.com/ovh/venom/executors/http"
"github.com/ovh/venom/executors/imap"
"github.com/ovh/venom/executors/readfile"
"github.com/ovh/venom/executors/smtp"
"github.com/ovh/venom/executors/ssh"
"github.com/ovh/venom/executors/web"
)
func init() {
venom.RegisterExecutor(exec.Name, exec.New())
venom.RegisterExecutor(http.Name, http.New())
venom.RegisterExecutor(imap.Name, imap.New())
venom.RegisterExecutor(readfile.Name, readfile.New())
venom.RegisterExecutor(smtp.Name, smtp.New())
venom.RegisterExecutor(ssh.Name, ssh.New())
venom.RegisterExecutor(web.Name, web.New())
venom.RegisterExecutor(dbfixtures.Name, dbfixtures.New())
venom.RegisterTestCaseContext(defaultctx.Name, defaultctx.New())
venom.RegisterTestCaseContext(webctx.Name, webctx.New())
}
//P is a map of test parameters
type P map[string]interface{}
//V is a map of Variables
type V map[string]string
//R is a map of Results
type R map[string]interface{}
//T is a superset of testing.T
type T struct {
*testing.T
ts *venom.TestSuite
tc *venom.TestCase
Name string
}
//Logger is a superset of the testing Logger compliant with logrus Entry
type Logger struct{ t *testing.T }
// Debugf calls testing.T.Logf
func (l *Logger) Debugf(format string, args ...interface{}) { l.t.Logf("[DEBUG] "+format, args...) }
// Infof calls testing.T.Logf
func (l *Logger) Infof(format string, args ...interface{}) { l.t.Logf("[INFO] "+format, args...) }
// Printf calls testing.T.Logf
func (l *Logger) Printf(format string, args ...interface{}) { l.t.Logf(format, args...) }
// Warnf calls testing.T.Logf
func (l *Logger) Warnf(format string, args ...interface{}) { l.t.Logf("[WARN] "+format, args...) }
// Warningf calls testing.T.Logf
func (l *Logger) Warningf(format string, args ...interface{}) { l.t.Logf("[WARN] "+format, args...) }
// Errorf calls testing.T.Logf
func (l *Logger) Errorf(format string, args ...interface{}) { l.t.Logf("[ERROR] "+format, args...) }
// Fatalf calls testing.T.Logf
func (l *Logger) Fatalf(format string, args ...interface{}) { l.t.Logf("[FATAL] "+format, args...) }
// WithField calls testing.T.Logf
func (l *Logger) WithField(key string, value interface{}) venom.Logger {
return l
}
//TestCase instanciates a veom testcase
func TestCase(t *testing.T, name string, variables map[string]string) *T {
return &T{
t,
&venom.TestSuite{
Templater: &venom.Templater{Values: variables},
Name: name,
},
&venom.TestCase{
Name: name,
},
name,
}
}
//Do executes a veom test steps
func (t *T) Do(teststepParams P) R {
ts := t.ts
tc := t.tc
tcc, errContext := venom.ContextWrap(tc)
if errContext != nil {
t.Error(errContext)
return nil
}
if err := tcc.Init(); err != nil {
tc.Errors = append(tc.Errors, venom.Failure{Value: err.Error()})
t.Error(err)
return nil
}
defer tcc.Close()
step, erra := ts.Templater.ApplyOnStep(venom.TestStep(teststepParams))
if erra != nil {
t.Error(erra)
return nil
}
e, err := venom.WrapExecutor(step, tcc)
if err != nil {
t.Error(err)
return nil
}
res := venom.RunTestStep(tcc, e, ts, tc, step, ts.Templater, &Logger{t.T}, "low")
for _, f := range tc.Failures {
t.Errorf("\r Failure %s", f.Value)
}
for _, e := range tc.Errors {
t.Errorf("\r Error %s", e.Value)
}
return R(res)
}
//NewExec returns a venom.P properly initialized for exec Executor
func NewExec(script string) P {
return P{
"type": "exec",
"script": script,
}
}
var HTTP = struct {
Get func(url, path string) P
Post func(url, path string, body []byte) P
PostJSON func(url, path string, body interface{}) P
Put func(url, path string, body []byte) P
PutJSON func(url, path string, body interface{}) P
Delete func(url, path string) P
}{
Get: func(url, path string) P {
return P{
"type": "http",
"method": "GET",
"url": url,
"path": path,
}
},
Post: func(url, path string, body []byte) P {
return P{
"type": "http",
"method": "POST",
"url": url,
"path": path,
"body": string(body),
}
},
PostJSON: func(url, path string, body interface{}) P {
b, err := json.Marshal(body)
if err != nil {
panic(err)
}
return P{
"type": "http",
"method": "POST",
"url": url,
"path": path,
"body": string(b),
}
},
Put: func(url, path string, body []byte) P {
return P{
"type": "http",
"method": "PUT",
"url": url,
"path": path,
"body": string(body),
}
},
PutJSON: func(url, path string, body interface{}) P {
b, err := json.Marshal(body)
if err != nil {
panic(err)
}
return P{
"type": "http",
"method": "PUT",
"url": url,
"path": path,
"body": string(b),
}
},
Delete: func(url, path string) P {
return P{
"type": "http",
"method": "DELETE",
"url": url,
"path": path,
}
},
}
func (p P) WithHeaders(headers http.Headers) P {
p["headers"] = headers
return p
}