Skip to content

Commit e018b3e

Browse files
Update unit test for formatTitle (#57)
* Update version of x/sys * Update unit tests for formatTitle
1 parent 95f0bcf commit e018b3e

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed

alerts/template_test.go

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
package alerts
1717

1818
import (
19-
"fmt"
2019
"html/template"
21-
"strings"
2220
"testing"
2321

2422
"github.com/prometheus/alertmanager/notify/webhook"
@@ -39,7 +37,7 @@ func Test_formatIssueBody(t *testing.T) {
3937
}
4038
}
4139

42-
func TestFormatTitleSimple(t *testing.T) {
40+
func TestReceiverHandler_formatTitle(t *testing.T) {
4341
msg := webhook.Message{
4442
Data: &amtmpl.Data{
4543
Status: "firing",
@@ -54,41 +52,46 @@ func TestFormatTitleSimple(t *testing.T) {
5452
},
5553
}
5654
tests := []struct {
57-
tmplTxt string
58-
expectErrTxt string
59-
expectOutput string
55+
name string
56+
template string
57+
want string
58+
wantErr bool
6059
}{
61-
{"foo", "", "foo"},
62-
{"{{ .Data.Status }}", "", "firing"},
63-
{"{{ .Status }}", "", "firing"},
64-
{"{{ range .Alerts }}{{ .Annotations.env }} {{ end }}", "", "prod stage "},
65-
{"{{ .Foo }}", "can't evaluate field Foo", ""},
60+
{
61+
name: "success-simple",
62+
template: "foo",
63+
want: "foo",
64+
},
65+
{
66+
name: "success-template-simple",
67+
template: "{{ .Data.Status }}",
68+
want: "firing",
69+
},
70+
{
71+
name: "success-template-complex",
72+
template: "{{ range .Alerts }}{{ .Annotations.env }} {{ end }}",
73+
want: "prod stage ",
74+
},
75+
{
76+
name: "error-bad-template",
77+
template: "{{ .Foo }}",
78+
wantErr: true,
79+
},
6680
}
67-
68-
for testNum, tc := range tests {
69-
testName := fmt.Sprintf("tc=%d", testNum)
70-
t.Run(testName, func(t *testing.T) {
71-
rh, err := NewReceiver(&fakeClient{}, "default", false, "", nil, tc.tmplTxt)
81+
for _, tt := range tests {
82+
t.Run(tt.name, func(t *testing.T) {
83+
rh, err := NewReceiver(&fakeClient{}, "default", false, "", nil, tt.template)
7284
if err != nil {
7385
t.Fatal(err)
7486
}
7587

76-
title, err := rh.formatTitle(&msg)
77-
if tc.expectErrTxt == "" && err != nil {
78-
t.Error(err)
79-
}
80-
if tc.expectErrTxt != "" {
81-
if err == nil {
82-
t.Error()
83-
} else if !strings.Contains(err.Error(), tc.expectErrTxt) {
84-
t.Error(err.Error())
85-
}
86-
}
87-
if tc.expectOutput == "" && title != "" {
88-
t.Error(title)
88+
got, err := rh.formatTitle(&msg)
89+
if (err != nil) != tt.wantErr {
90+
t.Errorf("ReceiverHandler.formatTitle() error = %v, wantErr %v", err, tt.wantErr)
91+
return
8992
}
90-
if !strings.Contains(title, tc.expectOutput) {
91-
t.Error(title)
93+
if got != tt.want {
94+
t.Errorf("ReceiverHandler.formatTitle() = %v, want %v", got, tt.want)
9295
}
9396
})
9497
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ require (
4646
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
4747
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd // indirect
4848
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
49-
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 // indirect
49+
golang.org/x/sys v0.3.0 // indirect
5050
golang.org/x/text v0.3.2 // indirect
5151
google.golang.org/appengine v1.6.6 // indirect
5252
google.golang.org/protobuf v1.23.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ golang.org/x/sys v0.0.0-20200409092240-59c9f1ba88fa/go.mod h1:h1NjWce9XRLGQEsW7w
417417
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
418418
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80=
419419
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
420+
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
421+
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
420422
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
421423
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
422424
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)