From 1e7701eeb32406ef5af753971ade22f4f6a2c14a Mon Sep 17 00:00:00 2001 From: mackjmr Date: Mon, 7 Oct 2024 11:03:04 -0400 Subject: [PATCH] [chore] [receiver/riak] Use confighttp.NewDefaultClientConfig instead of manually creating struct **Description:** This PR makes usage of `NewDefaultClientConfig` instead of manually creating the confighttp.ClientConfig struct. **Link to tracking Issue:** #35457 --- receiver/riakreceiver/client_test.go | 25 +++++++++--------- receiver/riakreceiver/config_test.go | 38 ++++++++++++--------------- receiver/riakreceiver/factory.go | 11 ++++---- receiver/riakreceiver/factory_test.go | 9 ++++--- receiver/riakreceiver/scraper_test.go | 25 +++++++++--------- 5 files changed, 54 insertions(+), 54 deletions(-) diff --git a/receiver/riakreceiver/client_test.go b/receiver/riakreceiver/client_test.go index 6730fc71cccc..087816fbcd4e 100644 --- a/receiver/riakreceiver/client_test.go +++ b/receiver/riakreceiver/client_test.go @@ -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 @@ -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, }, diff --git a/receiver/riakreceiver/config_test.go b/receiver/riakreceiver/config_test.go index 0874b07b7531..a503e4d05e86 100644 --- a/receiver/riakreceiver/config_test.go +++ b/receiver/riakreceiver/config_test.go @@ -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 @@ -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( @@ -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( @@ -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( @@ -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( @@ -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, diff --git a/receiver/riakreceiver/factory.go b/receiver/riakreceiver/factory.go index 8e561388c9d2..bce4a5f7565b 100644 --- a/receiver/riakreceiver/factory.go +++ b/receiver/riakreceiver/factory.go @@ -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(), } } diff --git a/receiver/riakreceiver/factory_test.go b/receiver/riakreceiver/factory_test.go index 55857cf37d21..1e13b8d07c9f 100644 --- a/receiver/riakreceiver/factory_test.go +++ b/receiver/riakreceiver/factory_test.go @@ -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) @@ -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(), } diff --git a/receiver/riakreceiver/scraper_test.go b/receiver/riakreceiver/scraper_test.go index db89f903c54d..896c478bf39f 100644 --- a/receiver/riakreceiver/scraper_test.go +++ b/receiver/riakreceiver/scraper_test.go @@ -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 @@ -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(), }, @@ -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(), },