Skip to content

Commit

Permalink
Move test funcs that load entire config to servicetest (#4606)
Browse files Browse the repository at this point in the history
Updates #4605

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Dec 21, 2021
1 parent ec90e1f commit 7de98eb
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 110 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Replace ConfigMapProvider and ConfigUnmarshaler in collector settings by one simpler ConfigProvider (#4590)
- Remove deprecated consumererror.Combine (#4597)
- Remove `configmapprovider.NewDefault`, `configmapprovider.NewExpand`, `configmapprovider.NewMerge` (#4600)
- Move `configtest.LoadConfig` and `configtest.LoadConfigAndValidate` to `servicetest` (#4606)

## 💡 Enhancements 💡

Expand Down
25 changes: 5 additions & 20 deletions config/configtest/configtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,20 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configmapprovider"
"go.opentelemetry.io/collector/config/configunmarshaler"
"go.opentelemetry.io/collector/internal/configprovider"
"go.opentelemetry.io/collector/service/servicetest"
)

// The regular expression for valid config field tag.
var configFieldTagRegExp = regexp.MustCompile("^[a-z0-9][a-z0-9_]*$")

// LoadConfig loads a config.Config from file, and does NOT validate the configuration.
// Deprecated: use servicetest.LoadConfig
func LoadConfig(fileName string, factories component.Factories) (*config.Config, error) {
// Read yaml config from file
cp, err := configprovider.NewExpand(configmapprovider.NewFile(fileName)).Retrieve(context.Background(), nil)
if err != nil {
return nil, err
}
// Unmarshal the config using the given factories.
m, err := cp.Get(context.Background())
if err != nil {
return nil, err
}
return configunmarshaler.NewDefault().Unmarshal(m, factories)
return servicetest.LoadConfig(fileName, factories)
}

// LoadConfigAndValidate loads a config from the file, and validates the configuration.
// Deprecated: use servicetest.LoadConfigAndValidate
func LoadConfigAndValidate(fileName string, factories component.Factories) (*config.Config, error) {
cfg, err := LoadConfig(fileName, factories)
if err != nil {
return nil, err
}
return cfg, cfg.Validate()
return servicetest.LoadConfigAndValidate(fileName, factories)
}

// LoadConfigMap loads a config.Map from file, and does NOT validate the configuration.
Expand Down
58 changes: 0 additions & 58 deletions config/configtest/configtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,71 +16,13 @@ package configtest

import (
"io"
"path"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
)

func TestLoadConfig(t *testing.T) {
factories, err := componenttest.NopFactories()
assert.NoError(t, err)

cfg, err := LoadConfig(path.Join("testdata", "config.yaml"), factories)
require.NoError(t, err)

// Verify extensions.
require.Len(t, cfg.Extensions, 2)
assert.Contains(t, cfg.Extensions, config.NewComponentID("nop"))
assert.Contains(t, cfg.Extensions, config.NewComponentIDWithName("nop", "myextension"))

// Verify receivers
require.Len(t, cfg.Receivers, 2)
assert.Contains(t, cfg.Receivers, config.NewComponentID("nop"))
assert.Contains(t, cfg.Receivers, config.NewComponentIDWithName("nop", "myreceiver"))

// Verify exporters
assert.Len(t, cfg.Exporters, 2)
assert.Contains(t, cfg.Exporters, config.NewComponentID("nop"))
assert.Contains(t, cfg.Exporters, config.NewComponentIDWithName("nop", "myexporter"))

// Verify Processors
assert.Len(t, cfg.Processors, 2)
assert.Contains(t, cfg.Processors, config.NewComponentID("nop"))
assert.Contains(t, cfg.Processors, config.NewComponentIDWithName("nop", "myprocessor"))

// Verify service.
require.Len(t, cfg.Service.Extensions, 1)
assert.Contains(t, cfg.Service.Extensions, config.NewComponentID("nop"))
require.Len(t, cfg.Service.Pipelines, 1)
assert.Equal(t,
&config.Pipeline{
Receivers: []config.ComponentID{config.NewComponentID("nop")},
Processors: []config.ComponentID{config.NewComponentID("nop")},
Exporters: []config.ComponentID{config.NewComponentID("nop")},
},
cfg.Service.Pipelines[config.NewComponentID("traces")],
"Did not load pipeline config correctly")
}

func TestLoadConfigAndValidate(t *testing.T) {
factories, err := componenttest.NopFactories()
assert.NoError(t, err)

cfgValidate, errValidate := LoadConfigAndValidate(path.Join("testdata", "config.yaml"), factories)
require.NoError(t, errValidate)

cfg, errLoad := LoadConfig(path.Join("testdata", "config.yaml"), factories)
require.NoError(t, errLoad)

assert.Equal(t, cfg, cfgValidate)
}

func TestCheckConfigStructPointerAndValue(t *testing.T) {
config := struct {
SomeFiled string `mapstructure:"test"`
Expand Down
4 changes: 2 additions & 2 deletions exporter/loggingexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -33,7 +33,7 @@ func TestLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Exporters[typeStr] = factory
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
4 changes: 2 additions & 2 deletions exporter/otlpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configauth"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -38,7 +38,7 @@ func TestLoadConfig(t *testing.T) {
factory := NewFactory()
factories.Exporters[typeStr] = factory

cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
4 changes: 2 additions & 2 deletions exporter/otlphttpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -36,7 +36,7 @@ func TestLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Exporters[typeStr] = factory
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
6 changes: 3 additions & 3 deletions extension/ballastextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Extensions[typeStr] = factory
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)

require.Nil(t, err)
require.NotNil(t, cfg)
Expand All @@ -59,7 +59,7 @@ func TestLoadInvalidConfig(t *testing.T) {

factory := NewFactory()
factories.Extensions[typeStr] = factory
_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config_invalid.yaml"), factories)
_, err = servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "config_invalid.yaml"), factories)

require.NotNil(t, err)
assert.Equal(t, err.Error(), "extension \"memory_ballast\" has invalid configuration: size_in_percentage is not in range 0 to 100")
Expand Down
4 changes: 2 additions & 2 deletions extension/zpagesextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -33,7 +33,7 @@ func TestLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Extensions[typeStr] = factory
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)

require.Nil(t, err)
require.NotNil(t, cfg)
Expand Down
4 changes: 2 additions & 2 deletions processor/batchprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -33,7 +33,7 @@ func TestLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Processors[typeStr] = factory
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)

require.Nil(t, err)
require.NotNil(t, cfg)
Expand Down
4 changes: 2 additions & 2 deletions processor/memorylimiterprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -34,7 +34,7 @@ func TestLoadConfig(t *testing.T) {
factories.Processors[typeStr] = factory
require.NoError(t, err)

cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)

require.Nil(t, err)
require.NotNil(t, cfg)
Expand Down
12 changes: 6 additions & 6 deletions receiver/otlpreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -37,7 +37,7 @@ func TestLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Receivers[typeStr] = factory
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down Expand Up @@ -203,15 +203,15 @@ func TestFailedLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Receivers[typeStr] = factory
_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "typo_default_proto_config.yaml"), factories)
_, err = servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "typo_default_proto_config.yaml"), factories)
assert.EqualError(t, err, "error reading receivers configuration for \"otlp\": 1 error(s) decoding:\n\n* 'protocols' has invalid keys: htttp")

