From 1d383a2c9783e67687c496ad01d915cceb414171 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Tue, 21 Dec 2021 14:18:04 -0800 Subject: [PATCH] Remove configtest dependency from collector, generate tests in builder Signed-off-by: Bogdan Drutu --- service/collector.go | 4 --- service/command_test.go | 25 ++++++++++++++ service/configcheck.go | 43 ----------------------- service/configcheck_test.go | 68 ------------------------------------- 4 files changed, 25 insertions(+), 115 deletions(-) delete mode 100644 service/configcheck.go delete mode 100644 service/configcheck_test.go diff --git a/service/collector.go b/service/collector.go index 6b9e4017ac3..a7e76ea57bb 100644 --- a/service/collector.go +++ b/service/collector.go @@ -85,10 +85,6 @@ type Collector struct { // New creates and returns a new instance of Collector. func New(set CollectorSettings) (*Collector, error) { - if err := validateConfigFromFactories(set.Factories); err != nil { - return nil, err - } - if set.ConfigProvider == nil { return nil, errors.New("invalid nil config provider") } diff --git a/service/command_test.go b/service/command_test.go index cb478958fee..209fab17ee4 100644 --- a/service/command_test.go +++ b/service/command_test.go @@ -15,11 +15,15 @@ package service import ( + "context" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/internal/internalinterface" "go.opentelemetry.io/collector/internal/testcomponents" ) @@ -49,3 +53,24 @@ func TestNewCommandInvalidFactories(t *testing.T) { err = cmd.Execute() require.Error(t, err) } + +// badConfigExtensionFactory was created to force error path from factory returning +// a config not satisfying the validation. +type badConfigExtensionFactory struct { + internalinterface.BaseInternal +} + +func (b badConfigExtensionFactory) Type() config.Type { + return "bad_config" +} + +func (b badConfigExtensionFactory) CreateDefaultConfig() config.Extension { + return &struct { + config.ExtensionSettings + BadTagField int `mapstructure:"tag-with-dashes"` + }{} +} + +func (b badConfigExtensionFactory) CreateExtension(_ context.Context, _ component.ExtensionCreateSettings, _ config.Extension) (component.Extension, error) { + return nil, nil +} diff --git a/service/configcheck.go b/service/configcheck.go deleted file mode 100644 index 9e068b6b8fe..00000000000 --- a/service/configcheck.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package service // import "go.opentelemetry.io/collector/service" - -import ( - "go.uber.org/multierr" - - "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/config/configtest" -) - -// validateConfigFromFactories checks if all configurations for the given factories -// are satisfying the patterns used by the collector. -func validateConfigFromFactories(factories component.Factories) error { - var errs error - - for _, factory := range factories.Receivers { - errs = multierr.Append(errs, configtest.CheckConfigStruct(factory.CreateDefaultConfig())) - } - for _, factory := range factories.Processors { - errs = multierr.Append(errs, configtest.CheckConfigStruct(factory.CreateDefaultConfig())) - } - for _, factory := range factories.Exporters { - errs = multierr.Append(errs, configtest.CheckConfigStruct(factory.CreateDefaultConfig())) - } - for _, factory := range factories.Extensions { - errs = multierr.Append(errs, configtest.CheckConfigStruct(factory.CreateDefaultConfig())) - } - - return errs -} diff --git a/service/configcheck_test.go b/service/configcheck_test.go deleted file mode 100644 index 1a4f6c5229f..00000000000 --- a/service/configcheck_test.go +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package service - -import ( - "context" - "testing" - - "github.com/stretchr/testify/require" - - "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/config" - "go.opentelemetry.io/collector/internal/internalinterface" - "go.opentelemetry.io/collector/internal/testcomponents" -) - -func TestValidateConfigFromFactories_Success(t *testing.T) { - factories, err := testcomponents.ExampleComponents() - require.NoError(t, err) - - err = validateConfigFromFactories(factories) - require.NoError(t, err) -} - -func TestValidateConfigFromFactories_Failure(t *testing.T) { - factories, err := testcomponents.ExampleComponents() - require.NoError(t, err) - - // Add a factory returning config not following pattern to force error. - f := &badConfigExtensionFactory{} - factories.Extensions[f.Type()] = f - - err = validateConfigFromFactories(factories) - require.Error(t, err) -} - -// badConfigExtensionFactory was created to force error path from factory returning -// a config not satisfying the validation. -type badConfigExtensionFactory struct { - internalinterface.BaseInternal -} - -func (b badConfigExtensionFactory) Type() config.Type { - return "bad_config" -} - -func (b badConfigExtensionFactory) CreateDefaultConfig() config.Extension { - return &struct { - config.ExtensionSettings - BadTagField int `mapstructure:"tag-with-dashes"` - }{} -} - -func (b badConfigExtensionFactory) CreateExtension(_ context.Context, _ component.ExtensionCreateSettings, _ config.Extension) (component.Extension, error) { - return nil, nil -}