Debian Codegraph @

exporters/otlp/otlptrace/otlptracehttp/internal/envconfig/envconfig_test.go

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/otlp/envconfig/envconfig_test.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 package envconfig
8
9 import (
10 "crypto/tls"
11 "crypto/x509"
12 "errors"
13 "net/url"
14 "testing"
15 "time"
16
17 "github.com/stretchr/testify/assert"
18 )
19
20 const WeakKey = `
21 -----BEGIN EC PRIVATE KEY-----
22 MHcCAQEEIEbrSPmnlSOXvVzxCyv+VR3a0HDeUTvOcqrdssZ2k4gFoAoGCCqGSM49
23 AwEHoUQDQgAEDMTfv75J315C3K9faptS9iythKOMEeV/Eep73nWX531YAkmmwBSB
24 2dXRD/brsgLnfG57WEpxZuY7dPRbxu33BA==
25 -----END EC PRIVATE KEY-----
26 `
27
28 const WeakCertificate = `
29 -----BEGIN CERTIFICATE-----
30 MIIBjjCCATWgAwIBAgIUKQSMC66MUw+kPp954ZYOcyKAQDswCgYIKoZIzj0EAwIw
31 EjEQMA4GA1UECgwHb3RlbC1nbzAeFw0yMjEwMTkwMDA5MTlaFw0yMzEwMTkwMDA5
32 MTlaMBIxEDAOBgNVBAoMB290ZWwtZ28wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNC
33 AAQMxN+/vknfXkLcr19qm1L2LK2Eo4wR5X8R6nvedZfnfVgCSabAFIHZ1dEP9uuy
34 Aud8bntYSnFm5jt09FvG7fcEo2kwZzAdBgNVHQ4EFgQUicGuhnTTkYLZwofXMNLK
35 SHFeCWgwHwYDVR0jBBgwFoAUicGuhnTTkYLZwofXMNLKSHFeCWgwDwYDVR0TAQH/
36 BAUwAwEB/zAUBgNVHREEDTALgglsb2NhbGhvc3QwCgYIKoZIzj0EAwIDRwAwRAIg
37 Lfma8FnnxeSOi6223AsFfYwsNZ2RderNsQrS0PjEHb0CIBkrWacqARUAu7uT4cGu
38 jVcIxYQqhId5L8p/mAv2PWZS
39 -----END CERTIFICATE-----
40 `
41
42 type testOption struct {
43 TestString string
44 TestBool bool
45 TestDuration time.Duration
46 TestHeaders map[string]string
47 TestURL *url.URL
48 TestTLS *tls.Config
49 }
50
51 func TestEnvConfig(t *testing.T) {
52 parsedURL, err := url.Parse("https://example.com")
53 assert.NoError(t, err)
54
55 options := []testOption{}
56 for _, testcase := range []struct {
57 name string
58 reader EnvOptionsReader
59 configs []ConfigFn
60 expectedOptions []testOption
61 }{
62 {
63 name: "with no namespace and a matching key",
64 reader: EnvOptionsReader{
65 GetEnv: func(n string) string {
66 if n == "HELLO" {
67 return "world"
68 }
69 return ""
70 },
71 },
72 configs: []ConfigFn{
73 WithString("HELLO", func(v string) {
74 options = append(options, testOption{TestString: v})
75 }),
76 },
77 expectedOptions: []testOption{
78 {
79 TestString: "world",
80 },
81 },
82 },
83 {
84 name: "with no namespace and a non-matching key",
85 reader: EnvOptionsReader{
86 GetEnv: func(n string) string {
87 if n == "HELLO" {
88 return "world"
89 }
90 return ""
91 },
92 },
93 configs: []ConfigFn{
94 WithString("HOLA", func(v string) {
95 options = append(options, testOption{TestString: v})
96 }),
97 },
98 expectedOptions: []testOption{},
99 },
100 {
101 name: "with a namespace and a matching key",
102 reader: EnvOptionsReader{
103 Namespace: "MY_NAMESPACE",
104 GetEnv: func(n string) string {
105 if n == "MY_NAMESPACE_HELLO" {
106 return "world"
107 }
108 return ""
109 },
110 },
111 configs: []ConfigFn{
112 WithString("HELLO", func(v string) {
113 options = append(options, testOption{TestString: v})
114 }),
115 },
116 expectedOptions: []testOption{
117 {
118 TestString: "world",
119 },
120 },
121 },
122 {
123 name: "with no namespace and a non-matching key",
124 reader: EnvOptionsReader{
125 Namespace: "MY_NAMESPACE",
126 GetEnv: func(n string) string {
127 if n == "HELLO" {
128 return "world"
129 }
130 return ""
131 },
132 },
133 configs: []ConfigFn{
134 WithString("HELLO", func(v string) {
135 options = append(options, testOption{TestString: v})
136 }),
137 },
138 expectedOptions: []testOption{},
139 },
140 {
141 name: "with a bool config",
142 reader: EnvOptionsReader{
143 GetEnv: func(n string) string {
144 switch n {
145 case "HELLO":
146 return "true"
147 case "WORLD":
148 return "false"
149 }
150 return ""
151 },
152 },
153 configs: []ConfigFn{
154 WithBool("HELLO", func(b bool) {
155 options = append(options, testOption{TestBool: b})
156 }),
157 WithBool("WORLD", func(b bool) {
158 options = append(options, testOption{TestBool: b})
159 }),
160 },
161 expectedOptions: []testOption{
162 {
163 TestBool: true,
164 },
165 {
166 TestBool: false,
167 },
168 },
169 },
170 {
171 name: "with an invalid bool config",
172 reader: EnvOptionsReader{
173 GetEnv: func(n string) string {
174 if n == "HELLO" {
175 return "world"
176 }
177 return ""
178 },
179 },
180 configs: []ConfigFn{
181 WithBool("HELLO", func(b bool) {
182 options = append(options, testOption{TestBool: b})
183 }),
184 },
185 expectedOptions: []testOption{
186 {
187 TestBool: false,
188 },
189 },
190 },
191 {
192 name: "with a duration config",
193 reader: EnvOptionsReader{
194 GetEnv: func(n string) string {
195 if n == "HELLO" {
196 return "60"
197 }
198 return ""
199 },
200 },
201 configs: []ConfigFn{
202 WithDuration("HELLO", func(v time.Duration) {
203 options = append(options, testOption{TestDuration: v})
204 }),
205 },
206 expectedOptions: []testOption{
207 {
208 TestDuration: 60_000_000, // 60 milliseconds
209 },
210 },
211 },
212 {
213 name: "with an invalid duration config",
214 reader: EnvOptionsReader{
215 GetEnv: func(n string) string {
216 if n == "HELLO" {
217 return "world"
218 }
219 return ""
220 },
221 },
222 configs: []ConfigFn{
223 WithDuration("HELLO", func(v time.Duration) {
224 options = append(options, testOption{TestDuration: v})
225 }),
226 },
227 expectedOptions: []testOption{},
228 },
229 {
230 name: "with headers",
231 reader: EnvOptionsReader{
232 GetEnv: func(n string) string {
233 if n == "HELLO" {
234 return "userId=42,userName=alice"
235 }
236 return ""
237 },
238 },
239 configs: []ConfigFn{
240 WithHeaders("HELLO", func(v map[string]string) {
241 options = append(options, testOption{TestHeaders: v})
242 }),
243 },
244 expectedOptions: []testOption{
245 {
246 TestHeaders: map[string]string{
247 "userId": "42",
248 "userName": "alice",
249 },
250 },
251 },
252 },
253 {
254 name: "with invalid headers",
255 reader: EnvOptionsReader{
256 GetEnv: func(n string) string {
257 if n == "HELLO" {
258 return "world"
259 }
260 return ""
261 },
262 },
263 configs: []ConfigFn{
264 WithHeaders("HELLO", func(v map[string]string) {
265 options = append(options, testOption{TestHeaders: v})
266 }),
267 },
268 expectedOptions: []testOption{
269 {
270 TestHeaders: map[string]string{},
271 },
272 },
273 },
274 {
275 name: "with percent-encoded headers",
276 reader: EnvOptionsReader{
277 GetEnv: func(n string) string {
278 if n == "HELLO" {
279 return "user%2Did=42,user%20name=alice%20smith"
280 }
281 return ""
282 },
283 },
284 configs: []ConfigFn{
285 WithHeaders("HELLO", func(v map[string]string) {
286 options = append(options, testOption{TestHeaders: v})
287 }),
288 },
289 expectedOptions: []testOption{
290 {
291 TestHeaders: map[string]string{
292 "user%2Did": "42",
293 "user%20name": "alice smith",
294 },
295 },
296 },
297 },
298 {
299 name: "with invalid header key",
300 reader: EnvOptionsReader{
301 GetEnv: func(n string) string {
302 if n == "HELLO" {
303 return "valid-key=value,invalid key=value"
304 }
305 return ""
306 },
307 },
308 configs: []ConfigFn{
309 WithHeaders("HELLO", func(v map[string]string) {
310 options = append(options, testOption{TestHeaders: v})
311 }),
312 },
313 expectedOptions: []testOption{
314 {
315 TestHeaders: map[string]string{
316 "valid-key": "value",
317 },
318 },
319 },
320 },
321 {
322 name: "with URL",
323 reader: EnvOptionsReader{
324 GetEnv: func(n string) string {
325 if n == "HELLO" {
326 return "https://example.com"
327 }
328 return ""
329 },
330 },
331 configs: []ConfigFn{
332 WithURL("HELLO", func(v *url.URL) {
333 options = append(options, testOption{TestURL: v})
334 }),
335 },
336 expectedOptions: []testOption{
337 {
338 TestURL: parsedURL,
339 },
340 },
341 },
342 {
343 name: "with invalid URL",
344 reader: EnvOptionsReader{
345 GetEnv: func(n string) string {
346 if n == "HELLO" {
347 return "i nvalid://url"
348 }
349 return ""
350 },
351 },
352 configs: []ConfigFn{
353 WithURL("HELLO", func(v *url.URL) {
354 options = append(options, testOption{TestURL: v})
355 }),
356 },
357 expectedOptions: []testOption{},
358 },
359 } {
360 t.Run(testcase.name, func(t *testing.T) {
361 testcase.reader.Apply(testcase.configs...)
362 assert.Equal(t, testcase.expectedOptions, options)
363 options = []testOption{}
364 })
365 }
366 }
367
368 func TestWithTLSConfig(t *testing.T) {
369 pool, err := createCertPool([]byte(WeakCertificate))
370 assert.NoError(t, err)
371
372 reader := EnvOptionsReader{
373 GetEnv: func(n string) string {
374 if n == "CERTIFICATE" {
375 return "/path/cert.pem"
376 }
377 return ""
378 },
379 ReadFile: func(p string) ([]byte, error) {
380 if p == "/path/cert.pem" {
381 return []byte(WeakCertificate), nil
382 }
383 return []byte{}, nil
384 },
385 }
386
387 var option testOption
388 reader.Apply(
389 WithCertPool("CERTIFICATE", func(cp *x509.CertPool) {
390 option = testOption{TestTLS: &tls.Config{RootCAs: cp}}
391 }),
392 )
393
394 // nolint:staticcheck // ignoring tlsCert.RootCAs.Subjects is deprecated ERR because cert does not come from SystemCertPool.
395 assert.Equal(t, pool.Subjects(), option.TestTLS.RootCAs.Subjects())
396 }
397
398 func TestWithClientCert(t *testing.T) {
399 cert, err := tls.X509KeyPair([]byte(WeakCertificate), []byte(WeakKey))
400 assert.NoError(t, err)
401
402 reader := EnvOptionsReader{
403 GetEnv: func(n string) string {
404 switch n {
405 case "CLIENT_CERTIFICATE":
406 return "/path/tls.crt"
407 case "CLIENT_KEY":
408 return "/path/tls.key"
409 }
410 return ""
411 },
412 ReadFile: func(n string) ([]byte, error) {
413 switch n {
414 case "/path/tls.crt":
415 return []byte(WeakCertificate), nil
416 case "/path/tls.key":
417 return []byte(WeakKey), nil
418 }
419 return []byte{}, nil
420 },
421 }
422
423 var option testOption
424 reader.Apply(
425 WithClientCert("CLIENT_CERTIFICATE", "CLIENT_KEY", func(c tls.Certificate) {
426 option = testOption{TestTLS: &tls.Config{Certificates: []tls.Certificate{c}}}
427 }),
428 )
429 assert.Equal(t, cert, option.TestTLS.Certificates[0])
430
431 reader.ReadFile = func(s string) ([]byte, error) { return nil, errors.New("oops") }
432 option.TestTLS = nil
433 reader.Apply(
434 WithClientCert("CLIENT_CERTIFICATE", "CLIENT_KEY", func(c tls.Certificate) {
435 option = testOption{TestTLS: &tls.Config{Certificates: []tls.Certificate{c}}}
436 }),
437 )
438 assert.Nil(t, option.TestTLS)
439
440 reader.GetEnv = func(s string) string { return "" }
441 option.TestTLS = nil
442 reader.Apply(
443 WithClientCert("CLIENT_CERTIFICATE", "CLIENT_KEY", func(c tls.Certificate) {
444 option = testOption{TestTLS: &tls.Config{Certificates: []tls.Certificate{c}}}
445 }),
446 )
447 assert.Nil(t, option.TestTLS)
448 }
449
450 func TestStringToHeader(t *testing.T) {
451 tests := []struct {
452 name string
453 value string
454 want map[string]string
455 }{
456 {
457 name: "simple test",
458 value: "userId=alice",
459 want: map[string]string{"userId": "alice"},
460 },
461 {
462 name: "simple test with spaces",
463 value: " userId = alice ",
464 want: map[string]string{"userId": "alice"},
465 },
466 {
467 name: "simple header conforms to RFC 3986 spec",
468 value: " userId = alice+test ",
469 want: map[string]string{"userId": "alice+test"},
470 },
471 {
472 name: "multiple headers encoded",
473 value: "userId=alice,serverNode=DF%3A28,isProduction=false",
474 want: map[string]string{
475 "userId": "alice",
476 "serverNode": "DF:28",
477 "isProduction": "false",
478 },
479 },
480 {
481 name: "multiple headers encoded per RFC 3986 spec",
482 value: "userId=alice+test,serverNode=DF%3A28,isProduction=false,namespace=localhost/test",
483 want: map[string]string{
484 "userId": "alice+test",
485 "serverNode": "DF:28",
486 "isProduction": "false",
487 "namespace": "localhost/test",
488 },
489 },
490 {
491 name: "invalid headers format",
492 value: "userId:alice",
493 want: map[string]string{},
494 },
495 {
496 name: "invalid key",
497 value: "%XX=missing,userId=alice",
498 want: map[string]string{
499 "%XX": "missing",
500 "userId": "alice",
501 },
502 },
503 {
504 name: "invalid value",
505 value: "missing=%XX,userId=alice",
506 want: map[string]string{
507 "userId": "alice",
508 },
509 },
510 }
511
512 for _, tt := range tests {
513 t.Run(tt.name, func(t *testing.T) {
514 assert.Equal(t, tt.want, stringToHeader(tt.value))
515 })
516 }
517 }