From 9bd3cb60c93875dd90081ca11dfb658764f91fe6 Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Wed, 6 Mar 2024 14:07:19 +0200 Subject: [PATCH 01/13] first commit with checks for hints vocabulary --- CHANGELOG.md | 9 +++++++++ utils/hints.go | 21 +++++++++++++++++++-- utils/hints_test.go | 18 +++++++++++++++++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e378d2ea9f..247ac580f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,3 +44,12 @@ This project adheres to [Semantic Versioning](http://semver.org/). [0.6.7]: https://github.com/elastic/elastic-agent-autodiscover/compare/v0.6.2...v0.6.7 + +## [0.6.9] + +### Changed + +- Update GenerateHints function to check supported list of hints + + +[0.6.9]: https://github.com/elastic/elastic-agent-autodiscover/compare/v0.6.8...v0.6.9 diff --git a/utils/hints.go b/utils/hints.go index f3b816b539..7cf427c8ee 100644 --- a/utils/hints.go +++ b/utils/hints.go @@ -202,8 +202,9 @@ func IsDisabled(hints mapstr.M, key string) bool { } // GenerateHints parses annotations based on a prefix and sets up hints that can be picked up by individual Beats. -func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M { +func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedHints []string) (mapstr.M, error) { hints := mapstr.M{} + returnerror := error(nil) if rawEntries, err := annotations.GetValue(prefix); err == nil { if entries, ok := rawEntries.(mapstr.M); ok { for key, rawValue := range entries { @@ -212,6 +213,18 @@ func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M { parts := strings.Split(key, "/") if len(parts) == 2 { hintKey := fmt.Sprintf("%s.%s", parts[0], parts[1]) + //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that start with co.elastic.hints + found := false + for _, checksupported := range allSupportedHints { + if parts[1] == checksupported { + found = true + break + } + } + if !found { + returnerror = fmt.Errorf("provided hint :%v is not in the supported list", parts[1]) + } //End of check + // Insert only if there is no entry already. container level annotations take // higher priority. if _, err := hints.GetValue(hintKey); err != nil { @@ -248,7 +261,11 @@ func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M { } } - return hints + if returnerror == nil { + return hints, nil + } + + return hints, returnerror } // GetHintsAsList gets a set of hints and tries to convert them into a list of hints diff --git a/utils/hints_test.go b/utils/hints_test.go index 624bab5456..2e40e2cdfa 100644 --- a/utils/hints_test.go +++ b/utils/hints_test.go @@ -50,6 +50,21 @@ func TestGetProcessors(t *testing.T) { } func TestGenerateHints(t *testing.T) { + const ( + integration = "package" + datastreams = "data_streams" + host = "host" + period = "period" + timeout = "timeout" + metricspath = "metrics_path" + username = "username" + password = "password" + stream = "stream" // this is the container stream: stdout/stderr + processors = "processors" + ) + + var allSupportedHints = []string{"enabled", integration, datastreams, host, period, timeout, metricspath, username, password, stream, processors} + tests := []struct { annotations map[string]string result mapstr.M @@ -219,7 +234,8 @@ func TestGenerateHints(t *testing.T) { continue } } - assert.Equal(t, test.result, GenerateHints(annMap, "foobar", "co.elastic")) + generateHints, _ := GenerateHints(annMap, "foobar", "co.elastic", allSupportedHints) + assert.Equal(t, test.result, generateHints) } } func TestGetHintsAsList(t *testing.T) { From d922ef01adb8117359d7aae6cc15954636f0b61b Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Wed, 6 Mar 2024 17:04:59 +0200 Subject: [PATCH 02/13] adding checks also to containers --- utils/hints.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/utils/hints.go b/utils/hints.go index 7cf427c8ee..69264c7617 100644 --- a/utils/hints.go +++ b/utils/hints.go @@ -208,22 +208,19 @@ func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedH if rawEntries, err := annotations.GetValue(prefix); err == nil { if entries, ok := rawEntries.(mapstr.M); ok { for key, rawValue := range entries { + found := false // If there are top level hints like co.elastic.logs/ then just add the values after the / // Only consider namespaced annotations parts := strings.Split(key, "/") if len(parts) == 2 { hintKey := fmt.Sprintf("%s.%s", parts[0], parts[1]) //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that start with co.elastic.hints - found := false for _, checksupported := range allSupportedHints { if parts[1] == checksupported { found = true break } } - if !found { - returnerror = fmt.Errorf("provided hint :%v is not in the supported list", parts[1]) - } //End of check // Insert only if there is no entry already. container level annotations take // higher priority. @@ -246,6 +243,13 @@ func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedH if strings.HasPrefix(hintKey, container) { // Split the key to get part[1] to be the hint parts := strings.Split(hintKey, "/") + //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that start with co.elastic.hints + for _, checksupported := range allSupportedHints { + if parts[1] == checksupported { + found = true + break + } + } if len(parts) == 2 { // key will be the hint type hintKey := fmt.Sprintf("%s.%s", key, parts[1]) @@ -257,6 +261,11 @@ func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedH } } } + + if !found { + returnerror = fmt.Errorf("provided hint :%v is not in the supported list", parts[1]) + } //End of check + } } } From 17950767e42c50365ecdd842349649cd70998f22 Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Thu, 7 Mar 2024 11:05:43 +0200 Subject: [PATCH 03/13] returning list with unsupported hints --- utils/hints.go | 16 +++++----------- utils/hints_test.go | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/utils/hints.go b/utils/hints.go index 69264c7617..879faa75e0 100644 --- a/utils/hints.go +++ b/utils/hints.go @@ -202,9 +202,9 @@ func IsDisabled(hints mapstr.M, key string) bool { } // GenerateHints parses annotations based on a prefix and sets up hints that can be picked up by individual Beats. -func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedHints []string) (mapstr.M, error) { +func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedHints []string) (mapstr.M, []string) { hints := mapstr.M{} - returnerror := error(nil) + var incorrecthints []string if rawEntries, err := annotations.GetValue(prefix); err == nil { if entries, ok := rawEntries.(mapstr.M); ok { for key, rawValue := range entries { @@ -261,20 +261,14 @@ func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedH } } } - if !found { - returnerror = fmt.Errorf("provided hint :%v is not in the supported list", parts[1]) - } //End of check - + incorrecthints = append(incorrecthints, key) + } } } } - if returnerror == nil { - return hints, nil - } - - return hints, returnerror + return hints, incorrecthints } // GetHintsAsList gets a set of hints and tries to convert them into a list of hints diff --git a/utils/hints_test.go b/utils/hints_test.go index 2e40e2cdfa..4dcd3a962c 100644 --- a/utils/hints_test.go +++ b/utils/hints_test.go @@ -63,7 +63,7 @@ func TestGenerateHints(t *testing.T) { processors = "processors" ) - var allSupportedHints = []string{"enabled", integration, datastreams, host, period, timeout, metricspath, username, password, stream, processors} + var allSupportedHints = []string{"enabled", "module", integration, datastreams, host, period, timeout, metricspath, username, password, stream, processors} tests := []struct { annotations map[string]string @@ -114,15 +114,15 @@ func TestGenerateHints(t *testing.T) { // metrics/metrics_path must be found in hints.metrics { annotations: map[string]string{ - "co.elastic.logs/multiline.pattern": "^test", - "co.elastic.metrics/module": "prometheus", - "co.elastic.metrics/period": "10s", - "co.elastic.metrics/metrics_path": "/metrics/prometheus", - "co.elastic.metrics/username": "user", - "co.elastic.metrics/password": "pass", - "co.elastic.metrics.foobar/period": "15s", - "co.elastic.metrics.foobar1/period": "15s", - "not.to.include": "true", + "co.elastic.logs/multiline.pattern": "^test", + "co.elastic.metrics/module": "prometheus", + "co.elastic.metrics/period": "10s", + "co.elastic.metrics/metrics_path": "/metrics/prometheus", + "co.elastic.metrics/username": "user", + "co.elastic.metrics/password": "pass", + "co.elastic.metrics.foobar/period": "15s", + "co.elastic.metrics.foobar1/periods": "15s", + "not.to.include": "true", }, result: mapstr.M{ "logs": mapstr.M{ From 93065164edbb02b7de267bfd26c1ce05cc749280 Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Thu, 7 Mar 2024 11:31:51 +0200 Subject: [PATCH 04/13] returning list with unsupported hints and fixing test to check those --- utils/hints_test.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/utils/hints_test.go b/utils/hints_test.go index 4dcd3a962c..caa0dd6e89 100644 --- a/utils/hints_test.go +++ b/utils/hints_test.go @@ -63,7 +63,7 @@ func TestGenerateHints(t *testing.T) { processors = "processors" ) - var allSupportedHints = []string{"enabled", "module", integration, datastreams, host, period, timeout, metricspath, username, password, stream, processors} + var allSupportedHints = []string{"enabled", "module", integration, datastreams, host, period, timeout, metricspath, username, password, stream, processors, "multiline", "json", "disable"} tests := []struct { annotations map[string]string @@ -90,6 +90,7 @@ func TestGenerateHints(t *testing.T) { "co.elastic.metrics/period": "10s", "co.elastic.metrics.foobar/period": "15s", "co.elastic.metrics.foobar1/period": "15s", + "co.elastic.hints.steam": "stdout", // On purpose this added with typo "not.to.include": "true", }, result: mapstr.M{ @@ -114,15 +115,15 @@ func TestGenerateHints(t *testing.T) { // metrics/metrics_path must be found in hints.metrics { annotations: map[string]string{ - "co.elastic.logs/multiline.pattern": "^test", - "co.elastic.metrics/module": "prometheus", - "co.elastic.metrics/period": "10s", - "co.elastic.metrics/metrics_path": "/metrics/prometheus", - "co.elastic.metrics/username": "user", - "co.elastic.metrics/password": "pass", - "co.elastic.metrics.foobar/period": "15s", - "co.elastic.metrics.foobar1/periods": "15s", - "not.to.include": "true", + "co.elastic.logs/multiline.pattern": "^test", + "co.elastic.metrics/module": "prometheus", + "co.elastic.metrics/period": "10s", + "co.elastic.metrics/metrics_path": "/metrics/prometheus", + "co.elastic.metrics/username": "user", + "co.elastic.metrics/password": "pass", + "co.elastic.metrics.foobar/period": "15s", + "co.elastic.metrics.foobar1/period": "15s", + "not.to.include": "true", }, result: mapstr.M{ "logs": mapstr.M{ @@ -226,7 +227,7 @@ func TestGenerateHints(t *testing.T) { }, } - for _, test := range tests { + for key, test := range tests { annMap := mapstr.M{} for k, v := range test.annotations { _, err := annMap.Put(k, v) @@ -234,7 +235,13 @@ func TestGenerateHints(t *testing.T) { continue } } - generateHints, _ := GenerateHints(annMap, "foobar", "co.elastic", allSupportedHints) + generateHints, incorrecthints := GenerateHints(annMap, "foobar", "co.elastic", allSupportedHints) + //Only in test1 we have added co.elastic.hints.steam annotation with a typo error + if key == 1 { + assert.Equal(t, 1, len(incorrecthints)) // We validate how many incorrect hints are provided in test1. + } else { + assert.Equal(t, 0, len(incorrecthints)) // We validate how many incorrect hints are provided in rest of tests + } assert.Equal(t, test.result, generateHints) } } From 98d9808d0fef242e8719faf3417b262863bdbf55 Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Thu, 7 Mar 2024 11:55:09 +0200 Subject: [PATCH 05/13] adding function for duplicate code --- utils/hints.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/utils/hints.go b/utils/hints.go index 879faa75e0..7ffbf083b7 100644 --- a/utils/hints.go +++ b/utils/hints.go @@ -205,22 +205,18 @@ func IsDisabled(hints mapstr.M, key string) bool { func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedHints []string) (mapstr.M, []string) { hints := mapstr.M{} var incorrecthints []string + found := false if rawEntries, err := annotations.GetValue(prefix); err == nil { if entries, ok := rawEntries.(mapstr.M); ok { for key, rawValue := range entries { - found := false + // If there are top level hints like co.elastic.logs/ then just add the values after the / // Only consider namespaced annotations parts := strings.Split(key, "/") if len(parts) == 2 { hintKey := fmt.Sprintf("%s.%s", parts[0], parts[1]) //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that start with co.elastic.hints - for _, checksupported := range allSupportedHints { - if parts[1] == checksupported { - found = true - break - } - } + found = checkSupportedHints(parts[1], allSupportedHints) // Insert only if there is no entry already. container level annotations take // higher priority. @@ -244,12 +240,7 @@ func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedH // Split the key to get part[1] to be the hint parts := strings.Split(hintKey, "/") //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that start with co.elastic.hints - for _, checksupported := range allSupportedHints { - if parts[1] == checksupported { - found = true - break - } - } + found = checkSupportedHints(parts[1], allSupportedHints) if len(parts) == 2 { // key will be the hint type hintKey := fmt.Sprintf("%s.%s", key, parts[1]) @@ -309,3 +300,15 @@ func GetHintsAsList(hints mapstr.M, key string) []mapstr.M { } return configs } + +// checkSupportedHints gets a specific hint annotation and compares it with the supported list of hints +func checkSupportedHints(actualannotation string, allSupportedHints []string) bool { + found := false + for _, checksupported := range allSupportedHints { + if actualannotation == checksupported { + found = true + break + } + } + return found +} From 60286d4f9eb5a691f425a8db74488a638c9771fe Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Thu, 7 Mar 2024 12:02:31 +0200 Subject: [PATCH 06/13] udpating comment --- utils/hints.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/hints.go b/utils/hints.go index 7ffbf083b7..cde1ae185c 100644 --- a/utils/hints.go +++ b/utils/hints.go @@ -215,7 +215,7 @@ func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedH parts := strings.Split(key, "/") if len(parts) == 2 { hintKey := fmt.Sprintf("%s.%s", parts[0], parts[1]) - //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that start with co.elastic.hints + //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that have prefix co.elastic found = checkSupportedHints(parts[1], allSupportedHints) // Insert only if there is no entry already. container level annotations take @@ -239,7 +239,7 @@ func GenerateHints(annotations mapstr.M, container, prefix string, allSupportedH if strings.HasPrefix(hintKey, container) { // Split the key to get part[1] to be the hint parts := strings.Split(hintKey, "/") - //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that start with co.elastic.hints + //We check whether the provided annotation follows the supported format and vocabulary. The check happens for annotations that have prefix co.elastic found = checkSupportedHints(parts[1], allSupportedHints) if len(parts) == 2 { // key will be the hint type From f22d479ec30696fe63822176198b5dca01c1742d Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Thu, 7 Mar 2024 12:53:08 +0200 Subject: [PATCH 07/13] fixing test error --- utils/hints_test.go | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/utils/hints_test.go b/utils/hints_test.go index caa0dd6e89..5b67594a79 100644 --- a/utils/hints_test.go +++ b/utils/hints_test.go @@ -66,23 +66,26 @@ func TestGenerateHints(t *testing.T) { var allSupportedHints = []string{"enabled", "module", integration, datastreams, host, period, timeout, metricspath, username, password, stream, processors, "multiline", "json", "disable"} tests := []struct { + name string annotations map[string]string result mapstr.M }{ // Empty annotations should return empty hints - { - annotations: map[string]string{}, - result: mapstr.M{}, - }, + // { + // name: "test0", + // annotations: map[string]string{}, + // result: mapstr.M{}, + // }, - // Scenarios being tested: - // logs/multiline.pattern must be a nested mapstr.M under hints.logs - // logs/processors.add_fields must be nested mapstr.M under hints.logs - // logs/json.keys_under_root must be a nested mapstr.M under hints.logs - // metrics/module must be found in hints.metrics - // not.to.include must not be part of hints - // period is annotated at both container and pod level. Container level value must be in hints + // // Scenarios being tested: + // // logs/multiline.pattern must be a nested mapstr.M under hints.logs + // // logs/processors.add_fields must be nested mapstr.M under hints.logs + // // logs/json.keys_under_root must be a nested mapstr.M under hints.logs + // // metrics/module must be found in hints.metrics + // // not.to.include must not be part of hints + // // period is annotated at both container and pod level. Container level value must be in hints { + name: "test1", annotations: map[string]string{ "co.elastic.logs/multiline.pattern": "^test", "co.elastic.logs/json.keys_under_root": "true", @@ -90,7 +93,6 @@ func TestGenerateHints(t *testing.T) { "co.elastic.metrics/period": "10s", "co.elastic.metrics.foobar/period": "15s", "co.elastic.metrics.foobar1/period": "15s", - "co.elastic.hints.steam": "stdout", // On purpose this added with typo "not.to.include": "true", }, result: mapstr.M{ @@ -114,6 +116,7 @@ func TestGenerateHints(t *testing.T) { // not.to.include must not be part of hints // metrics/metrics_path must be found in hints.metrics { + name: "test2", annotations: map[string]string{ "co.elastic.logs/multiline.pattern": "^test", "co.elastic.metrics/module": "prometheus", @@ -123,6 +126,7 @@ func TestGenerateHints(t *testing.T) { "co.elastic.metrics/password": "pass", "co.elastic.metrics.foobar/period": "15s", "co.elastic.metrics.foobar1/period": "15s", + "co.elastic.hints/steam": "stdout", // On purpose this added with typo "not.to.include": "true", }, result: mapstr.M{ @@ -131,6 +135,7 @@ func TestGenerateHints(t *testing.T) { "pattern": "^test", }, }, + "hints": mapstr.M{"steam": "stdout"}, "metrics": mapstr.M{ "module": "prometheus", "period": "15s", @@ -147,6 +152,7 @@ func TestGenerateHints(t *testing.T) { // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints { + name: "test3", annotations: map[string]string{ "co.elastic.logs/multiline.pattern": "^test", "co.elastic.metrics/module": "prometheus", @@ -174,6 +180,7 @@ func TestGenerateHints(t *testing.T) { // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints { + name: "test4", annotations: map[string]string{ "co.elastic.logs/disable": "false", "co.elastic.logs/multiline.pattern": "^test", @@ -203,6 +210,7 @@ func TestGenerateHints(t *testing.T) { // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints { + name: "test5", annotations: map[string]string{ "co.elastic.logs/disable": "true", "co.elastic.logs/multiline.pattern": "^test", @@ -227,7 +235,7 @@ func TestGenerateHints(t *testing.T) { }, } - for key, test := range tests { + for _, test := range tests { annMap := mapstr.M{} for k, v := range test.annotations { _, err := annMap.Put(k, v) @@ -237,7 +245,9 @@ func TestGenerateHints(t *testing.T) { } generateHints, incorrecthints := GenerateHints(annMap, "foobar", "co.elastic", allSupportedHints) //Only in test1 we have added co.elastic.hints.steam annotation with a typo error - if key == 1 { + if test.name == "test2" { + t.Log(annMap) + t.Log(incorrecthints) assert.Equal(t, 1, len(incorrecthints)) // We validate how many incorrect hints are provided in test1. } else { assert.Equal(t, 0, len(incorrecthints)) // We validate how many incorrect hints are provided in rest of tests From 3c1e817f802e282cc329b7963e5125439cf32a38 Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Thu, 7 Mar 2024 12:55:03 +0200 Subject: [PATCH 08/13] fixing test error --- utils/hints_test.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/utils/hints_test.go b/utils/hints_test.go index 5b67594a79..dbf3c0739a 100644 --- a/utils/hints_test.go +++ b/utils/hints_test.go @@ -70,20 +70,20 @@ func TestGenerateHints(t *testing.T) { annotations map[string]string result mapstr.M }{ - // Empty annotations should return empty hints - // { - // name: "test0", - // annotations: map[string]string{}, - // result: mapstr.M{}, - // }, + Empty annotations should return empty hints + { + name: "test0", + annotations: map[string]string{}, + result: mapstr.M{}, + }, - // // Scenarios being tested: - // // logs/multiline.pattern must be a nested mapstr.M under hints.logs - // // logs/processors.add_fields must be nested mapstr.M under hints.logs - // // logs/json.keys_under_root must be a nested mapstr.M under hints.logs - // // metrics/module must be found in hints.metrics - // // not.to.include must not be part of hints - // // period is annotated at both container and pod level. Container level value must be in hints + // Scenarios being tested: + // logs/multiline.pattern must be a nested mapstr.M under hints.logs + // logs/processors.add_fields must be nested mapstr.M under hints.logs + // logs/json.keys_under_root must be a nested mapstr.M under hints.logs + // metrics/module must be found in hints.metrics + // not.to.include must not be part of hints + // period is annotated at both container and pod level. Container level value must be in hints { name: "test1", annotations: map[string]string{ @@ -244,10 +244,8 @@ func TestGenerateHints(t *testing.T) { } } generateHints, incorrecthints := GenerateHints(annMap, "foobar", "co.elastic", allSupportedHints) - //Only in test1 we have added co.elastic.hints.steam annotation with a typo error + //Only in test2 we have added co.elastic.hints.steam annotation with a typo error if test.name == "test2" { - t.Log(annMap) - t.Log(incorrecthints) assert.Equal(t, 1, len(incorrecthints)) // We validate how many incorrect hints are provided in test1. } else { assert.Equal(t, 0, len(incorrecthints)) // We validate how many incorrect hints are provided in rest of tests From 516584a01133cc35fe8377c08db3b3b2ad50ed97 Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Thu, 7 Mar 2024 12:57:03 +0200 Subject: [PATCH 09/13] fixing test error --- utils/hints_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/utils/hints_test.go b/utils/hints_test.go index dbf3c0739a..2896296acb 100644 --- a/utils/hints_test.go +++ b/utils/hints_test.go @@ -70,7 +70,7 @@ func TestGenerateHints(t *testing.T) { annotations map[string]string result mapstr.M }{ - Empty annotations should return empty hints + //Empty annotations should return empty hints { name: "test0", annotations: map[string]string{}, @@ -243,12 +243,12 @@ func TestGenerateHints(t *testing.T) { continue } } - generateHints, incorrecthints := GenerateHints(annMap, "foobar", "co.elastic", allSupportedHints) + generateHints, incorrectHints := GenerateHints(annMap, "foobar", "co.elastic", allSupportedHints) //Only in test2 we have added co.elastic.hints.steam annotation with a typo error if test.name == "test2" { - assert.Equal(t, 1, len(incorrecthints)) // We validate how many incorrect hints are provided in test1. + assert.Equal(t, 1, len(incorrectHints)) // We validate how many incorrect hints are provided in test1. } else { - assert.Equal(t, 0, len(incorrecthints)) // We validate how many incorrect hints are provided in rest of tests + assert.Equal(t, 0, len(incorrectHints)) // We validate how many incorrect hints are provided in rest of tests } assert.Equal(t, test.result, generateHints) } From 72a0cc59bfa68593dc18494af9abf47e070c2594 Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Thu, 7 Mar 2024 13:28:18 +0200 Subject: [PATCH 10/13] Updating test definitions --- utils/hints_test.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/utils/hints_test.go b/utils/hints_test.go index 2896296acb..5b8fbbbb88 100644 --- a/utils/hints_test.go +++ b/utils/hints_test.go @@ -72,7 +72,7 @@ func TestGenerateHints(t *testing.T) { }{ //Empty annotations should return empty hints { - name: "test0", + name: "Empty_Annotations", annotations: map[string]string{}, result: mapstr.M{}, }, @@ -85,7 +85,7 @@ func TestGenerateHints(t *testing.T) { // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints { - name: "test1", + name: "Logs_multiline_and_metrics", annotations: map[string]string{ "co.elastic.logs/multiline.pattern": "^test", "co.elastic.logs/json.keys_under_root": "true", @@ -116,7 +116,7 @@ func TestGenerateHints(t *testing.T) { // not.to.include must not be part of hints // metrics/metrics_path must be found in hints.metrics { - name: "test2", + name: "Logs_multiline_and_metrics_with_metrics_path", annotations: map[string]string{ "co.elastic.logs/multiline.pattern": "^test", "co.elastic.metrics/module": "prometheus", @@ -146,13 +146,12 @@ func TestGenerateHints(t *testing.T) { }, }, // Scenarios being tested: - // have co.elastic.logs/disable set to false. // logs/multiline.pattern must be a nested mapstr.M under hints.logs // metrics/module must be found in hints.metrics // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints { - name: "test3", + name: "Logs_multiline_and_metrics", annotations: map[string]string{ "co.elastic.logs/multiline.pattern": "^test", "co.elastic.metrics/module": "prometheus", @@ -180,7 +179,7 @@ func TestGenerateHints(t *testing.T) { // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints { - name: "test4", + name: "Logs_disabled_false_and_metrics", annotations: map[string]string{ "co.elastic.logs/disable": "false", "co.elastic.logs/multiline.pattern": "^test", @@ -210,7 +209,7 @@ func TestGenerateHints(t *testing.T) { // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints { - name: "test5", + name: "Logs_disabled_true_and_metrics", annotations: map[string]string{ "co.elastic.logs/disable": "true", "co.elastic.logs/multiline.pattern": "^test", @@ -244,8 +243,8 @@ func TestGenerateHints(t *testing.T) { } } generateHints, incorrectHints := GenerateHints(annMap, "foobar", "co.elastic", allSupportedHints) - //Only in test2 we have added co.elastic.hints.steam annotation with a typo error - if test.name == "test2" { + //Only in Logs_multiline_and_metrics_with_metrics_path we have added co.elastic.hints.steam annotation with a typo error + if test.name == "Logs_multiline_and_metrics_with_metrics_path" { assert.Equal(t, 1, len(incorrectHints)) // We validate how many incorrect hints are provided in test1. } else { assert.Equal(t, 0, len(incorrectHints)) // We validate how many incorrect hints are provided in rest of tests From cea860a13f1f3608f2ef9619752a6cc6827bf302 Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Tue, 12 Mar 2024 11:46:15 +0200 Subject: [PATCH 11/13] adding expectedIncorrectHints on test cases for more clarity --- utils/hints_test.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/utils/hints_test.go b/utils/hints_test.go index 5b8fbbbb88..77c5182532 100644 --- a/utils/hints_test.go +++ b/utils/hints_test.go @@ -66,15 +66,17 @@ func TestGenerateHints(t *testing.T) { var allSupportedHints = []string{"enabled", "module", integration, datastreams, host, period, timeout, metricspath, username, password, stream, processors, "multiline", "json", "disable"} tests := []struct { - name string - annotations map[string]string - result mapstr.M + name string + annotations map[string]string + result mapstr.M + expectedIncorrectHints int // We set the number of hints that will be marked as incorrect and wont part of the acceptable supported list }{ //Empty annotations should return empty hints { - name: "Empty_Annotations", - annotations: map[string]string{}, - result: mapstr.M{}, + name: "Empty_Annotations", + annotations: map[string]string{}, + result: mapstr.M{}, + expectedIncorrectHints: 0, }, // Scenarios being tested: @@ -109,6 +111,7 @@ func TestGenerateHints(t *testing.T) { "period": "15s", }, }, + expectedIncorrectHints: 0, }, // Scenarios being tested: // logs/multiline.pattern must be a nested mapstr.M under hints.logs @@ -144,6 +147,7 @@ func TestGenerateHints(t *testing.T) { "password": "pass", }, }, + expectedIncorrectHints: 1, // Due to co.elastic.hints/steam and not co.elastic.hints/stream }, // Scenarios being tested: // logs/multiline.pattern must be a nested mapstr.M under hints.logs @@ -171,6 +175,7 @@ func TestGenerateHints(t *testing.T) { "period": "15s", }, }, + expectedIncorrectHints: 0, }, // Scenarios being tested: // have co.elastic.logs/disable set to false. @@ -201,6 +206,7 @@ func TestGenerateHints(t *testing.T) { "period": "15s", }, }, + expectedIncorrectHints: 0, }, // Scenarios being tested: // have co.elastic.logs/disable set to true. @@ -231,6 +237,7 @@ func TestGenerateHints(t *testing.T) { "period": "15s", }, }, + expectedIncorrectHints: 0, }, } @@ -243,12 +250,7 @@ func TestGenerateHints(t *testing.T) { } } generateHints, incorrectHints := GenerateHints(annMap, "foobar", "co.elastic", allSupportedHints) - //Only in Logs_multiline_and_metrics_with_metrics_path we have added co.elastic.hints.steam annotation with a typo error - if test.name == "Logs_multiline_and_metrics_with_metrics_path" { - assert.Equal(t, 1, len(incorrectHints)) // We validate how many incorrect hints are provided in test1. - } else { - assert.Equal(t, 0, len(incorrectHints)) // We validate how many incorrect hints are provided in rest of tests - } + assert.Equal(t, test.expectedIncorrectHints, len(incorrectHints)) // We validate how many incorrect hints are provided per test case. assert.Equal(t, test.result, generateHints) } } From 3c291a54eb6383bca7cbbfaa2da83bd84317d2a0 Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Tue, 12 Mar 2024 11:52:40 +0200 Subject: [PATCH 12/13] adding expectedIncorrectHints on test cases for more clarity --- utils/hints_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/hints_test.go b/utils/hints_test.go index 77c5182532..60f6348029 100644 --- a/utils/hints_test.go +++ b/utils/hints_test.go @@ -69,7 +69,7 @@ func TestGenerateHints(t *testing.T) { name string annotations map[string]string result mapstr.M - expectedIncorrectHints int // We set the number of hints that will be marked as incorrect and wont part of the acceptable supported list + expectedIncorrectHints int // We set the number of hints that will be marked as incorrect and wont be included in the acceptable supported list }{ //Empty annotations should return empty hints { From 95ed1dd3f90ab019d8236c3e2c87c8529d0d73c1 Mon Sep 17 00:00:00 2001 From: Andreas Gkizas Date: Wed, 13 Mar 2024 10:42:41 +0200 Subject: [PATCH 13/13] removing const from tests and added only array --- utils/hints_test.go | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/utils/hints_test.go b/utils/hints_test.go index 60f6348029..3072c90b36 100644 --- a/utils/hints_test.go +++ b/utils/hints_test.go @@ -50,20 +50,8 @@ func TestGetProcessors(t *testing.T) { } func TestGenerateHints(t *testing.T) { - const ( - integration = "package" - datastreams = "data_streams" - host = "host" - period = "period" - timeout = "timeout" - metricspath = "metrics_path" - username = "username" - password = "password" - stream = "stream" // this is the container stream: stdout/stderr - processors = "processors" - ) - var allSupportedHints = []string{"enabled", "module", integration, datastreams, host, period, timeout, metricspath, username, password, stream, processors, "multiline", "json", "disable"} + var allSupportedHints = []string{"enabled", "module", "integration", "datas_treams", "host", "period", "timeout", "metrics_path", "username", "password", "stream", "processors", "multiline", "json", "disable"} tests := []struct { name string