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] [receiver/riak] Use confighttp.NewDefaultClientConfig instead of manually creating struct #35650

Merged
merged 1 commit into from
Oct 15, 2024
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
25 changes: 13 additions & 12 deletions receiver/riakreceiver/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ const (
)

func TestNewClient(t *testing.T) {
clientConfigNonExistandCA := confighttp.NewDefaultClientConfig()
clientConfigNonExistandCA.Endpoint = defaultEndpoint
clientConfigNonExistandCA.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
}

clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint

testCase := []struct {
desc string
cfg *Config
Expand All @@ -36,24 +47,14 @@ func TestNewClient(t *testing.T) {
{
desc: "Invalid HTTP config",
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
},
},
ClientConfig: clientConfigNonExistandCA,
},
expectError: errors.New("failed to create HTTP Client"),
},
{
desc: "Valid Configuration",
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
TLSSetting: configtls.ClientConfig{},
Endpoint: defaultEndpoint,
},
ClientConfig: clientConfig,
},
expectError: nil,
},
Expand Down
38 changes: 17 additions & 21 deletions receiver/riakreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
)

func TestValidate(t *testing.T) {
clientConfigInvalid := confighttp.NewDefaultClientConfig()
clientConfigInvalid.Endpoint = "invalid://endpoint: 12efg"

clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint

testCases := []struct {
desc string
cfg *Config
Expand All @@ -22,9 +28,7 @@ func TestValidate(t *testing.T) {
{
desc: "missing username, password, and invalid endpoint",
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "invalid://endpoint: 12efg",
},
ClientConfig: clientConfigInvalid,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: multierr.Combine(
Expand All @@ -36,10 +40,8 @@ func TestValidate(t *testing.T) {
{
desc: "missing password and invalid endpoint",
cfg: &Config{
Username: "otelu",
ClientConfig: confighttp.ClientConfig{
Endpoint: "invalid://endpoint: 12efg",
},
Username: "otelu",
ClientConfig: clientConfigInvalid,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: multierr.Combine(
Expand All @@ -50,10 +52,8 @@ func TestValidate(t *testing.T) {
{
desc: "missing username and invalid endpoint",
cfg: &Config{
Password: "otelp",
ClientConfig: confighttp.ClientConfig{
Endpoint: "invalid://endpoint: 12efg",
},
Password: "otelp",
ClientConfig: clientConfigInvalid,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: multierr.Combine(
Expand All @@ -64,11 +64,9 @@ func TestValidate(t *testing.T) {
{
desc: "invalid endpoint",
cfg: &Config{
Username: "otelu",
Password: "otelp",
ClientConfig: confighttp.ClientConfig{
Endpoint: "invalid://endpoint: 12efg",
},
Username: "otelu",
Password: "otelp",
ClientConfig: clientConfigInvalid,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: multierr.Combine(
Expand All @@ -78,11 +76,9 @@ func TestValidate(t *testing.T) {
{
desc: "valid config",
cfg: &Config{
Username: "otelu",
Password: "otelp",
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
},
Username: "otelu",
Password: "otelp",
ClientConfig: clientConfig,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: nil,
Expand Down
11 changes: 6 additions & 5 deletions receiver/riakreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ func createDefaultConfig() component.Config {
cfg := scraperhelper.NewDefaultControllerConfig()
cfg.CollectionInterval = 10 * time.Second

clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint
clientConfig.Timeout = 10 * time.Second

return &Config{
ControllerConfig: cfg,
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
Timeout: 10 * time.Second,
},
ControllerConfig: cfg,
ClientConfig: clientConfig,
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
}
}
Expand Down
9 changes: 5 additions & 4 deletions receiver/riakreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
)

func TestNewFactory(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint
clientConfig.Timeout = 10 * time.Second

testCases := []struct {
desc string
testFunc func(*testing.T)
Expand All @@ -40,10 +44,7 @@ func TestNewFactory(t *testing.T) {
CollectionInterval: 10 * time.Second,
InitialDelay: time.Second,
},
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
Timeout: 10 * time.Second,
},
ClientConfig: clientConfig,
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
}

Expand Down
25 changes: 13 additions & 12 deletions receiver/riakreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ import (
)

func TestScraperStart(t *testing.T) {
clientConfigNonExistandCA := confighttp.NewDefaultClientConfig()
clientConfigNonExistandCA.Endpoint = defaultEndpoint
clientConfigNonExistandCA.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
}

clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint

testcases := []struct {
desc string
scraper *riakScraper
Expand All @@ -36,14 +47,7 @@ func TestScraperStart(t *testing.T) {
desc: "Bad Config",
scraper: &riakScraper{
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
},
},
ClientConfig: clientConfigNonExistandCA,
},
settings: componenttest.NewNopTelemetrySettings(),
},
Expand All @@ -54,10 +58,7 @@ func TestScraperStart(t *testing.T) {
desc: "Valid Config",
scraper: &riakScraper{
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
TLSSetting: configtls.ClientConfig{},
Endpoint: defaultEndpoint,
},
ClientConfig: clientConfig,
},
settings: componenttest.NewNopTelemetrySettings(),
},
Expand Down