Skip to content

Commit

Permalink
[chore] Change processor config tests to unmarshal config for that co…
Browse files Browse the repository at this point in the history
…mponent.

- resourcedetectionprocessor
- resourceprocessor
- routingprocessor
- schemaprocessor
- spanprocessor
- tailsamplingprocessor
- transformprocessor
  • Loading branch information
kovrus committed Aug 16, 2022
1 parent ebdb228 commit bbc6b16
Show file tree
Hide file tree
Showing 30 changed files with 720 additions and 966 deletions.
125 changes: 68 additions & 57 deletions processor/resourcedetectionprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,79 +20,90 @@ import (
"time"

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

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/aws/ec2"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/system"
)

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

factory := NewFactory()
factories.Processors[typeStr] = factory
cfg := confighttp.NewDefaultHTTPClientSettings()
cfg.Timeout = 2 * time.Second

cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
assert.NoError(t, err)
assert.NotNil(t, cfg)

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

p2 := cfg.Processors[config.NewComponentIDWithName(typeStr, "gce")].(*Config)
p2e := &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "gce")),
Detectors: []string{"env", "gce"},
HTTPClientSettings: confighttp.HTTPClientSettings{Timeout: 2 * time.Second, MaxIdleConns: p2.MaxIdleConns, IdleConnTimeout: p2.IdleConnTimeout},
Override: false,
}
assert.Equal(t, p2, p2e)

p3 := cfg.Processors[config.NewComponentIDWithName(typeStr, "ec2")]
p3e := &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "ec2")),
Detectors: []string{"env", "ec2"},
DetectorConfig: DetectorConfig{
EC2Config: ec2.Config{
Tags: []string{"^tag1$", "^tag2$"},
tests := []struct {
id config.ComponentID
expected config.Processor
errorMessage string
}{
{
id: config.NewComponentIDWithName(typeStr, "gce"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
Detectors: []string{"env", "gce"},
HTTPClientSettings: cfg,
Override: false,
},
},
HTTPClientSettings: confighttp.HTTPClientSettings{Timeout: 2 * time.Second, MaxIdleConns: p2.MaxIdleConns, IdleConnTimeout: p2.IdleConnTimeout},
Override: false,
}
assert.Equal(t, p3, p3e)

p4 := cfg.Processors[config.NewComponentIDWithName(typeStr, "system")]
p4e := &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "system")),
Detectors: []string{"env", "system"},
DetectorConfig: DetectorConfig{
SystemConfig: system.Config{
HostnameSources: []string{"os"},
{
id: config.NewComponentIDWithName(typeStr, "ec2"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
Detectors: []string{"env", "ec2"},
DetectorConfig: DetectorConfig{
EC2Config: ec2.Config{
Tags: []string{"^tag1$", "^tag2$"},
},
},
HTTPClientSettings: cfg,
Override: false,
},
},
HTTPClientSettings: confighttp.HTTPClientSettings{Timeout: 2 * time.Second, MaxIdleConns: p2.MaxIdleConns, IdleConnTimeout: p2.IdleConnTimeout},
Override: false,
Attributes: []string{"a", "b"},
{
id: config.NewComponentIDWithName(typeStr, "system"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
Detectors: []string{"env", "system"},
DetectorConfig: DetectorConfig{
SystemConfig: system.Config{
HostnameSources: []string{"os"},
},
},
HTTPClientSettings: cfg,
Override: false,
Attributes: []string{"a", "b"},
},
},
{
id: config.NewComponentIDWithName(typeStr, "invalid"),
errorMessage: "hostname_sources contains invalid value: \"invalid_source\"",
},
}
for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

factory := NewFactory()
cfg := factory.CreateDefaultConfig()

sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalProcessor(sub, cfg))

if tt.expected == nil {
assert.EqualError(t, cfg.Validate(), tt.errorMessage)
return
}
assert.NoError(t, cfg.Validate())
assert.Equal(t, tt.expected, cfg)
})
}
assert.Equal(t, p4, p4e)
}

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

factory := NewFactory()
factories.Processors[typeStr] = factory

cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "invalid_config.yaml"), factories)
assert.Error(t, err)
assert.NotNil(t, cfg)
}

func TestGetConfigFromType(t *testing.T) {
Expand Down
31 changes: 20 additions & 11 deletions processor/resourcedetectionprocessor/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"testing"

"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/configtest"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/service/servicetest"
)

