Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] Change exporter config tests to unmarshal config only for that component. (part2) #14773

Merged
merged 1 commit into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions exporter/f5cloudexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,27 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/exporter/exporterhelper"
otlphttp "go.opentelemetry.io/collector/exporter/otlphttpexporter"
"go.opentelemetry.io/collector/service/servicetest"
)

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

cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
factory := NewFactory()
factories.Exporters[typeStr] = factory
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
cfg := factory.CreateDefaultConfig()

sub, err := cm.Sub(config.NewComponentIDWithName(typeStr, "allsettings").String())
require.NoError(t, err)
require.NotNil(t, cfg)

assert.Equal(t, 2, len(cfg.Exporters))
require.NoError(t, config.UnmarshalExporter(sub, cfg))

exporter := cfg.Exporters[config.NewComponentIDWithName(typeStr, "allsettings")]
actualCfg := exporter.(*Config)
actualCfg := cfg.(*Config)
expectedCfg := &Config{
Config: otlphttp.Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "allsettings")),
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "")),
RetrySettings: exporterhelper.RetrySettings{
Enabled: true,
InitialInterval: 10 * time.Second,
Expand Down Expand Up @@ -77,7 +72,7 @@ func TestLoadConfig(t *testing.T) {
}
// testing function equality is not supported in Go hence these will be ignored for this test
expectedCfg.HTTPClientSettings.CustomRoundTripper = nil
exporter.(*Config).HTTPClientSettings.CustomRoundTripper = nil
actualCfg.HTTPClientSettings.CustomRoundTripper = nil
assert.Equal(t, expectedCfg, actualCfg)
}

Expand Down
59 changes: 23 additions & 36 deletions exporter/f5cloudexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
receivers:
nop:

processors:
nop:

exporters:
f5cloud:
endpoint: "https://f5cloud"
source: "dev"
f5cloud_auth:
credential_file: "/etc/creds/key.json"
f5cloud/allsettings:
endpoint: "https://f5cloud"
source: "dev"
f5cloud_auth:
credential_file: "/etc/creds/key.json"
audience: "exampleaudience"
timeout: 10s
read_buffer_size: 123
write_buffer_size: 345
sending_queue:
enabled: true
num_consumers: 2
queue_size: 10
retry_on_failure:
enabled: true
initial_interval: 10s
max_interval: 60s
max_elapsed_time: 10m
service:
pipelines:
metrics:
receivers: [ nop ]
processors: [ nop ]
exporters: [ f5cloud, f5cloud/allsettings ]
f5cloud:
endpoint: "https://f5cloud"
source: "dev"
f5cloud_auth:
credential_file: "/etc/creds/key.json"
f5cloud/allsettings:
endpoint: "https://f5cloud"
source: "dev"
f5cloud_auth:
credential_file: "/etc/creds/key.json"
audience: "exampleaudience"
timeout: 10s
read_buffer_size: 123
write_buffer_size: 345
sending_queue:
enabled: true
num_consumers: 2
queue_size: 10
retry_on_failure:
enabled: true
initial_interval: 10s
max_interval: 60s
max_elapsed_time: 10m
168 changes: 85 additions & 83 deletions exporter/fileexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,101 +20,103 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/service/servicetest"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

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

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

e0 := cfg.Exporters[config.NewComponentID(typeStr)]
assert.Equal(t, e0, factory.CreateDefaultConfig())

