@@ -8,41 +8,45 @@ import (
88 "net/http/httptest"
99 "testing"
1010
11+ "github.com/rs/zerolog"
1112 "github.com/stretchr/testify/assert"
1213 "github.com/stretchr/testify/require"
1314)
1415
1516const (
16- splunkToken = "test-token"
17- numberReports = 3
18- reportID = "123"
19- testsRun = 15
17+ splunkToken = "test-token"
18+ splunkEvent SplunkEvent = "test"
19+ numberReports = 3
20+ reportID = "123"
21+ testRunCount = 15
22+ uniqueTests = 18
2023)
2124
2225func TestAggregateResultsSplunk (t * testing.T ) {
26+ t .Parallel ()
27+
2328 srv := splunkServer (t )
2429 t .Cleanup (srv .Close )
2530
26- report , err := LoadAndAggregate ("./testdata" , WithReportID (reportID ), WithSplunk (srv .URL , splunkToken , "test" ))
31+ report , err := LoadAndAggregate ("./testdata" , WithReportID (reportID ), WithSplunk (srv .URL , splunkToken , splunkEvent ))
2732 require .NoError (t , err , "LoadAndAggregate failed" )
28- require . NotNil (t , report , "report is nil" )
33+ verifyAggregatedReport (t , report )
2934}
3035
3136func TestAggregateResults (t * testing.T ) {
37+ t .Parallel ()
38+
3239 report , err := LoadAndAggregate ("./testdata" , WithReportID (reportID ))
3340 require .NoError (t , err , "LoadAndAggregate failed" )
34- require .NotNil (t , report , "report is nil" )
35- assert .Equal (t , reportID , report .ID , "report ID mismatch" )
36- assert .Equal (t , testsRun , len (report .Results ), "report results count mismatch" )
37- assert .Equal (t , testsRun , report .TestRunCount , "report test run count mismatch" )
41+ verifyAggregatedReport (t , report )
3842}
3943
4044func splunkServer (t * testing.T ) * httptest.Server {
4145 return httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
42- assert .Equal (t , "application/json" , r .Header .Get ("Content-Type" ))
43- assert .Equal (t , fmt .Sprintf ("Splunk %s" , splunkToken ), r .Header .Get ("Authorization" ))
46+ require .Equal (t , "application/json" , r .Header .Get ("Content-Type" ), "unexpected content type" )
47+ require .Equal (t , fmt .Sprintf ("Splunk %s" , splunkToken ), r .Header .Get ("Authorization" ), "unexpected authorization header" )
4448
45- // Figure out what the payload is
49+ // Figure out what kind of splunk data the payload is
4650 bodyBytes , err := io .ReadAll (r .Body )
4751 require .NoError (t , err )
4852 defer r .Body .Close ()
@@ -59,22 +63,111 @@ func splunkServer(t *testing.T) *httptest.Server {
5963 require .NotNil (t , eventType , "error parsing splunk event type" )
6064
6165 if eventType == string (Report ) {
62- var report TestReport
66+ var report SplunkTestReport
6367 err := json .Unmarshal (bodyBytes , & report )
6468 require .NoError (t , err , "error parsing report data" )
6569 require .NotNil (t , report , "error parsing report data" )
66- assert .Equal (t , reportID , report .ID , "report ID mismatch" )
67- assert .Equal (t , testsRun , len ( report .Results ) , "report results count mismatch" )
68- assert .Equal (t , testsRun , report .TestRunCount , "report test run count mismatch" )
70+ assert .Equal (t , SplunkSourceType , report .SourceType , "source type mismatch" )
71+ assert .Equal (t , splunkEvent , report .Event . Event , "event mismatch" )
72+ assert .Equal (t , reportID , report .Event . Data . ID , "ID mismatch" )
6973 } else if eventType == string (Result ) {
7074 var result SplunkTestResult
7175 err := json .Unmarshal (bodyBytes , & result )
7276 require .NoError (t , err , "error parsing results data" )
7377 require .NotNil (t , result , "error parsing results data" )
78+ assert .Equal (t , SplunkSourceType , result .SourceType , "source type mismatch" )
79+ assert .Equal (t , splunkEvent , result .Event .Event , "event mismatch" )
7480 } else {
75- t .Errorf ("unexpected event type: %s" , eventType )
81+ t .Errorf ("unexpected splunk event type: %s" , eventType )
82+ }
83+
84+ w .WriteHeader (http .StatusOK )
85+ }))
86+ }
87+
88+ func verifyAggregatedReport (t * testing.T , report * TestReport ) {
89+ require .NotNil (t , report , "report is nil" )
90+ assert .Equal (t , reportID , report .ID , "report ID mismatch" )
91+ assert .Equal (t , uniqueTests , len (report .Results ), "report results count mismatch" )
92+ assert .Equal (t , testRunCount , report .TestRunCount , "report test run count mismatch" )
93+ assert .Equal (t , false , report .RaceDetection , "race detection should be false" )
94+
95+ var (
96+ testFail , testSkipped , testPass TestResult
97+ testFailName = "TestFail"
98+ testSkippedName = "TestSkipped"
99+ testPassName = "TestPass"
100+ )
101+ for _ , result := range report .Results {
102+ if result .TestName == testFailName {
103+ testFail = result
76104 }
105+ if result .TestName == testSkippedName {
106+ testSkipped = result
107+ }
108+ if result .TestName == testPassName {
109+ testPass = result
110+ }
111+ }
112+
113+ t .Run ("verify TestFail" , func (t * testing.T ) {
114+ t .Parallel ()
115+
116+ require .Equal (t , testFailName , testFail .TestName , "TestFail not found" )
117+ assert .False (t , testFail .Panic , "TestFail should not panic" )
118+ assert .False (t , testFail .Skipped , "TestFail should not be skipped" )
119+ assert .Equal (t , testRunCount , testFail .Runs , "TestFail should run every time" )
120+ assert .Zero (t , testFail .Skips , "TestFail should not be skipped" )
121+ assert .Equal (t , testRunCount , testFail .Failures , "TestFail should fail every time" )
122+ assert .Len (t , testFail .Durations , testRunCount , "TestFail should have durations" )
123+ })
124+
125+ t .Run ("verify TestSkipped" , func (t * testing.T ) {
126+ t .Parallel ()
127+
128+ require .Equal (t , testSkippedName , testSkipped .TestName , "TestSkip not found" )
129+ assert .False (t , testSkipped .Panic , "TestSkipped should not panic" )
130+ assert .Zero (t , testSkipped .Runs , "TestSkipped should not pass" )
131+ assert .True (t , testSkipped .Skipped , "TestSkipped should be skipped" )
132+ assert .Equal (t , testRunCount , testSkipped .Skips , "TestSkipped should be skipped entirely" )
133+ assert .Empty (t , testSkipped .Durations , "TestSkipped should not have durations" )
134+ })
135+
136+ t .Run ("verify TestPass" , func (t * testing.T ) {
137+ t .Parallel ()
77138
139+ require .Equal (t , testPassName , testPass .TestName , "TestPass not found" )
140+ assert .False (t , testPass .Panic , "TestPass should not panic" )
141+ assert .Equal (t , testRunCount , testPass .Runs , "TestPass should run every time" )
142+ assert .False (t , testPass .Skipped , "TestPass should not be skipped" )
143+ assert .Zero (t , testPass .Skips , "TestPass should not be skipped" )
144+ assert .Equal (t , testRunCount , testPass .Successes , "TestPass should pass every time" )
145+ assert .Len (t , testPass .Durations , testRunCount , "TestPass should have durations" )
146+ })
147+ }
148+
149+ func BenchmarkTestAggregateResults (b * testing.B ) {
150+ zerolog .SetGlobalLevel (zerolog .Disabled )
151+ for i := 0 ; i < b .N ; i ++ {
152+ _ , err := LoadAndAggregate ("./testdata" , WithReportID (reportID ))
153+ if err != nil {
154+ b .Fatalf ("LoadAndAggregate failed: %v" , err )
155+ }
156+ }
157+ }
158+
159+ func BenchmarkTestAggregateResultsSplunk (b * testing.B ) {
160+ zerolog .SetGlobalLevel (zerolog .Disabled )
161+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
78162 w .WriteHeader (http .StatusOK )
79163 }))
164+ defer srv .Close ()
165+
166+ b .ResetTimer ()
167+ for i := 0 ; i < b .N ; i ++ {
168+ _ , err := LoadAndAggregate ("./testdata" , WithReportID (reportID ), WithSplunk (srv .URL , splunkToken , "test" ))
169+ if err != nil {
170+ b .Fatalf ("LoadAndAggregate failed: %v" , err )
171+ }
172+ }
80173}
0 commit comments