Skip to content

Commit

Permalink
[chore] [receiver/riak] Use confighttp.NewDefaultClientConfig instead…
Browse files Browse the repository at this point in the history
… of manually creating struct (#35650)

**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 a39906a commit 389ba36
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 54 deletions.
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

0 comments on commit 389ba36

Please sign in to comment.