forked from ledgerwatch/erigon
/
suite.go
429 lines (339 loc) · 9.38 KB
/
suite.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
package scenarios
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/nebojsa94/erigon/cmd/devnet/devnet"
"github.com/ledgerwatch/log/v3"
)
type SimulationContext struct {
suite *suite
}
type BeforeScenarioHook func(context.Context, *Scenario) (context.Context, error)
type AfterScenarioHook func(context.Context, *Scenario, error) (context.Context, error)
type BeforeStepHook func(context.Context, *Step) (context.Context, error)
type AfterStepHook func(context.Context, *Step, StepStatus, error) (context.Context, error)
var TimeNowFunc = func() time.Time {
return time.Now()
}
type suite struct {
stepRunners []*stepRunner
failed bool
randomize bool
strict bool
stopOnFailure bool
testingT *testing.T
defaultContext context.Context
// suite event handlers
beforeScenarioHandlers []BeforeScenarioHook
afterScenarioHandlers []AfterScenarioHook
beforeStepHandlers []BeforeStepHook
afterStepHandlers []AfterStepHook
}
func (s *suite) runSteps(ctx context.Context, scenario *Scenario, steps []*Step) (context.Context, []StepResult, error) {
var results = make([]StepResult, 0, len(steps))
var err error
logger := devnet.Logger(ctx)
for i, step := range steps {
isLast := i == len(steps)-1
isFirst := i == 0
var stepResult StepResult
ctx, stepResult = s.runStep(ctx, scenario, step, err, isFirst, isLast, logger)
switch {
case stepResult.Err == nil:
case errors.Is(stepResult.Err, ErrUndefined):
// do not overwrite failed error
if err == nil {
err = stepResult.Err
}
default:
err = stepResult.Err
logger.Error("Step failed with error", "scenario", scenario.Name, "step", step.Text, "err", err)
}
results = append(results, stepResult)
}
return ctx, results, err
}
func (s *suite) runStep(ctx context.Context, scenario *Scenario, step *Step, prevStepErr error, isFirst, isLast bool, logger log.Logger) (rctx context.Context, sr StepResult) {
var match *stepRunner
sr = StepResult{Status: Undefined}
rctx = ctx
// user multistep definitions may panic
defer func() {
if e := recover(); e != nil {
logger.Error("Step failed with panic", "scenario", scenario.Name, "step", step.Text, "err", e)
sr.Err = &traceError{
msg: fmt.Sprintf("%v", e),
stack: callStack(),
}
}
earlyReturn := prevStepErr != nil || sr.Err == ErrUndefined
// Run after step handlers.
rctx, sr.Err = s.runAfterStepHooks(ctx, step, sr.Status, sr.Err)
// Trigger after scenario on failing or last step to attach possible hook error to step.
if isLast || (sr.Status != Skipped && sr.Status != Undefined && sr.Err != nil) {
rctx, sr.Err = s.runAfterScenarioHooks(rctx, scenario, sr.Err)
}
if earlyReturn {
return
}
switch sr.Err {
case nil:
sr.Status = Passed
default:
sr.Status = Failed
}
}()
// run before scenario handlers
if isFirst {
ctx, sr.Err = s.runBeforeScenarioHooks(ctx, scenario)
}
// run before step handlers
ctx, sr.Err = s.runBeforeStepHooks(ctx, step, sr.Err)
if sr.Err != nil {
sr = NewStepResult(step.Id, step)
sr.Status = Failed
return ctx, sr
}
ctx, undef, match, err := s.maybeUndefined(ctx, step.Text, step.Args, logger)
if err != nil {
return ctx, sr
} else if len(undef) > 0 {
sr = NewStepResult(scenario.Id, step)
sr.Status = Undefined
sr.Err = ErrUndefined
logger.Error("Step failed undefined step", "scenario", scenario.Name, "step", step.Text)
return ctx, sr
}
if prevStepErr != nil {
sr = NewStepResult(scenario.Id, step)
sr.Status = Skipped
return ctx, sr
}
ctx, res := match.Run(ctx, step.Text, step.Args, logger)
ctx, sr.Returns, sr.Err = s.maybeSubSteps(ctx, res, logger)
return ctx, sr
}
func (s *suite) maybeUndefined(ctx context.Context, text string, args []interface{}, logger log.Logger) (context.Context, []string, *stepRunner, error) {
step := s.matchStep(text)
if nil == step {
return ctx, []string{text}, nil, nil
}
var undefined []string
if !step.Nested {
return ctx, undefined, step, nil
}
ctx, steps := step.Run(ctx, text, args, logger)
for _, next := range steps.([]Step) {
ctx, undef, _, err := s.maybeUndefined(ctx, next.Text, nil, logger)
if err != nil {
return ctx, undefined, nil, err
}
undefined = append(undefined, undef...)
}
return ctx, undefined, nil, nil
}
func (s *suite) matchStep(text string) *stepRunner {
var matches []*stepRunner
for _, r := range s.stepRunners {
for _, expr := range r.Exprs {
if m := expr.FindStringSubmatch(text); len(m) > 0 {
matches = append(matches, r)
}
}
}
if len(matches) == 1 {
return matches[0]
}
for _, r := range matches {
for _, expr := range r.Exprs {
if m := expr.FindStringSubmatch(text); m[0] == text {
return r
}
}
}
for _, r := range stepRunnerRegistry {
for _, expr := range r.Exprs {
if m := expr.FindStringSubmatch(text); len(m) > 0 {
matches = append(matches, r)
}
}
}
if len(matches) == 1 {
return matches[0]
}
for _, r := range matches {
for _, expr := range r.Exprs {
if m := expr.FindStringSubmatch(text); m[0] == text {
return r
}
}
}
return nil
}
func (s *suite) maybeSubSteps(ctx context.Context, result interface{}, logger log.Logger) (context.Context, []interface{}, error) {
if nil == result {
return ctx, nil, nil
}
var steps []Step
switch result := result.(type) {
case error:
return ctx, nil, result
case []Step:
steps = result
case []interface{}:
if len(result) == 0 {
return ctx, result, nil
}
if err, ok := result[len(result)-1].(error); ok {
return ctx, result[0 : len(result)-1], err
}
return ctx, result, nil
default:
return ctx, nil, fmt.Errorf("unexpected results type: %T - %+v", result, result)
}
var err error
for _, step := range steps {
if def := s.matchStep(step.Text); def == nil {
return ctx, nil, ErrUndefined
} else {
ctx, res := def.Run(ctx, step.Text, step.Args, logger)
if ctx, _, err = s.maybeSubSteps(ctx, res, logger); err != nil {
return ctx, nil, fmt.Errorf("%s: %+v", step.Text, err)
}
}
}
return ctx, nil, nil
}
func (s *suite) runScenario(scenario *Scenario) (sr *ScenarioResult, err error) {
ctx := s.defaultContext
if scenario.Context != nil {
ctx = JoinContexts(scenario.Context, ctx)
}
if ctx == nil {
ctx = context.Background()
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if len(scenario.Steps) == 0 {
return &ScenarioResult{ScenarioId: scenario.Id, StartedAt: TimeNowFunc()}, ErrUndefined
}
// Before scenario hooks are called in context of first evaluated step
// so that error from handler can be added to step.
sr = &ScenarioResult{ScenarioId: scenario.Id, StartedAt: TimeNowFunc()}
// scenario
if s.testingT != nil {
// Running scenario as a subtest.
s.testingT.Run(scenario.Name, func(t *testing.T) {
ctx, sr.StepResults, err = s.runSteps(ctx, scenario, scenario.Steps)
if s.shouldFail(err) {
t.Error(err)
}
})
} else {
ctx, sr.StepResults, err = s.runSteps(ctx, scenario, scenario.Steps)
}
// After scenario handlers are called in context of last evaluated step
// so that error from handler can be added to step.
return sr, err
}
func (s *suite) shouldFail(err error) bool {
if err == nil {
return false
}
if errors.Is(err, ErrUndefined) {
return s.strict
}
return true
}
func (s *suite) runBeforeStepHooks(ctx context.Context, step *Step, err error) (context.Context, error) {
hooksFailed := false
for _, f := range s.beforeStepHandlers {
hctx, herr := f(ctx, step)
if herr != nil {
hooksFailed = true
if err == nil {
err = herr
} else {
err = fmt.Errorf("%v, %w", herr, err)
}
}
if hctx != nil {
ctx = hctx
}
}
if hooksFailed {
err = fmt.Errorf("before step hook failed: %w", err)
}
return ctx, err
}
func (s *suite) runAfterStepHooks(ctx context.Context, step *Step, status StepStatus, err error) (context.Context, error) {
for _, f := range s.afterStepHandlers {
hctx, herr := f(ctx, step, status, err)
// Adding hook error to resulting error without breaking hooks loop.
if herr != nil {
if err == nil {
err = herr
} else {
err = fmt.Errorf("%v, %w", herr, err)
}
}
if hctx != nil {
ctx = hctx
}
}
return ctx, err
}
func (s *suite) runBeforeScenarioHooks(ctx context.Context, scenario *Scenario) (context.Context, error) {
var err error
// run before scenario handlers
for _, f := range s.beforeScenarioHandlers {
hctx, herr := f(ctx, scenario)
if herr != nil {
if err == nil {
err = herr
} else {
err = fmt.Errorf("%v, %w", herr, err)
}
}
if hctx != nil {
ctx = hctx
}
}
if err != nil {
err = fmt.Errorf("before scenario hook failed: %w", err)
}
return ctx, err
}
func (s *suite) runAfterScenarioHooks(ctx context.Context, scenario *Scenario, lastStepErr error) (context.Context, error) {
err := lastStepErr
hooksFailed := false
isStepErr := true
// run after scenario handlers
for _, f := range s.afterScenarioHandlers {
hctx, herr := f(ctx, scenario, err)
// Adding hook error to resulting error without breaking hooks loop.
if herr != nil {
hooksFailed = true
if err == nil {
isStepErr = false
err = herr
} else {
if isStepErr {
err = fmt.Errorf("step error: %w", err)
isStepErr = false
}
err = fmt.Errorf("%v, %w", herr, err)
}
}
if hctx != nil {
ctx = hctx
}
}
if hooksFailed {
err = fmt.Errorf("after scenario hook failed: %w", err)
}
return ctx, err
}