From 3517ab6a820c26e5d402bf6710b9a93f12cb3e74 Mon Sep 17 00:00:00 2001 From: Gregor Jasny Date: Sat, 8 Mar 2025 19:10:29 +0100 Subject: [PATCH 1/2] [chore] [pkg/stanza] refactor test code into ConfigBuilderTests This is a follow-up from #37443. It extracts the test logic for operator build errors into re-useable methods. --- pkg/stanza/operator/operatortest/confmap.go | 25 ++++ .../transformer/regexreplace/config_test.go | 110 ++++++++---------- 2 files changed, 71 insertions(+), 64 deletions(-) diff --git a/pkg/stanza/operator/operatortest/confmap.go b/pkg/stanza/operator/operatortest/confmap.go index dc237dfaa6ceb..3c82324781209 100644 --- a/pkg/stanza/operator/operatortest/confmap.go +++ b/pkg/stanza/operator/operatortest/confmap.go @@ -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" @@ -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()) + }) + } +} diff --git a/pkg/stanza/operator/transformer/regexreplace/config_test.go b/pkg/stanza/operator/transformer/regexreplace/config_test.go index e569ffddd9f3c..aea7a90394c67 100644 --- a/pkg/stanza/operator/transformer/regexreplace/config_test.go +++ b/pkg/stanza/operator/transformer/regexreplace/config_test.go @@ -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" ) @@ -87,65 +84,50 @@ 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 ): `)`", - }, - } - 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()) - }) - } +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 ): `)`", + }}, + }.Run(t) } From b27137b54c94e87d1beef329af7fe2f9903d2667 Mon Sep 17 00:00:00 2001 From: Gregor Jasny Date: Mon, 24 Mar 2025 10:41:49 +0100 Subject: [PATCH 2/2] fixup! [chore] [pkg/stanza] refactor test code into ConfigBuilderTests --- pkg/stanza/operator/transformer/regexreplace/config_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/stanza/operator/transformer/regexreplace/config_test.go b/pkg/stanza/operator/transformer/regexreplace/config_test.go index aea7a90394c67..41d30bcac33e0 100644 --- a/pkg/stanza/operator/transformer/regexreplace/config_test.go +++ b/pkg/stanza/operator/transformer/regexreplace/config_test.go @@ -128,6 +128,7 @@ func TestBuild(t *testing.T) { return cfg }(), BuildError: "error parsing regexp: unexpected ): `)`", - }}, + }, + }, }.Run(t) }