|
|
|
@ -3,14 +3,12 @@ package notifier |
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"net/url" |
|
|
|
|
"os" |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
alertingImages "github.com/grafana/alerting/images" |
|
|
|
|
"github.com/grafana/alerting/receivers" |
|
|
|
|
alertingEmail "github.com/grafana/alerting/receivers/email" |
|
|
|
|
alertingTemplates "github.com/grafana/alerting/templates" |
|
|
|
|
"github.com/prometheus/alertmanager/template" |
|
|
|
|
"github.com/prometheus/alertmanager/types" |
|
|
|
|
"github.com/prometheus/common/model" |
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
@ -26,7 +24,7 @@ import ( |
|
|
|
|
func TestEmailNotifierIntegration(t *testing.T) { |
|
|
|
|
ns := createEmailSender(t) |
|
|
|
|
|
|
|
|
|
emailTmpl := templateForTests(t) |
|
|
|
|
emailTmpl := alertingTemplates.ForTests(t) |
|
|
|
|
externalURL, err := url.Parse("http://localhost/base") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
emailTmpl.ExternalURL = externalURL |
|
|
|
@ -187,7 +185,7 @@ func TestEmailNotifierIntegration(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func createSut(t *testing.T, messageTmpl string, subjectTmpl string, emailTmpl *template.Template, ns receivers.EmailSender) *alertingEmail.Notifier { |
|
|
|
|
func createSut(t *testing.T, messageTmpl string, subjectTmpl string, emailTmpl *alertingTemplates.Template, ns receivers.EmailSender) *alertingEmail.Notifier { |
|
|
|
|
t.Helper() |
|
|
|
|
if subjectTmpl == "" { |
|
|
|
|
subjectTmpl = alertingTemplates.DefaultMessageTitleEmbed |
|
|
|
@ -235,23 +233,3 @@ func createEmailSender(t *testing.T) *emailSender { |
|
|
|
|
|
|
|
|
|
return &emailSender{ns: ns} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func templateForTests(t *testing.T) *template.Template { |
|
|
|
|
f, err := os.CreateTemp("/tmp", "template") |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
defer func(f *os.File) { |
|
|
|
|
_ = f.Close() |
|
|
|
|
}(f) |
|
|
|
|
|
|
|
|
|
t.Cleanup(func() { |
|
|
|
|
require.NoError(t, os.RemoveAll(f.Name())) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
_, err = f.WriteString(alertingTemplates.TemplateForTestsString) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
tmpl, err := template.FromGlobs([]string{f.Name()}) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
return tmpl |
|
|
|
|
} |
|
|
|
|