@@ -12,6 +12,7 @@ import (
1212 "os"
1313 "reflect"
1414 "strings"
15+ "time"
1516
1617 "github.com/andreyvit/diff"
1718 "github.com/antonmedv/expr"
@@ -21,9 +22,94 @@ import (
2122 unstructured "github.com/linuxsuren/unstructured/pkg"
2223)
2324
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+
27113 if err = doPrepare (testcase ); err != nil {
28114 err = fmt .Errorf ("failed to prepare, error: %v" , err )
29115 return
@@ -77,13 +163,15 @@ func RunTestCase(testcase *testing.TestCase, dataContext interface{}, ctx contex
77163 if request , err = http .NewRequestWithContext (ctx , testcase .Request .Method , testcase .Request .API , requestBody ); err != nil {
78164 return
79165 }
166+ record .API = testcase .Request .API
167+ record .Method = testcase .Request .Method
80168
81169 // set headers
82170 for key , val := range testcase .Request .Header {
83171 request .Header .Add (key , val )
84172 }
85173
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 )
87175
88176 // send the HTTP request
89177 var resp * http.Response
@@ -178,6 +266,22 @@ func RunTestCase(testcase *testing.TestCase, dataContext interface{}, ctx contex
178266 return
179267}
180268
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+
181285func doPrepare (testcase * testing.TestCase ) (err error ) {
182286 for i := range testcase .Prepare .Kubernetes {
183287 item := testcase .Prepare .Kubernetes [i ]
0 commit comments