Skip to content

Commit

Permalink
[chore] change config tests to unmarshal only the config for that com…
Browse files Browse the repository at this point in the history
…ponent

The main motivation for this is to allow components (including tests) to not depend on the "service" configuration and be reusable without the otelcol service.

Signed-off-by: Bogdan <[email protected]>
  • Loading branch information
bogdandrutu committed Aug 10, 2022
1 parent c69aac5 commit 99f3f7e
Show file tree
Hide file tree
Showing 27 changed files with 358 additions and 680 deletions.
32 changes: 15 additions & 17 deletions exporter/loggingexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,29 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"

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

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

func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
factories.Exporters[typeStr] = factory
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
cfg := factory.CreateDefaultConfig()
assert.NoError(t, config.UnmarshalExporter(confmap.New(), cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

func TestUnmarshalConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
require.NotNil(t, cfg)

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

e1 := cfg.Exporters[config.NewComponentIDWithName(typeStr, "2")]
assert.Equal(t, e1,
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, config.UnmarshalExporter(cm, cfg))
assert.Equal(t,
&Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "2")),
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
LogLevel: zapcore.DebugLevel,
SamplingInitial: 10,
SamplingThereafter: 50,
})
}, cfg)
}
25 changes: 3 additions & 22 deletions exporter/loggingexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
receivers:
nop:

processors:
nop:

exporters:
logging:
logging/2:
loglevel: debug
sampling_initial: 10
sampling_thereafter: 50

service:
pipelines:
traces:
receivers: [nop]
processors: [nop]
exporters: [logging]
metrics:
receivers: [nop]
exporters: [logging,logging/2]
loglevel: debug
sampling_initial: 10
sampling_thereafter: 50
33 changes: 15 additions & 18 deletions exporter/otlpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,31 @@ 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/configauth"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/service/servicetest"
)

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

func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
factories.Exporters[typeStr] = factory

cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
cfg := factory.CreateDefaultConfig()
assert.NoError(t, config.UnmarshalExporter(confmap.New(), cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

func TestUnmarshalConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
require.NotNil(t, cfg)

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

e1 := cfg.Exporters[config.NewComponentIDWithName(typeStr, "2")]
assert.Equal(t, e1,
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, config.UnmarshalExporter(cm, cfg))
assert.Equal(t,
&Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "2")),
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
TimeoutSettings: exporterhelper.TimeoutSettings{
Timeout: 10 * time.Second,
},
Expand Down Expand Up @@ -87,5 +84,5 @@ func TestLoadConfig(t *testing.T) {
BalancerName: "round_robin",
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("nop")},
},
})
}, cfg)
}
70 changes: 25 additions & 45 deletions exporter/otlpexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,45 +1,25 @@
extensions:
nop:

receivers:
nop:

processors:
nop:

exporters:
otlp:
otlp/2:
endpoint: "1.2.3.4:1234"
compression: "gzip"
tls:
ca_file: /var/lib/mycert.pem
timeout: 10s
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
auth:
authenticator: nop
headers:
"can you have a . here?": "F0000000-0000-0000-0000-000000000000"
header1: 234
another: "somevalue"
keepalive:
time: 20s
timeout: 30s
permit_without_stream: true
balancer_name: "round_robin"

service:
extensions: [nop]
pipelines:
traces:
receivers: [nop]
processors: [nop]
exporters: [otlp]
endpoint: "1.2.3.4:1234"
compression: "gzip"
tls:
ca_file: /var/lib/mycert.pem
timeout: 10s
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
auth:
authenticator: nop
headers:
"can you have a . here?": "F0000000-0000-0000-0000-000000000000"
header1: 234
another: "somevalue"
keepalive:
time: 20s
timeout: 30s
permit_without_stream: true
balancer_name: "round_robin"
41 changes: 17 additions & 24 deletions exporter/otlphttpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,32 @@ 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/config/configtls"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/service/servicetest"
)

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

func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
factories.Exporters[typeStr] = factory

// Bad config
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "bad_empty_config.yaml"), factories)

require.Error(t, err)

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

// Good config
cfg, err = servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
cfg := factory.CreateDefaultConfig()
assert.NoError(t, config.UnmarshalExporter(confmap.New(), cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
// Default/Empty config is invalid.
assert.Error(t, cfg.Validate())
}

func TestUnmarshalConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
require.NotNil(t, cfg)

e1 := cfg.Exporters[config.NewComponentIDWithName(typeStr, "2")]
assert.Equal(t, e1,
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, config.UnmarshalExporter(cm, cfg))
assert.Equal(t,
&Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "2")),
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
RetrySettings: exporterhelper.RetrySettings{
Enabled: true,
InitialInterval: 10 * time.Second,
Expand Down Expand Up @@ -86,5 +79,5 @@ func TestLoadConfig(t *testing.T) {
Timeout: time.Second * 10,
Compression: "gzip",
},
})
}, cfg)
}
61 changes: 23 additions & 38 deletions exporter/otlphttpexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,38 +1,23 @@
receivers:
nop:

processors:
nop:

exporters:
otlphttp/2:
endpoint: "https://1.2.3.4:1234"
tls:
ca_file: /var/lib/mycert.pem
cert_file: certfile
key_file: keyfile
insecure: true
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
headers:
"can you have a . here?": "F0000000-0000-0000-0000-000000000000"
header1: 234
another: "somevalue"
compression: gzip

service:
pipelines:
traces:
receivers: [nop]
processors: [nop]
exporters: [otlphttp/2]
endpoint: "https://1.2.3.4:1234"
tls:
ca_file: /var/lib/mycert.pem
cert_file: certfile
key_file: keyfile
insecure: true
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
headers:
"can you have a . here?": "F0000000-0000-0000-0000-000000000000"
header1: 234
another: "somevalue"
compression: gzip
52 changes: 20 additions & 32 deletions extension/ballastextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,35 @@ 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"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

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

func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
factories.Extensions[typeStr] = factory
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)

require.Nil(t, err)
require.NotNil(t, cfg)

ext0 := cfg.Extensions[config.NewComponentID(typeStr)]
assert.Equal(t, factory.CreateDefaultConfig(), ext0)
cfg := factory.CreateDefaultConfig()
assert.NoError(t, config.UnmarshalExtension(confmap.New(), cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

ext1 := cfg.Extensions[config.NewComponentIDWithName(typeStr, "1")]
func TestUnmarshalConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, config.UnmarshalExtension(cm, cfg))
assert.Equal(t,
&Config{
ExtensionSettings: config.NewExtensionSettings(config.NewComponentIDWithName(typeStr, "1")),
ExtensionSettings: config.NewExtensionSettings(config.NewComponentID(typeStr)),
SizeMiB: 123,
SizeInPercentage: 20,
},
ext1)

assert.Equal(t, 1, len(cfg.Service.Extensions))
assert.Equal(t, config.NewComponentIDWithName(typeStr, "1"), cfg.Service.Extensions[0])
}, cfg)
}

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

factory := NewFactory()
factories.Extensions[typeStr] = factory
_, err = servicetest.LoadConfigAndValidate(filepath.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")

func TestConfigValidate(t *testing.T) {
cfg := &Config{SizeInPercentage: 200}
err := cfg.Validate()
require.Error(t, err)
assert.Equal(t, "size_in_percentage is not in range 0 to 100", err.Error())
}
Loading

0 comments on commit 99f3f7e

Please sign in to comment.