Skip to content

Commit

Permalink
[chore][pkg/stanza] Localize one-off test utility code (open-telemetr…
Browse files Browse the repository at this point in the history
…y#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 open-telemetry#29643, and ultimately towards sharing test
utilities across multiple packages.
  • Loading branch information
djaglowski authored Dec 4, 2023
1 parent 9940002 commit 25fb117
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 80 deletions.
7 changes: 6 additions & 1 deletion pkg/stanza/fileconsumer/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package fileconsumer

import (
"context"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions pkg/stanza/fileconsumer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
package fileconsumer

import (
"fmt"
"path/filepath"
"testing"
"time"

"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"
Expand Down Expand Up @@ -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")
}
17 changes: 16 additions & 1 deletion pkg/stanza/fileconsumer/rotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"sync"
"testing"
"time"

"github.com/observiq/nanojack"
"github.com/stretchr/testify/require"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil"
Expand Down Expand Up @@ -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'
Expand Down
78 changes: 0 additions & 78 deletions pkg/stanza/fileconsumer/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}

0 comments on commit 25fb117

Please sign in to comment.