forked from zoncoen/scenarigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step.go
174 lines (160 loc) · 4.22 KB
/
step.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
package scenarigo
import (
gocontext "context"
"fmt"
"path/filepath"
"time"
"github.com/lestrrat-go/backoff"
"github.com/zoncoen/scenarigo/assert"
"github.com/zoncoen/scenarigo/context"
"github.com/zoncoen/scenarigo/errors"
"github.com/zoncoen/scenarigo/plugin"
"github.com/zoncoen/scenarigo/reporter"
"github.com/zoncoen/scenarigo/schema"
)
func runStep(ctx *context.Context, scenario *schema.Scenario, s *schema.Step, stepIdx int) *context.Context {
if s.Vars != nil {
vars, err := ctx.ExecuteTemplate(s.Vars)
if err != nil {
ctx.Reporter().Fatal(
errors.WithNodeAndColored(
errors.WrapPath(
err,
fmt.Sprintf("steps[%d].vars", stepIdx),
"invalid vars",
),
ctx.Node(),
ctx.EnabledColor(),
),
)
}
ctx = ctx.WithVars(vars)
}
if s.Include != "" {
baseDir := filepath.Dir(scenario.Filepath())
include := filepath.Join(baseDir, s.Include)
scenarios, err := schema.LoadScenarios(include)
if err != nil {
ctx.Reporter().Fatalf(`failed to include "%s" as step: %s`, s.Include, err)
}
if len(scenarios) != 1 {
ctx.Reporter().Fatalf(`failed to include "%s" as step: must be a scenario`, s.Include)
}
testName, err := filepath.Rel(baseDir, include)
if err != nil {
ctx.Reporter().Fatalf(`failed to include "%s" as step: %s`, s.Include, err)
}
currentNode := ctx.Node()
ctx.Reporter().Run(testName, func(rptr reporter.Reporter) {
ctx = RunScenario(ctx.WithReporter(rptr).WithNode(scenarios[0].Node), scenarios[0])
})
if ctx.Reporter().Failed() {
ctx.Reporter().FailNow()
}
// back node to current node
ctx = ctx.WithNode(currentNode)
return ctx
}
if s.Ref != nil {
x, err := ctx.ExecuteTemplate(s.Ref)
if err != nil {
ctx.Reporter().Fatal(
errors.WithNodeAndColored(
errors.WrapPathf(
err,
fmt.Sprintf("steps[%d].ref", stepIdx),
`failed to reference "%s" as step`, s.Ref,
),
ctx.Node(),
ctx.EnabledColor(),
),
)
}
stp, ok := x.(plugin.Step)
if !ok {
ctx.Reporter().Fatal(
errors.WithNodeAndColored(
errors.ErrorPathf(
fmt.Sprintf("steps[%d].ref", stepIdx),
`failed to reference "%s" as step: not implement plugin.Step interface`, s.Ref,
),
ctx.Node(),
ctx.EnabledColor(),
),
)
}
startTime := time.Now()
ctx = stp.Run(ctx, s)
ctx.Reporter().Logf("Run %s: elapsed time %f sec", s.Ref, time.Since(startTime).Seconds())
return ctx
}
return invokeAndAssert(ctx, s, stepIdx)
}
func invokeAndAssert(ctx *context.Context, s *schema.Step, stepIdx int) *context.Context {
policy, err := s.Retry.Build()
if err != nil {
ctx.Reporter().Fatal(fmt.Errorf("invalid retry policy: %w", err))
}
b, cancel := policy.Start(ctx.RequestContext())
defer cancel()
var i int
for backoff.Continue(b) {
ctx.Reporter().Logf("[%d] send request", i)
i++
newCtx, ok := attempt(ctx, s, stepIdx)
if !ok {
continue
}
return newCtx
}
ctx.Reporter().FailNow()
return ctx
}
func attempt(ctx *context.Context, s *schema.Step, stepIdx int) (*context.Context, bool) {
reqTime := time.Now()
if s.Timeout != nil && *s.Timeout > 0 {
reqCtx, cancel := gocontext.WithTimeout(ctx.RequestContext(), time.Duration(*s.Timeout))
defer cancel()
ctx = ctx.WithRequestContext(reqCtx)
}
newCtx, resp, err := s.Request.Invoke(ctx)
ctx.Reporter().Logf("elapsed time: %f sec", time.Since(reqTime).Seconds())
if err != nil {
ctx.Reporter().Log(
errors.WithNodeAndColored(
errors.WithPath(err, fmt.Sprintf("steps[%d].request", stepIdx)),
ctx.Node(),
ctx.EnabledColor(),
),
)
return nil, false
}
assertion, err := s.Expect.Build(newCtx)
if err != nil {
ctx.Reporter().Log(
errors.WithNodeAndColored(
errors.WithPath(err, fmt.Sprintf("steps[%d].expect", stepIdx)),
ctx.Node(),
ctx.EnabledColor(),
),
)
return nil, false
}
if err := assertion.Assert(resp); err != nil {
err = errors.WithNodeAndColored(
errors.WithPath(err, fmt.Sprintf("steps[%d].expect", stepIdx)),
ctx.Node(),
ctx.EnabledColor(),
)
var assertErr *assert.Error
if errors.As(err, &assertErr) {
for _, err := range assertErr.Errors {
ctx.Reporter().Log(err)
}
} else {
ctx.Reporter().Log(err)
}
return nil, false
}
return newCtx, true
}