Alerting: make sure that rules in rule group are nil if not provided (#55301)

This commit is contained in:
Jean-Philippe Quéméner 2022-09-16 18:54:00 +02:00 committed by GitHub
parent bf5b21563c
commit 4dc0d49025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 8 deletions

View File

@ -191,20 +191,19 @@ type AlertRuleGroup struct {
}
func (a *AlertRuleGroup) ToModel() (models.AlertRuleGroup, error) {
rules := make([]models.AlertRule, 0, len(a.Rules))
ruleGroup := models.AlertRuleGroup{
Title: a.Title,
FolderUID: a.FolderUID,
Interval: a.Interval,
}
for i := range a.Rules {
converted, err := a.Rules[i].UpstreamModel()
if err != nil {
return models.AlertRuleGroup{}, err
}
rules = append(rules, converted)
ruleGroup.Rules = append(ruleGroup.Rules, converted)
}
return models.AlertRuleGroup{
Title: a.Title,
FolderUID: a.FolderUID,
Interval: a.Interval,
Rules: rules,
}, nil
return ruleGroup, nil
}
func NewAlertRuleGroupFromModel(d models.AlertRuleGroup) AlertRuleGroup {

View File

@ -0,0 +1,35 @@
package definitions
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestToModel(t *testing.T) {
t.Run("if no rules are provided the rule field should be nil", func(t *testing.T) {
ruleGroup := AlertRuleGroup{
Title: "123",
FolderUID: "123",
Interval: 10,
}
tm, err := ruleGroup.ToModel()
require.NoError(t, err)
require.Nil(t, tm.Rules)
})
t.Run("if rules are provided the rule field should be not nil", func(t *testing.T) {
ruleGroup := AlertRuleGroup{
Title: "123",
FolderUID: "123",
Interval: 10,
Rules: []ProvisionedAlertRule{
{
UID: "1",
},
},
}
tm, err := ruleGroup.ToModel()
require.NoError(t, err)
require.Len(t, tm.Rules, 1)
})
}