From 4883180ccd6943bb2ea366aa2a565ac846dd033f Mon Sep 17 00:00:00 2001 From: Mackenzie <63265430+mackjmr@users.noreply.github.com> Date: Tue, 15 Oct 2024 17:20:17 +0200 Subject: [PATCH] [chore] [receiver/rabbitmq] Use confighttp.NewDefaultClientConfig instead of manually creating struct (#35647) **Description:** This PR makes usage of `NewDefaultClientConfig` instead of manually creating the confighttp.ClientConfig struct. **Link to tracking Issue:** #35457 --- receiver/rabbitmqreceiver/client_test.go | 25 ++++++++------- receiver/rabbitmqreceiver/config_test.go | 38 ++++++++++------------- receiver/rabbitmqreceiver/factory.go | 11 ++++--- receiver/rabbitmqreceiver/factory_test.go | 9 +++--- receiver/rabbitmqreceiver/scraper_test.go | 25 ++++++++------- 5 files changed, 54 insertions(+), 54 deletions(-) diff --git a/receiver/rabbitmqreceiver/client_test.go b/receiver/rabbitmqreceiver/client_test.go index 8952e8792297..afbc7f69c2e6 100644 --- a/receiver/rabbitmqreceiver/client_test.go +++ b/receiver/rabbitmqreceiver/client_test.go @@ -29,6 +29,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 @@ -40,14 +51,7 @@ 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, }, host: componenttest.NewNopHost(), settings: componenttest.NewNopTelemetrySettings(), @@ -57,10 +61,7 @@ func TestNewClient(t *testing.T) { { desc: "Valid Configuration", cfg: &Config{ - ClientConfig: confighttp.ClientConfig{ - TLSSetting: configtls.ClientConfig{}, - Endpoint: defaultEndpoint, - }, + ClientConfig: clientConfig, }, host: componenttest.NewNopHost(), settings: componenttest.NewNopTelemetrySettings(), diff --git a/receiver/rabbitmqreceiver/config_test.go b/receiver/rabbitmqreceiver/config_test.go index 258386e6486d..f95175d341b2 100644 --- a/receiver/rabbitmqreceiver/config_test.go +++ b/receiver/rabbitmqreceiver/config_test.go @@ -19,6 +19,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 @@ -27,9 +33,7 @@ func TestValidate(t *testing.T) { { desc: "missing username, password, and invalid endpoint", cfg: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: "invalid://endpoint: 12efg", - }, + ClientConfig: clientConfigInvalid, }, expectedErr: errors.Join( errMissingUsername, @@ -39,10 +43,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, }, expectedErr: errors.Join( errMissingPassword, @@ -52,10 +54,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, }, expectedErr: errors.Join( errMissingUsername, @@ -65,11 +65,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, }, expectedErr: errors.Join( fmt.Errorf("%w: %s", errInvalidEndpoint, `parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`), @@ -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, }, expectedErr: nil, }, diff --git a/receiver/rabbitmqreceiver/factory.go b/receiver/rabbitmqreceiver/factory.go index 7b298c48e51c..1dbdd7479cab 100644 --- a/receiver/rabbitmqreceiver/factory.go +++ b/receiver/rabbitmqreceiver/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/rabbitmqreceiver/factory_test.go b/receiver/rabbitmqreceiver/factory_test.go index e94d77bc5c67..e523c4240460 100644 --- a/receiver/rabbitmqreceiver/factory_test.go +++ b/receiver/rabbitmqreceiver/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/rabbitmqreceiver/scraper_test.go b/receiver/rabbitmqreceiver/scraper_test.go index f994dc9be40d..7c97bb737ca0 100644 --- a/receiver/rabbitmqreceiver/scraper_test.go +++ b/receiver/rabbitmqreceiver/scraper_test.go @@ -26,6 +26,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 *rabbitmqScraper @@ -35,14 +46,7 @@ func TestScraperStart(t *testing.T) { desc: "Bad Config", scraper: &rabbitmqScraper{ cfg: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - TLSSetting: configtls.ClientConfig{ - Config: configtls.Config{ - CAFile: "/non/existent", - }, - }, - }, + ClientConfig: clientConfigNonExistandCA, }, settings: componenttest.NewNopTelemetrySettings(), }, @@ -52,10 +56,7 @@ func TestScraperStart(t *testing.T) { desc: "Valid Config", scraper: &rabbitmqScraper{ cfg: &Config{ - ClientConfig: confighttp.ClientConfig{ - TLSSetting: configtls.ClientConfig{}, - Endpoint: defaultEndpoint, - }, + ClientConfig: clientConfig, }, settings: componenttest.NewNopTelemetrySettings(), },