Skip to content

Commit 5c093c2

Browse files
committed
add lazy downloader to system tests that require geoip db
1 parent 6623ae1 commit 5c093c2

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

systemtest/agentconfig_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939

4040
func TestAgentConfig(t *testing.T) {
4141
systemtest.CleanupElasticsearch(t)
42+
systemtest.GeoIpLazyDownload(t)
4243

4344
serviceName := "systemtest_service"
4445
serviceEnvironment := "testing"

systemtest/aggregation_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import (
3939

4040
func TestTransactionAggregation(t *testing.T) {
4141
systemtest.CleanupElasticsearch(t)
42+
systemtest.GeoIpLazyDownload(t)
43+
4244
srv := apmservertest.NewUnstartedServerTB(t)
4345
require.NoError(t, srv.Start())
4446

@@ -118,6 +120,8 @@ func TestTransactionAggregation(t *testing.T) {
118120

119121
func TestTransactionAggregationShutdown(t *testing.T) {
120122
systemtest.CleanupElasticsearch(t)
123+
systemtest.GeoIpLazyDownload(t)
124+
121125
srv := apmservertest.NewServerTB(t)
122126

123127
// Send a transaction to the server to be aggregated.
@@ -146,6 +150,8 @@ func TestTransactionAggregationShutdown(t *testing.T) {
146150
func TestServiceDestinationAggregation(t *testing.T) {
147151
t.Setenv("ELASTIC_APM_GLOBAL_LABELS", "department_name=apm,organization=observability,company=elastic")
148152
systemtest.CleanupElasticsearch(t)
153+
systemtest.GeoIpLazyDownload(t)
154+
149155
srv := apmservertest.NewServerTB(t)
150156

151157
// Send spans to the server to be aggregated.

systemtest/geoip.go

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3234
var 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
}

systemtest/rum_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040

4141
func TestRUMXForwardedFor(t *testing.T) {
4242
systemtest.CleanupElasticsearch(t)
43+
systemtest.GeoIpLazyDownload(t)
4344

4445
srv := apmservertest.NewUnstartedServerTB(t)
4546
srv.Config.RUM = &apmservertest.RUMConfig{Enabled: true}
@@ -50,8 +51,6 @@ func TestRUMXForwardedFor(t *testing.T) {
5051
require.NoError(t, err)
5152
serverURL.Path = "/intake/v2/rum/events"
5253

53-
systemtest.GeoIpLazyDownload(t, serverURL.String())
54-
5554
const body = `{"metadata":{"service":{"name":"rum-js-test","agent":{"name":"rum-js","version":"5.5.0"}}}}
5655
{"transaction":{"trace_id":"611f4fa950f04631aaaaaaaaaaaaaaaa","id":"611f4fa950f04631","type":"page-load","duration":643,"span_count":{"started":0}}}
5756
{"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}}
@@ -86,10 +85,9 @@ func TestRUMXForwardedFor(t *testing.T) {
8685
Field: "processor.event",
8786
Values: []any{"transaction", "metric"},
8887
},
89-
// espoll.WithTimeout(30*time.Second),
9088
)
9189

92-
// Also checks for the absence of `tags` fields.
90+
// Includes checking for absence of `tags` field.
9391
approvaltest.ApproveFields(
9492
t, t.Name(), result.Hits.Hits,
9593
// RUM timestamps are set by the server based on the time the payload is received.

0 commit comments

Comments
 (0)