From 206b60c0f16ee4b3452d57fa03d3289cfcc03429 Mon Sep 17 00:00:00 2001 From: Daniel Lepage Date: Mon, 2 Dec 2024 14:22:26 -0500 Subject: [PATCH] Turn tag/mapping conflict into an actual error. --- .../config_validate_enrich.go | 5 +-- .../config_validate_enrich_test.go | 42 +------------------ 2 files changed, 3 insertions(+), 44 deletions(-) diff --git a/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich.go b/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich.go index 5268345d8744e5..499b93287d9d16 100644 --- a/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich.go +++ b/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich.go @@ -10,8 +10,6 @@ import ( "fmt" "regexp" - "github.com/DataDog/datadog-agent/pkg/util/log" - "github.com/DataDog/datadog-agent/pkg/networkdevice/profile/profiledefinition" ) @@ -174,6 +172,7 @@ func validateEnrichSymbol(symbol *profiledefinition.SymbolConfig, symbolContext } return errors } + func validateEnrichMetricTag(metricTag *profiledefinition.MetricTagConfig) []string { var errors []string if (metricTag.Column.OID != "" || metricTag.Column.Name != "") && (metricTag.Symbol.OID != "" || metricTag.Symbol.Name != "") { @@ -217,7 +216,7 @@ func validateEnrichMetricTag(metricTag *profiledefinition.MetricTagConfig) []str } } if len(metricTag.Mapping) > 0 && metricTag.Tag == "" { - log.Warnf("``tag` must be provided if `mapping` (`%s`) is defined", metricTag.Mapping) + errors = append(errors, fmt.Sprintf("``tag` must be provided if `mapping` (`%s`) is defined", metricTag.Mapping)) } for _, transform := range metricTag.IndexTransform { if transform.Start > transform.End { diff --git a/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich_test.go b/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich_test.go index 0047af78cc366e..e3e1db7ad1d3ab 100644 --- a/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich_test.go +++ b/pkg/collector/corechecks/snmp/internal/configvalidation/config_validate_enrich_test.go @@ -6,18 +6,12 @@ package configvalidation import ( - "bufio" - "bytes" "fmt" "regexp" - "strings" "testing" - "github.com/cihub/seelog" "github.com/stretchr/testify/assert" - "github.com/DataDog/datadog-agent/pkg/util/log" - "github.com/DataDog/datadog-agent/pkg/networkdevice/profile/profiledefinition" ) @@ -32,7 +26,6 @@ func Test_ValidateEnrichMetrics(t *testing.T) { metrics []profiledefinition.MetricsConfig expectedErrors []string expectedMetrics []profiledefinition.MetricsConfig - expectedLogs []logCount }{ { name: "either table symbol or scalar symbol must be provided", @@ -574,23 +567,11 @@ func Test_ValidateEnrichMetrics(t *testing.T) { }, }, }, - expectedErrors: []string{}, - expectedLogs: []logCount{ - { - "[WARN] validateEnrichMetricTag: ``tag` must be provided if `mapping` (`map[1:abc 2:def]`) is defined", - 1, - }, - }, + expectedErrors: []string{"``tag` must be provided if `mapping` (`map[1:abc 2:def]`) is defined"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var b bytes.Buffer - w := bufio.NewWriter(&b) - l, err := seelog.LoggerFromWriterWithMinLevelAndFormat(w, seelog.DebugLvl, "[%LEVEL] %FuncShort: %Msg") - assert.Nil(t, err) - log.SetupLogger(l, "debug") - errors := ValidateEnrichMetrics(tt.metrics) assert.Equal(t, len(tt.expectedErrors), len(errors), fmt.Sprintf("ERRORS: %v", errors)) for i := range errors { @@ -599,13 +580,6 @@ func Test_ValidateEnrichMetrics(t *testing.T) { if tt.expectedMetrics != nil { assert.Equal(t, tt.expectedMetrics, tt.metrics) } - - w.Flush() - logs := b.String() - - for _, aLogCount := range tt.expectedLogs { - assert.Equal(t, aLogCount.count, strings.Count(logs, aLogCount.log), logs) - } }) } } @@ -621,7 +595,6 @@ func Test_ValidateEnrichMetricTags(t *testing.T) { metrics []profiledefinition.MetricTagConfig expectedErrors []string expectedMetrics []profiledefinition.MetricTagConfig - expectedLogs []logCount }{ { name: "Move OID to Symbol", @@ -691,12 +664,6 @@ func Test_ValidateEnrichMetricTags(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var b bytes.Buffer - w := bufio.NewWriter(&b) - l, err := seelog.LoggerFromWriterWithMinLevelAndFormat(w, seelog.DebugLvl, "[%LEVEL] %FuncShort: %Msg") - assert.Nil(t, err) - log.SetupLogger(l, "debug") - errors := ValidateEnrichMetricTags(tt.metrics) assert.Equal(t, len(tt.expectedErrors), len(errors), fmt.Sprintf("ERRORS: %v", errors)) for i := range errors { @@ -705,13 +672,6 @@ func Test_ValidateEnrichMetricTags(t *testing.T) { if tt.expectedMetrics != nil { assert.Equal(t, tt.expectedMetrics, tt.metrics) } - - w.Flush() - logs := b.String() - - for _, aLogCount := range tt.expectedLogs { - assert.Equal(t, aLogCount.count, strings.Count(logs, aLogCount.log), logs) - } }) } }