@@ -12,6 +12,7 @@ import (
12
12
"os"
13
13
"reflect"
14
14
"strings"
15
+ "time"
15
16
16
17
"github.com/andreyvit/diff"
17
18
"github.com/antonmedv/expr"
@@ -21,9 +22,94 @@ import (
21
22
unstructured "github.com/linuxsuren/unstructured/pkg"
22
23
)
23
24
24
- // RunTestCase runs the test case
25
- func RunTestCase (testcase * testing.TestCase , dataContext interface {}, ctx context.Context ) (output interface {}, err error ) {
26
- fmt .Printf ("start to run: '%s'\n " , testcase .Name )
25
+ type TestCaseRunner interface {
26
+ RunTestCase (testcase * testing.TestCase , dataContext interface {}, ctx context.Context ) (output interface {}, err error )
27
+ WithOutputWriter (io.Writer ) TestCaseRunner
28
+ WithTestReporter (TestReporter ) TestCaseRunner
29
+ }
30
+
31
+ type ReportRecord struct {
32
+ Method string
33
+ API string
34
+ BeginTime time.Time
35
+ EndTime time.Time
36
+ Error error
37
+ }
38
+
39
+ // Duration returns the duration between begin and end time
40
+ func (r * ReportRecord ) Duration () time.Duration {
41
+ return r .EndTime .Sub (r .BeginTime )
42
+ }
43
+
44
+ func (r * ReportRecord ) ErrorCount () int {
45
+ if r .Error == nil {
46
+ return 0
47
+ }
48
+ return 1
49
+ }
50
+
51
+ // NewReportRecord creates a record, and set the begin time to be now
52
+ func NewReportRecord () * ReportRecord {
53
+ return & ReportRecord {
54
+ BeginTime : time .Now (),
55
+ }
56
+ }
57
+
58
+ type ReportResult struct {
59
+ API string
60
+ Count int
61
+ Average time.Duration
62
+ Max time.Duration
63
+ Min time.Duration
64
+ Error int
65
+ }
66
+
67
+ type ReportResultSlice []ReportResult
68
+
69
+ func (r ReportResultSlice ) Len () int {
70
+ return len (r )
71
+ }
72
+
73
+ func (r ReportResultSlice ) Less (i , j int ) bool {
74
+ return r [i ].Average > r [j ].Average
75
+ }
76
+
77
+ func (r ReportResultSlice ) Swap (i , j int ) {
78
+ tmp := r [i ]
79
+ r [i ] = r [j ]
80
+ r [j ] = tmp
81
+ }
82
+
83
+ type ReportResultWriter interface {
84
+ Output ([]ReportResult ) error
85
+ }
86
+
87
+ type TestReporter interface {
88
+ PutRecord (* ReportRecord )
89
+ GetAllRecords () []* ReportRecord
90
+ ExportAllReportResults () (ReportResultSlice , error )
91
+ }
92
+
93
+ type simpleTestCaseRunner struct {
94
+ testReporter TestReporter
95
+ writer io.Writer
96
+ }
97
+
98
+ // NewSimpleTestCaseRunner creates the instance of the simple test case runner
99
+ func NewSimpleTestCaseRunner () TestCaseRunner {
100
+ runner := & simpleTestCaseRunner {}
101
+ return runner .WithOutputWriter (io .Discard ).WithTestReporter (NewDiscardTestReporter ())
102
+ }
103
+
104
+ func (r * simpleTestCaseRunner ) RunTestCase (testcase * testing.TestCase , dataContext interface {}, ctx context.Context ) (output interface {}, err error ) {
105
+ fmt .Fprintf (r .writer , "start to run: '%s'\n " , testcase .Name )
106
+ record := NewReportRecord ()
107
+ defer func (rr * ReportRecord ) {
108
+ rr .EndTime = time .Now ()
109
+ rr .Error = err
110
+ r .testReporter .PutRecord (rr )
111
+ }(record )
112
+
27
113
if err = doPrepare (testcase ); err != nil {
28
114
err = fmt .Errorf ("failed to prepare, error: %v" , err )
29
115
return
@@ -77,13 +163,15 @@ func RunTestCase(testcase *testing.TestCase, dataContext interface{}, ctx contex
77
163
if request , err = http .NewRequestWithContext (ctx , testcase .Request .Method , testcase .Request .API , requestBody ); err != nil {
78
164
return
79
165
}
166
+ record .API = testcase .Request .API
167
+ record .Method = testcase .Request .Method
80
168
81
169
// set headers
82
170
for key , val := range testcase .Request .Header {
83
171
request .Header .Add (key , val )
84
172
}
85
173
86
- fmt .Println ( "start to send request to" , testcase .Request .API )
174
+ fmt .Fprintf ( r . writer , "start to send request to %s \n " , testcase .Request .API )
87
175
88
176
// send the HTTP request
89
177
var resp * http.Response
@@ -178,6 +266,22 @@ func RunTestCase(testcase *testing.TestCase, dataContext interface{}, ctx contex
178
266
return
179
267
}
180
268
269
+ func (r * simpleTestCaseRunner ) WithOutputWriter (writer io.Writer ) TestCaseRunner {
270
+ r .writer = writer
271
+ return r
272
+ }
273
+
274
+ func (r * simpleTestCaseRunner ) WithTestReporter (reporter TestReporter ) TestCaseRunner {
275
+ r .testReporter = reporter
276
+ return r
277
+ }
278
+
279
+ // Deprecated
280
+ // RunTestCase runs the test case.
281
+ func RunTestCase (testcase * testing.TestCase , dataContext interface {}, ctx context.Context ) (output interface {}, err error ) {
282
+ return NewSimpleTestCaseRunner ().WithOutputWriter (os .Stdout ).RunTestCase (testcase , dataContext , ctx )
283
+ }
284
+
181
285
func doPrepare (testcase * testing.TestCase ) (err error ) {
182
286
for i := range testcase .Prepare .Kubernetes {
183
287
item := testcase .Prepare .Kubernetes [i ]
0 commit comments