-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
config_test.go
204 lines (183 loc) · 5.61 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package elasticsearchreceiver
import (
"net/http"
"path/filepath"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/elasticsearchreceiver/internal/metadata"
)
func TestValidateCredentials(t *testing.T) {
testCases := []struct {
desc string
run func(t *testing.T)
}{
{
desc: "Password is empty, username specified",
run: func(t *testing.T) {
t.Parallel()
cfg := NewFactory().CreateDefaultConfig().(*Config)
cfg.Username = "user"
require.ErrorIs(t, component.ValidateConfig(cfg), errPasswordNotSpecified)
},
},
{
desc: "Username is empty, password specified",
run: func(t *testing.T) {
t.Parallel()
cfg := NewFactory().CreateDefaultConfig().(*Config)
cfg.Password = "pass"
require.ErrorIs(t, component.ValidateConfig(cfg), errUsernameNotSpecified)
},
},
{
desc: "Username and password are both specified",
run: func(t *testing.T) {
t.Parallel()
cfg := NewFactory().CreateDefaultConfig().(*Config)
cfg.Username = "user"
cfg.Password = "pass"
require.NoError(t, component.ValidateConfig(cfg))
},
},
{
desc: "Username and password are both not specified",
run: func(t *testing.T) {
t.Parallel()
cfg := NewFactory().CreateDefaultConfig().(*Config)
require.NoError(t, component.ValidateConfig(cfg))
},
},
}
for _, testCase := range testCases {
t.Run(testCase.desc, testCase.run)
}
}
func TestValidateEndpoint(t *testing.T) {
testCases := []struct {
desc string
rawURL string
expectedErr error
expectedErrStr string
}{
{
desc: "Default endpoint",
rawURL: defaultEndpoint,
},
{
desc: "Empty endpoint",
rawURL: "",
expectedErr: errEmptyEndpoint,
},
{
desc: "Endpoint with no scheme",
rawURL: "localhost",
expectedErr: errEndpointBadScheme,
},
{
desc: "Endpoint with unusable scheme",
rawURL: "tcp://192.168.1.0",
expectedErr: errEndpointBadScheme,
},
{
desc: "URL with control characters",
rawURL: "http://\x00",
expectedErrStr: "invalid endpoint",
},
{
desc: "Https url",
rawURL: "https://example.com",
},
{
desc: "IP + port URL",
rawURL: "https://192.168.1.1:9200",
},
}
for i := range testCases {
// Explicitly capture the testCase in this scope instead of using a loop variable;
// The loop variable will mutate, and all tests will run with the parameters of the last case,
// if we don't do this
testCase := testCases[i]
t.Run(testCase.desc, func(t *testing.T) {
t.Parallel()
cfg := NewFactory().CreateDefaultConfig().(*Config)
cfg.Endpoint = testCase.rawURL
err := component.ValidateConfig(cfg)
switch {
case testCase.expectedErr != nil:
require.ErrorIs(t, err, testCase.expectedErr)
case testCase.expectedErrStr != "":
require.ErrorContains(t, err, testCase.expectedErrStr)
default:
require.NoError(t, err)
}
})
}
}
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"))
require.NoError(t, err)
defaultMetrics := metadata.DefaultMetricsBuilderConfig()
defaultMetrics.Metrics.ElasticsearchNodeFsDiskAvailable.Enabled = false
tests := []struct {
id component.ID
expected component.Config
}{
{
id: component.NewIDWithName(metadata.Type, "defaults"),
expected: createDefaultConfig(),
},
{
id: component.NewIDWithName(metadata.Type, ""),
expected: &Config{
SkipClusterMetrics: true,
Nodes: []string{"_local"},
Indices: []string{".geoip_databases"},
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: 2 * time.Minute,
InitialDelay: time.Second,
},
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,
},
},
},
}
for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, sub.Unmarshal(cfg))
assert.NoError(t, component.ValidateConfig(cfg))
if diff := cmp.Diff(tt.expected, cfg, cmpopts.IgnoreUnexported(metadata.MetricConfig{}), cmpopts.IgnoreUnexported(metadata.ResourceAttributeConfig{})); diff != "" {
t.Errorf("Config mismatch (-expected +actual):\n%s", diff)
}
})
}
}