This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
scenario.go
396 lines (338 loc) · 9.31 KB
/
scenario.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/dchest/uniuri"
"github.com/gavv/httpexpect/v2"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"gopkg.in/yaml.v2"
)
//Asserts represents acceptance criteria for a test case
type Asserts struct {
Code int `yaml:"status_code"`
ValidateJSON string `yaml:"validate_json"`
Script string `yaml:"script"`
}
//RunHTTP represents configuration on how to run HTTP test
type RunHTTP struct {
Method string `yaml:"method"`
URL string `yaml:"url"`
Headers map[string]string `yaml:"headers"`
QueryParams map[string]string `yaml:"query_params"`
Files map[string]string `yaml:"files"`
Forms map[string]string `yaml:"forms"`
Payload string `yaml:"payload"`
ResponseOut string `yaml:"response_out"`
Asserts *Asserts `yaml:"asserts"`
}
//Run represent methods for testing
type Run struct {
HTTP RunHTTP `yaml:"http"`
}
//ReportPubsub represents configuration to report to pubsub
type ReportPubsub struct {
Scenario string `json:"scenario"`
Attributes map[string]string `json:"attributes"` // [status]=success|error
Status string `json:"status"` // success|error
Data string `json:"data"`
}
// Scenario represents a single scenario file to run.
type Scenario struct {
Maintainers []string `yaml:"maintainers"`
Tags map[string]string `yaml:"tags"`
Env map[string]string `yaml:"env"`
Prepare string `yaml:"prepare"`
Run []Run `yaml:"run"`
Check string `yaml:"check"`
me *Scenario
input *doScenarioInput
errs []error
}
func (s Scenario) getHead(file string) ([]byte, error) {
c := exec.Command("head", "-n", "1", file)
return c.CombinedOutput()
}
// RunScript runs file and returns the combined stdout+stderr result.
func (s *Scenario) RunScript(file string) ([]byte, error) {
l1, err := s.getHead(file)
if err != nil {
return nil, err
}
if !strings.HasPrefix(string(l1), "#!") {
return nil, fmt.Errorf("unsupported")
}
runner := strings.Split(string(l1), " ")[0] // don't support '/usr/bin/env xx'
runner = strings.Split(runner, "#!")[1]
runner = strings.Trim(filepath.Base(runner), "\n")
var c *exec.Cmd
switch {
case strings.Contains(runner, "python"):
c = exec.Command(runner, file)
default:
// Assume it's a shell interpreter.
c = exec.Command(runner, "-c", file)
}
c.Env = os.Environ()
if len(s.Env) > 0 {
for k, v := range s.Env {
add := fmt.Sprintf("%v=%v", k, v)
c.Env = append(c.Env, add)
}
}
return c.CombinedOutput()
}
// ParseValue tries to check if contents is in script form and if it is, writes it
// to disk as an executable, runs it and returns the resulting stream output.
// Otherwise, return the contents as is.
func (s *Scenario) ParseValue(contents string, file ...string) (string, error) {
if strings.HasPrefix(contents, "#!") {
f := fmt.Sprintf("oops_%v", uuid.NewV4())
f = filepath.Join(os.TempDir(), f)
if len(file) > 0 {
f = file[0]
}
_, err := s.WriteScript(f, contents)
if err != nil {
return contents, err
}
b, err := s.RunScript(f)
return string(b), err
}
return contents, nil
}
// Write writes b to file.
func (s *Scenario) Write(file string, b []byte) error {
return ioutil.WriteFile(file, b, 0644)
}
// WriteScript writes contents to file as an executable.
func (s *Scenario) WriteScript(file, contents string) (string, error) {
f, err := os.Create(file)
if err != nil {
return file, err
}
defer f.Close()
f.Chmod(os.ModePerm)
f.Write([]byte(contents))
err = f.Sync()
return file, err
}
//Logf interface for httpexpect.
func (s Scenario) Logf(fmt string, args ...interface{}) {
log.Printf(fmt, args...)
}
//Errorf returns formatted error message
func (s Scenario) Errorf(message string, args ...interface{}) {
m := fmt.Sprintf(message, args...)
s.me.errs = append(s.me.errs, fmt.Errorf(m))
log.Printf(message, args...)
}
type doScenarioInput struct {
app *appctx
ScenarioFiles []string
WorkDir string
ReportSlack string
ReportPubsub string
Verbose bool
}
func isAllowed(s *Scenario) bool {
if len(tags) == 0 {
return true
}
var matched int
for _, t := range tags {
tt := strings.Split(t, "=")
if len(tt) != 2 {
continue
}
for k, v := range s.Tags {
if k == tt[0] && v == tt[1] {
matched++
}
}
}
return matched == len(tags)
}
func doScenario(in *doScenarioInput) error {
for _, f := range in.ScenarioFiles {
yml, err := ioutil.ReadFile(f)
if err != nil {
continue
}
var s Scenario
err = yaml.Unmarshal(yml, &s)
if err != nil {
continue
}
if !isAllowed(&s) {
log.Printf("%v is not allowed by tags", f)
continue
}
s.me = &s // self-reference for our LoggerReporter functions
s.input = in // our copy
log.Printf("scenario: %v", f)
if s.Prepare != "" {
basef := filepath.Base(f)
fn := filepath.Join(os.TempDir(), fmt.Sprintf("%v_prepare", basef))
fn, _ = s.WriteScript(fn, s.Prepare)
b, err := s.RunScript(fn)
if err != nil {
s.errs = append(s.errs, errors.Wrapf(err,
"prepare:\n%v: %v", s.Prepare, string(b)))
} else {
if len(string(b)) > 0 {
log.Printf("prepare:\n%v", string(b))
}
}
}
for i, run := range s.Run {
basef := filepath.Base(f)
prefix := filepath.Join(os.TempDir(), fmt.Sprintf("%v_run%d", basef, i))
// Parse url.
fn := fmt.Sprintf("%v_url", prefix)
nv, err := s.ParseValue(run.HTTP.URL, fn)
if err != nil {
s.errs = append(s.errs, errors.Wrapf(err, "ParseValue[%v]: %v", i, run.HTTP.URL))
continue
}
u, err := url.Parse(nv)
if err != nil {
s.errs = append(s.errs, errors.Wrapf(err, "url.Parse[%v]", i))
continue
}
e := httpexpect.New(s, u.Scheme+"://"+u.Host)
req := e.Request(run.HTTP.Method, u.Path)
for k, v := range run.HTTP.Headers {
fn := fmt.Sprintf("%v_hdr.%v", prefix, k)
nv, err := s.ParseValue(v, fn)
if err != nil {
s.errs = append(s.errs, errors.Wrapf(err, "ParseValue[%v]: %v", i, v))
continue
}
req = req.WithHeader(k, nv)
log.Printf("[header] %v: %v", k, nv)
}
for k, v := range run.HTTP.QueryParams {
fn := fmt.Sprintf("%v_qparams.%v", prefix, k)
nv, _ := s.ParseValue(v, fn)
req = req.WithQuery(k, nv)
}
if len(run.HTTP.Files) > 0 {
req = req.WithMultipart()
}
for k, v := range run.HTTP.Files {
fn := fmt.Sprintf("%v_files.%v", prefix, k)
nv, _ := s.ParseValue(v, fn)
req = req.WithFile(k, nv)
}
for k, v := range run.HTTP.Forms {
fn := fmt.Sprintf("%v_forms.%v", prefix, k)
nv, _ := s.ParseValue(v, fn)
req = req.WithFormField(k, nv)
}
if run.HTTP.Payload != "" {
fn := fmt.Sprintf("%v_payload", prefix)
nv, _ := s.ParseValue(run.HTTP.Payload, fn)
req = req.WithBytes([]byte(nv))
}
resp := req.Expect()
if run.HTTP.ResponseOut != "" {
body := resp.Body().Raw()
s.Write(run.HTTP.ResponseOut, []byte(body))
log.Printf("[response] %v", body)
}
if run.HTTP.Asserts == nil {
continue
}
resp = resp.Status(run.HTTP.Asserts.Code)
if run.HTTP.Asserts.ValidateJSON != "" {
resp.JSON().Schema(run.HTTP.Asserts.ValidateJSON)
}
if run.HTTP.Asserts.Script != "" {
fn := fmt.Sprintf("%v_assertscript", prefix)
s.WriteScript(fn, run.HTTP.Asserts.Script)
b, err := s.RunScript(fn)
if err != nil {
s.errs = append(s.errs, errors.Wrapf(err,
"assert.script[%v]:\n%v: %v", i, run.HTTP.Asserts.Script, string(b)))
} else {
if len(string(b)) > 0 {
log.Printf("asserts.script[%v]:\n%v", i, string(b))
}
}
}
}
if s.Check != "" {
basef := filepath.Base(f)
fn := filepath.Join(os.TempDir(), fmt.Sprintf("%v_check", basef))
fn, _ = s.WriteScript(fn, s.Check)
b, err := s.RunScript(fn)
if err != nil {
s.errs = append(s.errs, errors.Wrapf(err,
"check:\n%v: %v", s.Check, string(b)))
} else {
if len(string(b)) > 0 {
log.Printf("check:\n%v", string(b))
}
}
}
if len(s.errs) > 0 {
log.Printf("errs: %v", s.errs)
}
if in.ReportSlack != "" {
if len(s.errs) > 0 {
// Send to slack, if any.
payload := SlackMessage{
Attachments: []SlackAttachment{
{
Color: "danger",
Title: fmt.Sprintf("%v - failure", filepath.Base(f)),
Text: fmt.Sprintf("Maintainers: %v\n%v", strings.Join(s.Maintainers, ", "), s.errs),
Footer: "oops",
Timestamp: time.Now().Unix(),
},
},
}
err = payload.Notify(in.ReportSlack)
if err != nil {
log.Printf("Notify (slack) failed: %v", err)
}
}
}
if in.ReportPubsub != "" && in.app != nil {
if in.app.rpub != nil {
status := "success"
var data string
if len(s.errs) > 0 {
status = "error"
data = fmt.Sprintf("%v", s.errs)
}
attr := make(map[string]string)
if snssqs != "" {
attr["snssqs"] = snssqs
}
if pubsub != "" {
attr["pubsub"] = pubsub
}
r := ReportPubsub{
Scenario: f,
Attributes: attr,
Status: status,
Data: data,
}
err := in.app.rpub.Publish(uniuri.NewLen(10), r)
if err != nil {
log.Printf("Publish failed: %v", err)
}
}
}
}
return nil
}