@@ -21,6 +21,18 @@ import (
2121 "github.com/elastic/go-lookslike/testslike"
2222)
2323
24+ func makeStepEvent (typ string , ts float64 , name string , index int , status string , urlstr string , err * SynthError ) * SynthEvent {
25+ return & SynthEvent {
26+ Type : typ ,
27+ TimestampEpochMicros : 1000 + ts ,
28+ PackageVersion : "1.0.0" ,
29+ Step : & Step {Name : name , Index : index , Status : status },
30+ Error : err ,
31+ Payload : common.MapStr {},
32+ URL : urlstr ,
33+ }
34+ }
35+
2436func TestJourneyEnricher (t * testing.T ) {
2537 journey := & Journey {
2638 Name : "A Journey Name" ,
@@ -50,17 +62,6 @@ func TestJourneyEnricher(t *testing.T) {
5062 Journey : journey ,
5163 Payload : common.MapStr {},
5264 }
53- makeStepEvent := func (typ string , ts float64 , name string , index int , status string , urlstr string , err * SynthError ) * SynthEvent {
54- return & SynthEvent {
55- Type : typ ,
56- TimestampEpochMicros : 1000 + ts ,
57- PackageVersion : "1.0.0" ,
58- Step : & Step {Name : name , Index : index , Status : status },
59- Error : err ,
60- Payload : common.MapStr {},
61- URL : urlstr ,
62- }
63- }
6465 url1 := "http://example.net/url1"
6566 url2 := "http://example.net/url2"
6667 url3 := "http://example.net/url3"
@@ -213,3 +214,75 @@ func TestEnrichSynthEvent(t *testing.T) {
213214 })
214215 }
215216}
217+
218+ func TestNoSummaryOnAfterHook (t * testing.T ) {
219+ journey := & Journey {
220+ Name : "A journey that fails after completing" ,
221+ Id : "my-bad-after-all-hook" ,
222+ }
223+ journeyStart := & SynthEvent {
224+ Type : "journey/start" ,
225+ TimestampEpochMicros : 1000 ,
226+ PackageVersion : "1.0.0" ,
227+ Journey : journey ,
228+ Payload : common.MapStr {},
229+ }
230+ syntherr := & SynthError {
231+ Message : "my-errmsg" ,
232+ Name : "my-errname" ,
233+ Stack : "my\n err\n stack" ,
234+ }
235+ journeyEnd := & SynthEvent {
236+ Type : "journey/end" ,
237+ TimestampEpochMicros : 2000 ,
238+ PackageVersion : "1.0.0" ,
239+ Journey : journey ,
240+ Payload : common.MapStr {},
241+ }
242+ cmdStatus := & SynthEvent {
243+ Type : "cmd/status" ,
244+ Error : & SynthError {Name : "cmdexit" , Message : "cmd err msg" },
245+ TimestampEpochMicros : 3000 ,
246+ }
247+
248+ badStepUrl := "https://example.com/bad-step"
249+ synthEvents := []* SynthEvent {
250+ journeyStart ,
251+ makeStepEvent ("step/start" , 10 , "Step1" , 1 , "" , "" , nil ),
252+ makeStepEvent ("step/end" , 20 , "Step1" , 1 , "failed" , badStepUrl , syntherr ),
253+ journeyEnd ,
254+ cmdStatus ,
255+ }
256+
257+ je := & journeyEnricher {}
258+
259+ for idx , se := range synthEvents {
260+ e := & beat.Event {}
261+ t .Run (fmt .Sprintf ("event %d" , idx ), func (t * testing.T ) {
262+ enrichErr := je .enrich (e , se )
263+
264+ if se != nil && se .Type == "cmd/status" {
265+ t .Run ("no summary in cmd/status" , func (t * testing.T ) {
266+ require .NotContains (t , e .Fields , "summary" )
267+ })
268+ }
269+
270+ // Only the journey/end event should get a summary when
271+ // it's emitted before the cmd/status (when an afterX hook fails).
272+ if se != nil && se .Type == "journey/end" {
273+ require .Equal (t , stepError (syntherr ), enrichErr )
274+
275+ u , _ := url .Parse (badStepUrl )
276+ t .Run ("summary in journey/end" , func (t * testing.T ) {
277+ v := lookslike .MustCompile (common.MapStr {
278+ "synthetics.type" : "heartbeat/summary" ,
279+ "url" : wrappers .URLFields (u ),
280+ "monitor.duration.us" : int64 (journeyEnd .Timestamp ().Sub (journeyStart .Timestamp ()) / time .Microsecond ),
281+ })
282+
283+ testslike .Test (t , v , e .Fields )
284+ })
285+ }
286+ })
287+ }
288+ }
0 commit comments