e1 := cfg.Exporters[config.NewComponentIDWithName(typeStr, "2")]
assert.Equal(t, e1,
&Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "2")),
Path: "./filename.json",
Rotation: &Rotation{
MaxMegabytes: 10,
MaxDays: 3,
MaxBackups: 3,
LocalTime: true,
tests := []struct {
id config.ComponentID
expected config.Exporter
errorMessage string
}{
{
id: config.NewComponentIDWithName(typeStr, "2"),
expected: &Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
Path: "./filename.json",
Rotation: &Rotation{
MaxMegabytes: 10,
MaxDays: 3,
MaxBackups: 3,
LocalTime: true,
},
FormatType: formatTypeJSON,
},
FormatType: formatTypeJSON,
})
e2 := cfg.Exporters[config.NewComponentIDWithName(typeStr, "3")]
assert.Equal(t, e2,
&Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "3")),
Path: "./filename",
Rotation: &Rotation{
MaxMegabytes: 10,
MaxDays: 3,
MaxBackups: 3,
LocalTime: true,
},
{
id: config.NewComponentIDWithName(typeStr, "3"),
expected: &Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
Path: "./filename",
Rotation: &Rotation{
MaxMegabytes: 10,
MaxDays: 3,
MaxBackups: 3,
LocalTime: true,
},
FormatType: formatTypeProto,
Compression: compressionZSTD,
},
FormatType: formatTypeProto,
Compression: compressionZSTD,
})
e3 := cfg.Exporters[config.NewComponentIDWithName(typeStr, "no_rotation")]
assert.Equal(t, e3,
&Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "no_rotation")),
Path: "./foo",
FormatType: formatTypeJSON,
})
e4 := cfg.Exporters[config.NewComponentIDWithName(typeStr, "rotation_with_default_settings")]
assert.Equal(t, e4,
&Config{
ExporterSettings: config.NewExporterSettings(
config.NewComponentIDWithName(typeStr, "rotation_with_default_settings")),
Path: "./foo",
FormatType: formatTypeJSON,
Rotation: &Rotation{
MaxBackups: defaultMaxBackups,
},
{
id: config.NewComponentIDWithName(typeStr, "rotation_with_default_settings"),
expected: &Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
Path: "./foo",
FormatType: formatTypeJSON,
Rotation: &Rotation{
MaxBackups: defaultMaxBackups,
},
},
})
e5 := cfg.Exporters[config.NewComponentIDWithName(typeStr, "rotation_with_custom_settings")]
assert.Equal(t, e5,
&Config{
ExporterSettings: config.NewExporterSettings(
config.NewComponentIDWithName(typeStr, "rotation_with_custom_settings")),
Path: "./foo",
Rotation: &Rotation{
MaxMegabytes: 1234,
MaxBackups: defaultMaxBackups,
},
{
id: config.NewComponentIDWithName(typeStr, "rotation_with_custom_settings"),
expected: &Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
Path: "./foo",
Rotation: &Rotation{
MaxMegabytes: 1234,
MaxBackups: defaultMaxBackups,
},
FormatType: formatTypeJSON,
},
FormatType: formatTypeJSON,
})
}
},
{
id: config.NewComponentIDWithName(typeStr, "compression_error"),
errorMessage: "compression is not supported",
},
{
id: config.NewComponentIDWithName(typeStr, "format_error"),
errorMessage: "format type is not supported",
},
{
id: config.NewComponentIDWithName(typeStr, ""),
errorMessage: "path must be non-empty",
},
}

func TestLoadConfigFormatError(t *testing.T) {
factories, err := componenttest.NopFactories()
assert.NoError(t, err)
for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()

factory := NewFactory()
factories.Exporters[typeStr] = factory
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config-format-error.yaml"), factories)
require.EqualError(t, err, "exporter \"file\" has invalid configuration: format type is not supported")
require.NotNil(t, cfg)
}
sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalExporter(sub, cfg))

func TestLoadConfiCompressionError(t *testing.T) {
factories, err := componenttest.NopFactories()
assert.NoError(t, err)
if tt.expected == nil {
assert.EqualError(t, cfg.Validate(), tt.errorMessage)
return
}

factory := NewFactory()
factories.Exporters[typeStr] = factory
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config-compression-error.yaml"), factories)
require.EqualError(t, err, "exporter \"file\" has invalid configuration: compression is not supported")
require.NotNil(t, cfg)
assert.NoError(t, cfg.Validate())
assert.Equal(t, tt.expected, cfg)
})
}
}
20 changes: 0 additions & 20 deletions exporter/fileexporter/testdata/config-compression-error.yaml

This file was deleted.

20 changes: 0 additions & 20 deletions exporter/fileexporter/testdata/config-format-error.yaml

This file was deleted.

Loading