func TestCreateDefaultConfig(t *testing.T) {
Expand All @@ -50,18 +52,25 @@ func TestCreateProcessor(t *testing.T) {
}

func TestCreateConfigProcessors(t *testing.T) {
factory := NewFactory()
factories, _ := componenttest.NopFactories()
factories.Processors[typeStr] = factory

cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
assert.NoError(t, err)
assert.NotNil(t, cfg)
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

for k, v := range cfg.Processors {
for k := range cm.ToStringMap() {
// Check if all processor variations that are defined in test config can be actually created
t.Run(k.Name(), func(t *testing.T) {
tt, err := factory.CreateTracesProcessor(context.Background(), componenttest.NewNopProcessorCreateSettings(), v, consumertest.NewNop())
t.Run(k, func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()

sub, err := cm.Sub(k)
require.NoError(t, err)
require.NoError(t, config.UnmarshalProcessor(sub, cfg))

tt, err := factory.CreateTracesProcessor(
context.Background(),
componenttest.NewNopProcessorCreateSettings(),
cfg,
consumertest.NewNop(),
)
assert.NoError(t, err)
assert.NotNil(t, tt)
})
Expand Down
91 changes: 41 additions & 50 deletions processor/resourcedetectionprocessor/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
receivers:
nop:
resourcedetection:
resourcedetection/gce:
detectors: [env, gce]
timeout: 2s
override: false

processors:
resourcedetection:
resourcedetection/gce:
detectors: [env, gce]
timeout: 2s
override: false
resourcedetection/ec2:
detectors: [env, ec2]
timeout: 2s
override: false
ec2:
tags:
- ^tag1$
- ^tag2$
resourcedetection/ecs:
detectors: [env, ecs]
timeout: 2s
override: false
resourcedetection/system:
detectors: [env, system]
timeout: 2s
override: false
system:
hostname_sources: [os]
attributes: ["a", "b"]
resourcedetection/docker:
detectors: [env, docker]
timeout: 2s
override: false
resourcedetection/azure:
detectors: [env, azure]
timeout: 2s
override: false
resourcedetection/ec2:
detectors: [env, ec2]
timeout: 2s
override: false
ec2:
tags:
- ^tag1$
- ^tag2$

exporters:
nop:
resourcedetection/ecs:
detectors: [env, ecs]
timeout: 2s
override: false

service:
pipelines:
metrics:
receivers: [nop]
processors:
# Choose one depending on your cloud provider and environment:
# - resourcedetection/system
# - resourcedetection/docker
# - resourcedetection/gce
# - resourcedetection/ec2
# - resourcedetection/ecs
# - resourcedetection/azure
exporters: [nop]
resourcedetection/system:
detectors: [env, system]
timeout: 2s
override: false
system:
hostname_sources: [os]
attributes: ["a", "b"]

resourcedetection/docker:
detectors: [env, docker]
timeout: 2s
override: false

resourcedetection/azure:
detectors: [env, azure]
timeout: 2s
override: false

resourcedetection/invalid:
detectors: [env, system]
timeout: 2s
override: false
system:
hostname_sources: [invalid_source]
22 changes: 0 additions & 22 deletions processor/resourcedetectionprocessor/testdata/invalid_config.yaml

This file was deleted.

56 changes: 37 additions & 19 deletions processor/resourceprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,51 @@ import (
"testing"

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

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/attraction"
)

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

factories.Processors[typeStr] = NewFactory()
tests := []struct {
id config.ComponentID
expected config.Processor
}{
{
id: config.NewComponentIDWithName(typeStr, ""),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
AttributesActions: []attraction.ActionKeyValue{
{Key: "cloud.availability_zone", Value: "zone-1", Action: attraction.UPSERT},
{Key: "k8s.cluster.name", FromAttribute: "k8s-cluster", Action: attraction.INSERT},
{Key: "redundant-attribute", Action: attraction.DELETE},
},
},
},
{
id: config.NewComponentIDWithName(typeStr, "invalid"),
expected: createDefaultConfig(),
},
}

cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
assert.NoError(t, err)
assert.NotNil(t, cfg)
for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

assert.Equal(t, cfg.Processors[config.NewComponentID(typeStr)], &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
AttributesActions: []attraction.ActionKeyValue{
{Key: "cloud.availability_zone", Value: "zone-1", Action: attraction.UPSERT},
{Key: "k8s.cluster.name", FromAttribute: "k8s-cluster", Action: attraction.INSERT},
{Key: "redundant-attribute", Action: attraction.DELETE},
},
})
factory := NewFactory()
cfg := factory.CreateDefaultConfig()

sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalProcessor(sub, cfg))

assert.Equal(t, cfg.Processors[config.NewComponentIDWithName(typeStr, "invalid")], &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "invalid")),
})
assert.NoError(t, cfg.Validate())
assert.Equal(t, tt.expected, cfg)
})
}
}
Loading

0 comments on commit bbc6b16

Please sign in to comment.