From f9a70c4716b4e1b8a746a08963b67d2039cd22bd Mon Sep 17 00:00:00 2001 From: Jack Baldry Date: Wed, 27 Jan 2021 11:28:08 +0000 Subject: [PATCH 1/2] test(notify/victorops): Add test for templating errors Signed-off-by: Jack Baldry --- notify/victorops/victorops_test.go | 89 ++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/notify/victorops/victorops_test.go b/notify/victorops/victorops_test.go index 59aad7f539..502e70ff3d 100644 --- a/notify/victorops/victorops_test.go +++ b/notify/victorops/victorops_test.go @@ -17,6 +17,8 @@ import ( "context" "encoding/json" "fmt" + "net/http" + "net/http/httptest" "net/url" "testing" "time" @@ -116,3 +118,90 @@ func TestVictorOpsRedactedURL(t *testing.T) { test.AssertNotifyLeaksNoSecret(t, ctx, notifier, secret) } + +func TestVictorOpsTemplating(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + dec := json.NewDecoder(r.Body) + out := make(map[string]interface{}) + err := dec.Decode(&out) + if err != nil { + panic(err) + } + })) + defer srv.Close() + u, _ := url.Parse(srv.URL) + + tests := []struct { + name string + cfg *config.VictorOpsConfig + errMsg string + }{ + { + name: "default valid templates", + cfg: &config.VictorOpsConfig{}, + }, + { + name: "invalid message_type", + cfg: &config.VictorOpsConfig{ + MessageType: "{{ .CommonLabels.alertname }", + }, + errMsg: "templating error", + }, + { + name: "invalid entity_display_name", + cfg: &config.VictorOpsConfig{ + EntityDisplayName: "{{ .CommonLabels.alertname }", + }, + errMsg: "templating error", + }, + { + name: "invalid state_message", + cfg: &config.VictorOpsConfig{ + StateMessage: "{{ .CommonLabels.alertname }", + }, + errMsg: "templating error", + }, + { + name: "invalid monitoring tool", + cfg: &config.VictorOpsConfig{ + MonitoringTool: "{{ .CommonLabels.alertname }", + }, + errMsg: "templating error", + }, + { + name: "invalid routing_key", + cfg: &config.VictorOpsConfig{ + RoutingKey: "{{ .CommonLabels.alertname }", + }, + errMsg: "templating error", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + tc.cfg.HTTPConfig = &commoncfg.HTTPClientConfig{} + tc.cfg.APIURL = &config.URL{URL: u} + vo, err := New(tc.cfg, test.CreateTmpl(t), log.NewNopLogger()) + require.NoError(t, err) + ctx := context.Background() + ctx = notify.WithGroupKey(ctx, "1") + + _, err = vo.Notify(ctx, []*types.Alert{{ + Alert: model.Alert{ + Labels: model.LabelSet{ + "lbl1": "val1", + }, + StartsAt: time.Now(), + EndsAt: time.Now().Add(time.Hour), + }, + }, + }...) + if tc.errMsg == "" { + require.NoError(t, err) + } else { + require.Contains(t, err.Error(), tc.errMsg) + } + }) + } + +} From 3311106f54d01526af638d8349a4eadcaca698a4 Mon Sep 17 00:00:00 2001 From: Jack Baldry Date: Wed, 27 Jan 2021 11:11:55 +0000 Subject: [PATCH 2/2] fix(notify/victorops): Catch routing_key templating errors Signed-off-by: Jack Baldry --- notify/victorops/victorops.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/notify/victorops/victorops.go b/notify/victorops/victorops.go index 648ca70f5f..d2f35e6f7b 100644 --- a/notify/victorops/victorops.go +++ b/notify/victorops/victorops.go @@ -72,6 +72,9 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) apiURL = n.conf.APIURL.Copy() ) apiURL.Path += fmt.Sprintf("%s/%s", n.conf.APIKey, tmpl(n.conf.RoutingKey)) + if err != nil { + return false, fmt.Errorf("templating error: %s", err) + } buf, err := n.createVictorOpsPayload(ctx, as...) if err != nil {