Skip to content

Commit 366f3ee

Browse files
Use unique index for TestOtelLogsIngestion (#11254) (#11419)
(cherry picked from commit 85241ff) Co-authored-by: Tiago Queiroz <[email protected]>
1 parent b2652a8 commit 366f3ee

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
@@ -59,6 +59,7 @@ fleet.yml
5959
fleet.yml.lock
6060
fleet.yml.old
6161
pkg/component/fake/component/component
62+
pkg/core/process/testsignal/testsignal
6263
internal/pkg/agent/install/testblocking/testblocking
6364
internal/pkg/otel/manager/testing/testing
6465
pkg/core/process/testsignal/testsignal

magefile.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,9 @@ func (Integration) Local(ctx context.Context, testName string) error {
22292229
// run the integration tests but only run test that can run locally
22302230
params := devtools.DefaultGoTestIntegrationArgs()
22312231
params.Tags = append(params.Tags, "local")
2232-
params.Packages = []string{"github.com/elastic/elastic-agent/testing/integration/..."}
2232+
params.Packages = []string{
2233+
"github.com/elastic/elastic-agent/testing/integration/...",
2234+
}
22332235

22342236
var goTestFlags []string
22352237
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
@@ -472,6 +472,15 @@ exporters:
472472
elasticsearch:
473473
api_key: {{.ESApiKey}}
474474
endpoint: {{.ESEndpoint}}
475+
logs_index: {{.TestId}}
476+
sending_queue:
477+
wait_for_result: true
478+
block_on_overflow: true
479+
enabled: true
480+
batch:
481+
min_size: 2000
482+
max_size: 10000
483+
flush_timeout: 1s
475484
mapping:
476485
mode: none
477486
@@ -498,6 +507,13 @@ service:
498507
- resource/add-test-id
499508
receivers:
500509
- filelog
510+
telemetry:
511+
logs:
512+
level: DEBUG
513+
encoding: json
514+
disable_stacktrace: true
515+
output_paths:
516+
- {{.OTelLogFile}}
501517
`
502518

503519
func TestOtelLogsIngestion(t *testing.T) {
@@ -515,8 +531,11 @@ func TestOtelLogsIngestion(t *testing.T) {
515531
// Prepare the OTel config.
516532
testId := info.Namespace
517533

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

521540
esHost, err := integration.GetESHost()
522541
require.NoError(t, err, "failed to get ES host")
@@ -533,6 +552,7 @@ func TestOtelLogsIngestion(t *testing.T) {
533552
logsIngestionConfig = strings.ReplaceAll(logsIngestionConfig, "{{.ESEndpoint}}", esHost)
534553
logsIngestionConfig = strings.ReplaceAll(logsIngestionConfig, "{{.InputFilePath}}", inputFilePath)
535554
logsIngestionConfig = strings.ReplaceAll(logsIngestionConfig, "{{.TestId}}", testId)
555+
logsIngestionConfig = strings.ReplaceAll(logsIngestionConfig, "{{.OTelLogFile}}", otelLogFilePath)
536556

537557
cfgFilePath := filepath.Join(tempDir, "otel.yml")
538558
require.NoError(t, os.WriteFile(cfgFilePath, []byte(logsIngestionConfig), 0o600))
@@ -566,26 +586,30 @@ func TestOtelLogsIngestion(t *testing.T) {
566586
require.NoError(t, err)
567587
}
568588
inputFile.Close()
569-
t.Cleanup(func() {
570-
_ = os.Remove(inputFilePath)
571-
})
572589

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

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

590614
cancel()
591615
fixtureWg.Wait()

0 commit comments

Comments
 (0)