Skip to content

Commit

Permalink
Return the current agent state on Fixture.IsHealthy method (#3982) (#…
Browse files Browse the repository at this point in the history
…4040)

(cherry picked from commit 58ef76d)

Co-authored-by: Anderson Queiroz <anderson.queiroz@elastic.co>
  • Loading branch information
mergify[bot] and AndersonQ authored Jan 9, 2024
1 parent 4587217 commit ddc453a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
19 changes: 13 additions & 6 deletions pkg/testing/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,16 +629,23 @@ func (f *Fixture) ExecVersion(ctx context.Context, opts ...process.CmdOption) (A
return version, err
}

// IsHealthy returns if the prepared Elastic Agent reports itself as healthy.
// It returns false, err if it cannot determine the state of the agent.
// It should work with any 8.6+ agent
func (f *Fixture) IsHealthy(ctx context.Context, opts ...process.CmdOption) (bool, error) {
// IsHealthy checks whether the prepared Elastic Agent reports itself as healthy.
// It returns an error if either the reported state isn't healthy or if it fails
// to fetch the current state. If the status is successfully fetched, but it
// isn't healthy, the error will contain the reported status.
// This function is compatible with any Elastic Agent version 8.6 or later.
func (f *Fixture) IsHealthy(ctx context.Context, opts ...process.CmdOption) error {
status, err := f.ExecStatus(ctx, opts...)
if err != nil {
return false, fmt.Errorf("agent status returned and error: %w", err)
return fmt.Errorf("agent status returned and error: %w", err)
}

if status.State != int(cproto.State_HEALTHY) {
return fmt.Errorf("agent isn't healthy, current status: %s",
client.State(status.State))
}

return status.State == int(cproto.State_HEALTHY), nil
return nil
}

// EnsurePrepared ensures that the fixture has been prepared.
Expand Down
5 changes: 2 additions & 3 deletions testing/integration/container_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,14 @@ func TestContainerCMD(t *testing.T) {
}

require.Eventuallyf(t, func() bool {
var healthy bool
// This will return errors until it connects to the agent,
// they're mostly noise because until the agent starts running
// we will get connection errors. If the test fails
// the agent logs will be present in the error message
// which should help to explain why the agent was not
// healthy.
healthy, err = agentFixture.IsHealthy(ctx)
return healthy
err = agentFixture.IsHealthy(ctx)
return err == nil
},
5*time.Minute, time.Second,
"Elastic-Agent did not report healthy. Agent status error: \"%v\", Agent logs\n%s",
Expand Down

0 comments on commit ddc453a

Please sign in to comment.