Skip to content
Open
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
193 changes: 193 additions & 0 deletions filebeat/input/filestream/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
"testing"

"github.com/stretchr/testify/require"
<<<<<<< HEAD

Check failure on line 24 in filebeat/input/filestream/config_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

missing import path (typecheck)
=======

Check failure on line 25 in filebeat/input/filestream/config_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

missing import path (typecheck)
"go.uber.org/zap/zaptest/observer"

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/logptest"
>>>>>>> b5789af35 (refactor: replace lopg.ObserverLogs with logptest (#45734))

Check failure on line 31 in filebeat/input/filestream/config_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

missing import path (typecheck)
)

func TestConfigValidate(t *testing.T) {
Expand All @@ -30,3 +38,188 @@
require.Error(t, err)
})
}
<<<<<<< HEAD
=======

func TestValidateInputIDs(t *testing.T) {
tcs := []struct {
name string
cfg []string
assertErr func(t *testing.T, err error)
assertLogs func(t *testing.T, buff *observer.ObservedLogs)
}{
{
name: "empty config",
cfg: []string{""},
assertErr: func(t *testing.T, err error) {
assert.NoError(t, err, "empty config should not return an error")
},
},
{
name: "one empty ID is allowed",
cfg: []string{`
type: filestream
`, `
type: filestream
id: some-id-1
`, `
type: filestream
id: some-id-2
`,
},
assertErr: func(t *testing.T, err error) {
assert.NoError(t, err, "one empty id is allowed")
},
},
{
name: "duplicated empty ID",
cfg: []string{`
type: filestream
paths:
- "/tmp/empty-1"
`, `
type: filestream
paths:
- "/tmp/empty-2"
`, `
type: filestream
id: unique-id-1
`, `
type: filestream
id: unique-id-2
`, `
type: filestream
id: unique-ID
`,
},
assertErr: func(t *testing.T, err error) {
assert.ErrorContains(t, err, `filestream inputs with duplicated IDs: ""`)

},
assertLogs: func(t *testing.T, obs *observer.ObservedLogs) {
want := `[{"paths":["/tmp/empty-1"],"type":"filestream"},{"paths":["/tmp/empty-2"],"type":"filestream"}]`

logs := obs.TakeAll()
require.Len(t, logs, 1, "there should be only one log entry")

got, err := json.Marshal(logs[0].ContextMap()["inputs"])
require.NoError(t, err, "could not marshal duplicated IDs inputs")
assert.Equal(t, want, string(got))
},
}, {
name: "duplicated IDs",
cfg: []string{`
type: filestream
id: duplicated-id-1
`, `
type: filestream
id: duplicated-id-1
`, `
type: filestream
id: duplicated-id-2
`, `
type: filestream
id: duplicated-id-2
`, `
type: filestream
id: duplicated-id-2
`, `
type: filestream
id: unique-ID
`,
},
assertErr: func(t *testing.T, err error) {
assert.ErrorContains(t, err, "filestream inputs with duplicated IDs")
assert.ErrorContains(t, err, "duplicated-id-1")
assert.ErrorContains(t, err, "duplicated-id-2")
assert.Equal(t, strings.Count(err.Error(), "duplicated-id-1"), 1, "each IDs should appear only once")
assert.Equal(t, strings.Count(err.Error(), "duplicated-id-2"), 1, "each IDs should appear only once")

},
assertLogs: func(t *testing.T, obs *observer.ObservedLogs) {
want := `[{"id":"duplicated-id-1","type":"filestream"},{"id":"duplicated-id-1","type":"filestream"},{"id":"duplicated-id-2","type":"filestream"},{"id":"duplicated-id-2","type":"filestream"},{"id":"duplicated-id-2","type":"filestream"}]`

logs := obs.TakeAll()
require.Len(t, logs, 1, "there should be only one log entry")

got, err := json.Marshal(logs[0].ContextMap()["inputs"])
require.NoError(t, err, "could not marshal duplicated IDs inputs")
assert.Equal(t, want, string(got))
},
},
{
name: "duplicated IDs and empty ID",
cfg: []string{`
type: filestream
`, `
type: filestream
`, `
type: filestream
id: duplicated-id-1
`, `
type: filestream
id: duplicated-id-1
`, `
type: filestream
id: duplicated-id-2
`, `
type: filestream
id: duplicated-id-2
`, `
type: filestream
id: unique-ID
`,
},
assertErr: func(t *testing.T, err error) {
assert.ErrorContains(t, err, "filestream inputs with duplicated IDs")
},
assertLogs: func(t *testing.T, obs *observer.ObservedLogs) {
want := `[{"type":"filestream"},{"type":"filestream"},{"id":"duplicated-id-1","type":"filestream"},{"id":"duplicated-id-1","type":"filestream"},{"id":"duplicated-id-2","type":"filestream"},{"id":"duplicated-id-2","type":"filestream"}]`

logs := obs.TakeAll()
require.Len(t, logs, 1, "there should be only one log entry")

got, err := json.Marshal(logs[0].ContextMap()["inputs"])
require.NoError(t, err, "could not marshal duplicated IDs inputs")
assert.Equal(t, want, string(got))

},
},
{
name: "only unique IDs",
cfg: []string{`
type: filestream
id: unique-id-1
`, `
type: filestream
id: unique-id-2
`, `
type: filestream
id: unique-id-3
`,
},
assertErr: func(t *testing.T, err error) {
assert.NoError(t, err, "only unique IDs should not return an error")
},
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {

Check failure on line 208 in filebeat/input/filestream/config_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected ';', found ',' (typecheck)
var inputs []*conf.C
for _, c := range tc.cfg {

Check failure on line 210 in filebeat/input/filestream/config_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

expected declaration, found 'for' (typecheck)
cfg, err := conf.NewConfigFrom(c)
require.NoError(t, err, "could not create input configuration")
inputs = append(inputs, cfg)
}

logger, observedLogs := logptest.NewTestingLoggerWithObserver(t, "")
err := ValidateInputIDs(inputs, logger)
tc.assertErr(t, err)
if tc.assertLogs != nil {
tc.assertLogs(t, observedLogs)
}
})
}
}
>>>>>>> b5789af35 (refactor: replace lopg.ObserverLogs with logptest (#45734))

Check failure on line 225 in filebeat/input/filestream/config_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

illegal character U+0023 '#' (typecheck)
9 changes: 7 additions & 2 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 @@
"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 @@
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,13 +101,17 @@
return &mock, nil
}

<<<<<<< HEAD

Check failure on line 104 in filebeat/input/journald/pkg/journalctl/reader_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: unexpected <<, expected }
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, "", false, factory)
>>>>>>> b5789af35 (refactor: replace lopg.ObserverLogs with logptest (#45734))

Check failure on line 108 in filebeat/input/journald/pkg/journalctl/reader_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

invalid character U+0023 '#'
if err != nil {
t.Fatalf("cannot instantiate journalctl reader: %s", err)
}

isEntryEmpty := func(entry JournalEntry) bool {

Check failure on line 113 in filebeat/input/journald/pkg/journalctl/reader_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: unexpected {, expected (
return len(entry.Fields) == 0 && entry.Cursor == "" && entry.MonotonicTimestamp == 0 && entry.RealtimeTimestamp == 0

Check failure on line 114 in filebeat/input/journald/pkg/journalctl/reader_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

syntax error: unexpected keyword return, expected ) (typecheck)
}

// In the first call the mock will return an error, simulating journalctl crashing
Expand All @@ -129,7 +134,7 @@
// - 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 @@ 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/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 @@ func testCommonWrap(t *testing.T, tt testDef) {
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
28 changes: 5 additions & 23 deletions libbeat/autodiscover/autodiscover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
package autodiscover

import (
<<<<<<< HEAD
"encoding/json"
=======
"bytes"
"errors"
>>>>>>> b5789af35 (refactor: replace lopg.ObserverLogs with logptest (#45734))
"fmt"
"reflect"
"strings"
Expand All @@ -30,7 +35,6 @@ import (
"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"
"github.com/elastic/beats/v7/libbeat/cfgfile"
Expand Down Expand Up @@ -166,7 +170,6 @@ func TestNilAutodiscover(t *testing.T) {
}

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

Expand Down Expand Up @@ -643,7 +646,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 @@ -742,26 +744,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
Loading
Loading