[v10.3.x] Alerting: Marshal incoming json.RawMessage in diff (#84852)

Alerting: Marshal incoming json.RawMessage in diff (#84692)

This will ensure the encoding is correct when comparing
to the existing rule.

(cherry picked from commit 6d16cf2699)

Co-authored-by: William Wernert <william.wernert@grafana.com>
This commit is contained in:
grafana-delivery-bot[bot]
2024-03-20 16:26:39 -04:00
committed by GitHub
co-authored by William Wernert
parent cd81c5e4f9
commit a58d078d5b
2 changed files with 20 additions and 1 deletions
+5 -1
View File
@@ -314,7 +314,11 @@ func (alertRule *AlertRule) Diff(rule *AlertRule, ignore ...string) cmputil.Diff
// json.RawMessage is a slice of bytes and therefore cmp's default behavior is to compare it by byte, which is not really useful
var jsonCmp = cmp.Transformer("", func(in json.RawMessage) string {
return string(in)
b, err := json.Marshal(in)
if err != nil {
return string(in)
}
return string(b)
})
ops = append(ops, cmp.Reporter(&reporter), cmpopts.IgnoreFields(AlertQuery{}, "modelProps"), jsonCmp, cmpopts.EquateEmpty())
@@ -652,6 +652,21 @@ func TestDiff(t *testing.T) {
}
})
t.Run("should correctly detect no change with '<' and '>' in query", func(t *testing.T) {
old := query1
new := query1
old.Model = json.RawMessage(`{"field1": "$A \u003c 1"}`)
new.Model = json.RawMessage(`{"field1": "$A < 1"}`)
rule1.Data = []AlertQuery{old}
rule2.Data = []AlertQuery{new}
diff := rule1.Diff(rule2)
assert.Nil(t, diff)
// reset rule1
rule1.Data = []AlertQuery{query1}
})
t.Run("should detect new changes in array if too many fields changed", func(t *testing.T) {
query2 := query1
query2.QueryType = "test"