From 25fb1173b57e740bd2f5118bfa4f965976fc0642 Mon Sep 17 00:00:00 2001 From: Daniel Jaglowski Date: Mon, 4 Dec 2023 12:10:44 -0800 Subject: [PATCH] [chore][pkg/stanza] Localize one-off test utility code (#29644) This PR moves localizes some test utility code. These functions are either used only once, or only in one test file. This is a step towards #29643, and ultimately towards sharing test utilities across multiple packages. --- pkg/stanza/fileconsumer/benchmark_test.go | 7 +- pkg/stanza/fileconsumer/config_test.go | 49 ++++++++++++++ pkg/stanza/fileconsumer/rotation_test.go | 17 ++++- pkg/stanza/fileconsumer/util_test.go | 78 ----------------------- 4 files changed, 71 insertions(+), 80 deletions(-) diff --git a/pkg/stanza/fileconsumer/benchmark_test.go b/pkg/stanza/fileconsumer/benchmark_test.go index 0d6cbd9335f8..bd5496c3eaaa 100644 --- a/pkg/stanza/fileconsumer/benchmark_test.go +++ b/pkg/stanza/fileconsumer/benchmark_test.go @@ -4,6 +4,7 @@ package fileconsumer import ( + "context" "os" "path/filepath" "testing" @@ -161,7 +162,11 @@ func BenchmarkFileInput(b *testing.B) { received := make(chan []byte) - op, err := cfg.Build(testutil.Logger(b), emitOnChan(received)) + callback := func(_ context.Context, token []byte, _ map[string]any) error { + received <- token + return nil + } + op, err := cfg.Build(testutil.Logger(b), callback) require.NoError(b, err) // write half the lines before starting diff --git a/pkg/stanza/fileconsumer/config_test.go b/pkg/stanza/fileconsumer/config_test.go index 43171be5c96c..d28cf9d0db9a 100644 --- a/pkg/stanza/fileconsumer/config_test.go +++ b/pkg/stanza/fileconsumer/config_test.go @@ -4,6 +4,7 @@ package fileconsumer import ( + "fmt" "path/filepath" "testing" "time" @@ -11,6 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/featuregate" + "go.uber.org/zap" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fingerprint" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/matcher" @@ -798,3 +800,50 @@ func TestBuildWithHeader(t *testing.T) { }) } } + +// includeDir is a builder-like helper for quickly setting up a test config +func (c *Config) includeDir(dir string) *Config { + c.Include = append(c.Include, fmt.Sprintf("%s/*", dir)) + return c +} + +// withHeader is a builder-like helper for quickly setting up a test config header +func (c *Config) withHeader(headerMatchPattern, extractRegex string) *Config { + regexOpConfig := regex.NewConfig() + regexOpConfig.Regex = extractRegex + + c.Header = &HeaderConfig{ + Pattern: headerMatchPattern, + MetadataOperators: []operator.Config{ + { + Builder: regexOpConfig, + }, + }, + } + + return c +} + +const mockOperatorType = "mock" + +func init() { + operator.Register(mockOperatorType, func() operator.Builder { return newMockOperatorConfig(NewConfig()) }) +} + +type mockOperatorConfig struct { + helper.BasicConfig `mapstructure:",squash"` + *Config `mapstructure:",squash"` +} + +func newMockOperatorConfig(cfg *Config) *mockOperatorConfig { + return &mockOperatorConfig{ + BasicConfig: helper.NewBasicConfig(mockOperatorType, mockOperatorType), + Config: cfg, + } +} + +// This function is impelmented for compatibility with operatortest +// but is not meant to be used directly +func (h *mockOperatorConfig) Build(*zap.SugaredLogger) (operator.Operator, error) { + panic("not impelemented") +} diff --git a/pkg/stanza/fileconsumer/rotation_test.go b/pkg/stanza/fileconsumer/rotation_test.go index f016149bb416..8e44a47c65fc 100644 --- a/pkg/stanza/fileconsumer/rotation_test.go +++ b/pkg/stanza/fileconsumer/rotation_test.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "io" + "log" "os" "path/filepath" "runtime" @@ -14,6 +15,7 @@ import ( "testing" "time" + "github.com/observiq/nanojack" "github.com/stretchr/testify/require" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil" @@ -255,7 +257,20 @@ func (rt rotationTest) run(tc rotationTest, copyTruncate, sequential bool) func( emitCalls := make(chan *emitParams, tc.totalLines) operator, _ := buildTestManager(t, cfg, withEmitChan(emitCalls)) - logger := getRotatingLogger(t, tempDir, tc.maxLinesPerFile, tc.maxBackupFiles, copyTruncate, sequential) + file, err := os.CreateTemp(tempDir, "") + require.NoError(t, err) + require.NoError(t, file.Close()) // will be managed by rotator + + rotator := nanojack.Logger{ + Filename: file.Name(), + MaxLines: tc.maxLinesPerFile, + MaxBackups: tc.maxBackupFiles, + CopyTruncate: copyTruncate, + Sequential: sequential, + } + t.Cleanup(func() { _ = rotator.Close() }) + + logger := log.New(&rotator, "", 0) expected := make([][]byte, 0, tc.totalLines) baseStr := string(tokenWithLength(46)) // + ' 123' diff --git a/pkg/stanza/fileconsumer/util_test.go b/pkg/stanza/fileconsumer/util_test.go index 0407333c7f4e..b3f9e1b61869 100644 --- a/pkg/stanza/fileconsumer/util_test.go +++ b/pkg/stanza/fileconsumer/util_test.go @@ -6,21 +6,15 @@ package fileconsumer import ( "context" "fmt" - "log" "math/rand" "os" "path/filepath" "testing" "time" - "github.com/observiq/nanojack" "github.com/stretchr/testify/require" - "go.uber.org/zap" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/emit" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/regex" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil" ) @@ -37,36 +31,6 @@ func testEmitFunc(emitChan chan *emitParams) emit.Callback { } } -// includeDir is a builder-like helper for quickly setting up a test config -func (c *Config) includeDir(dir string) *Config { - c.Include = append(c.Include, fmt.Sprintf("%s/*", dir)) - return c -} - -// withHeader is a builder-like helper for quickly setting up a test config header -func (c *Config) withHeader(headerMatchPattern, extractRegex string) *Config { - regexOpConfig := regex.NewConfig() - regexOpConfig.Regex = extractRegex - - c.Header = &HeaderConfig{ - Pattern: headerMatchPattern, - MetadataOperators: []operator.Config{ - { - Builder: regexOpConfig, - }, - }, - } - - return c -} - -func emitOnChan(received chan []byte) emit.Callback { - return func(_ context.Context, token []byte, _ map[string]any) error { - received <- token - return nil - } -} - type emitParams struct { attrs map[string]any token []byte @@ -117,24 +81,6 @@ func openTempWithPattern(t testing.TB, tempDir, pattern string) *os.File { return file } -func getRotatingLogger(t testing.TB, tempDir string, maxLines, maxBackups int, copyTruncate, sequential bool) *log.Logger { - file, err := os.CreateTemp(tempDir, "") - require.NoError(t, err) - require.NoError(t, file.Close()) // will be managed by rotator - - rotator := nanojack.Logger{ - Filename: file.Name(), - MaxLines: maxLines, - MaxBackups: maxBackups, - CopyTruncate: copyTruncate, - Sequential: sequential, - } - - t.Cleanup(func() { _ = rotator.Close() }) - - return log.New(&rotator, "", 0) -} - func writeString(t testing.TB, file *os.File, s string) { _, err := file.WriteString(s) require.NoError(t, err) @@ -218,27 +164,3 @@ func expectNoTokensUntil(t *testing.T, c chan *emitParams, d time.Duration) { case <-time.After(d): } } - -const mockOperatorType = "mock" - -func init() { - operator.Register(mockOperatorType, func() operator.Builder { return newMockOperatorConfig(NewConfig()) }) -} - -type mockOperatorConfig struct { - helper.BasicConfig `mapstructure:",squash"` - *Config `mapstructure:",squash"` -} - -func newMockOperatorConfig(cfg *Config) *mockOperatorConfig { - return &mockOperatorConfig{ - BasicConfig: helper.NewBasicConfig(mockOperatorType, mockOperatorType), - Config: cfg, - } -} - -// This function is impelmented for compatibility with operatortest -// but is not meant to be used directly -func (h *mockOperatorConfig) Build(*zap.SugaredLogger) (operator.Operator, error) { - panic("not impelemented") -}