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
25 changes: 25 additions & 0 deletions pkg/stanza/operator/operatortest/confmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"

Expand Down Expand Up @@ -64,3 +65,27 @@ func newAnyOpConfig(opCfg operator.Builder) *anyOpConfig {
func (a *anyOpConfig) Unmarshal(component *confmap.Conf) error {
return a.Operator.Unmarshal(component)
}

// ConfigBuilderTests is used for testing build failures
type ConfigBuilderTests struct {
Tests []ConfigBuilderTest
}

// ConfigBuilderTest is used for testing build failures
type ConfigBuilderTest struct {
Name string
Cfg operator.Builder
BuildError string
}

// Run Build on a malformed config and expect an error.
func (c ConfigBuilderTests) Run(t *testing.T) {
for _, tc := range c.Tests {
t.Run(tc.Name, func(t *testing.T) {
cfg := tc.Cfg
set := componenttest.NewNopTelemetrySettings()
_, err := cfg.Build(set)
require.Equal(t, tc.BuildError, err.Error())
})
}
}
109 changes: 46 additions & 63 deletions pkg/stanza/operator/transformer/regexreplace/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/operatortest"
)
Expand Down Expand Up @@ -87,65 +84,51 @@ func TestUnmarshal(t *testing.T) {
}.Run(t)
}

type invalidConfigCase struct {
name string
cfg *Config
expectErr string
}

func TestInvalidConfig(t *testing.T) {
cases := []invalidConfigCase{
{
name: "neither_regex_nor_regexname",
cfg: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewBodyField()
cfg.RegexName = ""
cfg.Regex = ""
return cfg
}(),
expectErr: "either regex or regex_name must be set",
},
{
name: "both_regex_and_regexname",
cfg: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewBodyField()
cfg.RegexName = "ansi_control_sequences"
cfg.Regex = ".*"
return cfg
}(),
expectErr: "either regex or regex_name must be set",
},
{
name: "unknown_regex_name",
cfg: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewBodyField()
cfg.RegexName = "i_do_not_exist"
return cfg
}(),
expectErr: "regex_name i_do_not_exist is unknown",
},
{
name: "invalid_regex",
cfg: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewBodyField()
cfg.Regex = ")"
return cfg
}(),
expectErr: "error parsing regexp: unexpected ): `)`",
func TestBuild(t *testing.T) {
operatortest.ConfigBuilderTests{
Tests: []operatortest.ConfigBuilderTest{
{
Name: "neither_regex_nor_regexname",
Cfg: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewBodyField()
cfg.RegexName = ""
cfg.Regex = ""
return cfg
}(),
BuildError: "either regex or regex_name must be set",
},
{
Name: "both_regex_and_regexname",
Cfg: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewBodyField()
cfg.RegexName = "ansi_control_sequences"
cfg.Regex = ".*"
return cfg
}(),
BuildError: "either regex or regex_name must be set",
},
{
Name: "unknown_regex_name",
Cfg: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewBodyField()
cfg.RegexName = "i_do_not_exist"
return cfg
}(),
BuildError: "regex_name i_do_not_exist is unknown",
},
{
Name: "invalid_regex",
Cfg: func() *Config {
cfg := NewConfig()
cfg.Field = entry.NewBodyField()
cfg.Regex = ")"
return cfg
}(),
BuildError: "error parsing regexp: unexpected ): `)`",
},
},
}
for _, tc := range cases {
t.Run("InvalidConfig/"+tc.name, func(t *testing.T) {
cfg := tc.cfg
cfg.OutputIDs = []string{"fake"}
cfg.OnError = "send"
set := componenttest.NewNopTelemetrySettings()
_, err := cfg.Build(set)
require.Equal(t, tc.expectErr, err.Error())
})
}
}.Run(t)
}