diff --git a/go.mod b/go.mod index 3fa217b..0d9a212 100644 --- a/go.mod +++ b/go.mod @@ -8,11 +8,13 @@ require ( github.com/prometheus/alertmanager v0.23.0 github.com/sirupsen/logrus v1.6.0 github.com/spf13/pflag v1.0.5 + github.com/stretchr/testify v1.7.0 ) require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.5.1 // indirect github.com/golang/protobuf v1.4.3 // indirect github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect @@ -22,6 +24,7 @@ require ( github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/pelletier/go-toml v1.9.4 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.11.0 // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.30.0 // indirect @@ -32,6 +35,7 @@ require ( github.com/valyala/histogram v1.2.0 // indirect golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect google.golang.org/protobuf v1.26.0-rc.1 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) go 1.18 diff --git a/go.sum b/go.sum index fb1a0fd..53bb7bc 100644 --- a/go.sum +++ b/go.sum @@ -74,7 +74,6 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -345,6 +344,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw= @@ -387,6 +387,7 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= @@ -814,6 +815,7 @@ gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUy gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= diff --git a/internal/providers/google_chat/google_chat.go b/internal/providers/google_chat/google_chat.go index eef29f9..b9d723b 100644 --- a/internal/providers/google_chat/google_chat.go +++ b/internal/providers/google_chat/google_chat.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "net/url" + "path/filepath" "strings" "text/template" "time" @@ -69,7 +70,7 @@ func NewGoogleChat(opts GoogleChatOpts) (*GoogleChatManager, error) { } // Load the template. - tmpl, err := template.New("message.tmpl").Funcs(templateFuncMap).ParseFiles(opts.Template) + tmpl, err := template.New(filepath.Base(opts.Template)).Funcs(templateFuncMap).ParseFiles(opts.Template) if err != nil { return nil, err } diff --git a/internal/providers/google_chat/google_chat_test.go b/internal/providers/google_chat/google_chat_test.go new file mode 100644 index 0000000..8a7f2bd --- /dev/null +++ b/internal/providers/google_chat/google_chat_test.go @@ -0,0 +1,47 @@ +package google_chat + +import ( + "path/filepath" + "testing" + + alertmgrtmpl "github.com/prometheus/alertmanager/template" + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" +) + +func TestGoogleChatTemplate(t *testing.T) { + + opts := &GoogleChatOpts{ + Log: logrus.New(), + Endpoint: "http://", + Room: "qa", + Template: "../../../static/message.tmpl", + DryRun: true, + } + + chat, err := NewGoogleChat(*opts) + if err != nil || chat == nil { + t.Fatal(err) + } + + alert := alertmgrtmpl.Alert{ + Status: "firing", + Labels: alertmgrtmpl.KV(map[string]string{ + "severity": "high", "alertname": "TestAlert", + }), + Annotations: alertmgrtmpl.KV(map[string]string{ + "team": "qa", "dryrun": "true", + }), + } + + expectedMessage := "*(HIGH) TestAlert - Firing*\nDryrun: true\nTeam: qa\n\n" + + msgs, err := chat.prepareMessage(alert) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "message.tmpl", filepath.Base(chat.msgTmpl.Name()), "Message template name") + assert.Equal(t, msgs[0].Text, expectedMessage) + +}