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 receiver config tests to unmarshal config only for that component. (part4) #14562

Merged
merged 2 commits into from
Sep 28, 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
21 changes: 10 additions & 11 deletions receiver/pulsarreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,19 @@ 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)

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

r := cfg.Receivers[config.NewComponentID(typeStr)].(*Config)
sub, err := cm.Sub(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

assert.Equal(t, &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
Expand All @@ -46,5 +43,7 @@ func TestLoadConfig(t *testing.T) {
Encoding: defaultEncoding,
TLSTrustCertsFilePath: "ca.pem",
Authentication: Authentication{TLS: &TLS{CertFile: "cert.pem", KeyFile: "key.pem"}},
}, r)
},
cfg,
)
}
36 changes: 11 additions & 25 deletions receiver/pulsarreceiver/testdata/config.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
receivers:
pulsar:
topic: otel-pulsar
endpoint: pulsar://localhost:6500
consumer_name: otel-collector
subscription: otel-collector
tls_trust_certs_file_path: ca.pem
tls_allow_insecure_connection: false
auth:
tls:
cert_file: cert.pem
key_file: key.pem

processors:
nop:

exporters:
nop:

service:
pipelines:
traces:
receivers: [ pulsar ]
processors: [ nop ]
exporters: [ nop ]
pulsar:
topic: otel-pulsar
endpoint: pulsar://localhost:6500
consumer_name: otel-collector
subscription: otel-collector
tls_trust_certs_file_path: ca.pem
tls_allow_insecure_connection: false
auth:
tls:
cert_file: cert.pem
key_file: key.pem
24 changes: 5 additions & 19 deletions receiver/rabbitmqreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
receivers:
rabbitmq:
endpoint: http://localhost:15672
username: otelu
password: $RABBITMQ_PASSWORD
collection_interval: 10s

processors:
nop:

exporters:
nop:

service:
pipelines:
metrics:
receivers: [rabbitmq]
processors: [nop]
exporters: [nop]
rabbitmq:
endpoint: http://localhost:15672
username: otelu
password: $RABBITMQ_PASSWORD
collection_interval: 10s
15 changes: 7 additions & 8 deletions receiver/redisreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,24 @@ 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/confignet"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.opentelemetry.io/collector/service/servicetest"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver/internal/metadata"
)

func TestConfig(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.Receivers[factory.Type()] = factory
cfg := factory.CreateDefaultConfig()

cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
sub, err := cm.Sub(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NotNil(t, cfg)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

assert.Equal(t,
&Config{
Expand All @@ -57,6 +56,6 @@ func TestConfig(t *testing.T) {
},
Metrics: metadata.DefaultMetricsSettings(),
},
cfg.Receivers[config.NewComponentID(typeStr)],
cfg,
)
}
26 changes: 6 additions & 20 deletions receiver/redisreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
receivers:
redis:
endpoint: "localhost:6379"
password: "test"
collection_interval: 10s
tls:
insecure: true

processors:
nop:

exporters:
nop:

service:
pipelines:
traces:
receivers: [redis]
processors: [nop]
exporters: [nop]
redis:
endpoint: "localhost:6379"
password: "test"
collection_interval: 10s
tls:
insecure: true
24 changes: 5 additions & 19 deletions receiver/riakreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
receivers:
riak:
endpoint: http://localhost:8098
username: otelu
password: $RIAK_PASSWORD
collection_interval: 10s

processors:
nop:

exporters:
nop:

service:
pipelines:
metrics:
receivers: [riak]
processors: [nop]
exporters: [nop]
riak:
endpoint: http://localhost:8098
username: otelu
password: $RIAK_PASSWORD
collection_interval: 10s
30 changes: 8 additions & 22 deletions receiver/saphanareceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
receivers:
saphana:
metrics:
saphana.cpu.used:
enabled: false
endpoint: example.com:30015
username: otel
password: password
collection_interval: 2m

processors:
nop:

exporters:
nop:

service:
pipelines:
metrics:
receivers: [saphana]
processors: [nop]
exporters: [nop]
saphana:
metrics:
saphana.cpu.used:
enabled: false
endpoint: example.com:30015
username: otel
password: password
collection_interval: 2m
101 changes: 56 additions & 45 deletions receiver/sapmreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,66 +20,77 @@ 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/service/servicetest"
"go.opentelemetry.io/collector/confmap/confmaptest"

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

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

factory := NewFactory()
factories.Receivers[typeStr] = factory
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
t.Parallel()

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

// The receiver `sapm/disabled` doesn't count because disabled receivers
// are excluded from the final list.
assert.Equal(t, len(cfg.Receivers), 4)

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

r1 := cfg.Receivers[config.NewComponentIDWithName(typeStr, "customname")].(*Config)
assert.Equal(t, r1,
&Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentIDWithName(typeStr, "customname")),
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "0.0.0.0:7276",
tests := []struct {
id config.ComponentID
expected config.Receiver
}{
{
id: config.NewComponentIDWithName(typeStr, ""),
expected: createDefaultConfig(),
},
{
id: config.NewComponentIDWithName(typeStr, "customname"),
expected: &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "0.0.0.0:7276",
},
},
})

