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. (part5) #14563

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
115 changes: 64 additions & 51 deletions receiver/splunkhecreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,72 +20,85 @@ 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)

assert.Equal(t, len(cfg.Receivers), 3)

r0 := cfg.Receivers[config.NewComponentID(typeStr)].(*Config)
assert.Equal(t, r0, createDefaultConfig())

r1 := cfg.Receivers[config.NewComponentIDWithName(typeStr, "allsettings")].(*Config)
expectedAllSettings := &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentIDWithName(typeStr, "allsettings")),
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "localhost:8088",
},
AccessTokenPassthroughConfig: splunk.AccessTokenPassthroughConfig{
AccessTokenPassthrough: true,
},
RawPath: "/foo",
HecToOtelAttrs: splunk.HecToOtelAttrs{
Source: "file.name",
SourceType: "foobar",
Index: "myindex",
Host: "myhostfield",
tests := []struct {
id config.ComponentID
expected config.Receiver
}{
{
id: config.NewComponentID(typeStr),
expected: createDefaultConfig(),
},
}
assert.Equal(t, expectedAllSettings, r1)

r2 := cfg.Receivers[config.NewComponentIDWithName(typeStr, "tls")].(*Config)
expectedTLSConfig := &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentIDWithName(typeStr, "tls")),
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: ":8088",
TLSSetting: &configtls.TLSServerSetting{
TLSSetting: configtls.TLSSetting{
CertFile: "/test.crt",
KeyFile: "/test.key",
{
id: config.NewComponentIDWithName(typeStr, "allsettings"),
expected: &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "localhost:8088",
},
AccessTokenPassthroughConfig: splunk.AccessTokenPassthroughConfig{
AccessTokenPassthrough: true,
},
RawPath: "/foo",
HecToOtelAttrs: splunk.HecToOtelAttrs{
Source: "file.name",
SourceType: "foobar",
Index: "myindex",
Host: "myhostfield",
},
},
},
AccessTokenPassthroughConfig: splunk.AccessTokenPassthroughConfig{
AccessTokenPassthrough: false,
},
RawPath: "/services/collector/raw",
HecToOtelAttrs: splunk.HecToOtelAttrs{
Source: "com.splunk.source",
SourceType: "com.splunk.sourcetype",
Index: "com.splunk.index",
Host: "host.name",
{
id: config.NewComponentIDWithName(typeStr, "tls"),
expected: &Config{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: ":8088",
TLSSetting: &configtls.TLSServerSetting{
TLSSetting: configtls.TLSSetting{
CertFile: "/test.crt",
KeyFile: "/test.key",
},
},
},
AccessTokenPassthroughConfig: splunk.AccessTokenPassthroughConfig{
AccessTokenPassthrough: false,
},
RawPath: "/services/collector/raw",
HecToOtelAttrs: splunk.HecToOtelAttrs{
Source: "com.splunk.source",
SourceType: "com.splunk.sourcetype",
Index: "com.splunk.index",
Host: "host.name",
},
},
},
}
assert.Equal(t, expectedTLSConfig, r2)

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)
})
}
}
46 changes: 16 additions & 30 deletions receiver/splunkhecreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
receivers:
splunk_hec:
splunk_hec/allsettings:
# endpoint specifies the network interface and port which will receive
# Splunk metrics.
endpoint: localhost:8088
access_token_passthrough: true
raw_path: "/foo"
hec_metadata_to_otel_attrs:
source: "file.name"
sourcetype: "foobar"
index: "myindex"
host: "myhostfield"
splunk_hec/tls:
tls:
cert_file: /test.crt
key_file: /test.key

processors:
nop:

exporters:
nop:

service:
pipelines:
metrics:
receivers: [splunk_hec, splunk_hec/allsettings]
processors: [nop]
exporters: [nop]
splunk_hec:
splunk_hec/allsettings:
# endpoint specifies the network interface and port which will receive
# Splunk metrics.
endpoint: localhost:8088
access_token_passthrough: true
raw_path: "/foo"
hec_metadata_to_otel_attrs:
source: "file.name"
sourcetype: "foobar"
index: "myindex"
host: "myhostfield"
splunk_hec/tls:
tls:
cert_file: /test.crt
key_file: /test.key
Loading