Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the current agent state on Fixture.IsHealthy method #3982

Merged
merged 11 commits into from
Jan 8, 2024
19 changes: 13 additions & 6 deletions pkg/testing/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"encoding/json"
"errors"
"fmt"
"io/ioutil"

Check failure on line 12 in pkg/testing/fixture.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -724,16 +724,23 @@
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the reason for adding this, but this kinda changes the meaning of the error in this function. The error was more that it failed to get the actual health. Now this changes it to return an error when its not healthy, that is probably not was the caller is expecting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true, but it's also why I changed it docs. Besides there is just one usage of it, that is the test which needed this change. I could change the function signature, but I believe the doc change clarifies what is returned in the error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the way your changing it, would you be willing to change it to EnsureHealthy and remove the boolean? Doesn't seem like true/false is relevant to what this function is even doing at more, so just have it return error or nil.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does make sense, I just kept the name because it isn't ensuring anything, just checking the status

}

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
Loading