-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_test.go
More file actions
292 lines (262 loc) · 5.96 KB
/
Copy pathengine_test.go
File metadata and controls
292 lines (262 loc) · 5.96 KB
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
package engine
import (
"context"
"strings"
"sync"
"testing"
)
func TestNew_Defaults(t *testing.T) {
e := New()
defer e.Close()
if !e.parallel {
t.Error("default parallel should be true")
}
if e.maxConcurrent <= 0 {
t.Error("default maxConcurrent should be > 0")
}
}
func TestRun_NilInput(t *testing.T) {
e := New()
defer e.Close()
res, err := e.Run(context.Background(), nil)
if err == nil {
t.Error("nil input should error")
}
if res == nil {
t.Error("Results should always be non-nil")
}
}
func TestCheck_NilInput(t *testing.T) {
e := New()
defer e.Close()
if _, err := e.Check(context.Background(), nil); err == nil {
t.Error("nil input should error")
}
}
func TestRun_ParseError(t *testing.T) {
e := New()
defer e.Close()
res, err := e.Run(context.Background(), FromBytes([]byte("not valid yaml :{")))
if err == nil {
t.Error("parse error should propagate")
}
if res == nil {
t.Fatal("Results should be non-nil on error")
}
}
func TestRun_NoTests(t *testing.T) {
e := New()
defer e.Close()
yaml := `# Empty file with no tests`
res, err := e.Run(context.Background(), FromBytes([]byte(yaml)))
if err != nil {
t.Fatal(err)
}
if res.Total != 0 {
t.Errorf("empty input should yield 0 tests, got %d", res.Total)
}
}
func TestRun_NoPluginForProtocol(t *testing.T) {
e := New()
defer e.Close()
yaml := `tests:
- name: x
method: GET
target: http://example.com
expect:
status: 200
`
res, err := e.Run(context.Background(), FromBytes([]byte(yaml)))
if err != nil {
t.Fatal(err)
}
if res.Total != 1 {
t.Fatalf("got %d tests", res.Total)
}
if res.Errored != 1 {
t.Errorf("test should be errored (no plugin), got: %+v", res.Tests[0])
}
}
func TestCheck_ValidatesGraph(t *testing.T) {
e := New()
defer e.Close()
// Cyclic depends should produce a validation error.
yaml := `tests:
- name: a
alias: a
depends: [b]
method: GET
target: x://t
- name: b
alias: b
depends: [a]
method: GET
target: x://t
`
errs, err := e.Check(context.Background(), FromBytes([]byte(yaml)))
if err != nil {
t.Fatal(err)
}
if len(errs) == 0 {
t.Error("cycle should produce validation error")
}
if len(errs) > 0 && !strings.Contains(errs[0].Message, "cycle") {
t.Errorf("error message should mention cycle: %q", errs[0].Message)
}
}
func TestCheck_Valid(t *testing.T) {
e := New()
defer e.Close()
yaml := `tests:
- "GET / -> 200"
`
errs, err := e.Check(context.Background(), FromBytes([]byte(yaml)))
if err != nil {
t.Fatal(err)
}
if len(errs) != 0 {
t.Errorf("valid manifest should produce no errors, got %v", errs)
}
}
func TestRun_EventsEmitted(t *testing.T) {
e := New()
defer e.Close()
var gotRunStarted, gotRunCompleted bool
handler := handlerFunc(func(ev Event) {
switch ev.(type) {
case RunStarted:
gotRunStarted = true
case RunCompleted:
gotRunCompleted = true
}
})
yaml := `tests: []`
_, _ = e.Run(context.Background(), FromBytes([]byte(yaml)), WithHandler(handler))
if !gotRunStarted {
t.Error("RunStarted should be emitted")
}
if !gotRunCompleted {
t.Error("RunCompleted should be emitted")
}
}
func TestRun_FromReader(t *testing.T) {
e := New()
defer e.Close()
r := strings.NewReader(`tests: []`)
if _, err := e.Run(context.Background(), FromReader(r)); err != nil {
t.Fatal(err)
}
}
func TestRun_ConcurrentSafe(t *testing.T) {
e := New()
defer e.Close()
yaml := `tests:
- "GET / -> 200"
`
var wg sync.WaitGroup
for range 5 {
wg.Go(func() {
_, _ = e.Run(context.Background(), FromBytes([]byte(yaml)))
})
}
wg.Wait()
}
func TestPlugins_BeforeRun(t *testing.T) {
e := New()
defer e.Close()
if got := e.Plugins(); got != nil {
t.Errorf("Plugins() should be nil before any Run, got %v", got)
}
if got := e.EventSchema("x.y"); got != nil {
t.Errorf("EventSchema before Run should return nil, got %v", got)
}
}
func TestEngine_Close_IsIdempotent(t *testing.T) {
e := New()
if err := e.Close(); err != nil {
t.Errorf("first Close: %v", err)
}
if err := e.Close(); err != nil {
t.Errorf("second Close: %v", err)
}
}
func TestRun_GraphError(t *testing.T) {
e := New()
defer e.Close()
yaml := `tests:
- name: a
alias: a
depends: [a]
method: GET
target: x://t
`
_, err := e.Run(context.Background(), FromBytes([]byte(yaml)))
// Self-cycle is ignored by toposort but we can produce a real cycle below.
_ = err
yamlCycle := `tests:
- name: a
alias: a
depends: [b]
method: GET
target: x://t
- name: b
alias: b
depends: [a]
method: GET
target: x://t
`
_, err = e.Run(context.Background(), FromBytes([]byte(yamlCycle)))
if err == nil {
t.Error("cycle should propagate as run-level error")
}
}
func TestRun_PluginEventEmitted(t *testing.T) {
// Tests that the OnPluginEvent hook wires through; without a real plugin
// we can only assert that the handler dispatcher works for PluginEvent
// values.
got := false
handler := handlerFunc(func(ev Event) {
if _, ok := ev.(PluginEvent); ok {
got = true
}
})
pe := PluginEvent{Plugin: "p", Kind: "K"}
handler.Handle(pe)
if !got {
t.Error("handler should receive PluginEvent")
}
if pe.FullName() != "p.K" {
t.Errorf("FullName wrong: %q", pe.FullName())
}
}
func TestRun_FailFast(t *testing.T) {
e := New(WithFailFast(true))
defer e.Close()
// Without a plugin, all tests will error; failFast just skips remaining.
yaml := `tests:
- name: a
method: GET
target: x://t
- name: b
method: GET
target: x://t
`
res, err := e.Run(context.Background(), FromBytes([]byte(yaml)))
if err != nil {
t.Fatal(err)
}
if len(res.Tests) != 2 {
t.Errorf("got %d tests; want 2", len(res.Tests))
}
}
func TestErrNoInput(t *testing.T) {
if ErrNoInput == nil {
t.Fatal("ErrNoInput must be defined")
}
if ErrNoInput.Error() == "" {
t.Error("ErrNoInput should have a message")
}
}
// handlerFunc adapts a function into the EventHandler interface for tests.
type handlerFunc func(Event)
func (f handlerFunc) Handle(e Event) { f(e) }