From a8c8bfe19643b670cc518da8000e5110a419ff54 Mon Sep 17 00:00:00 2001 From: Roger Coll Date: Fri, 6 Dec 2024 20:08:11 +0100 Subject: [PATCH] chore: use default confighttp struct for tests (#36681) #### Description These components are constructing `confighttp.ClientConfig` field by field and adding the corresponding defaults. Instead of using default values to construct a new struct, the new struct should already contain the defaults. This becomes an issue when adding new fields with defaults in the `ClientConfig` structure, see: https://github.com/open-telemetry/opentelemetry-collector/pull/10877 Error in the core collector CI: ``` === FAIL: . TestLoadConfig/elasticsearch (re-run 1) (0.00s) config_test.go:200: Config mismatch (-expected +actual): &elasticsearchreceiver.Config{ ControllerConfig: {CollectionInterval: s"2m0s", InitialDelay: s"1s"}, ClientConfig: confighttp.ClientConfig{ ... // 15 identical fields HTTP2PingTimeout: s"0s", Cookies: nil, - MaxRedirects: 0, + MaxRedirects: 10, }, MetricsBuilderConfig: {Metrics: {ElasticsearchBreakerMemoryEstimated: {Enabled: true}, ElasticsearchBreakerMemoryLimit: {Enabled: true}, ElasticsearchBreakerTripped: {Enabled: true}, ElasticsearchClusterDataNodes: {Enabled: true}, ...}, ResourceAttributes: {ElasticsearchClusterName: {Enabled: true}, ElasticsearchIndexName: {Enabled: true}, ElasticsearchNodeName: {Enabled: true}, ElasticsearchNodeVersion: {Enabled: true}}}, Nodes: {"_local"}, ... // 4 identical fields } ``` #### Link to tracking issue Fixes #### Testing #### Documentation --- exporter/alertmanagerexporter/config_test.go | 28 ++++++++----------- receiver/elasticsearchreceiver/config_test.go | 22 +++++---------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/exporter/alertmanagerexporter/config_test.go b/exporter/alertmanagerexporter/config_test.go index 07802fbc3e5a..0766c02704b6 100644 --- a/exporter/alertmanagerexporter/config_test.go +++ b/exporter/alertmanagerexporter/config_test.go @@ -4,7 +4,6 @@ package alertmanagerexporter import ( - "net/http" "path/filepath" "testing" "time" @@ -24,7 +23,6 @@ import ( ) func TestLoadConfig(t *testing.T) { - defaultTransport := http.DefaultTransport.(*http.Transport) t.Parallel() cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) @@ -63,26 +61,24 @@ func TestLoadConfig(t *testing.T) { NumConsumers: 2, QueueSize: 10, }, - ClientConfig: confighttp.ClientConfig{ - Headers: map[string]configopaque.String{ + ClientConfig: func() confighttp.ClientConfig { + client := confighttp.NewDefaultClientConfig() + client.Headers = map[string]configopaque.String{ "can you have a . here?": "F0000000-0000-0000-0000-000000000000", "header1": "234", "another": "somevalue", - }, - Endpoint: "a.new.alertmanager.target:9093", - TLSSetting: configtls.ClientConfig{ + } + client.Endpoint = "a.new.alertmanager.target:9093" + client.TLSSetting = configtls.ClientConfig{ Config: configtls.Config{ CAFile: "/var/lib/mycert.pem", }, - }, - ReadBufferSize: 0, - WriteBufferSize: 524288, - Timeout: time.Second * 10, - MaxIdleConns: &defaultTransport.MaxIdleConns, - MaxIdleConnsPerHost: &defaultTransport.MaxIdleConnsPerHost, - MaxConnsPerHost: &defaultTransport.MaxConnsPerHost, - IdleConnTimeout: &defaultTransport.IdleConnTimeout, - }, + } + client.ReadBufferSize = 0 + client.WriteBufferSize = 524288 + client.Timeout = time.Second * 10 + return client + }(), }, }, } diff --git a/receiver/elasticsearchreceiver/config_test.go b/receiver/elasticsearchreceiver/config_test.go index 149f9bed0b1f..4d6974984246 100644 --- a/receiver/elasticsearchreceiver/config_test.go +++ b/receiver/elasticsearchreceiver/config_test.go @@ -4,7 +4,6 @@ package elasticsearchreceiver import ( - "net/http" "path/filepath" "testing" "time" @@ -140,11 +139,6 @@ func TestValidateEndpoint(t *testing.T) { } func TestLoadConfig(t *testing.T) { - defaultMaxIdleConns := http.DefaultTransport.(*http.Transport).MaxIdleConns - defaultMaxIdleConnsPerHost := http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost - defaultMaxConnsPerHost := http.DefaultTransport.(*http.Transport).MaxConnsPerHost - defaultIdleConnTimeout := http.DefaultTransport.(*http.Transport).IdleConnTimeout - t.Parallel() cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) @@ -173,15 +167,13 @@ func TestLoadConfig(t *testing.T) { MetricsBuilderConfig: defaultMetrics, Username: "otel", Password: "password", - ClientConfig: confighttp.ClientConfig{ - Timeout: 10000000000, - Endpoint: "http://example.com:9200", - Headers: map[string]configopaque.String{}, - MaxIdleConns: &defaultMaxIdleConns, - MaxIdleConnsPerHost: &defaultMaxIdleConnsPerHost, - MaxConnsPerHost: &defaultMaxConnsPerHost, - IdleConnTimeout: &defaultIdleConnTimeout, - }, + ClientConfig: func() confighttp.ClientConfig { + client := confighttp.NewDefaultClientConfig() + client.Timeout = 10000000000 + client.Endpoint = "http://example.com:9200" + client.Headers = map[string]configopaque.String{} + return client + }(), }, }, }