r2 := cfg.Receivers[config.NewComponentIDWithName(typeStr, "tls")].(*Config)
assert.Equal(t, r2,
&Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentIDWithName(typeStr, "tls")),
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: ":7276",
TLSSetting: &configtls.TLSServerSetting{
TLSSetting: configtls.TLSSetting{
CertFile: "/test.crt",
KeyFile: "/test.key",
},
{
id: config.NewComponentIDWithName(typeStr, "tls"),
expected: &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: ":7276",
TLSSetting: &configtls.TLSServerSetting{
TLSSetting: configtls.TLSSetting{
CertFile: "/test.crt",
KeyFile: "/test.key",
},
},
},
},
})

r3 := cfg.Receivers[config.NewComponentIDWithName(typeStr, "passthrough")].(*Config)
assert.Equal(t, r3,
&Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentIDWithName(typeStr, "passthrough")),
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: ":7276",
},
AccessTokenPassthroughConfig: splunk.AccessTokenPassthroughConfig{
AccessTokenPassthrough: true,
},
{
id: config.NewComponentIDWithName(typeStr, "passthrough"),
expected: &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: ":7276",
},
AccessTokenPassthroughConfig: splunk.AccessTokenPassthroughConfig{
AccessTokenPassthrough: true,
},
},
},
}

for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()

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

assert.NoError(t, cfg.Validate())
assert.Equal(t, tt.expected, cfg)
})
}
}
48 changes: 16 additions & 32 deletions receiver/sapmreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
receivers:
# The following demonstrates initializing the default sapm receiver.
# By sapm-http is enabled and the default endpoint is specified in factory.go
sapm:
# The following demonstrates specifying different endpoints.
# The SAPM receiver connects to ports on all available network interfaces.
# Ex: `endpoint: "7276"` is incorrect.
# Ex: `endpoint: "1.2.3.4:7276"` and ":7276" is correct
sapm/customname:
endpoint: "0.0.0.0:7276"
# The following demonstrates initializing the default sapm receiver.
# By sapm-http is enabled and the default endpoint is specified in factory.go
sapm:
# The following demonstrates specifying different endpoints.
# The SAPM receiver connects to ports on all available network interfaces.
# Ex: `endpoint: "7276"` is incorrect.
# Ex: `endpoint: "1.2.3.4:7276"` and ":7276" is correct
sapm/customname:
endpoint: "0.0.0.0:7276"

# The following demonstrates how to specify TLS for the receiver.
sapm/tls:
tls:
cert_file: /test.crt
key_file: /test.key

sapm/passthrough:
access_token_passthrough: true


processors:
nop:

exporters:
nop:

service:
pipelines:
traces:
receivers: [sapm]
processors: [nop]
exporters: [nop]
# The following demonstrates how to specify TLS for the receiver.
sapm/tls:
tls:
cert_file: /test.crt
key_file: /test.key

sapm/passthrough:
access_token_passthrough: true
Loading