From acb31ccd03946ceda6aab4561bb656bbc3fa2041 Mon Sep 17 00:00:00 2001 From: Luca Bigliardi Date: Thu, 5 Nov 2020 09:48:02 +0100 Subject: [PATCH] add strings functions test Signed-off-by: Luca Bigliardi --- format_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/format_test.go b/format_test.go index 129535d..4569899 100644 --- a/format_test.go +++ b/format_test.go @@ -82,3 +82,33 @@ func TestAlertsDispatchedOnce(t *testing.T) { } } + +func TestStringsFunctions(t *testing.T) { + testingConfig := Config{ + MsgTemplate: "Alert {{ .GroupLabels.alertname | ToUpper }} is {{ .Status }}", + MsgOnce: true, + } + formatter, _ := NewFormatter(&testingConfig) + + expectedAlertMsgs := []AlertMsg{ + AlertMsg{ + Channel: "#somechannel", + Alert: "Alert AIRDOWN is resolved", + }, + } + + var alertMessage = promtmpl.Data{} + 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)) + + } + +}