mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* copy AlertQuery from ngmodels to the definition package * replaces usages of ngmodels.AlertQuery in API models * create a converter between models of AlertQuery --------- Co-authored-by: Alex Moreno <alexander.moreno@grafana.com>
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package definitions
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRulePayloadMarshaling(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
desc string
|
|
input TestRulePayload
|
|
err bool
|
|
}{
|
|
{
|
|
desc: "success lotex",
|
|
input: TestRulePayload{
|
|
Expr: "rate({cluster=\"us-central1\", job=\"loki-prod/loki-canary\"}[1m]) > 0",
|
|
},
|
|
},
|
|
{
|
|
desc: "success grafana",
|
|
input: func() TestRulePayload {
|
|
data := AlertQuery{}
|
|
|
|
// hack around that the struct embeds the json message inside of it as well
|
|
raw, _ := json.Marshal(data)
|
|
data.Model = raw
|
|
|
|
return TestRulePayload{
|
|
GrafanaManagedCondition: &EvalAlertConditionCommand{
|
|
Condition: "placeholder",
|
|
Data: []AlertQuery{data},
|
|
},
|
|
}
|
|
}(),
|
|
},
|
|
{
|
|
desc: "failure mixed",
|
|
input: TestRulePayload{
|
|
Expr: "rate({cluster=\"us-central1\", job=\"loki-prod/loki-canary\"}[1m]) > 0",
|
|
GrafanaManagedCondition: &EvalAlertConditionCommand{},
|
|
},
|
|
err: true,
|
|
},
|
|
{
|
|
desc: "failure both empty",
|
|
input: TestRulePayload{},
|
|
err: true,
|
|
},
|
|
} {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
encoded, err := json.Marshal(tc.input)
|
|
require.Nil(t, err)
|
|
|
|
var out TestRulePayload
|
|
err = json.Unmarshal(encoded, &out)
|
|
|
|
if tc.err {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.Nil(t, err)
|
|
require.Equal(t, tc.input, out)
|
|
}
|
|
})
|
|
}
|
|
}
|