mirror of
https://github.com/google/alertmanager-irc-relay.git
synced 2025-01-07 10:42:48 +01:00
deduplicate format test code
Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
parent
aa63009ab4
commit
c07a4e5288
@ -23,9 +23,27 @@ import (
|
|||||||
promtmpl "github.com/prometheus/alertmanager/template"
|
promtmpl "github.com/prometheus/alertmanager/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func CreateFormatterAndCheckOutput(t *testing.T, c *Config, expected []AlertMsg) {
|
||||||
|
f, _ := NewFormatter(c)
|
||||||
|
|
||||||
|
var alertMessage = promtmpl.Data{}
|
||||||
|
if err := json.Unmarshal([]byte(testdataSimpleAlertJson), &alertMessage); err != nil {
|
||||||
|
t.Fatal(fmt.Sprintf("Could not unmarshal %s", testdataSimpleAlertJson))
|
||||||
|
}
|
||||||
|
|
||||||
|
alertMsgs := f.GetMsgsFromAlertMessage("#somechannel", &alertMessage)
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(expected, alertMsgs) {
|
||||||
|
t.Error(fmt.Sprintf(
|
||||||
|
"Unexpected alert msg.\nExpected: %s\nActual: %s",
|
||||||
|
expected, alertMsgs))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestTemplateErrorsCreateRawAlertMsg(t *testing.T) {
|
func TestTemplateErrorsCreateRawAlertMsg(t *testing.T) {
|
||||||
testingConfig := Config{MsgTemplate: "Bogus template {{ nil }}"}
|
testingConfig := Config{MsgTemplate: "Bogus template {{ nil }}"}
|
||||||
formatter, _ := NewFormatter(&testingConfig)
|
|
||||||
|
|
||||||
expectedAlertMsgs := []AlertMsg{
|
expectedAlertMsgs := []AlertMsg{
|
||||||
AlertMsg{
|
AlertMsg{
|
||||||
@ -38,20 +56,7 @@ func TestTemplateErrorsCreateRawAlertMsg(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var alertMessage = promtmpl.Data{}
|
CreateFormatterAndCheckOutput(t, &testingConfig, expectedAlertMsgs)
|
||||||
if err := json.Unmarshal([]byte(testdataSimpleAlertJson), &alertMessage); err != nil {
|
|
||||||
t.Fatal(fmt.Sprintf("Could not unmarshal %s", testdataSimpleAlertJson))
|
|
||||||
}
|
|
||||||
|
|
||||||
alertMsgs := formatter.GetMsgsFromAlertMessage("#somechannel", &alertMessage)
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(expectedAlertMsgs, alertMsgs) {
|
|
||||||
t.Error(fmt.Sprintf(
|
|
||||||
"Unexpected alert msg.\nExpected: %s\nActual: %s",
|
|
||||||
expectedAlertMsgs, alertMsgs))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAlertsDispatchedOnce(t *testing.T) {
|
func TestAlertsDispatchedOnce(t *testing.T) {
|
||||||
@ -59,7 +64,6 @@ func TestAlertsDispatchedOnce(t *testing.T) {
|
|||||||
MsgTemplate: "Alert {{ .GroupLabels.alertname }} is {{ .Status }}",
|
MsgTemplate: "Alert {{ .GroupLabels.alertname }} is {{ .Status }}",
|
||||||
MsgOnce: true,
|
MsgOnce: true,
|
||||||
}
|
}
|
||||||
formatter, _ := NewFormatter(&testingConfig)
|
|
||||||
|
|
||||||
expectedAlertMsgs := []AlertMsg{
|
expectedAlertMsgs := []AlertMsg{
|
||||||
AlertMsg{
|
AlertMsg{
|
||||||
@ -68,19 +72,7 @@ func TestAlertsDispatchedOnce(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var alertMessage = promtmpl.Data{}
|
CreateFormatterAndCheckOutput(t, &testingConfig, expectedAlertMsgs)
|
||||||
if err := json.Unmarshal([]byte(testdataSimpleAlertJson), &alertMessage); err != nil {
|
|
||||||
t.Fatal(fmt.Sprintf("Could not unmarshal %s", testdataSimpleAlertJson))
|
|
||||||
}
|
|
||||||
|
|
||||||
alertMsgs := formatter.GetMsgsFromAlertMessage("#somechannel", &alertMessage)
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(expectedAlertMsgs, alertMsgs) {
|
|
||||||
t.Error(fmt.Sprintf(
|
|
||||||
"Unexpected alert msg.\nExpected: %s\nActual: %s",
|
|
||||||
expectedAlertMsgs, alertMsgs))
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringsFunctions(t *testing.T) {
|
func TestStringsFunctions(t *testing.T) {
|
||||||
@ -88,7 +80,6 @@ func TestStringsFunctions(t *testing.T) {
|
|||||||
MsgTemplate: "Alert {{ .GroupLabels.alertname | ToUpper }} is {{ .Status }}",
|
MsgTemplate: "Alert {{ .GroupLabels.alertname | ToUpper }} is {{ .Status }}",
|
||||||
MsgOnce: true,
|
MsgOnce: true,
|
||||||
}
|
}
|
||||||
formatter, _ := NewFormatter(&testingConfig)
|
|
||||||
|
|
||||||
expectedAlertMsgs := []AlertMsg{
|
expectedAlertMsgs := []AlertMsg{
|
||||||
AlertMsg{
|
AlertMsg{
|
||||||
@ -97,18 +88,5 @@ func TestStringsFunctions(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var alertMessage = promtmpl.Data{}
|
CreateFormatterAndCheckOutput(t, &testingConfig, expectedAlertMsgs)
|
||||||
if err := json.Unmarshal([]byte(testdataSimpleAlertJson), &alertMessage); err != nil {
|
|
||||||
t.Fatal(fmt.Sprintf("Could not unmarshal %s", testdataSimpleAlertJson))
|
|
||||||
}
|
|
||||||
|
|
||||||
alertMsgs := formatter.GetMsgsFromAlertMessage("#somechannel", &alertMessage)
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(expectedAlertMsgs, alertMsgs) {
|
|
||||||
t.Error(fmt.Sprintf(
|
|
||||||
"Unexpected alert msg.\nExpected: %s\nActual: %s",
|
|
||||||
expectedAlertMsgs, alertMsgs))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user