@@ -31,18 +31,12 @@ func (t *NotificationTemplate) Validate() error {
|
||||
content = fmt.Sprintf("{{ define \"%s\" }}\n%s\n{{ end }}", t.Name, content)
|
||||
}
|
||||
t.Template = content
|
||||
|
||||
// Validate template contents. We try to stick as close to what will actually happen when the templates are parsed
|
||||
// by the alertmanager as possible.
|
||||
tmpl, err := templates.NewTemplate()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create template: %w", err)
|
||||
def := templates.TemplateDefinition{
|
||||
Name: t.Name,
|
||||
Template: t.Template,
|
||||
Kind: templates.GrafanaKind,
|
||||
}
|
||||
if err := tmpl.Parse(strings.NewReader(t.Template)); err != nil {
|
||||
return fmt.Errorf("invalid template: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return def.Validate()
|
||||
}
|
||||
|
||||
func (mt *MuteTimeInterval) Validate() error {
|
||||
|
||||
@@ -139,6 +139,7 @@ func ToTemplateDefinitions(cfg *apimodels.PostableUserConfig) []alertingTemplate
|
||||
out = append(out, alertingTemplates.TemplateDefinition{
|
||||
Name: name,
|
||||
Template: tmpl,
|
||||
Kind: alertingTemplates.GrafanaKind,
|
||||
})
|
||||
}
|
||||
return out
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user