Skip to content

Commit

Permalink
Split LoadConfig into LoadConfig and LoadAndVerifyConfig.
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed May 4, 2020
1 parent 5bdba60 commit d494396
Show file tree
Hide file tree
Showing 29 changed files with 61 additions and 47 deletions.
16 changes: 12 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestDecodeConfig(t *testing.T) {
assert.NoError(t, err)

// Load the config
config, err := LoadConfigFile(t, path.Join(".", "testdata", "valid-config.yaml"), factories)
config, err := LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "valid-config.yaml"), factories)
if err != nil {
t.Fatalf("unable to load config, %v", err)
}
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestSimpleConfig(t *testing.T) {
assert.NoError(t, err)

// Load the config
config, err := LoadConfigFile(t, path.Join(".", "testdata", test.name+".yaml"), factories)
config, err := LoadAndVerifyConfigFile(t, path.Join(".", "testdata", test.name+".yaml"), factories)
if err != nil {
t.Fatalf("TEST[%s] unable to load config, %v", test.name, err)
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestDecodeConfig_MultiProto(t *testing.T) {
assert.NoError(t, err)

// Load the config
config, err := LoadConfigFile(t, path.Join(".", "testdata", "multiproto-config.yaml"), factories)
config, err := LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "multiproto-config.yaml"), factories)
if err != nil {
t.Fatalf("unable to load config, %v", err)
}
Expand Down Expand Up @@ -386,7 +386,7 @@ func TestDecodeConfig_Invalid(t *testing.T) {
assert.NoError(t, err)

for _, test := range testCases {
_, err := LoadConfigFile(t, path.Join(".", "testdata", test.name+".yaml"), factories)
_, err := LoadAndVerifyConfigFile(t, path.Join(".", "testdata", test.name+".yaml"), factories)
if err == nil {
t.Errorf("expected error but succeeded on invalid config case: %s", test.name)
} else if test.expected != 0 {
Expand All @@ -407,3 +407,11 @@ func TestDecodeConfig_Invalid(t *testing.T) {
}
}
}

func TestLoadEmptyConfig(t *testing.T) {
factories, err := ExampleComponents()
assert.NoError(t, err)

_, err = LoadConfigFile(t, path.Join(".", "testdata", "empty-config.yaml"), factories)
assert.NoError(t, err)
}
10 changes: 8 additions & 2 deletions config/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
)

// LoadConfigFile loads a config from file.
// LoadConfigFile loads a config from file, without verifying it.
func LoadConfigFile(t *testing.T, fileName string, factories Factories) (*configmodels.Config, error) {
// Open the file for reading.
file, err := os.Open(filepath.Clean(fileName))
Expand All @@ -49,10 +49,16 @@ func LoadConfigFile(t *testing.T, fileName string, factories Factories) (*config
}

// Load the config from viper using the given factories.
cfg, err := Load(v, factories)
return Load(v, factories)
}

// LoadAndVerifyConfigFile loads and verifies a config from file.
func LoadAndVerifyConfigFile(t *testing.T, fileName string, factories Factories) (*configmodels.Config, error) {
cfg, err := LoadConfigFile(t, fileName, factories)
if err != nil {
return nil, err
}

err = ValidateConfig(cfg, zap.NewNop())
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion exporter/fileexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestLoadConfig(t *testing.T) {

factory := &Factory{}
factories.Exporters[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(t, err)
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 @@ -33,7 +33,7 @@ func TestLoadConfig(t *testing.T) {

factory := &Factory{}
factories.Exporters[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)
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 @@ -31,7 +31,7 @@ func TestLoadConfig(t *testing.T) {

factory := &Factory{}
factories.Exporters[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, 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 @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {

factory := &Factory{}
factories.Exporters[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, 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 @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {

factory := &Factory{}
factories.Exporters[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, 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 @@ -31,7 +31,7 @@ func TestLoadConfig(t *testing.T) {

factory := &Factory{}
factories.Exporters[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

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

factory := &Factory{}
factories.Exporters[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(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 @@ -31,7 +31,7 @@ func TestLoadConfig(t *testing.T) {

factory := &Factory{}
factories.Extensions[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, 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 @@ -31,7 +31,7 @@ func TestLoadConfig(t *testing.T) {

factory := &Factory{}
factories.Extensions[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, 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 @@ -31,7 +31,7 @@ func TestLoadConfig(t *testing.T) {

factory := &Factory{}
factories.Extensions[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, 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 @@ -32,7 +32,7 @@ func TestLoadingConifg(t *testing.T) {

factory := &Factory{}
factories.Processors[typeStr] = factory
config, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
config, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

assert.Nil(t, err)
require.NotNil(t, config)
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 @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {

factory := &Factory{}
factories.Processors[typeStr] = &Factory{}
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

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

config, err := config.LoadConfigFile(
config, err := config.LoadAndVerifyConfigFile(
t,
path.Join(".", "testdata", "config.yaml"),
factories)
Expand Down
2 changes: 1 addition & 1 deletion processor/queuedprocessor/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 := &Factory{}
factories.Processors[typeStr] = &Factory{}
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

require.Nil(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 @@ -31,7 +31,7 @@ func TestLoadConfig(t *testing.T) {
factory := &Factory{}
factories.Processors[typeStr] = &Factory{}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {

factory := &Factory{}
factories.Processors[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

require.Nil(t, err)
require.NotNil(t, cfg)
Expand All @@ -57,7 +57,7 @@ func TestLoadConfigEmpty(t *testing.T) {
require.NotNil(t, factories.Processors)
require.NoError(t, err)

config, err := config.LoadConfigFile(t, path.Join(".", "testdata", "empty.yaml"), factories)
config, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "empty.yaml"), factories)

require.Nil(t, err)
require.NotNil(t, config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestLoadConfig(t *testing.T) {
factory := &Factory{}
factories.Processors[factory.Type()] = factory

cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "tail_sampling_config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "tail_sampling_config.yaml"), factories)
require.Nil(t, err)
require.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 @@ -32,7 +32,7 @@ func TestLoadConfig(t *testing.T) {
factory := &Factory{}
factories.Processors[typeStr] = factory

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

assert.Nil(t, err)
assert.NotNil(t, config)
Expand Down
2 changes: 1 addition & 1 deletion receiver/hostmetricsreceiver/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.Receivers[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
8 changes: 4 additions & 4 deletions receiver/jaegerreceiver/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 := &Factory{}
factories.Receivers[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

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

factory := &Factory{}
factories.Receivers[typeStr] = factory
_, err = config.LoadConfigFile(t, path.Join(".", "testdata", "bad_proto_config.yaml"), factories)
_, err = config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "bad_proto_config.yaml"), factories)
assert.EqualError(t, err, `error reading settings for receiver type "jaeger": unknown Jaeger protocol badproto`)

_, err = config.LoadConfigFile(t, path.Join(".", "testdata", "bad_no_proto_config.yaml"), factories)
_, err = config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "bad_no_proto_config.yaml"), factories)
assert.EqualError(t, err, `error reading settings for receiver type "jaeger": must specify at least one protocol when using the Jaeger receiver`)

_, err = config.LoadConfigFile(t, path.Join(".", "testdata", "bad_empty_config.yaml"), factories)
_, err = config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "bad_empty_config.yaml"), factories)
assert.EqualError(t, err, `error reading settings for receiver type "jaeger": empty config for Jaeger receiver`)
}
2 changes: 1 addition & 1 deletion receiver/opencensusreceiver/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 := &Factory{}
factories.Receivers[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
2 changes: 1 addition & 1 deletion receiver/otlpreceiver/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 := &Factory{}
factories.Receivers[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
8 changes: 4 additions & 4 deletions receiver/prometheusreceiver/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 := &Factory{}
factories.Receivers[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

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

factory := &Factory{}
factories.Receivers[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config_env.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config_env.yaml"), factories)
require.NoError(t, err)
require.NotNil(t, cfg)

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

factory := &Factory{}
factories.Receivers[typeStr] = factory
cfg, err := config.LoadConfigFile(
cfg, err := config.LoadAndVerifyConfigFile(
t,
path.Join(".", "testdata", "invalid-config-section.yaml"), factories)

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

factory := &Factory{}
factories.Receivers[typeStr] = factory
cfg, err := config.LoadConfigFile(
cfg, err := config.LoadAndVerifyConfigFile(
t,
path.Join(".", "testdata", "invalid-config-prometheus-section.yaml"), factories)

Expand Down
2 changes: 1 addition & 1 deletion receiver/vmmetricsreceiver/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 := &Factory{}
factories.Receivers[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

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

factory := &Factory{}
factories.Receivers[typeStr] = factory
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)
cfg, err := config.LoadAndVerifyConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories)

require.NoError(t, err)
require.NotNil(t, cfg)
Expand Down
8 changes: 4 additions & 4 deletions service/builder/pipelines_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func testPipeline(t *testing.T, pipelineName string, exporterNames []string) {
assert.Nil(t, err)
attrFactory := &attributesprocessor.Factory{}
factories.Processors[attrFactory.Type()] = attrFactory
cfg, err := config.LoadConfigFile(t, "testdata/pipelines_builder.yaml", factories)
cfg, err := config.LoadAndVerifyConfigFile(t, "testdata/pipelines_builder.yaml", factories)
// Load the config
require.Nil(t, err)

Expand Down Expand Up @@ -209,11 +209,11 @@ func TestPipelinesBuilder_Error(t *testing.T) {
assert.Nil(t, err)
attrFactory := &attributesprocessor.Factory{}
factories.Processors[attrFactory.Type()] = attrFactory
cfg, err := config.LoadConfigFile(t, "testdata/pipelines_builder.yaml", factories)
cfg, err := config.LoadAndVerifyConfigFile(t, "testdata/pipelines_builder.yaml", factories)
require.Nil(t, err)

// Corrupt the pipeline, change data type to metrics. We have to forcedly do it here
// since there is no way to have such config loaded by LoadConfigFile, it would not
// since there is no way to have such config loaded by LoadAndVerifyConfigFile, it would not
// pass validation. We are doing this to test failure mode of PipelinesBuilder.
pipeline := cfg.Service.Pipelines["traces"]
pipeline.InputType = configmodels.MetricsDataType
Expand All @@ -235,7 +235,7 @@ func TestProcessorsBuilder_ErrorOnNilProcessor(t *testing.T) {
bf := &badProcessorFactory{}
factories.Processors[bf.Type()] = bf

cfg, err := config.LoadConfigFile(t, "testdata/bad_processor_factory.yaml", factories)
cfg, err := config.LoadAndVerifyConfigFile(t, "testdata/bad_processor_factory.yaml", factories)
require.Nil(t, err)

allExporters, err := NewExportersBuilder(zap.NewNop(), cfg, factories.Exporters).Build()
Expand Down
Loading

0 comments on commit d494396

Please sign in to comment.