diff --git a/CHANGELOG.md b/CHANGELOG.md index 90a16bece01..d453a0b8bfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 💡 diff --git a/config/configtest/configtest.go b/config/configtest/configtest.go index ed5a02f1242..14455e72eb7 100644 --- a/config/configtest/configtest.go +++ b/config/configtest/configtest.go @@ -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. diff --git a/config/configtest/configtest_test.go b/config/configtest/configtest_test.go index 50c86ad865c..6b2a0a0d2b6 100644 --- a/config/configtest/configtest_test.go +++ b/config/configtest/configtest_test.go @@ -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"` diff --git a/exporter/loggingexporter/config_test.go b/exporter/loggingexporter/config_test.go index f5ffed361fd..dac62d79736 100644 --- a/exporter/loggingexporter/config_test.go +++ b/exporter/loggingexporter/config_test.go @@ -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) { @@ -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) diff --git a/exporter/otlpexporter/config_test.go b/exporter/otlpexporter/config_test.go index 2426250881c..4d8180ae3e9 100644 --- a/exporter/otlpexporter/config_test.go +++ b/exporter/otlpexporter/config_test.go @@ -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) { @@ -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) diff --git a/exporter/otlphttpexporter/config_test.go b/exporter/otlphttpexporter/config_test.go index 0f4c1a64787..e8943490956 100644 --- a/exporter/otlphttpexporter/config_test.go +++ b/exporter/otlphttpexporter/config_test.go @@ -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) { @@ -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) diff --git a/extension/ballastextension/config_test.go b/extension/ballastextension/config_test.go index 9def8cb2f66..e782aaf91cf 100644 --- a/extension/ballastextension/config_test.go +++ b/extension/ballastextension/config_test.go @@ -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) { @@ -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) @@ -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") diff --git a/extension/zpagesextension/config_test.go b/extension/zpagesextension/config_test.go index 36c5aec90e7..048be612b6d 100644 --- a/extension/zpagesextension/config_test.go +++ b/extension/zpagesextension/config_test.go @@ -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) { @@ -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) diff --git a/processor/batchprocessor/config_test.go b/processor/batchprocessor/config_test.go index 4f1aa007866..d404d470def 100644 --- a/processor/batchprocessor/config_test.go +++ b/processor/batchprocessor/config_test.go @@ -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) { @@ -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) diff --git a/processor/memorylimiterprocessor/config_test.go b/processor/memorylimiterprocessor/config_test.go index cb3fa4b4350..b851096f57f 100644 --- a/processor/memorylimiterprocessor/config_test.go +++ b/processor/memorylimiterprocessor/config_test.go @@ -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) { @@ -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) diff --git a/receiver/otlpreceiver/config_test.go b/receiver/otlpreceiver/config_test.go index 27ccb4cc0ad..8e1f65e7b0b 100644 --- a/receiver/otlpreceiver/config_test.go +++ b/receiver/otlpreceiver/config_test.go @@ -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) { @@ -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) @@ -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") } diff --git a/service/internal/builder/exporters_builder_test.go b/service/internal/builder/exporters_builder_test.go index da250df3d6f..d638f5923b5 100644 --- a/service/internal/builder/exporters_builder_test.go +++ b/service/internal/builder/exporters_builder_test.go @@ -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) { @@ -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) diff --git a/service/internal/builder/pipelines_builder_test.go b/service/internal/builder/pipelines_builder_test.go index ae7745fb096..65b2745f054 100644 --- a/service/internal/builder/pipelines_builder_test.go +++ b/service/internal/builder/pipelines_builder_test.go @@ -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) { @@ -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) @@ -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) diff --git a/service/internal/builder/receivers_builder_test.go b/service/internal/builder/receivers_builder_test.go index a78864bd6c7..c2793dd8eac 100644 --- a/service/internal/builder/receivers_builder_test.go +++ b/service/internal/builder/receivers_builder_test.go @@ -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 { @@ -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 @@ -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 @@ -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) diff --git a/service/service_test.go b/service/service_test.go index ebb55a8a24c..461fee3a7db 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -25,7 +25,7 @@ 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/service/servicetest" ) func TestService_GetFactory(t *testing.T) { @@ -100,7 +100,7 @@ func createExampleService(t *testing.T) *service { // Create some factories. factories, err := componenttest.NopFactories() require.NoError(t, err) - cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "otelcol-nop.yaml"), factories) + cfg, err := servicetest.LoadConfigAndValidate(path.Join(".", "testdata", "otelcol-nop.yaml"), factories) require.NoError(t, err) srv, err := newService(&svcSettings{ diff --git a/service/servicetest/configprovider.go b/service/servicetest/configprovider.go new file mode 100644 index 00000000000..8f32d73f6b5 --- /dev/null +++ b/service/servicetest/configprovider.go @@ -0,0 +1,49 @@ +// 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 servicetest // import "go.opentelemetry.io/collector/service/servicetest" + +import ( + "context" + + "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" +) + +// LoadConfig loads a config.Config from file, and does NOT validate the configuration. +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) +} + +// LoadConfigAndValidate loads a config from the file, and validates the configuration. +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() +} diff --git a/service/servicetest/configprovider_test.go b/service/servicetest/configprovider_test.go new file mode 100644 index 00000000000..8cfeab42a05 --- /dev/null +++ b/service/servicetest/configprovider_test.go @@ -0,0 +1,80 @@ +// 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 servicetest + +import ( + "path" + "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) +} diff --git a/config/configtest/testdata/config.yaml b/service/servicetest/testdata/config.yaml similarity index 100% rename from config/configtest/testdata/config.yaml rename to service/servicetest/testdata/config.yaml