From 8af75ff035742ebca5a59833ec884ba08be56020 Mon Sep 17 00:00:00 2001 From: Fae Charlton Date: Tue, 5 Dec 2023 11:20:32 -0500 Subject: [PATCH] Add a unit test for the config namespace corner case --- .../elasticsearch/config_presets_test.go | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/libbeat/outputs/elasticsearch/config_presets_test.go b/libbeat/outputs/elasticsearch/config_presets_test.go index 17fabe6025e8..972b874dda74 100644 --- a/libbeat/outputs/elasticsearch/config_presets_test.go +++ b/libbeat/outputs/elasticsearch/config_presets_test.go @@ -199,3 +199,32 @@ func TestApplyPresetCustom(t *testing.T) { assert.Equal(t, testHost, host, "Computed hosts should match user config") } } + +func TestFlattenedKeysRemovesNamespace(t *testing.T) { + // A test exhibiting the namespace corner case that breaks the baseline + // behavior of FlattenedKeys, and ensuring that flattenedKeysForConfig + // fixes it. + rawCfg := config.MustNewConfigFrom(map[string]interface{}{ + "namespace.testkey": "testvalue", + }) + ns := config.Namespace{} + err := ns.Unpack(rawCfg) + require.NoError(t, err, "Namespace unpack should succeed") + + // Extract the sub-config from the Namespace object. + cfg := ns.Config() + + // FlattenedKeys on the config object parsed via a Namespace will still + // include the namespace in the reported keys + nsFlattenedKeys := cfg.FlattenedKeys() + assert.ElementsMatch(t, []string{"namespace.testkey"}, nsFlattenedKeys, + "Expected keys from FlattenedKeys to include original namespace") + + // flattenedKeysForConfig should strip the namespace prefix so we can + // reliably compare output config fields no matter how they were + // originally created. + cfgFlattenedKeys, err := flattenedKeysForConfig(cfg) + require.NoError(t, err, "flattenedKeysForConfig should succeed") + assert.ElementsMatch(t, []string{"testkey"}, cfgFlattenedKeys, + "Expected flattenedKeysForConfig to remove original namespace") +}