Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions filebeat/input/filestream/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

loginp "github.com/elastic/beats/v7/filebeat/input/filestream/internal/input-logfile"
conf "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/logp/logptest"
)

func TestConfigValidate(t *testing.T) {
Expand Down Expand Up @@ -229,13 +229,12 @@ id: unique-id-3
require.NoError(t, err, "could not create input configuration")
inputs = append(inputs, cfg)
}
err := logp.DevelopmentSetup(logp.ToObserverOutput())
require.NoError(t, err, "could not setup log for development")

err = ValidateInputIDs(inputs, logp.L())
logger, observedLogs := logptest.NewTestingLoggerWithObserver(t, "")
err := ValidateInputIDs(inputs, logger)
tc.assertErr(t, err)
if tc.assertLogs != nil {
tc.assertLogs(t, logp.ObserverLogs())
tc.assertLogs(t, observedLogs)
}
})
}
Expand Down
7 changes: 4 additions & 3 deletions filebeat/input/journald/pkg/journalctl/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/elastic/beats/v7/filebeat/input/journald/pkg/journalfield"
input "github.com/elastic/beats/v7/filebeat/input/v2"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/logp/logptest"
)

//go:embed testdata/corner-cases.json
Expand Down Expand Up @@ -71,7 +72,7 @@ func TestEventWithNonStringData(t *testing.T) {
var jdEvent []byte

func TestRestartsJournalctlOnError(t *testing.T) {
logp.DevelopmentSetup(logp.ToObserverOutput())
logger, observedLogs := logptest.NewTestingLoggerWithObserver(t, "")
ctx := context.Background()

mock := JctlMock{
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestRestartsJournalctlOnError(t *testing.T) {
return &mock, nil
}

reader, err := New(logp.L(), ctx, nil, nil, nil, journalfield.IncludeMatches{}, []int{}, SeekHead, "", 0, "", factory)
reader, err := New(logger, ctx, nil, nil, nil, journalfield.IncludeMatches{}, []int{}, SeekHead, "", 0, "", factory)
if err != nil {
t.Fatalf("cannot instantiate journalctl reader: %s", err)
}
Expand Down Expand Up @@ -129,7 +130,7 @@ func TestRestartsJournalctlOnError(t *testing.T) {
// - reader error: 'journalctl exited with code 42', restarting...
// - starting new mock journalclt ID: 2

logs := logp.ObserverLogs().TakeAll()
logs := observedLogs.TakeAll()
if len(logs) != 3 {
t.Fatalf("expecting 3 log lines from 'input.journald.reader.journalctl-runner', got %d", len(logs))
}
Expand Down
15 changes: 5 additions & 10 deletions heartbeat/monitors/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"

"github.com/elastic/beats/v7/heartbeat/eventext"
"github.com/elastic/beats/v7/heartbeat/monitors/wrappers/summarizer/jobsummary"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/logp/logptest"
"github.com/elastic/elastic-agent-libs/mapstr"
)

Expand All @@ -57,10 +56,8 @@ func generateFakeNetworkInfo() NetworkInfo {

func TestLogRun(t *testing.T) {
t.Run("should log the monitor completion", func(t *testing.T) {
core, observed := observer.New(zapcore.InfoLevel)
SetLogger(logp.NewLogger("t", zap.WrapCore(func(in zapcore.Core) zapcore.Core {
return zapcore.NewTee(in, core)
})))
log, observed := logptest.NewTestingLoggerWithObserver(t, "t")
SetLogger(log)

durationUs := int64(5000 * time.Microsecond)
steps := 1337
Expand Down Expand Up @@ -97,10 +94,8 @@ func TestLogRun(t *testing.T) {
})

t.Run("should log network information if available", func(t *testing.T) {
core, observed := observer.New(zapcore.InfoLevel)
SetLogger(logp.NewLogger("t", zap.WrapCore(func(in zapcore.Core) zapcore.Core {
return zapcore.NewTee(in, core)
})))
log, observed := logptest.NewTestingLoggerWithObserver(t, "t")
SetLogger(log)

durationUs := int64(5000 * time.Microsecond)
steps := 1337
Expand Down
8 changes: 3 additions & 5 deletions heartbeat/monitors/wrappers/wrappers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"

"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/logp/logptest"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/go-lookslike"
"github.com/elastic/go-lookslike/isdef"
Expand Down Expand Up @@ -78,10 +78,8 @@
t.Run(tt.name, func(t *testing.T) {
wrapped := WrapCommon(tt.jobs, tt.sFields, nil)

core, observedLogs := observer.New(zapcore.InfoLevel)
logger.SetLogger(logp.NewLogger("t", zap.WrapCore(func(in zapcore.Core) zapcore.Core {
return zapcore.NewTee(in, core)
})))
log, observedLogs := logptest.NewTestingLoggerWithObserver(t, "t")
logger.SetLogger(log)

results, err := jobs.ExecJobsAndConts(t, wrapped)
assert.NoError(t, err)
Expand Down Expand Up @@ -140,7 +138,7 @@
expectedMonitor := logger.MonitorRunInfo{
MonitorID: testMonFields.ID,
Type: testMonFields.Type,
Duration: durationUs.(int64),

Check failure on line 141 in heartbeat/monitors/wrappers/wrappers_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Error return value is not checked (errcheck)
Status: "up",
Attempt: 1,
NetworkInfo: logger.NetworkInfo{},
Expand Down
24 changes: 0 additions & 24 deletions libbeat/autodiscover/autodiscover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package autodiscover

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"path/filepath"
Expand All @@ -34,7 +33,6 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/cfgfile"
Expand Down Expand Up @@ -179,7 +177,6 @@ func TestNilAutodiscover(t *testing.T) {
}

func TestAutodiscover(t *testing.T) {
printDebugLogsOnFailure(t)
goroutines := resources.NewGoroutinesChecker()
defer goroutines.Check(t)

Expand Down Expand Up @@ -656,7 +653,6 @@ func TestAutodiscoverWithMutlipleEntries(t *testing.T) {
}

func TestAutodiscoverDebounce(t *testing.T) {
printDebugLogsOnFailure(t)
// Register mock autodiscover provider
busChan := make(chan bus.Bus, 1)
Registry = NewRegistry()
Expand Down Expand Up @@ -755,26 +751,6 @@ func TestAutodiscoverDebounce(t *testing.T) {
requireRunningRunners(t, autodiscover, 0)
}

func printDebugLogsOnFailure(t *testing.T) {
observed, zapLogs := observer.New(zapcore.DebugLevel)
_, err := logp.ConfigureWithCoreLocal(logp.Config{}, observed)
require.NoError(t, err)

t.Cleanup(func() {
if t.Failed() {
t.Logf("Debug Logs:\n")
for _, log := range zapLogs.TakeAll() {
data, err := json.Marshal(log)
if err != nil {
t.Errorf("failed encoding log as JSON: %s", err)
}
t.Logf("%s", string(data))
}
return
}
})
}

func requireRunningRunners(t *testing.T, autodiscover *Autodiscover, nRunners int) {
t.Helper()
nRunnersStr := strings.Builder{}
Expand Down
8 changes: 1 addition & 7 deletions libbeat/monitoring/report/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"

"github.com/elastic/beats/v7/libbeat/beat"
conf "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/logp/logptest"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent-libs/monitoring"
Expand Down Expand Up @@ -82,9 +78,7 @@ func TestMakeDeltaSnapshot(t *testing.T) {
}

func TestReporterLog(t *testing.T) {
observed, zapLogs := observer.New(zapcore.DebugLevel)
logger, err := logp.ConfigureWithCoreLocal(logp.Config{}, observed)
require.NoError(t, err)
logger, zapLogs := logptest.NewTestingLoggerWithObserver(t, "")

reporter := reporter{config: defaultConfig(), logger: logger.Named("monitoring")}

Expand Down
20 changes: 3 additions & 17 deletions packetbeat/npcap/npcap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,10 @@
"runtime"
"testing"

"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/logp/logptest"
)

func TestNpcap(t *testing.T) {
// Ugh.
var lcfg logp.Config
logp.ToObserverOutput()(&lcfg)
logp.Configure(lcfg)
obs := logp.ObserverLogs()

// Working space.
path := filepath.Join(t.TempDir(), "installer")
if runtime.GOOS == "windows" {
Expand All @@ -46,22 +40,18 @@
if err != nil {
t.Fatalf("failed to build mock installer: %v\n%s", err, b)
}
log := logp.NewLogger("npcap_test_install")
log := logptest.NewTestingLogger(t, "npcap_test_install")
for _, compat := range []bool{false, true} {
for _, dst := range []string{
"", // Default.
`C:\some\other\location`,

Check failure on line 47 in packetbeat/npcap/npcap_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

`ther` is a misspelling of `there` (misspell)
} {
err = install(context.Background(), log, path, dst, compat)
messages := obs.TakeAll()
if err != nil {
if dst == "" {
dst = "default location"
}
t.Errorf("unexpected error running installer to %s with compat=%t: %v", dst, compat, err)
for _, e := range messages {
t.Log(e.Message)
}
}
}
}
Expand All @@ -74,14 +64,10 @@
if err != nil {
t.Fatalf("failed to build mock uninstaller: %v\n%s", err, b)
}
log := logp.NewLogger("npcap_test_uninstall")
log := logptest.NewTestingLogger(t, "npcap_test_install")
err = uninstall(context.Background(), log, path)
messages := obs.TakeAll()
if err != nil {
t.Errorf("unexpected error running uninstaller: %v", err)
for _, e := range messages {
t.Log(e.Message)
}
}
})
}
25 changes: 11 additions & 14 deletions x-pack/metricbeat/module/gcp/metrics/metrics_requester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/logp/logptest"
)

func TestGetTimeIntervalAligner(t *testing.T) {
Expand Down Expand Up @@ -76,9 +77,6 @@ func TestGetTimeIntervalAligner(t *testing.T) {
}

func TestGetFilterForMetric(t *testing.T) {
logp.DevelopmentSetup(logp.ToObserverOutput())
var logger = logp.NewLogger("TestGetFilterForMetric")

cases := []struct {
title string
s string
Expand All @@ -90,68 +88,67 @@ func TestGetFilterForMetric(t *testing.T) {
"compute service with empty config",
gcp.ServiceCompute,
"",
metricsRequester{config: config{}, logger: logger},
metricsRequester{config: config{}},
"metric.type=\"dummy\"",
},
{
"compute service with configured region",
gcp.ServiceCompute,
"",
metricsRequester{config: config{Region: "foo"}, logger: logger},
metricsRequester{config: config{Region: "foo"}},
"metric.type=\"dummy\" AND resource.labels.zone = starts_with(\"foo\")",
},
{
"compute service with configured zone",
gcp.ServiceCompute,
"",
metricsRequester{config: config{Zone: "foo"}, logger: logger},
metricsRequester{config: config{Zone: "foo"}},
"metric.type=\"dummy\" AND resource.labels.zone = starts_with(\"foo\")",
},
{
"compute service with configured regions",
gcp.ServiceCompute,
"",
metricsRequester{config: config{Regions: []string{"foo", "bar"}}, logger: logger},
metricsRequester{config: config{Regions: []string{"foo", "bar"}}},
"metric.type=\"dummy\" AND (resource.labels.zone = starts_with(\"foo\") OR resource.labels.zone = starts_with(\"bar\"))",
},
{
"compute service with configured region and zone",
gcp.ServiceCompute,
"",
metricsRequester{config: config{Region: "foo", Zone: "bar"}, logger: logger},
metricsRequester{config: config{Region: "foo", Zone: "bar"}},
"metric.type=\"dummy\" AND resource.labels.zone = starts_with(\"foo\")",
},
{
"compute service with configured region and regions",
gcp.ServiceCompute,
"",
metricsRequester{config: config{Region: "foobar", Regions: []string{"foo", "bar"}}, logger: logger},
metricsRequester{config: config{Region: "foobar", Regions: []string{"foo", "bar"}}},
"metric.type=\"dummy\" AND resource.labels.zone = starts_with(\"foobar\")",
},
{
"aiplatform service with configured region and zone",
"aiplatform",
"",
metricsRequester{config: config{Region: "foo", Zone: "bar", LocationLabel: "resource.label.location"}, logger: logger},
metricsRequester{config: config{Region: "foo", Zone: "bar", LocationLabel: "resource.label.location"}},
"metric.type=\"dummy\" AND resource.label.location = starts_with(\"foo\")",
},
}

for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
logger, observedLogs := logptest.NewTestingLoggerWithObserver(t, "TestGetFilterForMetric")
c.r.logger = logger
filter := c.r.getFilterForMetric(c.s, "dummy")
assert.Equal(t, c.expectedFilter, filter)

// NOTE: test that we output a log message with the filter value, as this is **extremely**
// useful to debug issues and we want to make sure is being done.
logs := logp.ObserverLogs().
logs := observedLogs.
FilterLevelExact(zapcore.DebugLevel). // we are OK it being logged at debug level
FilterMessageSnippet(filter).
Len()
assert.Equal(t, logs, 1)
// NOTE: cleanup observed logs at each iteration to start with no messages.
_ = logp.ObserverLogs().TakeAll()

})
}
}
Expand Down
Loading