Skip to content

Commit f61a6e8

Browse files
committed
add better test
1 parent caaf199 commit f61a6e8

File tree

1 file changed

+64
-37
lines changed

1 file changed

+64
-37
lines changed

internal/pkg/otel/translate/otelconfig_test.go

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -210,35 +210,62 @@ func TestGetOtelConfig(t *testing.T) {
210210
},
211211
},
212212
}
213-
esOutputConfig := map[string]any{
214-
"type": "elasticsearch",
215-
"hosts": []any{"localhost:9200"},
216-
"username": "elastic",
217-
"password": "password",
218-
"preset": "balanced",
219-
"queue.mem.events": 3200,
220-
"ssl.enabled": true,
213+
214+
type extraParams struct {
215+
key string
216+
value any
221217
}
218+
// pass ssl params as extra args to this method
219+
esOutputConfig := func(extra ...extraParams) map[string]any {
220+
finalOutput := map[string]any{
221+
"type": "elasticsearch",
222+
"hosts": []any{"localhost:9200"},
223+
"username": "elastic",
224+
"password": "password",
225+
"preset": "balanced",
226+
"queue.mem.events": 3200,
227+
"ssl.enabled": true,
228+
}
222229

223-
expectedExtensionConfig := map[string]any{
224-
"idle_connection_timeout": "3s",
225-
"proxy_disable": false,
226-
"ssl": map[string]interface{}{
227-
"ca_sha256": []interface{}{},
228-
"ca_trusted_fingerprint": "",
229-
"certificate": "",
230-
"certificate_authorities": []interface{}{},
231-
"cipher_suites": []interface{}{},
232-
"curve_types": []interface{}{},
233-
"enabled": true,
234-
"key": "",
235-
"key_passphrase": "",
236-
"key_passphrase_path": "",
237-
"renegotiation": int64(0),
238-
"supported_protocols": []interface{}{},
239-
"verification_mode": uint64(0),
240-
},
241-
"timeout": "1m30s",
230+
for _, v := range extra {
231+
finalOutput[v.key] = v.value
232+
}
233+
return finalOutput
234+
}
235+
236+
expectedExtensionConfig := func(extra ...extraParams) map[string]any {
237+
finalOutput := map[string]any{
238+
"idle_connection_timeout": "3s",
239+
"proxy_disable": false,
240+
"ssl": map[string]interface{}{
241+
"ca_sha256": []interface{}{},
242+
"ca_trusted_fingerprint": "",
243+
"certificate": "",
244+
"certificate_authorities": []interface{}{},
245+
"cipher_suites": []interface{}{},
246+
"curve_types": []interface{}{},
247+
"enabled": true,
248+
"key": "",
249+
"key_passphrase": "",
250+
"key_passphrase_path": "",
251+
"renegotiation": int64(0),
252+
"supported_protocols": []interface{}{},
253+
"verification_mode": uint64(0),
254+
},
255+
"timeout": "1m30s",
256+
}
257+
for _, v := range extra {
258+
// accepts one level deep parameters to replace
259+
if _, ok := v.value.(map[string]any); ok {
260+
for newkey, newvalue := range v.value.(map[string]any) {
261+
// this is brittle - it is expected that developers will pass expected params correctly here
262+
finalOutput[v.key].(map[string]any)[newkey] = newvalue
263+
}
264+
continue
265+
}
266+
finalOutput[v.key] = v.value
267+
}
268+
return finalOutput
242269
}
243270

244271
expectedESConfig := func(outputName string) map[string]any {
@@ -456,7 +483,7 @@ func TestGetOtelConfig(t *testing.T) {
456483
{
457484
ID: "filestream-default",
458485
Type: client.UnitTypeOutput,
459-
Config: component.MustExpectedConfig(esOutputConfig),
486+
Config: component.MustExpectedConfig(esOutputConfig()),
460487
},
461488
},
462489
},
@@ -467,7 +494,7 @@ func TestGetOtelConfig(t *testing.T) {
467494
"elasticsearch/_agent-component/default": expectedESConfig("default"),
468495
},
469496
"extensions": map[string]any{
470-
"beatsauth/_agent-component/default": expectedExtensionConfig,
497+
"beatsauth/_agent-component/default": expectedExtensionConfig(),
471498
},
472499
"receivers": map[string]any{
473500
"filebeatreceiver/_agent-component/filestream-default": expectedFilestreamConfig("filestream-default"),
@@ -508,7 +535,7 @@ func TestGetOtelConfig(t *testing.T) {
508535
{
509536
ID: "filestream-primaryOutput",
510537
Type: client.UnitTypeOutput,
511-
Config: component.MustExpectedConfig(esOutputConfig),
538+
Config: component.MustExpectedConfig(esOutputConfig(extraParams{"ssl.verification_mode", "certificate"})),
512539
},
513540
},
514541
},
@@ -533,7 +560,7 @@ func TestGetOtelConfig(t *testing.T) {
533560
{
534561
ID: "filestream-secondaryOutput",
535562
Type: client.UnitTypeOutput,
536-
Config: component.MustExpectedConfig(esOutputConfig),
563+
Config: component.MustExpectedConfig(esOutputConfig(extraParams{"ssl.ca_trusted_fingerprint", "b9a10bbe64ee9826abeda6546fc988c8bf798b41957c33d05db736716513dc9c"})),
537564
},
538565
},
539566
},
@@ -545,8 +572,8 @@ func TestGetOtelConfig(t *testing.T) {
545572
"elasticsearch/_agent-component/secondaryOutput": expectedESConfig("secondaryOutput"),
546573
},
547574
"extensions": map[string]any{
548-
"beatsauth/_agent-component/primaryOutput": expectedExtensionConfig,
549-
"beatsauth/_agent-component/secondaryOutput": expectedExtensionConfig,
575+
"beatsauth/_agent-component/primaryOutput": expectedExtensionConfig(extraParams{"ssl", map[string]any{"verification_mode": uint64(2)}}),
576+
"beatsauth/_agent-component/secondaryOutput": expectedExtensionConfig(extraParams{"ssl", map[string]any{"ca_trusted_fingerprint": "b9a10bbe64ee9826abeda6546fc988c8bf798b41957c33d05db736716513dc9c"}}),
550577
},
551578
"receivers": map[string]any{
552579
"filebeatreceiver/_agent-component/filestream-primaryOutput": expectedFilestreamConfig("filestream-primaryOutput"),
@@ -592,7 +619,7 @@ func TestGetOtelConfig(t *testing.T) {
592619
{
593620
ID: "beat/metrics-default",
594621
Type: client.UnitTypeOutput,
595-
Config: component.MustExpectedConfig(esOutputConfig),
622+
Config: component.MustExpectedConfig(esOutputConfig()),
596623
},
597624
},
598625
},
@@ -603,7 +630,7 @@ func TestGetOtelConfig(t *testing.T) {
603630
"elasticsearch/_agent-component/default": expectedESConfig("default"),
604631
},
605632
"extensions": map[string]any{
606-
"beatsauth/_agent-component/default": expectedExtensionConfig,
633+
"beatsauth/_agent-component/default": expectedExtensionConfig(),
607634
},
608635
"receivers": map[string]any{
609636
"metricbeatreceiver/_agent-component/beat-metrics-monitoring": map[string]any{
@@ -691,7 +718,7 @@ func TestGetOtelConfig(t *testing.T) {
691718
{
692719
ID: "system/metrics-default",
693720
Type: client.UnitTypeOutput,
694-
Config: component.MustExpectedConfig(esOutputConfig),
721+
Config: component.MustExpectedConfig(esOutputConfig()),
695722
},
696723
},
697724
},
@@ -702,7 +729,7 @@ func TestGetOtelConfig(t *testing.T) {
702729
"elasticsearch/_agent-component/default": expectedESConfig("default"),
703730
},
704731
"extensions": map[string]any{
705-
"beatsauth/_agent-component/default": expectedExtensionConfig,
732+
"beatsauth/_agent-component/default": expectedExtensionConfig(),
706733
},
707734
"receivers": map[string]any{
708735
"metricbeatreceiver/_agent-component/system-metrics": map[string]any{

0 commit comments

Comments
 (0)