_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "bad_proto_config.yaml"), factories)
_, err = servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "bad_proto_config.yaml"), factories)
assert.EqualError(t, err, "error reading receivers configuration for \"otlp\": 1 error(s) decoding:\n\n* 'protocols' has invalid keys: thrift")

_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "bad_no_proto_config.yaml"), factories)
_, err = servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "bad_no_proto_config.yaml"), factories)
assert.EqualError(t, err, "receiver \"otlp\" has invalid configuration: must specify at least one protocol when using the OTLP receiver")

_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "bad_empty_config.yaml"), factories)
_, err = servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "bad_empty_config.yaml"), factories)
assert.EqualError(t, err, "error reading receivers configuration for \"otlp\": empty config for OTLP receiver")
}
4 changes: 2 additions & 2 deletions service/internal/builder/exporters_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/exporter/otlpexporter"
"go.opentelemetry.io/collector/internal/testcomponents"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestBuildExporters(t *testing.T) {
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestBuildExporters_NotSupportedDataType(t *testing.T) {
for _, test := range tests {
t.Run(test.configFile, func(t *testing.T) {

cfg, err := configtest.LoadConfigAndValidate(path.Join("testdata", test.configFile), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join("testdata", test.configFile), factories)
require.Nil(t, err)

exporters, err := BuildExporters(componenttest.NewNopTelemetrySettings(), component.NewDefaultBuildInfo(), cfg, factories.Exporters)
Expand Down
6 changes: 3 additions & 3 deletions service/internal/builder/pipelines_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/internal/testcomponents"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/model/pdata"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestBuildPipelines(t *testing.T) {
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestBuildPipelines_BuildVarious(t *testing.T) {
func testPipeline(t *testing.T, pipelineID config.ComponentID, exporterIDs []config.ComponentID) {
factories, err := testcomponents.ExampleComponents()
assert.NoError(t, err)
cfg, err := configtest.LoadConfigAndValidate(path.Join("testdata", "pipelines_builder.yaml"), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join("testdata", "pipelines_builder.yaml"), factories)
// Unmarshal the config
require.Nil(t, err)

Expand Down Expand Up @@ -251,7 +251,7 @@ func TestBuildPipelines_NotSupportedDataType(t *testing.T) {
for _, test := range tests {
t.Run(test.configFile, func(t *testing.T) {

cfg, err := configtest.LoadConfigAndValidate(path.Join("testdata", test.configFile), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join("testdata", test.configFile), factories)
require.Nil(t, err)

allExporters, err := BuildExporters(componenttest.NewNopTelemetrySettings(), component.NewDefaultBuildInfo(), cfg, factories.Exporters)
Expand Down
8 changes: 4 additions & 4 deletions service/internal/builder/receivers_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/internal/testcomponents"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/model/pdata"
"go.opentelemetry.io/collector/service/servicetest"
)

type testCase struct {
Expand Down Expand Up @@ -90,7 +90,7 @@ func testReceivers(t *testing.T, test testCase) {
factories, err := testcomponents.ExampleComponents()
assert.NoError(t, err)

cfg, err := configtest.LoadConfigAndValidate(path.Join("testdata", "pipelines_builder.yaml"), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join("testdata", "pipelines_builder.yaml"), factories)
require.NoError(t, err)

// Build the pipeline
Expand Down Expand Up @@ -275,7 +275,7 @@ func TestBuildReceivers_Unused(t *testing.T) {
factories, err := testcomponents.ExampleComponents()
assert.NoError(t, err)

cfg, err := configtest.LoadConfigAndValidate(path.Join("testdata", "unused_receiver.yaml"), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join("testdata", "unused_receiver.yaml"), factories)
assert.NoError(t, err)

// Build the pipeline
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestBuildReceivers_NotSupportedDataType(t *testing.T) {
for _, test := range tests {
t.Run(test.configFile, func(t *testing.T) {

cfg, err := configtest.LoadConfigAndValidate(path.Join("testdata", test.configFile), factories)
cfg, err := servicetest.LoadConfigAndValidate(path.Join("testdata", test.configFile), factories)
assert.NoError(t, err)
require.NotNil(t, cfg)

Expand Down
Loading

0 comments on commit 7de98eb

Please sign in to comment.