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

(cherry picked from commit 4dc0d49025)

Co-authored-by: Jean-Philippe Quéméner <JohnnyQQQQ@users.noreply.github.com>
This commit is contained in:
Grot (@grafanabot)
2022-10-04 04:50:02 -04:00
committed by GitHub
co-authored by Jean-Philippe Quéméner
parent af9bde9f08
commit b0ef776ed8
2 changed files with 42 additions and 8 deletions
@@ -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 {
@@ -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)
})
}