Skip to content

Commit

Permalink
Rename configtest.LoadConfigFile to configtest.LoadConfigAndValidate (o…
Browse files Browse the repository at this point in the history
…pen-telemetry#3306)

Add a new configtest.LoadConfig to simplify testing errors on Validate.

Signed-off-by: Bogdan Drutu [email protected]
  • Loading branch information
bogdandrutu authored May 28, 2021
1 parent 1f972bb commit ba91818
Show file tree
Hide file tree
Showing 38 changed files with 91 additions and 83 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Introduce `AppSettings` instead of `Parameters` (#3163)
- Remove unused testutil.TempSocketName (#3291)
- Move BigEndian helper functions in `tracetranslator` to an internal package.(#3298)
- Rename `configtest.LoadConfigFile` to `configtest.LoadConfigAndValidate` (#3306)

## 💡 Enhancements 💡

Expand Down
23 changes: 13 additions & 10 deletions config/configtest/configtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@
package configtest

import (
"testing"

"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configloader"
"go.opentelemetry.io/collector/config/configparser"
)

// LoadConfigFile loads a config from file.
func LoadConfigFile(t *testing.T, fileName string, factories component.Factories) (*config.Config, error) {
// Read yaml config from file.
// LoadConfig loads a 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 := configparser.NewParserFromFile(fileName)
require.NoError(t, err)
// Load the config from viper using the given factories.
cfg, err := configloader.Load(cp, factories)
if err != nil {
return nil, err
}
// Load the config using the given factories.
return configloader.Load(cp, 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
}
Expand Down
19 changes: 16 additions & 3 deletions config/configtest/configtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
"go.opentelemetry.io/collector/config"
)

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

cfg, err := LoadConfigFile(t, "testdata/config.yaml", factories)
require.NoError(t, err, "Unable to load config")
cfg, err := LoadConfig("testdata/config.yaml", factories)
require.NoError(t, err)

// Verify extensions.
require.Len(t, cfg.Extensions, 2)
Expand Down Expand Up @@ -66,3 +66,16 @@ func TestLoadConfigFile(t *testing.T) {
cfg.Service.Pipelines["traces"],
"Did not load pipeline config correctly")
}

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

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

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

assert.Equal(t, cfg, cfgValidate)
}
2 changes: 1 addition & 1 deletion exporter/fileexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Exporters[typeStr] = factory
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
require.EqualError(t, err, "exporter \"file\" has invalid configuration: path must be non-empty")
require.NotNil(t, cfg)

Expand Down
2 changes: 1 addition & 1 deletion exporter/jaegerexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestLoadConfig(t *testing.T) {

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

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion exporter/kafkaexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Exporters[typeStr] = factory
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
require.NoError(t, err)
require.Equal(t, 1, len(cfg.Exporters))

Expand Down
2 changes: 1 addition & 1 deletion exporter/loggingexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {

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

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion exporter/opencensusexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestLoadConfig(t *testing.T) {

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

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion exporter/otlpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestLoadConfig(t *testing.T) {
factory := NewFactory()
factories.Exporters[typeStr] = factory

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

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion exporter/otlphttpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestLoadConfig(t *testing.T) {

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

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion exporter/prometheusexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestLoadConfig(t *testing.T) {

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

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
6 changes: 3 additions & 3 deletions exporter/prometheusremotewriteexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Test_loadConfig(t *testing.T) {

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

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestNegativeQueueSize(t *testing.T) {

factory := NewFactory()
factories.Exporters[typeStr] = factory
_, err = configtest.LoadConfigFile(t, path.Join(".", "testdata", "negative_queue_size.yaml"), factories)
_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "negative_queue_size.yaml"), factories)
assert.Error(t, err)
}

Expand All @@ -99,6 +99,6 @@ func TestNegativeNumConsumers(t *testing.T) {

factory := NewFactory()
factories.Exporters[typeStr] = factory
_, err = configtest.LoadConfigFile(t, path.Join(".", "testdata", "negative_num_consumers.yaml"), factories)
_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "negative_num_consumers.yaml"), factories)
assert.Error(t, err)
}
2 changes: 1 addition & 1 deletion exporter/zipkinexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestLoadConfig(t *testing.T) {

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

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion extension/ballastextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {

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

require.Nil(t, err)
require.NotNil(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion extension/healthcheckextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestLoadConfig(t *testing.T) {

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

require.Nil(t, err)
require.NotNil(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion extension/pprofextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestLoadConfig(t *testing.T) {

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

require.Nil(t, err)
require.NotNil(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion extension/zpagesextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestLoadConfig(t *testing.T) {

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

require.Nil(t, err)
require.NotNil(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion processor/attributesprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestLoadingConfig(t *testing.T) {

factory := NewFactory()
factories.Processors[typeStr] = factory
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
assert.NoError(t, err)
require.NotNil(t, cfg)

Expand Down
2 changes: 1 addition & 1 deletion processor/batchprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestLoadConfig(t *testing.T) {

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

require.Nil(t, err)
require.NotNil(t, cfg)
Expand Down
6 changes: 3 additions & 3 deletions processor/filterprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestLoadingConfigStrict(t *testing.T) {

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

assert.Nil(t, err)
require.NotNil(t, cfg)
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestLoadingConfigRegexp(t *testing.T) {

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

assert.Nil(t, err)
require.NotNil(t, cfg)
Expand Down Expand Up @@ -193,7 +193,7 @@ func TestLoadingConfigExpr(t *testing.T) {
require.NoError(t, err)
factory := NewFactory()
factories.Processors[typeStr] = factory
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config_expr.yaml"), factories)
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config_expr.yaml"), factories)
require.NoError(t, err)
require.NotNil(t, cfg)

Expand Down
2 changes: 1 addition & 1 deletion processor/filterprocessor/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestCreateProcessors(t *testing.T) {

factory := NewFactory()
factories.Processors[typeStr] = factory
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", test.configName), factories)
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", test.configName), factories)
assert.Nil(t, err)

for name, cfg := range cfg.Processors {
Expand Down
5 changes: 1 addition & 4 deletions processor/memorylimiter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ func TestLoadConfig(t *testing.T) {
factories.Processors[typeStr] = factory
require.NoError(t, err)

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

require.Nil(t, err)
require.NotNil(t, cfg)
Expand Down
4 changes: 2 additions & 2 deletions processor/probabilisticsamplerprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Processors[typeStr] = factory
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
require.NoError(t, err)
require.NotNil(t, cfg)

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

cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "empty.yaml"), factories)
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "empty.yaml"), factories)
require.NoError(t, err)
require.NotNil(t, cfg)

Expand Down
2 changes: 1 addition & 1 deletion processor/resourceprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {

factories.Processors[typeStr] = NewFactory()

cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
assert.NoError(t, err)
assert.NotNil(t, cfg)

Expand Down
2 changes: 1 addition & 1 deletion processor/spanprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestLoadConfig(t *testing.T) {
factory := NewFactory()
factories.Processors[typeStr] = factory

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

assert.NoError(t, err)
assert.NotNil(t, cfg)
Expand Down
6 changes: 3 additions & 3 deletions receiver/hostmetricsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestLoadConfig(t *testing.T) {

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

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestLoadInvalidConfig_NoScrapers(t *testing.T) {

factory := NewFactory()
factories.Receivers[typeStr] = factory
_, err = configtest.LoadConfigFile(t, path.Join(".", "testdata", "config-noscrapers.yaml"), factories)
_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config-noscrapers.yaml"), factories)

require.EqualError(t, err, "receiver \"hostmetrics\" has invalid configuration: must specify at least one scraper when using hostmetrics receiver")
}
Expand All @@ -109,7 +109,7 @@ func TestLoadInvalidConfig_InvalidScraperKey(t *testing.T) {

factory := NewFactory()
factories.Receivers[typeStr] = factory
_, err = configtest.LoadConfigFile(t, path.Join(".", "testdata", "config-invalidscraperkey.yaml"), factories)
_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config-invalidscraperkey.yaml"), factories)

require.EqualError(t, err, "error reading receivers configuration for hostmetrics: invalid scraper key: invalidscraperkey")
}
10 changes: 5 additions & 5 deletions receiver/jaegerreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestLoadConfig(t *testing.T) {

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

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

factory := NewFactory()
factories.Receivers[typeStr] = factory
_, err = configtest.LoadConfigFile(t, path.Join(".", "testdata", "bad_typo_default_proto_config.yaml"), factories)
_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "bad_typo_default_proto_config.yaml"), factories)
assert.EqualError(t, err, "error reading receivers configuration for jaeger: unknown protocols in the Jaeger receiver")

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

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

_, err = configtest.LoadConfigFile(t, path.Join(".", "testdata", "bad_empty_config.yaml"), factories)
_, err = configtest.LoadConfigAndValidate(path.Join(".", "testdata", "bad_empty_config.yaml"), factories)
assert.EqualError(t, err, "error reading receivers configuration for jaeger: empty config for Jaeger receiver")
}
2 changes: 1 addition & 1 deletion receiver/jaegerreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestCreateReceiverGeneralConfig(t *testing.T) {
factory := NewFactory()
factories.Receivers[typeStr] = factory

cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
require.NoError(t, err)
require.NotNil(t, cfg)

Expand Down
2 changes: 1 addition & 1 deletion receiver/kafkareceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestLoadConfig(t *testing.T) {

factory := NewFactory()
factories.Receivers[typeStr] = factory
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := configtest.LoadConfigAndValidate(path.Join(".", "testdata", "config.yaml"), factories)
require.NoError(t, err)
require.Equal(t, 1, len(cfg.Receivers))

Expand Down
Loading

0 comments on commit ba91818

Please sign in to comment.