Skip to content

Commit a6f9393

Browse files
Make all beats receiver integration tests use standard tools (#10717) (#10719)
This way, the tests generate diagnostics and ensure agent is not running after test failure. (cherry picked from commit 020adf8) Co-authored-by: Mikołaj Świątek <[email protected]>
1 parent 1918f6a commit a6f9393

File tree

1 file changed

+11
-54
lines changed

1 file changed

+11
-54
lines changed

testing/integration/ess/beat_receivers_test.go

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"io"
1515
"net/http"
1616
"os"
17-
"os/exec"
1817
"runtime"
1918
"strings"
2019
"testing"
@@ -513,17 +512,17 @@ outputs:
513512
ctx, cancel := testcontext.WithDeadline(t, t.Context(), time.Now().Add(5*time.Minute))
514513
defer cancel()
515514

516-
fixture, cmd, output := prepareAgentCmd(t, ctx, configContents)
515+
// set up a standalone agent
516+
fixture, err := define.NewFixtureFromLocalBuild(t, define.Version())
517+
require.NoError(t, err)
517518

518-
err = cmd.Start()
519+
err = fixture.Prepare(ctx)
520+
require.NoError(t, err)
521+
err = fixture.Configure(ctx, configContents)
519522
require.NoError(t, err)
520523

521-
t.Cleanup(func() {
522-
if t.Failed() {
523-
t.Log("Elastic-Agent output:")
524-
t.Log(output.String())
525-
}
526-
})
524+
output, err := fixture.Install(ctx, &atesting.InstallOpts{Privileged: true, Force: true})
525+
require.NoError(t, err, "failed to install agent: %s", output)
527526

528527
require.Eventually(t, func() bool {
529528
err = fixture.IsHealthy(ctx)
@@ -568,9 +567,6 @@ outputs:
568567
30*time.Second, 1*time.Second,
569568
"Expected to find at least one document for metricset %s in index %s and runtime %q, got 0", mset, index, tt.runtimeExperimental)
570569
}
571-
572-
cancel()
573-
cmd.Wait()
574570
})
575571
}
576572

@@ -963,27 +959,6 @@ func getBeatStartLogRecords(logs string) []map[string]any {
963959
return logRecords
964960
}
965961

966-
func prepareAgentCmd(t *testing.T, ctx context.Context, config []byte) (*atesting.Fixture, *exec.Cmd, *strings.Builder) {
967-
// set up a standalone agent
968-
fixture, err := define.NewFixtureFromLocalBuild(t, define.Version())
969-
require.NoError(t, err)
970-
971-
err = fixture.Prepare(ctx)
972-
require.NoError(t, err)
973-
err = fixture.Configure(ctx, config)
974-
require.NoError(t, err)
975-
976-
cmd, err := fixture.PrepareAgentCommand(ctx, nil)
977-
require.NoError(t, err)
978-
cmd.WaitDelay = 1 * time.Second
979-
980-
var output strings.Builder
981-
cmd.Stderr = &output
982-
cmd.Stdout = &output
983-
984-
return fixture, cmd, &output
985-
}
986-
987962
func genIgnoredFields(goos string) []string {
988963
switch goos {
989964
case "windows":
@@ -1009,6 +984,7 @@ func TestSensitiveLogsESExporter(t *testing.T) {
1009984
info := define.Require(t, define.Requirements{
1010985
Group: integration.Default,
1011986
Local: true,
987+
Sudo: true,
1012988
OS: []define.OS{
1013989
{Type: define.Windows},
1014990
{Type: define.Linux},
@@ -1097,32 +1073,13 @@ agent.logging.stderr: true
10971073
err = fixture.Configure(ctx, configBuffer.Bytes())
10981074
require.NoError(t, err)
10991075

1100-
cmd, err := fixture.PrepareAgentCommand(ctx, nil)
1101-
require.NoError(t, err, "cannot prepare Elastic-Agent command: %w", err)
1102-
11031076
err = setStrictMapping(info.ESClient, index)
11041077
require.NoError(t, err, "could not set strict mapping due to %v", err)
11051078

11061079
timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
11071080

1108-
output := strings.Builder{}
1109-
cmd.Stderr = &output
1110-
cmd.Stdout = &output
1111-
1112-
err = cmd.Start()
1113-
require.NoError(t, err)
1114-
1115-
// Make sure the Elastic-Agent process is not running before
1116-
// exiting the test
1117-
t.Cleanup(func() {
1118-
// Ignore the error because we cancelled the context,
1119-
// and that always returns an error
1120-
_ = cmd.Wait()
1121-
if t.Failed() {
1122-
t.Log("Elastic-Agent output:")
1123-
t.Log(output.String())
1124-
}
1125-
})
1081+
output, err := fixture.Install(ctx, &atesting.InstallOpts{Privileged: true, Force: true})
1082+
require.NoError(t, err, "Elastic Agent installation failed with error: %w, output: %s", err, string(output))
11261083

11271084
require.EventuallyWithT(t, func(collect *assert.CollectT) {
11281085
var statusErr error

0 commit comments

Comments
 (0)