Skip to content

Commit 509b1cc

Browse files
authored
[tests][integration] fix monitoring test cases (elastic#5208)
* chore: fix monitoring test cases * fix: update long running tests * fix: change ExecStatus and return nil * fix: update docstring, non-empty * fix: reword * fix: nit * fix: empty line * fix: join errors. * temproray logs to examine CI * Revert "temproray logs to examine CI" remove logs as the test failure has been identified This reverts commit dc392b3. * fix: update condition
1 parent e0d77f0 commit 509b1cc

4 files changed

+28
-18
lines changed

pkg/testing/fixture.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -709,26 +709,20 @@ func (e *ExecErr) Unwrap() error {
709709
// ExecStatus executes the status subcommand on the prepared Elastic Agent binary.
710710
// It returns the parsed output and the error from the execution. Keep in mind
711711
// the agent exits with status 1 if it's unhealthy, but it still outputs the
712-
// status successfully. Therefore, a non-empty AgentStatusOutput is valid
713-
// regardless of the error. An empty AgentStatusOutput and non nil error
714-
// means the output could not be parsed. Use AgentStatusOutput.IsZero() to
715-
// determine if the returned AgentStatusOutput is empty or not.
712+
// status successfully. An empty AgentStatusOutput and non nil error
713+
// means the output could not be parsed.
714+
// As long as we get some output, we don't return any error.
716715
// It should work with any 8.6+ agent
717716
func (f *Fixture) ExecStatus(ctx context.Context, opts ...process.CmdOption) (AgentStatusOutput, error) {
718717
out, err := f.Exec(ctx, []string{"status", "--output", "json"}, opts...)
719718
status := AgentStatusOutput{}
720719
if uerr := json.Unmarshal(out, &status); uerr != nil {
721720
return AgentStatusOutput{},
722-
fmt.Errorf("could not unmarshal agent status output: %w",
723-
errors.Join(&ExecErr{
724-
err: err,
725-
Output: out,
726-
}, uerr))
721+
fmt.Errorf("could not unmarshal agent status output: %w", errors.Join(uerr, err))
722+
} else if status.IsZero() {
723+
return status, fmt.Errorf("agent status output is empty: %w", err)
727724
}
728725

729-
if err != nil {
730-
return status, fmt.Errorf("error running command (output: %s): %w", string(out), err)
731-
}
732726
return status, nil
733727
}
734728

@@ -1243,7 +1237,7 @@ type AgentStatusOutput struct {
12431237
}
12441238

12451239
func (aso *AgentStatusOutput) IsZero() bool {
1246-
return aso.Info.ID == ""
1240+
return aso.Info.ID == "" && aso.Message == "" && aso.Info.Version == ""
12471241
}
12481242

12491243
type AgentInspectOutput struct {

testing/integration/agent_long_running_leak_test.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,16 @@ func (runner *ExtendedRunner) CheckHealthAtStartup(ctx context.Context) {
210210
require.Eventually(runner.T(), func() bool {
211211
allHealthy := true
212212
status, err := runner.agentFixture.ExecStatus(ctx)
213+
if err != nil {
214+
runner.T().Logf("agent status returned an error: %v", err)
215+
return false
216+
}
213217

214218
apacheMatch := "logfile-apache"
215219
foundApache := false
216220
systemMatch := "system/metrics"
217221
foundSystem := false
218222

219-
require.NoError(runner.T(), err)
220223
for _, comp := range status.Components {
221224
// make sure the components include the expected integrations
222225
for _, v := range comp.Units {
@@ -270,7 +273,10 @@ func (gm *goroutinesMonitor) Init(ctx context.Context, t *testing.T, fixture *at
270273
paths.SetTop("/opt/Elastic/Agent")
271274
// fetch the unit ID of the component, use that to generate the path to the unix socket
272275
status, err := fixture.ExecStatus(ctx)
273-
require.NoError(t, err)
276+
if err != nil {
277+
t.Logf("agent status returned an error: %v", err)
278+
}
279+
274280
for _, comp := range status.Components {
275281
unitId := comp.ID
276282
socketPath := utils.SocketURLWithFallback(unitId, paths.TempDir())
@@ -352,7 +358,10 @@ func (handleMon *handleMonitor) Init(ctx context.Context, t *testing.T, fixture
352358
// so separately fetch the PIDs
353359
pidInStatusMessageRegex := regexp.MustCompile(`[\d]+`)
354360
status, err := fixture.ExecStatus(ctx)
355-
require.NoError(t, err)
361+
if err != nil {
362+
t.Logf("agent status returned an error: %v", err)
363+
}
364+
356365
for _, comp := range status.Components {
357366
pidStr := pidInStatusMessageRegex.FindString(comp.Message)
358367
pid, err := strconv.ParseInt(pidStr, 10, 64)

testing/integration/monitoring_probe_preserve_text_cfg_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ inputs:
5454
- filesystem
5555
data_stream.dataset: system.filesystem
5656
agent.monitoring:
57+
metrics_period: 1s
5758
http:
5859
enabled: true
5960
port: 6791
@@ -188,8 +189,11 @@ func (runner *MonitoringTextRunner) AllComponentsHealthy(ctx context.Context) {
188189
require.Eventually(runner.T(), func() bool {
189190
allHealthy := true
190191
status, err := runner.agentFixture.ExecStatus(ctx)
192+
if err != nil {
193+
runner.T().Logf("agent status returned an error: %v", err)
194+
return false
195+
}
191196

192-
require.NoError(runner.T(), err)
193197
for _, comp := range status.Components {
194198
runner.T().Logf("component state: %s", comp.Message)
195199
if comp.State != int(cproto.State_HEALTHY) {

testing/integration/monitoring_probe_reload_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,11 @@ func (runner *MonitoringRunner) AllComponentsHealthy(ctx context.Context) {
166166
require.Eventually(runner.T(), func() bool {
167167
allHealthy := true
168168
status, err := runner.agentFixture.ExecStatus(ctx)
169+
if err != nil {
170+
runner.T().Logf("agent status returned an error: %v", err)
171+
return false
172+
}
169173

170-
require.NoError(runner.T(), err)
171174
for _, comp := range status.Components {
172175
runner.T().Logf("component state: %s", comp.Message)
173176
if comp.State != int(cproto.State_HEALTHY) {

0 commit comments

Comments
 (0)