From 88446af7ac562da0b53b4ca06141e3a1c9aa3954 Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Tue, 25 Nov 2025 12:46:46 +0100 Subject: [PATCH 1/9] test(logging): remove observeroutput we're using local loggers now and the observer output is unused --- internal/beatcmd/logging_test.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/internal/beatcmd/logging_test.go b/internal/beatcmd/logging_test.go index 8cf035a4b47..153637165ea 100644 --- a/internal/beatcmd/logging_test.go +++ b/internal/beatcmd/logging_test.go @@ -27,18 +27,6 @@ import ( "github.com/elastic/elastic-agent-libs/logp" ) -func init() { - // Configure tests to log at debug level, and to send - // logs to logp.ObserverLogs(). It is important to not - // log to files by default in tests, as logp will keep - // the files open and prevent temporary directories from - // being removed on Windows. - logOptions = append(logOptions, - logp.ToObserverOutput(), - logp.WithLevel(logp.DebugLevel), - ) -} - var environments = []logp.Environment{ logp.DefaultEnvironment, logp.SystemdEnvironment, From b59a88823b2bc2c0c21d903f0c0cef53b4d08450 Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Tue, 25 Nov 2025 13:25:18 +0100 Subject: [PATCH 2/9] test(beatcmd): use testing logger --- internal/beatcmd/beat.go | 8 +++++++- internal/beatcmd/beat_test.go | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/beatcmd/beat.go b/internal/beatcmd/beat.go index af127cf2794..8fa9b651501 100644 --- a/internal/beatcmd/beat.go +++ b/internal/beatcmd/beat.go @@ -97,6 +97,9 @@ type BeatParams struct { // ElasticLicensed indicates whether this build of APM Server // is licensed with the Elastic License v2. ElasticLicensed bool + + // Logger holds the logger used by the runner + Logger *logp.Logger } // NewBeat creates a new Beat. @@ -144,6 +147,7 @@ func NewBeat(args BeatParams) (*Beat, error) { Hostname: hostname, StartTime: time.Now(), EphemeralID: ephemeralID, + Logger: args.Logger, }, Keystore: keystore, Config: &beat.BeatConfig{Output: cfg.Output}, @@ -170,7 +174,9 @@ func (b *Beat) init() error { if err := configureLogging(b.Config); err != nil { return fmt.Errorf("failed to configure logging: %w", err) } - b.Beat.Info.Logger = logp.NewLogger("") + if b.Info.Logger == nil { + b.Info.Logger = logp.NewLogger("") + } // log paths values to help with troubleshooting b.Info.Logger.Infof("%s", paths.Paths.String()) diff --git a/internal/beatcmd/beat_test.go b/internal/beatcmd/beat_test.go index cb9b049a9c9..0267bf7afc8 100644 --- a/internal/beatcmd/beat_test.go +++ b/internal/beatcmd/beat_test.go @@ -663,6 +663,7 @@ func newBeat(t testing.TB, configYAML string, newRunner NewRunnerFunc) *Beat { beat, err := NewBeat(BeatParams{ NewRunner: newRunner, ElasticLicensed: true, + Logger: logptest.NewTestingLogger(t, ""), }) require.NoError(t, err) return beat From 8a4dd507a2f370ce1139b11777337cca9d2c6917 Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Tue, 25 Nov 2025 14:35:35 +0100 Subject: [PATCH 3/9] test: use testing logger --- internal/beatcmd/locker_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/beatcmd/locker_test.go b/internal/beatcmd/locker_test.go index e1bb02b0bf7..142addc1efa 100644 --- a/internal/beatcmd/locker_test.go +++ b/internal/beatcmd/locker_test.go @@ -46,13 +46,12 @@ func TestLocker(t *testing.T) { // Create another Beat using the same configuration and data directory; // its Run method should fail to acquire the lock while beat1 is running. - beat2, err := NewBeat(BeatParams{ - NewRunner: func(RunnerParams) (Runner, error) { + beat2 := newBeat(t, "", func(rp RunnerParams) (Runner, error) { + return runnerFunc(func(ctx context.Context) error { panic("should not be called") - }, + }), nil }) - require.NoError(t, err) - err = beat2.Run(context.Background()) + err := beat2.Run(context.Background()) require.ErrorIs(t, err, ErrAlreadyLocked) assert.NoError(t, stopBeat1()) From 168fbd5557c424f7cdfb6c94e109fabceb50a810 Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Tue, 25 Nov 2025 14:41:18 +0100 Subject: [PATCH 4/9] Update locker_test.go --- internal/beatcmd/locker_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/beatcmd/locker_test.go b/internal/beatcmd/locker_test.go index 142addc1efa..db495919961 100644 --- a/internal/beatcmd/locker_test.go +++ b/internal/beatcmd/locker_test.go @@ -47,9 +47,7 @@ func TestLocker(t *testing.T) { // Create another Beat using the same configuration and data directory; // its Run method should fail to acquire the lock while beat1 is running. beat2 := newBeat(t, "", func(rp RunnerParams) (Runner, error) { - return runnerFunc(func(ctx context.Context) error { panic("should not be called") - }), nil }) err := beat2.Run(context.Background()) require.ErrorIs(t, err, ErrAlreadyLocked) From 4b7c51f2e2f77b87c127fed429ddbb6a84d8b9c0 Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Tue, 25 Nov 2025 14:41:37 +0100 Subject: [PATCH 5/9] Update locker_test.go --- internal/beatcmd/locker_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/beatcmd/locker_test.go b/internal/beatcmd/locker_test.go index db495919961..e4d2cf2d425 100644 --- a/internal/beatcmd/locker_test.go +++ b/internal/beatcmd/locker_test.go @@ -46,7 +46,7 @@ func TestLocker(t *testing.T) { // Create another Beat using the same configuration and data directory; // its Run method should fail to acquire the lock while beat1 is running. - beat2 := newBeat(t, "", func(rp RunnerParams) (Runner, error) { + beat2 := newBeat(t, "", func(RunnerParams) (Runner, error) { panic("should not be called") }) err := beat2.Run(context.Background()) From 7c8683144b89f49554d11b00917ace9e4a9ab7cd Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Tue, 25 Nov 2025 14:44:48 +0100 Subject: [PATCH 6/9] Update locker_test.go --- internal/beatcmd/locker_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/beatcmd/locker_test.go b/internal/beatcmd/locker_test.go index e4d2cf2d425..e13ca59a4e9 100644 --- a/internal/beatcmd/locker_test.go +++ b/internal/beatcmd/locker_test.go @@ -47,7 +47,7 @@ func TestLocker(t *testing.T) { // Create another Beat using the same configuration and data directory; // its Run method should fail to acquire the lock while beat1 is running. beat2 := newBeat(t, "", func(RunnerParams) (Runner, error) { - panic("should not be called") + panic("should not be called") }) err := beat2.Run(context.Background()) require.ErrorIs(t, err, ErrAlreadyLocked) From 84726b51fa3cbab3c70aae0d3d0cac03f5645229 Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Tue, 25 Nov 2025 15:10:57 +0100 Subject: [PATCH 7/9] Update locker_test.go --- internal/beatcmd/locker_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/beatcmd/locker_test.go b/internal/beatcmd/locker_test.go index e13ca59a4e9..5989e7ee4dd 100644 --- a/internal/beatcmd/locker_test.go +++ b/internal/beatcmd/locker_test.go @@ -46,7 +46,7 @@ func TestLocker(t *testing.T) { // Create another Beat using the same configuration and data directory; // its Run method should fail to acquire the lock while beat1 is running. - beat2 := newBeat(t, "", func(RunnerParams) (Runner, error) { + beat2 := newBeat(t, `output.console.enabled: true`, func(RunnerParams) (Runner, error) { panic("should not be called") }) err := beat2.Run(context.Background()) From b235485150c85045ecda0cbf915d69f0c268dda5 Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Tue, 25 Nov 2025 15:30:00 +0100 Subject: [PATCH 8/9] test(TestLocker): pass path.home to beat2 and rely on test infra replace panic with t.Fatal pass path.home to ensure we're using the same home without relying on global variables --- internal/beatcmd/config_test.go | 11 ++++++++--- internal/beatcmd/locker_test.go | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/internal/beatcmd/config_test.go b/internal/beatcmd/config_test.go index 8d8c96a8f76..7c62c4e7025 100644 --- a/internal/beatcmd/config_test.go +++ b/internal/beatcmd/config_test.go @@ -92,8 +92,13 @@ processors: } func initCfgfile(t testing.TB, content string) (home string) { - home = t.TempDir() - content += "\npath.home: " + home + _, after, _ := strings.Cut(content, "\npath.home: ") + home, _, _ = strings.Cut(after, "\n") + + if home == "" { + home = t.TempDir() + content += "\npath.home: " + home + } origConfigPath := cfgfile.GetPathConfig() origConfigFile := strings.TrimSuffix(cfgfile.GetDefaultCfgfile(), ".yml") @@ -103,7 +108,7 @@ func initCfgfile(t testing.TB, content string) (home string) { }) configFile := filepath.Join(home, "testing.yml") - err := os.WriteFile(configFile, []byte(strings.TrimSpace(content)), 0644) + err := os.WriteFile(configFile, []byte(strings.TrimSpace(content)), 0o644) require.NoError(t, err) cfgfile.SetConfigPath(home) cfgfile.ChangeDefaultCfgfileFlag("testing") diff --git a/internal/beatcmd/locker_test.go b/internal/beatcmd/locker_test.go index 5989e7ee4dd..dabefe3a5a1 100644 --- a/internal/beatcmd/locker_test.go +++ b/internal/beatcmd/locker_test.go @@ -29,8 +29,12 @@ import ( ) func TestLocker(t *testing.T) { + configYAML := "output.console.enabled: true" + home := t.TempDir() + configYAML += "\npath.home: " + home + running := make(chan struct{}) - beat1 := newBeat(t, `output.console.enabled: true`, func(RunnerParams) (Runner, error) { + beat1 := newBeat(t, configYAML, func(RunnerParams) (Runner, error) { return runnerFunc(func(ctx context.Context) error { close(running) <-ctx.Done() @@ -46,8 +50,9 @@ func TestLocker(t *testing.T) { // Create another Beat using the same configuration and data directory; // its Run method should fail to acquire the lock while beat1 is running. - beat2 := newBeat(t, `output.console.enabled: true`, func(RunnerParams) (Runner, error) { - panic("should not be called") + beat2 := newBeat(t, configYAML, func(RunnerParams) (Runner, error) { + t.Fatal("should not be called") + return nil, nil }) err := beat2.Run(context.Background()) require.ErrorIs(t, err, ErrAlreadyLocked) From 8df8a2001d21eba0bb279e1381f469f72c03154c Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Tue, 25 Nov 2025 15:58:17 +0100 Subject: [PATCH 9/9] Update config_test.go --- internal/beatcmd/config_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/beatcmd/config_test.go b/internal/beatcmd/config_test.go index 7c62c4e7025..fa31752c11f 100644 --- a/internal/beatcmd/config_test.go +++ b/internal/beatcmd/config_test.go @@ -108,7 +108,7 @@ func initCfgfile(t testing.TB, content string) (home string) { }) configFile := filepath.Join(home, "testing.yml") - err := os.WriteFile(configFile, []byte(strings.TrimSpace(content)), 0o644) + err := os.WriteFile(configFile, []byte(strings.TrimSpace(content)), 0644) require.NoError(t, err) cfgfile.SetConfigPath(home) cfgfile.ChangeDefaultCfgfileFlag("testing")