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. (Part 2) #14476

Merged
merged 1 commit 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
435 changes: 207 additions & 228 deletions receiver/jmxreceiver/config_test.go

Large diffs are not rendered by default.

134 changes: 60 additions & 74 deletions receiver/jmxreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,74 +1,60 @@
receivers:
jmx:
jmx/all:
jar_path: testdata/fake_jmx.jar
endpoint: myendpoint:12345
target_system: jvm
collection_interval: 15s
username: myusername
password: mypassword
otlp:
endpoint: myotlpendpoint
headers:
x-header-1: value1
x-header-2: value2
timeout: 5s
keystore_path: mykeystorepath
keystore_password: mykeystorepassword
keystore_type: mykeystoretype
truststore_path: mytruststorepath
truststore_password: mytruststorepassword
remote_profile: myremoteprofile
realm: myrealm
log_level: trace
resource_attributes:
one: two
additional_jars:
- testdata/fake_additional.jar
jmx/missingendpoint:
jar_path: testdata/fake_jmx.jar
target_system: jvm
jmx/missingtarget:
jar_path: testdata/fake_jmx.jar
endpoint: service:jmx:rmi:///jndi/rmi://host:12345/jmxrmi
jmx/invalidinterval:
jar_path: testdata/fake_jmx.jar
endpoint: myendpoint:23456
target_system: jvm
collection_interval: -100ms
jmx/invalidotlptimeout:
jar_path: testdata/fake_jmx.jar
endpoint: myendpoint:34567
target_system: jvm
otlp:
timeout: -100ms
jmx/nonexistentjar:
jar_path: testdata/file_does_not_exist.jar
endpoint: myendpoint:23456
target_system: jvm
jmx/invalidjar:
jar_path: testdata/fake_jmx_wrong.jar
endpoint: myendpoint:23456
target_system: jvm
jmx/invalidloglevel:
jar_path: testdata/fake_jmx.jar
endpoint: myendpoint:55555
target_system: jvm
log_level: truth
jmx/invalidtargetsystem:
jar_path: testdata/fake_jmx.jar
endpoint: myendpoint:55555
target_system: jvm,fakejvmtechnology

processors:
nop:

exporters:
nop:

service:
pipelines:
metrics:
receivers: [jmx, jmx/all]
processors: [nop]
exporters: [nop]
jmx:
jmx/all:
jar_path: testdata/fake_jmx.jar
endpoint: myendpoint:12345
target_system: jvm
collection_interval: 15s
username: myusername
password: mypassword
otlp:
endpoint: myotlpendpoint
headers:
x-header-1: value1
x-header-2: value2
timeout: 5s
keystore_path: mykeystorepath
keystore_password: mykeystorepassword
keystore_type: mykeystoretype
truststore_path: mytruststorepath
truststore_password: mytruststorepassword
remote_profile: myremoteprofile
realm: myrealm
log_level: trace
resource_attributes:
one: two
additional_jars:
- testdata/fake_additional.jar
jmx/missingendpoint:
jar_path: testdata/fake_jmx.jar
target_system: jvm
jmx/missingtarget:
jar_path: testdata/fake_jmx.jar
endpoint: service:jmx:rmi:///jndi/rmi://host:12345/jmxrmi
jmx/invalidinterval:
jar_path: testdata/fake_jmx.jar
endpoint: myendpoint:23456
target_system: jvm
collection_interval: -100ms
jmx/invalidotlptimeout:
jar_path: testdata/fake_jmx.jar
endpoint: myendpoint:34567
target_system: jvm
otlp:
timeout: -100ms
jmx/nonexistentjar:
jar_path: testdata/file_does_not_exist.jar
endpoint: myendpoint:23456
target_system: jvm
jmx/invalidjar:
jar_path: testdata/fake_jmx_wrong.jar
endpoint: myendpoint:23456
target_system: jvm
jmx/invalidloglevel:
jar_path: testdata/fake_jmx.jar
endpoint: myendpoint:55555
target_system: jvm
log_level: truth
jmx/invalidtargetsystem:
jar_path: testdata/fake_jmx.jar
endpoint: myendpoint:55555
target_system: jvm,fakejvmtechnology
18 changes: 8 additions & 10 deletions receiver/journaldreceiver/journald_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,25 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/service/servicetest"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/journald"
)

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)
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
require.NotNil(t, cfg)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()

