[release-11.3.7] Alerting: Ensure field validators return the proper type (#105286)
Alerting: Ensure field validators return the proper type (#104050)
* Ensure field validators return the proper type
This ensures correct error propagation through services up to
the API layer.
* Move error wrapping up to call site
(cherry picked from commit 820c338414)
This commit is contained in:
@@ -565,7 +565,7 @@ func (alertRule *AlertRule) ValidateAlertRule(cfg setting.UnifiedAlertingSetting
|
||||
err = validateAlertRuleFields(alertRule)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("%w: %s", ErrAlertRuleFailedValidation, err)
|
||||
}
|
||||
|
||||
if alertRule.For < 0 {
|
||||
@@ -606,10 +606,10 @@ func validateAlertRuleFields(rule *AlertRule) error {
|
||||
func validateRecordingRuleFields(rule *AlertRule) error {
|
||||
metricName := prommodels.LabelValue(rule.Record.Metric)
|
||||
if !metricName.IsValid() {
|
||||
return fmt.Errorf("%w: %s", ErrAlertRuleFailedValidation, "metric name for recording rule must be a valid utf8 string")
|
||||
return errors.New("metric name for recording rule must be a valid utf8 string")
|
||||
}
|
||||
if !prommodels.IsValidMetricName(metricName) {
|
||||
return fmt.Errorf("%w: %s", ErrAlertRuleFailedValidation, "metric name for recording rule must be a valid Prometheus metric name")
|
||||
return errors.New("metric name for recording rule must be a valid Prometheus metric name")
|
||||
}
|
||||
|
||||
clearRecordingRuleIgnoredFields(rule)
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/grafana/grafana/pkg/util/cmputil"
|
||||
)
|
||||
@@ -890,3 +891,50 @@ func TestTimeRangeYAML(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, yamlRaw, string(serialized))
|
||||
}
|
||||
|
||||
func TestValidateAlertRule(t *testing.T) {
|
||||
t.Run("ExecErrState & NoDataState", func(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
execErrState string
|
||||
noDataState string
|
||||
error bool
|
||||
}{
|
||||
{
|
||||
name: "invalid error state",
|
||||
execErrState: "invalid",
|
||||
error: true,
|
||||
},
|
||||
{
|
||||
name: "invalid no data state",
|
||||
noDataState: "invalid",
|
||||
error: true,
|
||||
},
|
||||
{
|
||||
name: "valid states",
|
||||
error: false,
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rule := RuleGen.With(
|
||||
RuleMuts.WithIntervalSeconds(10),
|
||||
).Generate()
|
||||
if tc.execErrState != "" {
|
||||
rule.ExecErrState = ExecutionErrorState(tc.execErrState)
|
||||
}
|
||||
if tc.noDataState != "" {
|
||||
rule.NoDataState = NoDataState(tc.noDataState)
|
||||
}
|
||||
|
||||
err := rule.ValidateAlertRule(setting.UnifiedAlertingSettings{BaseInterval: 10 * time.Second})
|
||||
if tc.error {
|
||||
require.Error(t, err)
|
||||
require.ErrorIs(t, err, ErrAlertRuleFailedValidation)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user