Skip to content

Commit

Permalink
[chore] [receiver/bigip] Use confighttp.NewDefaultClientConfig instea…
Browse files Browse the repository at this point in the history
…d of manually creating struct (#35586)

**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 4, 2024
1 parent 6fb3266 commit dac54a2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 53 deletions.
26 changes: 14 additions & 12 deletions receiver/bigipreceiver/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ const (
)

func TestNewClient(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.TLSSetting = configtls.ClientConfig{}
clientConfig.Endpoint = defaultEndpoint

clientConfigNonExistentCA := confighttp.NewDefaultClientConfig()
clientConfigNonExistentCA.Endpoint = defaultEndpoint
clientConfigNonExistentCA.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
}

testCase := []struct {
desc string
cfg *Config
Expand All @@ -49,14 +61,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: clientConfigNonExistentCA,
},
host: componenttest.NewNopHost(),
settings: componenttest.NewNopTelemetrySettings(),
Expand All @@ -66,10 +71,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/bigipreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
)

func TestValidate(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint

clientConfigInvalid := confighttp.NewDefaultClientConfig()
clientConfigInvalid.Endpoint = "invalid://endpoint: 12efg"
defaultConfig := createDefaultConfig().(*Config)
defaultConfig.Username = "otelu"
defaultConfig.Password = "otelp"
Expand All @@ -31,9 +36,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 @@ -45,10 +48,9 @@ func TestValidate(t *testing.T) {
{
desc: "missing password and invalid endpoint",
cfg: &Config{
Username: "otelu",
ClientConfig: confighttp.ClientConfig{
Endpoint: "invalid://endpoint: 12efg",
}, ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Username: "otelu",
ClientConfig: clientConfigInvalid,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: multierr.Combine(
errMissingPassword,
Expand All @@ -58,10 +60,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 @@ -72,11 +72,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 @@ -86,11 +84,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
8 changes: 4 additions & 4 deletions receiver/bigipreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func NewFactory() receiver.Factory {

// createDefaultConfig creates a config for Big-IP with as many default values as possible
func createDefaultConfig() component.Config {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint
clientConfig.Timeout = 10 * time.Second
return &Config{
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: 10 * time.Second,
},
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
Timeout: 10 * time.Second,
},
ClientConfig: clientConfig,
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
}
}
Expand Down
8 changes: 4 additions & 4 deletions receiver/bigipreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ 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 @@ -39,10 +42,7 @@ func TestNewFactory(t *testing.T) {
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: 10 * time.Second,
},
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
Timeout: 10 * time.Second,
},
ClientConfig: clientConfig,
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
}

Expand Down
26 changes: 14 additions & 12 deletions receiver/bigipreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ import (
)

func TestScraperStart(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.TLSSetting = configtls.ClientConfig{}
clientConfig.Endpoint = defaultEndpoint

clientConfigNonExistentCA := confighttp.NewDefaultClientConfig()
clientConfigNonExistentCA.Endpoint = defaultEndpoint
clientConfigNonExistentCA.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
}

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

0 comments on commit dac54a2

Please sign in to comment.