assert.Equal(t, len(cfg.Receivers), 1)
sub, err := cm.Sub(config.NewComponentIDWithName(typeStr, "").String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

assert.Equal(t, testdataConfigYaml(), cfg.Receivers[config.NewComponentID("journald")])
assert.Equal(t, testdataConfigYaml(), cfg)
}

func TestInputConfigFailure(t *testing.T) {
Expand Down
24 changes: 5 additions & 19 deletions receiver/journaldreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
receivers:
journald:
units:
- ssh
priority: info
directory: /run/log/journal

processors:
nop:

exporters:
nop:

service:
pipelines:
logs:
receivers: [journald]
processors: [nop]
exporters: [nop]
journald:
units:
- ssh
priority: info
directory: /run/log/journal
85 changes: 49 additions & 36 deletions receiver/k8sclusterreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,68 @@ 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"

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

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

factory := NewFactory()
receiverType := "k8s_cluster"
factories.Receivers[config.Type(receiverType)] = 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)

assert.Equal(t, len(cfg.Receivers), 3)
tests := []struct {
id config.ComponentID
expected config.Receiver
expectedErr error
}{
{
id: config.NewComponentIDWithName(typeStr, ""),
expected: createDefaultConfig(),
},
{
id: config.NewComponentIDWithName(typeStr, "all_settings"),
expected: &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
Distribution: distributionKubernetes,
CollectionInterval: 30 * time.Second,
NodeConditionTypesToReport: []string{"Ready", "MemoryPressure"},
AllocatableTypesToReport: []string{"cpu", "memory"},
MetadataExporters: []string{"nop"},
APIConfig: k8sconfig.APIConfig{
AuthType: k8sconfig.AuthTypeServiceAccount,
},
},
},
{
id: config.NewComponentIDWithName(typeStr, "partial_settings"),
expected: &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
Distribution: distributionOpenShift,
CollectionInterval: 30 * time.Second,
NodeConditionTypesToReport: []string{"Ready"},
APIConfig: k8sconfig.APIConfig{
AuthType: k8sconfig.AuthTypeServiceAccount,
},
},
},
}

r1 := cfg.Receivers[config.NewComponentID(typeStr)]
assert.Equal(t, r1, factory.CreateDefaultConfig())
for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()

r2 := cfg.Receivers[config.NewComponentIDWithName(typeStr, "all_settings")].(*Config)
assert.Equal(t, r2,
&Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentIDWithName(typeStr, "all_settings")),
Distribution: distributionKubernetes,
CollectionInterval: 30 * time.Second,
NodeConditionTypesToReport: []string{"Ready", "MemoryPressure"},
AllocatableTypesToReport: []string{"cpu", "memory"},
MetadataExporters: []string{"nop"},
APIConfig: k8sconfig.APIConfig{
AuthType: k8sconfig.AuthTypeServiceAccount,
},
})
sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalReceiver(sub, cfg))

r3 := cfg.Receivers[config.NewComponentIDWithName(typeStr, "partial_settings")].(*Config)
assert.Equal(t, r3,
&Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentIDWithName(typeStr, "partial_settings")),
Distribution: distributionOpenShift,
CollectionInterval: 30 * time.Second,
NodeConditionTypesToReport: []string{"Ready"},
APIConfig: k8sconfig.APIConfig{
AuthType: k8sconfig.AuthTypeServiceAccount,
},
assert.NoError(t, cfg.Validate())
assert.Equal(t, tt.expected, cfg)
})
}
}

func TestInvalidConfig(t *testing.T) {
Expand Down
33 changes: 9 additions & 24 deletions receiver/k8sclusterreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
receivers:
k8s_cluster:
k8s_cluster/all_settings:
collection_interval: 30s
node_conditions_to_report: ["Ready", "MemoryPressure"]
allocatable_types_to_report: ["cpu","memory"]
metadata_exporters: [nop]
k8s_cluster/partial_settings:
collection_interval: 30s
distribution: openshift


processors:
nop:

exporters:
nop:

service:
pipelines:
metrics:
receivers: [k8s_cluster]
processors: [nop]
exporters: [nop]
k8s_cluster:
k8s_cluster/all_settings:
collection_interval: 30s
node_conditions_to_report: [ "Ready", "MemoryPressure" ]
allocatable_types_to_report: [ "cpu","memory" ]
metadata_exporters: [ nop ]
k8s_cluster/partial_settings:
collection_interval: 30s
distribution: openshift
Loading