-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-case.go
467 lines (397 loc) · 12.5 KB
/
test-case.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package arp
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/gorilla/websocket"
)
const (
// Test Config keys
CFG_SKIP = "skip"
CFG_TAGS = "tags"
CFG_RESPONSE_CODE = "code"
CFG_RESPONSE_TYPE_BIN = "binary"
CFG_RESPONSE_TYPE_JSON = "json"
CFG_RESPONSE_TYPE_HTML = "html"
// Mime types
MIME_JSON = "application/json"
MIME_TEXT = "text/plain"
//Headers
HEADER_CONTENT_TYPE = "Content-Type"
// MISC
RESPONSE_PATH_FMT = "binary-response-*"
//DataStore Vars
DS_WS_CLIENT = "ws"
)
type TestCaseRpcCfg struct {
Protocol string `yaml:"protocol"`
Address string `yaml:"address"`
Procedure string `yaml:"procedure"`
}
type TestCaseResponseCfg struct {
// status code could end up being either a number or an object defining a validation definition
StatusCode interface{} `yaml:"code"`
Type string `yaml:"type"`
FilePath string `yaml:"filePath"`
Payload map[interface{}]interface{} `yaml:"payload"`
Headers map[interface{}]interface{} `yaml:"headers"`
}
type TestCaseCfg struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
ExitOnRun bool `yaml:"exit"`
Skip bool `yaml:"skip"`
Input map[interface{}]interface{} `yaml:"input"`
FormInput bool `yaml:"formInput"`
Tags []string `yaml:"tags"`
Headers map[interface{}]interface{} `yaml:"headers"`
Route string `yaml:"route"`
Method string `yaml:"method"`
RPC TestCaseRpcCfg `yaml:"rpc"`
Websocket bool `yaml:"websocket"`
Response TestCaseResponseCfg `yaml:"response"`
}
type TestCase struct {
Config TestCaseCfg
IsRPC bool
ResponseHeaderMatcher ResponseMatcher
StatusCodeMatcher ResponseMatcher
ResponseMatcher ResponseMatcher
GlobalDataStore *DataStore
Tags map[string]bool
}
type TestResult struct {
TestCase TestCase
Fields []*FieldMatcherResult
Passed bool
Response map[string]interface{}
RawResponse interface{}
ResponseHeaders map[string]interface{}
RequestHeaders http.Header
ResolvedRoute string
StatusCode int
StartTime time.Time
EndTime time.Time
}
type InputReader struct {
FormWriter *multipart.Writer
BodyReader io.Reader
ErrorChan chan error
}
// tag string can contain 1 or more tags separated by ",". This syntax will OR the tags.
func (t *TestCase) HasTag(tagList string) bool {
hasTag := false
tags := strings.Split(tagList, ",")
for _, tt := range tags {
tagStr := tt
negated := false
if strings.HasPrefix(tt, "!") {
negated = true
tagStr = strings.TrimPrefix(tagStr, "!")
}
_, hasTag = t.Tags[tagStr]
if !negated && hasTag {
return true
} else if negated && !hasTag {
return true
}
}
return false
}
func (t *TestCase) LoadConfig(test *TestCaseCfg) error {
t.ResponseMatcher = NewResponseMatcher(t.GlobalDataStore)
t.ResponseHeaderMatcher = NewResponseMatcher(t.GlobalDataStore)
t.StatusCodeMatcher = NewResponseMatcher(t.GlobalDataStore)
t.Config = *test
switch t.Config.Response.Type {
case CFG_RESPONSE_TYPE_JSON, CFG_RESPONSE_TYPE_BIN, CFG_RESPONSE_TYPE_HTML:
case "":
t.Config.Response.Type = CFG_RESPONSE_TYPE_JSON
default:
return fmt.Errorf("Invalid 'response.type' specified for %v: %v", t.Config.Name, t.Config.Response.Type)
}
if t.Config.RPC.Address != "" && t.Config.RPC.Procedure != "" && t.Config.RPC.Protocol != "" {
t.IsRPC = true
t.Config.Method = "RPC"
t.Config.Route = fmt.Sprintf("%v://%v#%v", t.Config.RPC.Protocol, t.Config.RPC.Address, t.Config.RPC.Procedure)
}
if t.Config.Websocket {
t.Config.Method = "WS"
}
if t.Config.Method == "" || t.Config.Response.Type == CFG_RESPONSE_TYPE_HTML {
t.Config.Method = "GET"
}
// generate a mapping for tags to improve look up times
t.Tags = make(map[string]bool)
for _, tag := range t.Config.Tags {
t.Tags[tag] = true
}
// Start loading our matchers
sc := t.Config.Response.StatusCode
if sc != nil {
keyPath := FieldMatcherPath{
Keys: []FieldMatcherKey{{Name: CFG_RESPONSE_CODE, RealKey: JsonKey{Name: CFG_RESPONSE_CODE}}},
}
if statusMatcher, mOk := sc.(map[interface{}]interface{}); mOk {
if err := t.StatusCodeMatcher.loadField(sc, statusMatcher, keyPath); err != nil {
return err
}
} else {
if err := t.StatusCodeMatcher.loadSimplifiedField(sc, sc, keyPath); err != nil {
return err
}
}
}
payload := t.Config.Response.Payload
if payload != nil {
if err := t.ResponseMatcher.loadObjectFields(payload, payload, FieldMatcherPath{}); err != nil {
return err
}
}
respHeaders := t.Config.Response.Headers
if respHeaders != nil {
if err := t.ResponseHeaderMatcher.
loadObjectFields(respHeaders, respHeaders, FieldMatcherPath{}); err != nil {
return err
}
}
return nil
}
func (t *TestCase) GetTestRoute() (string, error) {
resolvedRoute, err := t.GlobalDataStore.ExpandVariable(t.Config.Route)
if err != nil {
return "", err
}
return varToString(resolvedRoute, t.Config.Route), nil
}
func (t *TestCase) GetTestRpcAddr() (string, error) {
resolvedAddr, err := t.GlobalDataStore.ExpandVariable(t.Config.RPC.Address)
if err != nil {
return "", err
}
return varToString(resolvedAddr, t.Config.RPC.Address), nil
}
// Returns a new input object with all included variables resolved
func (t *TestCase) GetResolvedTestInput() (interface{}, error) {
node, err := t.GlobalDataStore.RecursiveResolveVariables(t.Config.Input)
if err != nil {
return nil, err
}
node, err = RecursiveExecuteCommand(node)
if err != nil {
return nil, err
}
return node, err
}
func (t *TestCase) GetTestHeaders(inputReader *InputReader) (map[interface{}]interface{}, error) {
node, err := t.GlobalDataStore.RecursiveResolveVariables(t.Config.Headers)
if err != nil {
return nil, err
}
node, err = RecursiveExecuteCommand(node)
if err != nil {
return nil, err
}
headersMap, ok := node.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("failed to load headers for test - expected an object")
}
if inputReader != nil && t.Config.FormInput {
headersMap[HEADER_CONTENT_TYPE] = inputReader.FormWriter.FormDataContentType()
}
return headersMap, nil
}
func (t *TestCase) StepExecWebsocket(step int, result *TestResult) (passed bool, remaining int, err error) {
defer func() { result.EndTime = time.Now().UTC() }()
input, err := t.GetResolvedTestInput()
if err != nil {
return false, 0, fmt.Errorf("failed to get test input: %v", err)
}
if remaining, err = executeWebSocket(t, result, input, step); err != nil {
return false, remaining, err
}
result.Passed, result.Fields, err = t.ResponseMatcher.Match(result.Response)
return
}
func (t *TestCase) GetStubbedFailResult(errorMsg string) *TestResult {
return &TestResult{
TestCase: *t,
StartTime: time.Now().UTC(),
EndTime: time.Now().UTC(),
Fields: []*FieldMatcherResult{
{
Error: errorMsg,
ObjectKeyPath: "test.Error",
Status: false,
ShowExtendedMsg: true,
},
},
Passed: false,
}
}
func (t *TestCase) Execute(testTags []string) (passed bool, result *TestResult, err error) {
respParser, respValidator := LoadExtensions(nil)
result = &TestResult{
TestCase: *t,
StartTime: time.Now().UTC(),
}
defer func() { result.EndTime = time.Now().UTC() }()
if t.Config.Skip {
result.Fields = []*FieldMatcherResult{
{
Error: "Skipping test as configured",
ObjectKeyPath: fmt.Sprintf("test.%v", CFG_SKIP),
Status: true,
},
}
result.Passed = true
return true, result, nil
}
if t.SkipTestOnTags(testTags) {
result.Fields = []*FieldMatcherResult{
{
Error: fmt.Sprintf("Skipping test - no tags matching the combination of: %v", testTags),
ObjectKeyPath: fmt.Sprintf("test.%v", CFG_TAGS),
Status: true,
},
}
result.Passed = true
return true, result, nil
}
input, err := t.GetResolvedTestInput()
if err != nil {
return false, result, fmt.Errorf("failed to get test input: %v", err)
}
if t.Config.Websocket {
if _, err := executeWebSocket(t, result, input, -1); err != nil {
return false, result, err
}
} else if !t.IsRPC {
if err := executeRest(t, result, respParser, input); err != nil {
return false, result, err
}
} else {
if err := executeRPC(t, result, input); err != nil {
return false, result, err
}
}
result.Passed, result.Fields, err = respValidator.Handle(t, result)
return result.Passed, result, err
}
func (t *TestCase) CloseWebsocket() {
if wsc, ok := t.GlobalDataStore.Store[DS_WS_CLIENT]; ok {
c := wsc.(*websocket.Conn)
c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
c.Close()
delete(t.GlobalDataStore.Store, DS_WS_CLIENT)
}
}
func (t *TestCase) GetWebsocketClient() (*websocket.Conn, string, error) {
route, err := t.GetTestRoute()
if err != nil {
return nil, "", fmt.Errorf("failed to determine test route: %v", err)
}
// Get the client. If a client was already initialized and connected in this test suite, then re-use that one
// so that the test suite can preserve its session across multiple test cases. Maybe in the future (if there's demand)
// it a new flag can be added to the test case as to whether or not the connection should be closed forcing the next
// test to create a new connection.
// Otherwise, if no client exists already, we'll create a new one and connect it.
var client *websocket.Conn
if prevClient, ok := t.GlobalDataStore.Store[DS_WS_CLIENT]; !ok {
inputHeaders := http.Header{}
headers, err := t.GetTestHeaders(nil)
if err != nil {
return nil, route, fmt.Errorf("failed to resolve test headers parameter: %v", err)
}
for k := range headers {
key := fmt.Sprintf("%v", k)
val := headers[k].(string)
inputHeaders.Set(key, val)
}
client, _, err = websocket.DefaultDialer.Dial(route, inputHeaders)
if err != nil {
return nil, route, fmt.Errorf("failed to start websocket client: %v", err)
}
t.GlobalDataStore.Put(DS_WS_CLIENT, client)
} else {
client = prevClient.(*websocket.Conn)
}
return client, route, nil
}
func (t *TestCase) GetWebsocketInput(input interface{}) (*WSInput, error) {
jNode := YamlToJson(input)
b, err := json.Marshal(&jNode)
if err != nil {
return nil, err
}
var inputs WSInput
json.Unmarshal(b, &inputs)
return &inputs, nil
}
func (t *TestCase) GetRestInput(input interface{}) (*InputReader, error) {
// if we aren't passing in form input, just provide the input object as JSON
if !t.Config.FormInput || t.Config.Websocket {
jsonNode := YamlToJson(input)
b, err := json.Marshal(jsonNode)
if err != nil {
return nil, err
}
return &InputReader{BodyReader: bytes.NewReader(b)}, nil
}
// Otherwise, take the fields from the input objet and write them as form fields.
// Files can be identified as arrays of strings to allow for multi-file uploading
mappedNode, mOk := input.(map[interface{}]interface{})
if !mOk {
return nil, fmt.Errorf("failed to read test input - expected test input to be an object")
}
// setup our pipe so we can stream our input files from disk rather than loading to memory
outputReader, outputWriter, _ := os.Pipe()
inputReader := &InputReader{
BodyReader: outputReader,
FormWriter: multipart.NewWriter(outputWriter),
ErrorChan: make(chan error),
}
// Start our form provider to pipe in form data as it is read
go func() {
for k := range mappedNode {
key := fmt.Sprintf("%v", k)
switch v := mappedNode[k].(type) {
default:
inputReader.FormWriter.WriteField(key, fmt.Sprintf("%v", v))
case []interface{}:
for _, f := range v {
path := f.(string)
input, err := os.Open(path)
if err != nil {
inputReader.ErrorChan <- fmt.Errorf("failed to open file for form input: %v: %v", f, err)
}
w, err := inputReader.FormWriter.CreateFormFile(key, filepath.Base(path))
if err != nil {
inputReader.ErrorChan <- fmt.Errorf("failed reading file for form input: %v: %v", f, err)
}
io.Copy(w, input)
input.Close()
}
}
}
outputWriter.Close()
inputReader.FormWriter.Close()
inputReader.ErrorChan <- nil
}()
return inputReader, nil
}
func (t *TestCase) SkipTestOnTags(testTags []string) bool {
for _, inTag := range testTags {
if !t.HasTag(inTag) {
return true
}
}
return false
}