-
Notifications
You must be signed in to change notification settings - Fork 3
/
regression.go
618 lines (513 loc) · 13.9 KB
/
regression.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package regression
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/blockchyp/blockchyp-go/v2"
"log"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
)
// TestRunner runs the regression test suite.
type TestRunner struct {
CLI string
Acquirer bool
CloudRelay bool
TestCase string
Terminal string
log *log.Logger
}
// NewTestRunner creates a new RegressionTestRunner by parsing
// command line flags.
func NewTestRunner() *TestRunner {
var app TestRunner
app.log = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
flag.StringVar(&app.CLI, "cli", "go run ./cmd/blockchyp", "CLI executable")
flag.BoolVar(&app.Acquirer, "acquirer", false, "execute in acquirer-only mode")
flag.BoolVar(&app.CloudRelay, "cloud-relay", false, "execute in cloud-relay-only mode")
flag.StringVar(&app.TestCase, "run", "", "regex pattern matching the test cases to run")
flag.StringVar(&app.Terminal, "terminal", "Test Terminal", "name of the terminal to test against")
flag.Parse()
return &app
}
// Run runs the test cases.
func (app *TestRunner) Run() error {
var tests testCases
tests = append(tests, asyncTests...)
tests = append(tests, batchTests...)
tests = append(tests, cashDiscountTests...)
tests = append(tests, chargeTests...)
tests = append(tests, customerTests...)
tests = append(tests, duplicateTests...)
tests = append(tests, ebtTests...)
tests = append(tests, failureModeTests...)
tests = append(tests, giftCardTests...)
tests = append(tests, interactionTests...)
tests = append(tests, preauthTests...)
tests = append(tests, refundTests...)
tests = append(tests, reversalTests...)
tests = append(tests, terminalPingTests...)
tests = append(tests, terminalStatusTests...)
tests = append(tests, tipTests...)
tests = append(tests, tokenTests...)
tests = append(tests, voidTests...)
tests.sort()
os.MkdirAll("/tmp/blockchyp-regression-test", 0755)
var last testGroup
TEST:
for _, test := range tests {
if match, _ := regexp.MatchString(app.TestCase, test.name); !match {
continue
}
pauseForSetup := shouldPauseForSetup(test, last)
// Deep copy to re-generate args for every test run.
ops := copyOps(test.operations)
RETRY:
for {
test.operations = copyOps(ops)
if err := app.runTest(test, pauseForSetup); err != nil {
app.log.Printf("%sFAIL: %s%s: %+v", format(red), test.name, format(), err)
switch multiprompt([]string{"[R]entry", "(S)kip", "(A)bort"}, 0) {
case 0:
pauseForSetup = false
continue RETRY
case 1:
continue TEST
case 2:
return errors.New("test run failed")
}
}
break RETRY
}
last = test.group
}
return nil
}
func copyOps(v []operation) []operation {
ops := append([]operation{}, v...)
for i := range ops {
ops[i].args = append([]string{}, ops[i].args...)
}
return ops
}
func (app *TestRunner) runTest(test testCase, pauseForSetup bool) error {
if test.local && app.CloudRelay {
app.log.Printf("SKIP: %s", test.name)
return nil
}
if test.sim && app.Acquirer {
app.log.Printf("SKIP: %s", test.name)
return nil
}
app.log.Printf("%sRUN: %s%s", format(cyan), test.name, format())
delete(testCache, test.name)
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for i := range test.operations {
app.substituteConstants(&test, i)
if test.operations[i].background {
wg.Add(1)
go func(i int) {
app.runOperation(ctx, &test, i, false, false)
wg.Done()
}(i)
continue
}
if i > 0 {
// Always show prompts for steps after the first one.
pauseForSetup = true
}
if _, err := app.runOperation(ctx, &test, i, pauseForSetup, false); err != nil {
cancel()
return err
}
}
wg.Wait()
app.log.Printf("%sPASS: %s%s", format(green), test.name, format())
return nil
}
func (app *TestRunner) runOperation(ctx context.Context, test *testCase, i int, pauseForSetup, recurse bool) (interface{}, error) {
op := test.operations[i]
if op.timeout > 0 && !recurse {
var res interface{}
var err error
done := make(chan struct{})
go func() {
res, err = app.runOperation(ctx, test, i, pauseForSetup, true)
close(done)
}()
timer := time.NewTimer(op.timeout)
select {
case <-timer.C:
return nil, errors.New("timed out")
case <-done:
return res, err
}
}
if op.wait > 0 {
wait(op.wait)
}
if op.validation != nil {
if err := app.validate(*op.validation); err != nil {
return nil, err
}
}
if op.msg != "" {
app.setup(op.msg, pauseForSetup)
}
var result interface{}
if op.expect != nil {
result = reflect.New(reflect.TypeOf(op.expect)).Interface()
}
if len(op.args) > 0 {
app.exec(ctx, test.name, op.args, result)
}
if op.fn != nil {
if err := op.fn(); err != nil {
return nil, err
}
}
if result != nil {
if err := cmp(op.expect, result); err != nil {
return nil, err
}
if txID := reflect.ValueOf(result).Elem().FieldByName("TransactionID"); txID.IsValid() {
test.operations[i].txID = txID.String()
}
if batchID := reflect.ValueOf(result).Elem().FieldByName("BatchID"); batchID.IsValid() {
test.operations[i].batchID = batchID.String()
}
if customer := reflect.ValueOf(result).Elem().FieldByName("Customer"); customer.IsValid() && !customer.IsZero() {
if customer.Kind() == reflect.Ptr {
customer = customer.Elem()
}
if id := customer.FieldByName("ID"); id.IsValid() {
test.operations[i].customerID = id.String()
}
}
if token := reflect.ValueOf(result).Elem().FieldByName("Token"); token.IsValid() {
test.operations[i].token = token.String()
}
}
return result, nil
}
// testCase contains a single set of transactions and assertions.
type testCase struct {
name string
group testGroup
local bool
sim bool
operations []operation
}
type testCases []testCase
func (t testCases) sort() {
sort.SliceStable(t, func(i, j int) bool {
return t[i].group < t[j].group
})
}
// operation contains a single test operation and its assertions.
type operation struct {
// timeout is a timeout for the entire operation.
timeout time.Duration
// wait is a delay before performing the operation.
wait time.Duration
// args are passed to the CLI.
args []string
// fn is an arbitrary function to call.
fn func() error
// returned by responses and used in subsequent tests.
txID string
batchID string
customerID string
token string
// expect is compared to the response from the CLI.
expect interface{}
// validation prompts the user for manual assertions.
validation *validation
// msg prints a msg for the user and pauses.
msg string
// background runs the operation without blocking.
background bool
}
func (o *operation) setTXID(s string) {
o.setArg("tx", s)
}
func (o *operation) setTXRef(s string) {
o.setArg("txRef", s)
}
func (o *operation) setBatchID(s string) {
o.setArg("batchId", s)
}
func (o *operation) setArg(arg, value string) {
if arg[0] != '-' {
arg = "-" + arg
}
for i := range o.args {
if o.args[i] == arg || strings.HasPrefix(o.args[i], arg+"=") {
o.args[i] = arg + "=" + value
}
}
}
func cmp(expect, result interface{}) error {
resultVal := reflect.ValueOf(result)
if resultVal.Kind() == reflect.Ptr {
resultVal = resultVal.Elem()
}
// The expected value needs to be a pointer so we can swap in the
// calculated amounts.
expectVal := reflect.ValueOf(expect)
if expectVal.Kind() == reflect.Ptr {
expectVal = expectVal.Elem()
} else {
expectVal = reflect.New(expectVal.Type()).Elem()
expectVal.Set(reflect.ValueOf(expect))
}
for i := 0; i < expectVal.NumField(); i++ {
if !expectVal.Field(i).CanInterface() {
continue
}
switch expectVal.Field(i).Kind() {
case reflect.Bool:
// Check booleans even if they are falsey
if !reflect.DeepEqual(expectVal.Field(i).Interface(), resultVal.Field(i).Interface()) {
return fmt.Errorf("%s should be %+v", expectVal.Type().Field(i).Name, expectVal.Field(i).Interface())
}
case reflect.String:
if expectVal.Field(i).IsZero() {
continue
}
s := expectVal.Field(i).Interface().(string)
if s == notEmpty {
if resultVal.Field(i).IsZero() {
return fmt.Errorf("%s should not be empty", expectVal.Type().Field(i).Name)
}
continue
}
case reflect.Struct:
if err := cmp(expectVal.Field(i).Interface(), resultVal.Field(i).Interface()); err != nil {
return err
}
continue
case reflect.Ptr:
if expectVal.Field(i).IsNil() {
continue
}
if expectVal.Field(i).Elem().Kind() == reflect.Struct {
if err := cmp(expectVal.Field(i).Interface(), resultVal.Field(i).Interface()); err != nil {
return err
}
continue
}
case reflect.Slice:
if expectVal.Field(i).Len() == 0 {
continue
}
resultLn := resultVal.Field(i).Len()
expectLn := expectVal.Field(i).Len()
if resultLn != expectLn {
return fmt.Errorf("%s: expected %d element, got %d", expectVal.Type().Field(i).Name, expectLn, resultLn)
}
switch expectVal.Field(i).Kind() {
case reflect.Struct:
for j := 0; j < resultLn; j++ {
expectValN := expectVal.Field(i).Index(j)
resultvalN := resultVal.Field(i).Index(j)
if err := cmp(expectValN, resultvalN); err != nil {
return err
}
}
continue
}
default:
// For all other types, only check them if they are set
if expectVal.Field(i).IsZero() {
continue
}
if !reflect.DeepEqual(expectVal.Field(i).Interface(), resultVal.Field(i).Interface()) {
return fmt.Errorf("%s should be %+v", expectVal.Type().Field(i).Name, expectVal.Field(i).Interface())
}
}
}
return nil
}
func (app *TestRunner) exec(ctx context.Context, name string, args []string, v interface{}) {
app.printCmd(app.CLI, args)
bin := strings.Split(app.CLI, " ")
bin = append(bin, args...)
cmd := exec.Command(bin[0], bin[1:]...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Start()
done := make(chan struct{})
go func() {
cmd.Wait()
close(done)
}()
select {
case <-ctx.Done():
return
case <-done:
}
if s := stdout.String(); s != "" {
app.log.Print(s)
}
if s := stderr.String(); s != "" {
app.log.Print(s)
}
if v == nil {
return
}
if err := json.Unmarshal(stdout.Bytes(), v); err != nil {
app.log.Fatalf("Failed to unmarshal: %+v; Raw output: %s", err, string(stdout.Bytes()))
}
}
func (app *TestRunner) printCmd(bin string, args []string) {
cpy := make([]string, len(args))
copy(cpy, args)
for i := range cpy {
if strings.Contains(cpy[i], " ") {
cpy[i] = "'" + cpy[i] + "'"
}
}
app.log.Printf("+ %s %s", app.CLI, strings.Join(cpy, " "))
}
func (app *TestRunner) substituteConstants(test *testCase, i int) {
for j := range test.operations[i].args {
app.substituteConstant(reflect.ValueOf(&test.operations[i].args[j]).Elem(), i, test)
}
fv := reflect.ValueOf(test.operations[i].expect)
if !fv.IsValid() || fv.IsZero() {
return
}
ptr := reflect.New(fv.Type())
v := reflect.ValueOf(ptr.Interface()).Elem()
v.Set(fv)
app.substituteInExpect(v, test, i)
test.operations[i].expect = ptr.Elem().Interface()
}
func (app *TestRunner) substituteInExpect(v reflect.Value, test *testCase, i int) {
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
for j := 0; j < v.NumField(); j++ {
if !v.Field(j).CanInterface() {
continue
}
app.substituteInExpect(v.Field(j), test, i)
}
case reflect.Slice:
for j := 0; j < v.Len(); j++ {
app.substituteInExpect(v.Index(j), test, i)
}
case reflect.String:
app.substituteConstant(v, i, test)
}
}
func (app *TestRunner) substituteConstant(s reflect.Value, i int, test *testCase) {
var newVal string
v, ok := s.Interface().(string)
if !ok {
return
}
switch {
case v == terminalName:
newVal = app.Terminal
case v == previousAmount:
if i > 0 {
newVal = argFrom("-amount", test.operations[i-1].args)
}
case v == newTxRef:
newVal = randomStr()
case strings.HasPrefix(v, txRefPrefix):
op := opN(v, i, test.operations)
newVal = argFrom("-txRef", op.args)
case strings.HasPrefix(v, txIDPrefix):
op := opN(v, i, test.operations)
newVal = op.txID
case strings.HasPrefix(v, batchIDPrefix):
op := opN(v, i, test.operations)
newVal = op.batchID
case strings.HasPrefix(v, tokenPrefix):
op := opN(v, i, test.operations)
newVal = op.token
case strings.HasPrefix(v, customerIDPrefix):
op := opN(v, i, test.operations)
newVal = op.customerID
case strings.HasPrefix(v, replacementPrefix):
newVal = getAmount(test.name, v)
}
if newVal != "" {
s.SetString(newVal)
}
}
func opN(arg string, i int, ops []operation) operation {
idx := strings.Index(arg, "[")
s := arg[idx+1 : len(arg)-1]
n, _ := strconv.Atoi(s)
if n >= 0 {
return ops[n]
}
return ops[i+n]
}
func argFrom(arg string, args []string) string {
for i, s := range args {
if s == arg {
if i < len(args)-1 {
return args[i+1]
}
}
if strings.HasPrefix(s, arg+"=") {
return strings.TrimPrefix(s, arg+"=")
}
}
return ""
}
func (app *TestRunner) isCloudRelay() bool {
// Make sure the cache has actually been generated first.
app.exec(context.Background(), "", []string{"-type", "ping", "-terminal", app.Terminal, "-test"}, nil)
path := filepath.Join(os.TempDir(), ".blockchyp_routes")
f, err := os.Open(path)
if err != nil {
app.log.Fatal(err)
}
defer f.Close()
var cache blockchyp.RouteCache
if err := json.NewDecoder(f).Decode(&cache); err != nil {
app.log.Fatal(err)
}
for k, v := range cache.Routes {
if strings.HasSuffix(k, app.Terminal) && v.Route.CloudRelayEnabled {
return true
}
}
return false
}
func shouldPauseForSetup(test testCase, last testGroup) bool {
// For the following test categories:
// testGroupNonInteractive, testGroupNoCVM, testGroupSignature
// Skip showing setup instructions for tests after the first in a series.
//
// For all other test groups:
// Show setup steps for every test.
switch test.group {
case testGroupNonInteractive, testGroupNoCVM, testGroupSignature:
default:
return true
}
return last == 0 || test.group != last
}