forked from zoncoen/scenarigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenario.go
147 lines (135 loc) · 3.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
package scenarigo
import (
gocontext "context"
"fmt"
"path/filepath"
"time"
"github.com/zoncoen/scenarigo/context"
"github.com/zoncoen/scenarigo/errors"
"github.com/zoncoen/scenarigo/plugin"
"github.com/zoncoen/scenarigo/schema"
)
// RunScenario runs a test scenario s.
func RunScenario(ctx *context.Context, s *schema.Scenario) *context.Context {
ctx = ctx.WithScenarioFilepath(s.Filepath())
var setups setupFuncList
if s.Plugins != nil {
plugs := map[string]interface{}{}
for name, path := range s.Plugins {
path := path
if root := ctx.PluginDir(); root != "" {
path = filepath.Join(root, path)
}
p, err := plugin.Open(path)
if err != nil {
ctx.Reporter().Fatalf("failed to open plugin: %s", err)
}
plugs[name] = p
if setup := p.GetSetupEachScenario(); setup != nil {
setups = append(setups, setupFunc{
name: name,
f: setup,
})
}
}
ctx = ctx.WithPlugins(plugs)
}
if s.Vars != nil {
vars, err := ctx.ExecuteTemplate(s.Vars)
if err != nil {
ctx.Reporter().Fatalf("invalid vars: %s", err)
}
ctx = ctx.WithVars(vars)
}
ctx, teardown := setups.setup(ctx)
if ctx.Reporter().Failed() {
if teardown != nil {
teardown(ctx)
}
return ctx
}
scnCtx := ctx
var failed bool
for idx, step := range s.Steps {
step := step
ok := context.RunWithRetry(scnCtx, step.Title, func(ctx *context.Context) {
// following steps are skipped if the previous step failed
if failed {
ctx.Reporter().SkipNow()
}
if step.Timeout != nil && *step.Timeout > 0 {
reqCtx, cancel := gocontext.WithTimeout(ctx.RequestContext(), time.Duration(*step.Timeout))
defer cancel()
ctx = ctx.WithRequestContext(reqCtx)
}
ctx = runStepWithTimeout(ctx, s, step, idx)
// bind values to the scenario context for enable to access from following steps
if step.Bind.Vars != nil {
vars, err := ctx.ExecuteTemplate(step.Bind.Vars)
if err != nil {
ctx.Reporter().Fatal(
errors.WithNodeAndColored(
errors.WrapPath(
err,
fmt.Sprintf("steps[%d].bind.vars", idx),
"invalid bind",
),
ctx.Node(),
ctx.EnabledColor(),
),
)
}
scnCtx = scnCtx.WithVars(vars)
}
}, step.Retry)
if !failed {
failed = !ok
}
}
if teardown != nil {
teardown(scnCtx)
}
return scnCtx
}
func runStepWithTimeout(ctx *context.Context, scenario *schema.Scenario, step *schema.Step, idx int) *context.Context {
done := make(chan *context.Context)
go func() {
var finished bool
defer func() {
if !finished {
done <- ctx
}
}()
done <- runStep(ctx, scenario, step, idx)
finished = true
}()
select {
case ctx = <-done:
case <-ctx.RequestContext().Done():
ctx.Reporter().Error(
errors.WithNodeAndColored(
errors.ErrorPath(
fmt.Sprintf("steps[%d].timeout", idx),
"timeout exceeded",
),
ctx.Node(),
ctx.EnabledColor(),
),
)
// wait for the result context for a little
limit := 10 * time.Second
if step.PostTimeoutWaitingLimit != nil {
limit = time.Duration(*step.PostTimeoutWaitingLimit)
}
select {
case ctx = <-done:
case <-time.After(limit):
go func() { <-done }()
ctx.Reporter().Fatalf("step hasn't finished in %s despite the context canceled", limit)
}
}
if ctx.Reporter().Failed() {
ctx.Reporter().FailNow()
}
return ctx
}