Skip to content

Commit 415485c

Browse files
committed
test: update TestBeatDiagnostics test
Signed-off-by: Dominik Rosiek <dominik.rosiek@elastic.co>
1 parent 3a6d6d9 commit 415485c

1 file changed

Lines changed: 86 additions & 12 deletions

File tree

testing/integration/ess/diagnostics_test.go

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"fmt"
1616
"io"
1717
"io/fs"
18+
"net/http"
19+
"net/http/httptest"
1820
"os"
1921
"path"
2022
"path/filepath"
@@ -371,7 +373,14 @@ func TestBeatDiagnostics(t *testing.T) {
371373

372374
esURL := integration.StartMockES(t, 0, 0, 0, 0)
373375

374-
configTemplate := `
376+
// Mock HTTP server used by the httpjson tracer test case.
377+
httpjsonServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
378+
w.Header().Set("Content-Type", "application/json")
379+
fmt.Fprintln(w, `{"message":"hello"}`)
380+
}))
381+
t.Cleanup(httpjsonServer.Close)
382+
383+
fileStreamConfigTemplate := `
375384
inputs:
376385
- id: filestream-filebeat
377386
type: filestream
@@ -390,34 +399,67 @@ agent.monitoring.enabled: {{ .MonitoringEnabled }}
390399
agent.internal.runtime.filebeat.filestream: {{ .Runtime }}
391400
`
392401

393-
var filebeatSetup = map[string]integrationtest.ComponentState{
402+
// httpjsonTracerConfigTemplate runs httpjson inside the filebeat OTel receiver with request
403+
// tracing enabled. The receiver writes trace files under {versionedHome}/components/logs/httpjson/,
404+
// which is the path the diagnostic bundle fix now collects.
405+
httpjsonTracerConfigTemplate := `
406+
inputs:
407+
- id: httpjson-trace-test
408+
type: httpjson
409+
request.url: {{ .MockServerURL }}
410+
interval: 1s
411+
request.tracer.filename: httpjson/logs/http-request-trace-*.ndjson
412+
use_output: default
413+
outputs:
414+
default:
415+
type: elasticsearch
416+
hosts: [{{ .ESHost }}]
417+
api_key: placeholder
418+
agent.monitoring.enabled: false
419+
agent.internal.runtime.filebeat.httpjson: otel
420+
`
421+
422+
fileStreamAgentCompState := map[string]integrationtest.ComponentState{
394423
"filestream-default": {
395424
State: integrationtest.NewClientState(client.Healthy),
425+
Units: map[integrationtest.ComponentUnitKey]integrationtest.ComponentUnitState{
426+
integrationtest.ComponentUnitKey{UnitType: client.UnitTypeOutput, UnitID: "filestream-default"}: {
427+
State: integrationtest.NewClientState(client.Healthy),
428+
},
429+
integrationtest.ComponentUnitKey{UnitType: client.UnitTypeInput, UnitID: "filestream-default-filestream-filebeat"}: {
430+
State: integrationtest.NewClientState(client.Healthy),
431+
},
432+
},
396433
},
397434
}
398435

399-
ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
400-
defer cancel()
401-
expectedComponentState := map[string]integrationtest.ComponentState{
402-
"filestream-default": {
436+
httpjsonAgentCompState := map[string]integrationtest.ComponentState{
437+
"httpjson-default": {
403438
State: integrationtest.NewClientState(client.Healthy),
404439
Units: map[integrationtest.ComponentUnitKey]integrationtest.ComponentUnitState{
405-
integrationtest.ComponentUnitKey{UnitType: client.UnitTypeOutput, UnitID: "filestream-default"}: {
440+
integrationtest.ComponentUnitKey{UnitType: client.UnitTypeOutput, UnitID: "httpjson-default"}: {
406441
State: integrationtest.NewClientState(client.Healthy),
407442
},
408-
integrationtest.ComponentUnitKey{UnitType: client.UnitTypeInput, UnitID: "filestream-default-filestream-filebeat"}: {
443+
integrationtest.ComponentUnitKey{UnitType: client.UnitTypeInput, UnitID: "httpjson-default-httpjson-trace-test"}: {
409444
State: integrationtest.NewClientState(client.Healthy),
410445
},
411446
},
412447
},
413448
}
449+
450+
ctx, cancel := testcontext.WithDeadline(t, context.Background(), time.Now().Add(10*time.Minute))
451+
defer cancel()
414452
expectedAgentState := integrationtest.NewClientState(client.Healthy)
415453

416454
testCases := []struct {
417455
name string
418456
runtime string
419457
monitoringEnabled bool
420458
expectedCompDiagnosticsFiles []string
459+
agentCompState map[string]integrationtest.ComponentState
460+
diagCompSetup map[string]integrationtest.ComponentState
461+
configTemplate string
462+
extraPatterns []filePattern
421463
}{
422464
{
423465
name: "filebeat process",
@@ -430,6 +472,11 @@ agent.internal.runtime.filebeat.filestream: {{ .Runtime }}
430472
"beat-rendered-config.yml",
431473
"global_processors.txt",
432474
),
475+
agentCompState: fileStreamAgentCompState,
476+
diagCompSetup: map[string]integrationtest.ComponentState{
477+
"filestream-default": {State: integrationtest.NewClientState(client.Healthy)},
478+
},
479+
configTemplate: fileStreamConfigTemplate,
433480
},
434481
{
435482
name: "filebeat receiver",
@@ -440,6 +487,32 @@ agent.internal.runtime.filebeat.filestream: {{ .Runtime }}
440487
"beat_metrics.json",
441488
"input_metrics.json",
442489
},
490+
agentCompState: fileStreamAgentCompState,
491+
diagCompSetup: map[string]integrationtest.ComponentState{
492+
"filestream-default": {State: integrationtest.NewClientState(client.Healthy)},
493+
},
494+
configTemplate: fileStreamConfigTemplate,
495+
},
496+
{
497+
// Verifies that beat receiver trace logs written to {versionedHome}/components/logs/
498+
// are included in the diagnostic bundle under logs/<commit>/httpjson/.
499+
name: "filebeat receiver with trace logs",
500+
runtime: "otel",
501+
expectedCompDiagnosticsFiles: []string{
502+
"beat_metrics.json",
503+
"input_metrics.json",
504+
},
505+
agentCompState: httpjsonAgentCompState,
506+
diagCompSetup: map[string]integrationtest.ComponentState{
507+
"httpjson-default": {State: integrationtest.NewClientState(client.Healthy)},
508+
},
509+
configTemplate: httpjsonTracerConfigTemplate,
510+
extraPatterns: []filePattern{
511+
{
512+
pattern: path.Join("logs", "*", "httpjson", "http-request-trace-*.ndjson"),
513+
optional: false,
514+
},
515+
},
443516
},
444517
}
445518

@@ -458,14 +531,15 @@ agent.internal.runtime.filebeat.filestream: {{ .Runtime }}
458531

459532
var configBuffer bytes.Buffer
460533
require.NoError(t,
461-
template.Must(template.New("config").Parse(configTemplate)).Execute(&configBuffer, map[string]any{
534+
template.Must(template.New("config").Parse(tc.configTemplate)).Execute(&configBuffer, map[string]any{
462535
"Runtime": tc.runtime,
463536
"InputFile": inputFilePath,
464537
"ESHost": esURL.Host,
465538
"MonitoringEnabled": tc.monitoringEnabled,
539+
"MockServerURL": httpjsonServer.URL,
466540
}))
467541
expDiagFiles := append([]string{}, diagnosticsFiles...)
468-
var extraPatterns []filePattern
542+
extraPatterns := append([]filePattern{}, tc.extraPatterns...)
469543
if tc.runtime == "otel" {
470544
// EDOT adds these extra files.
471545
// TestBeatDiagnostics is quite strict about what it expects to see in the archive.
@@ -490,8 +564,8 @@ agent.internal.runtime.filebeat.filestream: {{ .Runtime }}
490564
err = f.Run(ctx, integrationtest.State{
491565
Configure: configBuffer.String(),
492566
AgentState: expectedAgentState,
493-
Components: expectedComponentState,
494-
After: testDiagnosticsFactory(t, filebeatSetup, expDiagFiles, tc.expectedCompDiagnosticsFiles, f, []string{"diagnostics", "collect"}, extraPatterns...),
567+
Components: tc.agentCompState,
568+
After: testDiagnosticsFactory(t, tc.diagCompSetup, expDiagFiles, tc.expectedCompDiagnosticsFiles, f, []string{"diagnostics", "collect"}, extraPatterns...),
495569
})
496570
assert.NoError(t, err)
497571
})

0 commit comments

Comments
 (0)