Skip to content

Commit

Permalink
chore: use default confighttp struct for tests (open-telemetry#36681)
Browse files Browse the repository at this point in the history
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### 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:
open-telemetry/opentelemetry-collector#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
          }
```
          

<!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Fixes

<!--Describe what testing was performed and which tests were added.-->
#### Testing

<!--Describe the documentation added.-->
#### Documentation

<!--Please delete paragraphs that you did not use before submitting.-->
  • Loading branch information
rogercoll authored and ZenoCC-Peng committed Dec 6, 2024
1 parent 39b7950 commit a8c8bfe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
28 changes: 12 additions & 16 deletions exporter/alertmanagerexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package alertmanagerexporter

import (
"net/http"
"path/filepath"
"testing"
"time"
Expand All @@ -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"))
Expand Down Expand Up @@ -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
}(),
},
},
}
Expand Down
22 changes: 7 additions & 15 deletions receiver/elasticsearchreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package elasticsearchreceiver

import (
"net/http"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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
}(),
},
},
}
Expand Down

0 comments on commit a8c8bfe

Please sign in to comment.