@@ -694,8 +694,6 @@ func TestBeatsReceiverLogs(t *testing.T) {
694694 Stack : nil ,
695695 })
696696
697- t .Skip ("Skip this test as it's flaky. See https://github.com/elastic/elastic-agent/issues/9890" )
698-
699697 type configOptions struct {
700698 RuntimeExperimental string
701699 }
@@ -735,53 +733,45 @@ agent.monitoring.enabled: false
735733 ctx , cancel := testcontext .WithDeadline (t , t .Context (), time .Now ().Add (5 * time .Minute ))
736734 defer cancel ()
737735
738- // use a subcontext for the agent
739- agentProcessCtx , agentProcessCancel := context .WithCancel (ctx )
740- fixture , cmd , output := prepareAgentCmd (t , agentProcessCtx , processConfig )
736+ // set up a standalone agent
737+ fixture , err := define .NewFixtureFromLocalBuild (t , define .Version ())
738+ require .NoError (t , err )
739+
740+ err = fixture .Prepare (ctx )
741+ require .NoError (t , err )
742+ err = fixture .Configure (ctx , processConfig )
743+ require .NoError (t , err )
741744
742- require .NoError (t , cmd .Start ())
745+ output , err := fixture .Install (ctx , & atesting.InstallOpts {Privileged : true , Force : true })
746+ require .NoError (t , err , "failed to install agent: %s" , output )
743747
744748 require .EventuallyWithT (t , func (collect * assert.CollectT ) {
745749 var statusErr error
746- status , statusErr := fixture .ExecStatus (agentProcessCtx )
750+ status , statusErr := fixture .ExecStatus (ctx )
747751 assert .NoError (collect , statusErr )
748752 assertBeatsHealthy (collect , & status , component .ProcessRuntimeManager , 1 )
749753 return
750754 }, 1 * time .Minute , 1 * time .Second )
751755
752- agentProcessCancel ()
753- require .Error (t , cmd .Wait ())
754- processLogsString := output .String ()
755- output .Reset ()
756-
757- // use a subcontext for the agent
758- agentReceiverCtx , agentReceiverCancel := context .WithCancel (ctx )
759- fixture , cmd , output = prepareAgentCmd (t , agentReceiverCtx , receiverConfig )
760-
761- require .NoError (t , cmd .Start ())
762-
763- t .Cleanup (func () {
764- if t .Failed () {
765- t .Log ("Elastic-Agent output:" )
766- t .Log (output .String ())
767- }
768- })
756+ // change configuration and wait until the beats receiver is healthy
757+ err = fixture .Configure (ctx , receiverConfig )
758+ require .NoError (t , err )
769759
770760 require .EventuallyWithT (t , func (collect * assert.CollectT ) {
771761 var statusErr error
772- status , statusErr := fixture .ExecStatus (agentReceiverCtx )
762+ status , statusErr := fixture .ExecStatus (ctx )
773763 assert .NoError (collect , statusErr )
774764 assertBeatsHealthy (collect , & status , component .OtelRuntimeManager , 1 )
775765 return
776766 }, 1 * time .Minute , 1 * time .Second )
777- agentReceiverCancel ()
778- require .Error (t , cmd .Wait ())
779- receiverLogsString := output .String ()
780767
781- processLog := getBeatStartLogRecord (processLogsString )
782- assert .NotEmpty (t , processLog )
783- receiverLog := getBeatStartLogRecord (receiverLogsString )
784- assert .NotEmpty (t , receiverLog )
768+ logsBytes , err := fixture .Exec (ctx , []string {"logs" , "-n" , "1000" , "--exclude-events" })
769+ require .NoError (t , err , "failed to read logs: %v" , err )
770+
771+ beatStartLogs := getBeatStartLogRecords (string (logsBytes ))
772+
773+ require .Len (t , beatStartLogs , 2 , "expected to find one log line for each configuration" )
774+ processLog , receiverLog := beatStartLogs [0 ], beatStartLogs [1 ]
785775
786776 // Check that the process log is a subset of the receiver log
787777 for key , value := range processLog {
@@ -918,9 +908,10 @@ func assertBeatsHealthy(t *assert.CollectT, status *atesting.AgentStatusOutput,
918908 }
919909}
920910
921- // getBeatStartLogRecord returns the log record for the a particular log line emitted when the beat starts
911+ // getBeatStartLogRecords returns the log records for a particular log line emitted when the beat starts
922912// This log line is identical between beats processes and receivers, so it's a good point of comparison
923- func getBeatStartLogRecord (logs string ) map [string ]any {
913+ func getBeatStartLogRecords (logs string ) []map [string ]any {
914+ var logRecords []map [string ]any
924915 for _ , line := range strings .Split (logs , "\n " ) {
925916 line = strings .TrimSpace (line )
926917 if line == "" {
@@ -931,13 +922,11 @@ func getBeatStartLogRecord(logs string) map[string]any {
931922 continue
932923 }
933924
934- if message , ok := logRecord ["message" ].(string ); ! ok || ! strings .HasPrefix (message , "Beat name:" ) {
935- continue
925+ if message , ok := logRecord ["message" ].(string ); ok && strings .HasPrefix (message , "Beat name:" ) {
926+ logRecords = append ( logRecords , logRecord )
936927 }
937-
938- return logRecord
939928 }
940- return nil
929+ return logRecords
941930}
942931
943932func prepareAgentCmd (t * testing.T , ctx context.Context , config []byte ) (* atesting.Fixture , * exec.Cmd , * strings.Builder ) {
0 commit comments