-
Notifications
You must be signed in to change notification settings - Fork 18
/
test.go
693 lines (556 loc) · 15.6 KB
/
test.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
package cute
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"testing"
"time"
"github.com/ozontech/allure-go/pkg/framework/provider"
cuteErrors "github.com/ozontech/cute/errors"
"github.com/ozontech/cute/internal/utils"
)
const (
defaultExecuteTestTime = 10 * time.Second
defaultDelayRepeat = 1 * time.Second
)
var (
errorRequestMethodEmpty = errors.New("request method must be not empty")
errorRequestURLEmpty = errors.New("url request must be not empty")
)
// Test is a main struct of test.
// You may field Request and Expect for create simple test
// Parallel can be used to control the parallelism of a Test
type Test struct {
httpClient *http.Client
jsonMarshaler JSONMarshaler
Name string
Parallel bool
Retry *Retry
AllureStep *AllureStep
Middleware *Middleware
Request *Request
Expect *Expect
}
// Retry is a struct to control the retry of a whole single test (not only the request)
// The test will be retried up to MaxAttempts times
// The retries will only be executed if the test is having errors
// If the test is successful at any iteration between attempt 1 and MaxAttempts, the loop will break and return the result as successful
// The status of the test (success or fail) will be based on either the first attempt that is successful, or, if no attempt
// is successful, it will be based on the latest execution
// Delay is the number of seconds to wait before attempting to run the test again. It will only wait if Delay is set.
type Retry struct {
currentCount int
MaxAttempts int
Delay time.Duration
}
// Request is struct with HTTP request.
// You may use your *http.Request or create new with help Builders
type Request struct {
Base *http.Request
Builders []RequestBuilder
Retry *RequestRetryPolitic
}
// RequestRetryPolitic is struct for repeat politic
// if Optional is true and request is failed, than test step allure will be skip, and t.Fail() will not execute.
// If Broken is true and request is failed, than test step allure will be broken, and t.Fail() will not execute.
// If Optional and Broken is false, than test step will be failed, and t.Fail() will execute.
// If response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
type RequestRetryPolitic struct {
Count int
Delay time.Duration
Optional bool
Broken bool
}
// RequestRepeatPolitic is struct for repeat politic
// Deprecated: use RequestRetryPolitic
type RequestRepeatPolitic struct {
Count int
Delay time.Duration
Optional bool
Broken bool
}
// Middleware is struct for executeInsideAllure something before or after test
type Middleware struct {
After []AfterExecute
AfterT []AfterExecuteT
Before []BeforeExecute
BeforeT []BeforeExecuteT
}
// AllureStep is struct with test name
type AllureStep struct {
Name string
}
// Expect is structs with validate politics for response
type Expect struct {
ExecuteTime time.Duration
Code int
JSONSchema *ExpectJSONSchema
AssertBody []AssertBody
AssertHeaders []AssertHeaders
AssertResponse []AssertResponse
AssertBodyT []AssertBodyT
AssertHeadersT []AssertHeadersT
AssertResponseT []AssertResponseT
}
// ExpectJSONSchema is structs with JSON politics for response
type ExpectJSONSchema struct {
String string
Byte []byte
File string
}
// Execute is common method for run test from builder
func (it *Test) Execute(ctx context.Context, t tProvider) ResultsHTTPBuilder {
var (
internalT allureProvider
res ResultsHTTPBuilder
)
if t == nil {
panic("could not start test without testing.T")
}
stepCtx, isStepCtx := t.(provider.StepCtx)
if isStepCtx {
return it.executeInsideStep(ctx, stepCtx)
}
tOriginal, ok := t.(*testing.T)
if ok {
tOriginal.Helper()
internalT = createAllureT(tOriginal)
}
allureT, ok := t.(provider.T)
if ok {
internalT = allureT
}
internalT.Run(it.Name, func(inT provider.T) {
if it.Parallel {
inT.Parallel()
}
res = it.executeInsideAllure(ctx, inT)
})
return res
}
func (it *Test) clearFields() {
it.AllureStep = new(AllureStep)
it.Middleware = new(Middleware)
it.Expect = new(Expect)
it.Request = new(Request)
it.Request.Retry = new(RequestRetryPolitic)
it.Expect.JSONSchema = new(ExpectJSONSchema)
}
func (it *Test) initEmptyFields() {
if it.httpClient == nil {
it.httpClient = http.DefaultClient
}
if it.AllureStep == nil {
it.AllureStep = new(AllureStep)
}
if it.Middleware == nil {
it.Middleware = new(Middleware)
}
if it.Expect == nil {
it.Expect = new(Expect)
}
if it.Request == nil {
it.Request = new(Request)
}
if it.Request.Retry == nil {
it.Request.Retry = new(RequestRetryPolitic)
}
if it.Expect.JSONSchema == nil {
it.Expect.JSONSchema = new(ExpectJSONSchema)
}
if it.Retry == nil {
it.Retry = &Retry{
// we set the default value to 1, because we count the first attempt as 1
MaxAttempts: 1,
currentCount: 1,
}
}
// We need to set the current count to 1 here, because we count the first attempt as 1
it.Retry.currentCount = 1
}
// executeInsideStep is method for start test with provider.StepCtx
// It's test inside the step
func (it *Test) executeInsideStep(ctx context.Context, t internalT) ResultsHTTPBuilder {
// Set empty fields in test
it.initEmptyFields()
// we don't want to defer the finish message, because it will be logged in processTestErrors
it.Info(t, "Start test")
return it.startRepeatableTest(ctx, t)
}
func (it *Test) executeInsideAllure(ctx context.Context, allureProvider allureProvider) ResultsHTTPBuilder {
// Set empty fields in test
it.initEmptyFields()
// we don't want to defer the finish message, because it will be logged in processTestErrors
it.Info(allureProvider, "Start test")
if it.AllureStep.Name != "" {
// Execute test inside step
return it.startTestInsideStep(ctx, allureProvider)
} else {
// Execute Test
return it.startRepeatableTest(ctx, allureProvider)
}
}
// startRepeatableTest is method for start test with repeatable execution
func (it *Test) startRepeatableTest(ctx context.Context, t internalT) ResultsHTTPBuilder {
var (
resp *http.Response
errs []error
resultState ResultState
)
for ; it.Retry.currentCount <= it.Retry.MaxAttempts; it.Retry.currentCount++ {
resp, errs = it.startTest(ctx, t)
resultState = it.processTestErrors(t, errs)
// we don't want to keep errors if we will retry test
// we have to return to user only errors from last try
// if the test is successful, we break the loop
if resultState == ResultStateSuccess {
break
}
// if we have a delay, we wait before the next attempt
// and we only wait if we are not at the last attempt
if it.Retry.currentCount != it.Retry.MaxAttempts && it.Retry.Delay != 0 {
it.Info(t, "The test had errors, retrying...")
time.Sleep(it.Retry.Delay)
}
}
switch resultState {
case ResultStateBroken:
t.BrokenNow()
it.Info(t, "Test broken")
case ResultStateFail:
t.Fail()
it.Error(t, "Test failed")
case resultStateFailNow:
t.FailNow()
it.Error(t, "Test failed")
case ResultStateSuccess:
it.Info(t, "Test finished successfully")
}
return newTestResult(it.Name, resp, resultState, errs)
}
func (it *Test) startTestInsideStep(ctx context.Context, t internalT) ResultsHTTPBuilder {
var (
result ResultsHTTPBuilder
)
t.WithNewStep(it.AllureStep.Name, func(stepCtx provider.StepCtx) {
it.Info(t, "Start step %v", it.AllureStep.Name)
defer it.Info(t, "Finish step %v", it.AllureStep.Name)
result = it.startRepeatableTest(ctx, stepCtx)
if result.GetResultState() == ResultStateFail {
stepCtx.Fail()
}
})
return result
}
// processTestErrors returns flag, which mean finish test or not.
// If test has only optional errors, than test will be success
// If test has broken errors, than test will be broken on allure
// If test has require errors, than test will be failed on allure
func (it *Test) processTestErrors(t internalT, errs []error) ResultState {
if len(errs) == 0 {
it.Info(t, "Test finished successfully")
return ResultStateSuccess
}
var (
countNotOptionalErrors = 0
state ResultState
)
for _, err := range errs {
message := fmt.Sprintf("error %v", err.Error())
if tErr, ok := err.(cuteErrors.OptionalError); ok {
if tErr.IsOptional() {
it.Info(t, "[OPTIONAL ERROR] %v", err.Error())
state = ResultStateSuccess
continue
}
}
if tErr, ok := err.(cuteErrors.BrokenError); ok {
if tErr.IsBroken() {
it.Error(t, "[BROKEN ERROR], error %v", err.Error())
state = ResultStateBroken
continue
}
}
if tErr, ok := err.(cuteErrors.RequireError); ok {
if tErr.IsRequire() {
state = resultStateFailNow
}
}
if tErr, ok := err.(cuteErrors.WithFields); ok {
actual := tErr.GetFields()[cuteErrors.ActualField]
expected := tErr.GetFields()[cuteErrors.ExpectedField]
if actual != nil || expected != nil {
message = fmt.Sprintf("%s\nActual %v\nExpected %v", message, actual, expected)
}
}
it.Error(t, message)
countNotOptionalErrors++
}
if countNotOptionalErrors != 0 {
state = ResultStateFail
it.Error(t, "Test finished with %v errors", countNotOptionalErrors)
}
return state
}
func (it *Test) startTest(ctx context.Context, t internalT) (*http.Response, []error) {
var (
resp *http.Response
err error
)
// CreateWithStep executeInsideAllure timer
if it.Expect.ExecuteTime == 0 {
it.Expect.ExecuteTime = defaultExecuteTestTime
}
ctx, cancel := context.WithTimeout(ctx, it.Expect.ExecuteTime)
defer cancel()
// CreateWithStep request
req, err := it.createRequest(ctx)
if err != nil {
return nil, []error{err}
}
// Execute Before
if errs := it.beforeTest(t, req); len(errs) > 0 {
return nil, errs
}
it.Info(t, "Start make request")
// Make request
resp, errs := it.makeRequest(t, req)
if len(errs) > 0 {
return resp, errs
}
it.Info(t, "Finish make request")
// Validate response body
errs = it.validateResponse(t, resp)
// Execute After
afterTestErrs := it.afterTest(t, resp, errs)
// Return results
errs = append(errs, afterTestErrs...)
if len(errs) > 0 {
return resp, errs
}
return resp, nil
}
func (it *Test) afterTest(t internalT, resp *http.Response, errs []error) []error {
if len(it.Middleware.After) == 0 && len(it.Middleware.AfterT) == 0 {
return nil
}
return it.executeWithStep(t, "After", func(t T) []error {
scope := make([]error, 0)
for _, execute := range it.Middleware.After {
if err := execute(resp, errs); err != nil {
scope = append(scope, err)
}
}
for _, executeSuite := range it.Middleware.AfterT {
if err := executeSuite(t, resp, errs); err != nil {
scope = append(scope, err)
}
}
return scope
})
}
func (it *Test) beforeTest(t internalT, req *http.Request) []error {
if len(it.Middleware.Before) == 0 && len(it.Middleware.BeforeT) == 0 {
return nil
}
return it.executeWithStep(t, "Before", func(t T) []error {
scope := make([]error, 0)
for _, execute := range it.Middleware.Before {
if err := execute(req); err != nil {
scope = append(scope, err)
}
}
for _, executeT := range it.Middleware.BeforeT {
if err := executeT(t, req); err != nil {
scope = append(scope, err)
}
}
return scope
})
}
func (it *Test) createRequest(ctx context.Context) (*http.Request, error) {
var (
req = it.Request.Base
err error
)
if req == nil {
req, err = it.buildRequest(ctx)
if err != nil {
return nil, err
}
}
// Validate Request
if err := it.validateRequest(req); err != nil {
return nil, err
}
return req, nil
}
// buildRequest
// Priority for create body:
// 1. requestOptions.body <- low priority
// 2. requestOptions.bodyMarshal
// 3. requestOptions.forms and requestOptions.fileForms <- high priority.
func (it *Test) buildRequest(ctx context.Context) (*http.Request, error) {
var (
req *http.Request
err error
o = newRequestOptions()
)
// Set builder parameters
for _, builder := range it.Request.Builders {
builder(o)
}
reqURL := o.url
if reqURL == nil {
reqURL, err = url.Parse(o.uri)
if err != nil {
return nil, err
}
}
// Set query parameters
query := reqURL.Query()
for key, values := range o.query {
for _, value := range values {
query.Add(key, value)
}
}
reqURL.RawQuery = query.Encode()
// Set body
body := o.body
if o.bodyMarshal != nil {
body, err = it.jsonMarshaler.Marshal(o.bodyMarshal)
if err != nil {
return nil, err
}
}
// Set multipart
if len(o.fileForms) != 0 || len(o.forms) != 0 {
var (
buffer = new(bytes.Buffer)
mp = multipart.NewWriter(buffer)
)
// set file forms
for fieldName, file := range o.fileForms {
err = createFormFile(mp, fieldName, file)
if err != nil {
return nil, err
}
}
// set forms
for fieldName, fieldBody := range o.forms {
err = createFormField(mp, fieldName, fieldBody)
if err != nil {
return nil, err
}
}
if err = mp.Close(); err != nil {
return nil, err
}
req, err = http.NewRequestWithContext(ctx, o.method, reqURL.String(), buffer)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", mp.FormDataContentType())
} else {
req, err = http.NewRequestWithContext(ctx, o.method, reqURL.String(), io.NopCloser(bytes.NewReader(body)))
if err != nil {
return nil, err
}
}
// Set headers
for nameHeader, valuesHeader := range o.headers {
req.Header[nameHeader] = valuesHeader
}
return req, nil
}
func createFormFile(mp *multipart.Writer, fieldName string, file *File) error {
var (
data = file.Body
name = file.Name
)
// read file, if path is not empty
if len(file.Path) != 0 {
f, err := os.Open(file.Path)
if err != nil {
return err
}
data, err = io.ReadAll(f)
if err != nil {
return err
}
name = f.Name()
}
field, err := mp.CreateFormFile(fieldName, name)
if err != nil {
return fmt.Errorf("error when creating %v file form field, %w", fieldName, err)
}
_, err = field.Write(data)
if err != nil {
return fmt.Errorf("error when writing %v file form field, %w", fieldName, err)
}
return nil
}
func createFormField(mp *multipart.Writer, fieldName string, body []byte) error {
field, err := mp.CreateFormField(fieldName)
if err != nil {
return fmt.Errorf("error when creating %v form field, %w", fieldName, err)
}
_, err = field.Write(body)
if err != nil {
return fmt.Errorf("error when writing %v form field, %w", fieldName, err)
}
return nil
}
func (it *Test) validateRequest(req *http.Request) error {
if req.URL == nil {
return errorRequestURLEmpty
}
if req.Method == "" {
return errorRequestMethodEmpty
}
return nil
}
func (it *Test) validateResponse(t internalT, resp *http.Response) []error {
var (
err error
saveBody io.ReadCloser
scope = make([]error, 0)
)
// Execute asserts for headers
if errs := it.assertHeaders(t, resp.Header); len(errs) > 0 {
scope = append(scope, errs...)
}
// Prepare body for validate
if resp.Body == nil {
// todo create errors if body is empty, but assert is not empty
return scope
}
saveBody, resp.Body, err = utils.DrainBody(resp.Body)
if err != nil {
return append(scope, fmt.Errorf("could not drain response body. error %w", err))
}
body, err := utils.GetBody(saveBody)
if err != nil {
return append(scope, fmt.Errorf("could not get response body. error %w", err))
}
// Execute asserts for body
if errs := it.assertBody(t, body); len(errs) > 0 {
// add assert
scope = append(scope, errs...)
}
// Validate response by json schema
if errs := it.validateJSONSchema(t, body); len(errs) > 0 {
scope = append(scope, errs...)
}
// Execute asserts for response body
if errs := it.assertResponse(t, resp); len(errs) > 0 {
scope = append(scope, errs...)
}
return scope
}