Move break at the beginning fo the for loop (#1928)

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2019-07-04 15:31:09 +02:00 committed by GitHub
parent 75d3ba991c
commit bb3df011a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 257 additions and 3 deletions

View File

@ -15,16 +15,15 @@ package template
import ( import (
"bytes" "bytes"
tmplhtml "html/template"
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"path/filepath" "path/filepath"
"regexp" "regexp"
"sort" "sort"
"strings" "strings"
"time"
tmplhtml "html/template"
tmpltext "text/template" tmpltext "text/template"
"time"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
@ -315,6 +314,9 @@ func (t *Template) Data(recv string, groupLabels model.LabelSet, alerts ...*type
commonAnnotations = alerts[0].Annotations.Clone() commonAnnotations = alerts[0].Annotations.Clone()
) )
for _, a := range alerts[1:] { for _, a := range alerts[1:] {
if len(commonLabels) == 0 && len(commonAnnotations) == 0 {
break
}
for ln, lv := range commonLabels { for ln, lv := range commonLabels {
if a.Labels[ln] != lv { if a.Labels[ln] != lv {
delete(commonLabels, ln) delete(commonLabels, ln)

View File

@ -14,10 +14,14 @@
package template package template
import ( import (
"net/url"
"testing" "testing"
"time"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/prometheus/alertmanager/types"
) )
func TestPairNames(t *testing.T) { func TestPairNames(t *testing.T) {
@ -118,3 +122,251 @@ func TestAlertsResolved(t *testing.T) {
} }
} }
} }
func TestData(t *testing.T) {
u, err := url.Parse("http://example.com/")
require.NoError(t, err)
tmpl := &Template{ExternalURL: u}
startTime := time.Time{}.Add(1 * time.Second)
endTime := time.Time{}.Add(2 * time.Second)
for _, tc := range []struct {
receiver string
groupLabels model.LabelSet
alerts []*types.Alert
exp *Data
}{
{
receiver: "webhook",
exp: &Data{
Receiver: "webhook",
Status: "resolved",
Alerts: Alerts{},
GroupLabels: KV{},
CommonLabels: KV{},
CommonAnnotations: KV{},
ExternalURL: u.String(),
},
},
{
receiver: "webhook",
groupLabels: model.LabelSet{
model.LabelName("job"): model.LabelValue("foo"),
},
alerts: []*types.Alert{
{
Alert: model.Alert{
StartsAt: startTime,
Labels: model.LabelSet{
model.LabelName("severity"): model.LabelValue("warning"),
model.LabelName("job"): model.LabelValue("foo"),
},
Annotations: model.LabelSet{
model.LabelName("description"): model.LabelValue("something happened"),
model.LabelName("runbook"): model.LabelValue("foo"),
},
},
},
{
Alert: model.Alert{
StartsAt: startTime,
EndsAt: endTime,
Labels: model.LabelSet{
model.LabelName("severity"): model.LabelValue("critical"),
model.LabelName("job"): model.LabelValue("foo"),
},
Annotations: model.LabelSet{
model.LabelName("description"): model.LabelValue("something else happened"),
model.LabelName("runbook"): model.LabelValue("foo"),
},
},
},
},
exp: &Data{
Receiver: "webhook",
Status: "firing",
Alerts: Alerts{
{
Status: "firing",
Labels: KV{"severity": "warning", "job": "foo"},
Annotations: KV{"description": "something happened", "runbook": "foo"},
StartsAt: startTime,
},
{
Status: "resolved",
Labels: KV{"severity": "critical", "job": "foo"},
Annotations: KV{"description": "something else happened", "runbook": "foo"},
StartsAt: startTime,
EndsAt: endTime,
},
},
GroupLabels: KV{"job": "foo"},
CommonLabels: KV{"job": "foo"},
CommonAnnotations: KV{"runbook": "foo"},
ExternalURL: u.String(),
},
},
{
receiver: "webhook",
groupLabels: model.LabelSet{},
alerts: []*types.Alert{
{
Alert: model.Alert{
StartsAt: startTime,
Labels: model.LabelSet{
model.LabelName("severity"): model.LabelValue("warning"),
model.LabelName("job"): model.LabelValue("foo"),
},
Annotations: model.LabelSet{
model.LabelName("description"): model.LabelValue("something happened"),
model.LabelName("runbook"): model.LabelValue("foo"),
},
},
},
{
Alert: model.Alert{
StartsAt: startTime,
EndsAt: endTime,
Labels: model.LabelSet{
model.LabelName("severity"): model.LabelValue("critical"),
model.LabelName("job"): model.LabelValue("bar"),
},
Annotations: model.LabelSet{
model.LabelName("description"): model.LabelValue("something else happened"),
model.LabelName("runbook"): model.LabelValue("bar"),
},
},
},
},
exp: &Data{
Receiver: "webhook",
Status: "firing",
Alerts: Alerts{
{
Status: "firing",
Labels: KV{"severity": "warning", "job": "foo"},
Annotations: KV{"description": "something happened", "runbook": "foo"},
StartsAt: startTime,
},
{
Status: "resolved",
Labels: KV{"severity": "critical", "job": "bar"},
Annotations: KV{"description": "something else happened", "runbook": "bar"},
StartsAt: startTime,
EndsAt: endTime,
},
},
GroupLabels: KV{},
CommonLabels: KV{},
CommonAnnotations: KV{},
ExternalURL: u.String(),
},
},
} {
tc := tc
t.Run("", func(t *testing.T) {
got := tmpl.Data(tc.receiver, tc.groupLabels, tc.alerts...)
require.Equal(t, tc.exp, got)
})
}
}
func TestTemplateExpansion(t *testing.T) {
tmpl, err := FromGlobs()
require.NoError(t, err)
for _, tc := range []struct {
title string
in string
data interface{}
html bool
exp string
fail bool
}{
{
title: "Template without action",
in: `abc`,
exp: "abc",
},
{
title: "Template with simple action",
in: `{{ "abc" }}`,
exp: "abc",
},
{
title: "Template with invalid syntax",
in: `{{ `,
fail: true,
},
{
title: "Template using toUpper",
in: `{{ "abc" | toUpper }}`,
exp: "ABC",
},
{
title: "Template using toLower",
in: `{{ "ABC" | toLower }}`,
exp: "abc",
},
{
title: "Template using title",
in: `{{ "abc" | title }}`,
exp: "Abc",
},
{
title: "Template using positive match",
in: `{{ if match "^a" "abc"}}abc{{ end }}`,
exp: "abc",
},
{
title: "Template using negative match",
in: `{{ if match "abcd" "abc" }}abc{{ end }}`,
exp: "",
},
{
title: "Template using join",
in: `{{ . | join "," }}`,
data: []string{"a", "b", "c"},
exp: "a,b,c",
},
{
title: "Text template without HTML escaping",
in: `{{ "<b>" }}`,
exp: "<b>",
},
{
title: "HTML template with escaping",
in: `{{ "<b>" }}`,
html: true,
exp: "&lt;b&gt;",
},
{
title: "HTML template using safeHTML",
in: `{{ "<b>" | safeHtml }}`,
html: true,
exp: "<b>",
},
{
title: "Template using reReplaceAll",
in: `{{ reReplaceAll "ab" "AB" "abcdabcda"}}`,
exp: "ABcdABcda",
},
} {
tc := tc
t.Run(tc.title, func(t *testing.T) {
f := tmpl.ExecuteTextString
if tc.html {
f = tmpl.ExecuteHTMLString
}
got, err := f(tc.in, tc.data)
if tc.fail {
require.NotNil(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tc.exp, got)
})
}
}