Skip to content

Commit

Permalink
[chore] [receiver/rabbitmq] Use confighttp.NewDefaultClientConfig ins…
Browse files Browse the repository at this point in the history
…tead 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
  • Loading branch information
mackjmr authored Oct 15, 2024
1 parent 389ba36 commit 17e5fee
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 54 deletions.
25 changes: 13 additions & 12 deletions receiver/rabbitmqreceiver/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
38 changes: 17 additions & 21 deletions receiver/rabbitmqreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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`),
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,
},
expectedErr: nil,
},
Expand Down
11 changes: 6 additions & 5 deletions receiver/rabbitmqreceiver/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/rabbitmqreceiver/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/rabbitmqreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
},
Expand All @@ -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(),
},
Expand Down

0 comments on commit 17e5fee

Please sign in to comment.