@@ -16,6 +16,7 @@ import (
1616 "strings"
1717 "sync"
1818 "testing"
19+ "text/template"
1920 "time"
2021
2122 "github.com/stretchr/testify/require"
@@ -126,7 +127,7 @@ func TestOtelFileProcessing(t *testing.T) {
126127 // otel mode should be detected automatically
127128 tempDir := t .TempDir ()
128129 cfgFilePath := filepath .Join (tempDir , "otel.yml" )
129- require .NoError (t , os .WriteFile (cfgFilePath , []byte (fileProcessingConfig ), 0600 ))
130+ require .NoError (t , os .WriteFile (cfgFilePath , []byte (fileProcessingConfig ), 0o600 ))
130131
131132 fixture , err := define .NewFixtureFromLocalBuild (t , define .Version (), aTesting .WithAdditionalArgs ([]string {"--config" , cfgFilePath }))
132133 require .NoError (t , err )
@@ -183,7 +184,7 @@ func TestOtelFileProcessing(t *testing.T) {
183184
184185func validateCommandIsWorking (t * testing.T , ctx context.Context , fixture * aTesting.Fixture , tempDir string ) {
185186 cfgFilePath := filepath .Join (tempDir , "otel-valid.yml" )
186- require .NoError (t , os .WriteFile (cfgFilePath , []byte (fileProcessingConfig ), 0600 ))
187+ require .NoError (t , os .WriteFile (cfgFilePath , []byte (fileProcessingConfig ), 0o600 ))
187188
188189 // check `elastic-agent otel validate` command works for otel config
189190 cmd , err := fixture .PrepareAgentCommand (ctx , []string {"otel" , "validate" , "--config" , cfgFilePath })
@@ -199,7 +200,7 @@ func validateCommandIsWorking(t *testing.T, ctx context.Context, fixture *aTesti
199200
200201 // check `elastic-agent otel validate` command works for invalid otel config
201202 cfgFilePath = filepath .Join (tempDir , "otel-invalid.yml" )
202- require .NoError (t , os .WriteFile (cfgFilePath , []byte (fileInvalidOtelConfig ), 0600 ))
203+ require .NoError (t , os .WriteFile (cfgFilePath , []byte (fileInvalidOtelConfig ), 0o600 ))
203204
204205 out , err = fixture .Exec (ctx , []string {"otel" , "validate" , "--config" , cfgFilePath })
205206 require .Error (t , err )
@@ -276,7 +277,7 @@ func TestOtelLogsIngestion(t *testing.T) {
276277 logsIngestionConfig = strings .ReplaceAll (logsIngestionConfig , "{{.TestId}}" , testId )
277278
278279 cfgFilePath := filepath .Join (tempDir , "otel.yml" )
279- require .NoError (t , os .WriteFile (cfgFilePath , []byte (logsIngestionConfig ), 0600 ))
280+ require .NoError (t , os .WriteFile (cfgFilePath , []byte (logsIngestionConfig ), 0o600 ))
280281
281282 fixture , err := define .NewFixtureFromLocalBuild (t , define .Version (), aTesting .WithAdditionalArgs ([]string {"--config" , cfgFilePath }))
282283 require .NoError (t , err )
@@ -300,7 +301,7 @@ func TestOtelLogsIngestion(t *testing.T) {
300301
301302 // Write logs to input file.
302303 logsCount := 10_000
303- inputFile , err := os .OpenFile (inputFilePath , os .O_CREATE | os .O_WRONLY , 0600 )
304+ inputFile , err := os .OpenFile (inputFilePath , os .O_CREATE | os .O_WRONLY , 0o600 )
304305 require .NoError (t , err )
305306 for i := 0 ; i < logsCount ; i ++ {
306307 _ , err = fmt .Fprintf (inputFile , "This is a test log message %d\n " , i + 1 )
@@ -357,8 +358,8 @@ func TestOtelAPMIngestion(t *testing.T) {
357358 cfgFilePath := filepath .Join (tempDir , "otel.yml" )
358359 fileName := "content.log"
359360 apmConfig := fmt .Sprintf (apmOtelConfig , filepath .Join (tempDir , fileName ), testId )
360- require .NoError (t , os .WriteFile (cfgFilePath , []byte (apmConfig ), 0600 ))
361- require .NoError (t , os .WriteFile (filepath .Join (tempDir , fileName ), []byte {}, 0600 ))
361+ require .NoError (t , os .WriteFile (cfgFilePath , []byte (apmConfig ), 0o600 ))
362+ require .NoError (t , os .WriteFile (filepath .Join (tempDir , fileName ), []byte {}, 0o600 ))
362363
363364 fixture , err := define .NewFixtureFromLocalBuild (t , define .Version (), aTesting .WithAdditionalArgs ([]string {"--config" , cfgFilePath }))
364365 require .NoError (t , err )
@@ -426,7 +427,7 @@ func TestOtelAPMIngestion(t *testing.T) {
426427 )
427428 require .NoError (t , err , "APM not initialized" )
428429
429- require .NoError (t , os .WriteFile (filepath .Join (tempDir , fileName ), []byte (apmProcessingContent ), 0600 ))
430+ require .NoError (t , os .WriteFile (filepath .Join (tempDir , fileName ), []byte (apmProcessingContent ), 0o600 ))
430431
431432 // check index
432433 var hits int
@@ -436,10 +437,12 @@ func TestOtelAPMIngestion(t *testing.T) {
436437
437438 // apm mismatch or proper docs in ES
438439
439- watchLines := linesTrackMap ([]string {"This is a test error message" ,
440+ watchLines := linesTrackMap ([]string {
441+ "This is a test error message" ,
440442 "This is a test debug message 2" ,
441443 "This is a test debug message 3" ,
442- "This is a test debug message 4" })
444+ "This is a test debug message 4" ,
445+ })
443446
444447 // failed to get APM version mismatch in time
445448 // processing should be running
@@ -535,3 +538,108 @@ func mapAtLeastOneTrue(mm map[string]bool) bool {
535538
536539 return false
537540}
541+
542+ func TestFileBeatReceiver (t * testing.T ) {
543+ define .Require (t , define.Requirements {
544+ Group : Default ,
545+ Local : true ,
546+ OS : []define.OS {
547+ // {Type: define.Windows}, we don't support otel on Windows yet
548+ {Type : define .Linux },
549+ {Type : define .Darwin },
550+ },
551+ })
552+
553+ type otelConfigOptions struct {
554+ Message string
555+ Output string
556+ HomeDir string
557+ }
558+ testMessage := "supercalifragilisticexpialidocious"
559+ tmpDir := t .TempDir ()
560+ exporterOutputPath := filepath .Join (tmpDir , "output.json" )
561+ t .Cleanup (func () {
562+ if t .Failed () {
563+ contents , err := os .ReadFile (exporterOutputPath )
564+ if err != nil {
565+ t .Logf ("No exporter output file" )
566+ return
567+ }
568+ t .Logf ("Contents of exporter output file:\n %s\n " , string (contents ))
569+ }
570+ })
571+ otelConfigPath := filepath .Join (tmpDir , "otel.yml" )
572+ otelConfigTemplate := `receivers:
573+ filebeatreceiver:
574+ filebeat:
575+ inputs:
576+ - type: benchmark
577+ enabled: true
578+ count: 1
579+ message: {{.Message}}
580+ output:
581+ otelconsumer:
582+ logging:
583+ level: info
584+ selectors:
585+ - '*'
586+ path.home: {{.HomeDir}}
587+ exporters:
588+ file/no_rotation:
589+ path: {{.Output}}
590+ service:
591+ pipelines:
592+ logs:
593+ receivers: [filebeatreceiver]
594+ exporters: [file/no_rotation]
595+ `
596+
597+ var otelConfigBuffer bytes.Buffer
598+ require .NoError (t ,
599+ template .Must (template .New ("otelConfig" ).Parse (otelConfigTemplate )).Execute (& otelConfigBuffer ,
600+ otelConfigOptions {
601+ Message : testMessage ,
602+ Output : exporterOutputPath ,
603+ HomeDir : tmpDir ,
604+ }))
605+ require .NoError (t , os .WriteFile (otelConfigPath , otelConfigBuffer .Bytes (), 0o600 ))
606+ t .Cleanup (func () {
607+ if t .Failed () {
608+ contents , err := os .ReadFile (otelConfigPath )
609+ if err != nil {
610+ t .Logf ("no otel config file" )
611+ return
612+ }
613+ t .Logf ("Contents of otel config file:\n %s\n " , string (contents ))
614+ }
615+ })
616+ fixture , err := define .NewFixtureFromLocalBuild (t , define .Version (), aTesting .WithAdditionalArgs ([]string {"--config" , otelConfigPath }))
617+ require .NoError (t , err )
618+
619+ ctx , cancel := testcontext .WithDeadline (t , context .Background (), time .Now ().Add (5 * time .Minute ))
620+ defer cancel ()
621+ err = fixture .Prepare (ctx , fakeComponent )
622+ require .NoError (t , err )
623+
624+ var fixtureWg sync.WaitGroup
625+ fixtureWg .Add (1 )
626+ go func () {
627+ defer fixtureWg .Done ()
628+ err = fixture .RunOtelWithClient (ctx )
629+ }()
630+
631+ require .Eventually (t ,
632+ func () bool {
633+ content , err := os .ReadFile (exporterOutputPath )
634+ if err != nil || len (content ) == 0 {
635+ return false
636+ }
637+ return bytes .Contains (content , []byte (testMessage ))
638+ },
639+ 3 * time .Minute , 1 * time .Second ,
640+ fmt .Sprintf ("there should be exported logs by now" ))
641+
642+ cancel ()
643+ fixtureWg .Wait ()
644+ require .True (t , err == nil || err == context .Canceled || err == context .DeadlineExceeded , "Retrieved unexpected error: %s" , err .Error ())
645+ }
0 commit comments