@@ -27,6 +27,8 @@ import (
2727 "time"
2828
2929 "github.com/stretchr/testify/require"
30+
31+ "github.com/elastic/apm-server/systemtest/apmservertest"
3032)
3133
3234var stats struct {
@@ -37,66 +39,59 @@ type GeoIPStats struct {
3739 DatabasesCount int `json:"databases_count"`
3840}
3941
40- const requestBody = `{"metadata":{"service":{"name":"rum-js-test","agent":{"name":"rum-js","version":"5.5.0"}}}}
41- {"transaction":{"trace_id":"611f4fa950f04631aaaaaaaaaaaaaaaa","id":"611f4fa950f04631","type":"page-load","duration":643,"span_count":{"started":0}}}
42- {"metricset":{"samples":{"transaction.breakdown.count":{"value":12},"transaction.duration.sum.us":{"value":12},"transaction.duration.count":{"value":2},"transaction.self_time.sum.us":{"value":10},"transaction.self_time.count":{"value":2},"span.self_time.count":{"value":1},"span.self_time.sum.us":{"value":633.288}},"transaction":{"type":"request","name":"GET /"},"span":{"type":"external","subtype":"http"},"timestamp": 1496170422281000}}
43- `
42+ const requestBody = `{"metadata": { "service": {"name": "alice", "agent": {"version": "3.14.0", "name": "elastic-node"}}}}
43+ {"metricset": {"samples":{"a":{"value":3.2}}, "timestamp": 1496170422281000}}`
4444
45- func GeoIpLazyDownload (t * testing.T , url string ) {
46- t .Helper ()
45+ func GeoIpLazyDownload (t testing.TB ) {
46+ srv := apmservertest .NewUnstartedServerTB (t )
47+ require .NoError (t , srv .Start ())
4748
48- req , err := http .NewRequest ("POST" , url , strings .NewReader (requestBody ))
49+ serverURL := srv .URL + "/intake/v2/events"
50+ req , err := http .NewRequest ("POST" , serverURL , strings .NewReader (requestBody ))
4951 require .NoError (t , err )
50-
5152 req .Header .Set ("Content-Type" , "application/x-ndjson" )
5253 req .Header .Set ("X-Forwarded-For" , "8.8.8.8" )
5354
5455 resp , err := http .DefaultClient .Do (req )
5556 require .NoError (t , err )
5657 require .Equal (t , http .StatusAccepted , resp .StatusCode )
57-
5858 io .Copy (io .Discard , resp .Body )
5959 resp .Body .Close ()
6060
61- err = waitGeoIPDownload (t )
62- require .NoError (t , err )
61+ require .NoError (t , waitGeoIPDownload (t ))
6362
6463 // Clean all datastreams containing tags.
6564 CleanupElasticsearch (t )
6665}
6766
68- func waitGeoIPDownload (t * testing.T ) error {
67+ func waitGeoIPDownload (t testing.TB ) error {
6968 t .Helper ()
70-
71- timer := time .NewTimer (30 * time .Second )
69+ timer := time .NewTimer (2 * time .Minute )
7270 ticker := time .NewTicker (500 * time .Millisecond )
7371 defer ticker .Stop ()
72+ defer timer .Stop ()
7473
75- first := true
7674 for {
7775 select {
7876 case <- ticker .C :
7977 resp , err := Elasticsearch .Ingest .GeoIPStats ()
80- require .NoError (t , err )
81-
78+ if err != nil {
79+ return err
80+ }
8281 body , err := io .ReadAll (resp .Body )
83- require .NoError (t , err )
84-
85- err = json .Unmarshal ([]byte (body ), & stats )
86- require .NoError (t , err )
87-
88- // [GeoLite2-ASN.mmdb, GeoLite2-City.mmdb, GeoLite2-Country.mmdb]
82+ resp .Body .Close ()
83+ if err != nil {
84+ return err
85+ }
86+ if err := json .Unmarshal (body , & stats ); err != nil {
87+ return err
88+ }
8989 if stats .DatabasesCount == 3 {
90- t .Log ("GeoIp database downloaded" )
90+ t .Log ("GeoIP database downloaded" )
9191 return nil
9292 }
9393 case <- timer .C :
9494 return fmt .Errorf ("download timeout exceeded" )
9595 }
96-
97- if first {
98- t .Log ("waiting for GeoIP database to download" )
99- first = false
100- }
10196 }
10297}
0 commit comments