Skip to content

Commit 7afeacd

Browse files
Use unique index for TestOtelLogsIngestion (#11254) (#11421)
(cherry picked from commit 85241ff) Co-authored-by: Tiago Queiroz <[email protected]>
1 parent 9ac5994 commit 7afeacd

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ fleet.yml
6161
fleet.yml.lock
6262
fleet.yml.old
6363
pkg/component/fake/component/component
64+
pkg/core/process/testsignal/testsignal
6465
internal/pkg/agent/install/testblocking/testblocking
6566
internal/pkg/otel/manager/testing/testing
6667
pkg/core/process/testsignal/testsignal

magefile.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2258,7 +2258,9 @@ func (Integration) Local(ctx context.Context, testName string) error {
22582258
// run the integration tests but only run test that can run locally
22592259
params := devtools.DefaultGoTestIntegrationArgs()
22602260
params.Tags = append(params.Tags, "local")
2261-
params.Packages = []string{"github.com/elastic/elastic-agent/testing/integration/..."}
2261+
params.Packages = []string{
2262+
"github.com/elastic/elastic-agent/testing/integration/...",
2263+
}
22622264

22632265
var goTestFlags []string
22642266
rawTestFlags := os.Getenv("GOTEST_FLAGS")

testing/integration/ess/otel_test.go

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,15 @@ exporters:
474474
elasticsearch:
475475
api_key: {{.ESApiKey}}
476476
endpoint: {{.ESEndpoint}}
477+
logs_index: {{.TestId}}
478+
sending_queue:
479+
wait_for_result: true
480+
block_on_overflow: true
481+
enabled: true
482+
batch:
483+
min_size: 2000
484+
max_size: 10000
485+
flush_timeout: 1s
477486
mapping:
478487
mode: none
479488
@@ -500,6 +509,13 @@ service:
500509
- resource/add-test-id
501510
receivers:
502511
- filelog
512+
telemetry:
513+
logs:
514+
level: DEBUG
515+
encoding: json
516+
disable_stacktrace: true
517+
output_paths:
518+
- {{.OTelLogFile}}
503519
`
504520

505521
func TestOtelLogsIngestion(t *testing.T) {
@@ -517,8 +533,11 @@ func TestOtelLogsIngestion(t *testing.T) {
517533
// Prepare the OTel config.
518534
testId := info.Namespace
519535

520-
tempDir := t.TempDir()
536+
// Ensure everything is saved in case of test failure
537+
// this folder is also collected on CI.
538+
tempDir := aTesting.TempDir(t, "..", "..", "..", "build")
521539
inputFilePath := filepath.Join(tempDir, "input.log")
540+
otelLogFilePath := filepath.Join(tempDir, "elastic-agent.ndjson")
522541

523542
esHost, err := integration.GetESHost()
524543
require.NoError(t, err, "failed to get ES host")
@@ -535,6 +554,7 @@ func TestOtelLogsIngestion(t *testing.T) {
535554
logsIngestionConfig = strings.ReplaceAll(logsIngestionConfig, "{{.ESEndpoint}}", esHost)
536555
logsIngestionConfig = strings.ReplaceAll(logsIngestionConfig, "{{.InputFilePath}}", inputFilePath)
537556
logsIngestionConfig = strings.ReplaceAll(logsIngestionConfig, "{{.TestId}}", testId)
557+
logsIngestionConfig = strings.ReplaceAll(logsIngestionConfig, "{{.OTelLogFile}}", otelLogFilePath)
538558

539559
cfgFilePath := filepath.Join(tempDir, "otel.yml")
540560
require.NoError(t, os.WriteFile(cfgFilePath, []byte(logsIngestionConfig), 0o600))
@@ -568,26 +588,30 @@ func TestOtelLogsIngestion(t *testing.T) {
568588
require.NoError(t, err)
569589
}
570590
inputFile.Close()
571-
t.Cleanup(func() {
572-
_ = os.Remove(inputFilePath)
573-
})
574591

575-
actualHits := &struct{ Hits int }{}
576-
require.Eventually(t,
577-
func() bool {
578-
findCtx, findCancel := context.WithTimeout(context.Background(), 10*time.Second)
592+
// It takes about 45s to ingest all files on local tests,
593+
// so set the timeout to 5min to be on the safe side.
594+
require.EventuallyWithT(
595+
t,
596+
func(c *assert.CollectT) {
597+
findCtx, findCancel := context.WithTimeout(t.Context(), 10*time.Second)
579598
defer findCancel()
580599

581-
docs, err := estools.GetLogsForIndexWithContext(findCtx, esClient, ".ds-logs-generic-default*", map[string]interface{}{
582-
"Resource.test.id": testId,
583-
})
584-
require.NoError(t, err)
585-
586-
actualHits.Hits = docs.Hits.Total.Value
587-
return actualHits.Hits == logsCount
600+
docs, err := estools.GetAllLogsForIndexWithContext(
601+
findCtx,
602+
esClient,
603+
testId)
604+
require.NoError(c, err)
605+
require.Equalf(
606+
c,
607+
logsCount,
608+
docs.Hits.Total.Value,
609+
"expecting %d events",
610+
logsCount)
588611
},
589-
2*time.Minute, 1*time.Second,
590-
"Expected %v logs, got %v", logsCount, actualHits)
612+
5*time.Minute,
613+
time.Second,
614+
"did not find the expected number of events")
591615

592616
cancel()
593617
fixtureWg.Wait()

0 commit comments

Comments
 (0)