-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract templating so it can be used from multiple notifiers
- Loading branch information
Showing
3 changed files
with
143 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package notifier | ||
|
||
import ( | ||
"bytes" | ||
"html/template" | ||
) | ||
|
||
type TemplateData struct { | ||
ClusterName string | ||
SystemStatus string | ||
FailCount int | ||
WarnCount int | ||
PassCount int | ||
Nodes map[string]Messages | ||
} | ||
|
||
func (t TemplateData) IsCritical() bool { | ||
return t.SystemStatus == SYSTEM_CRITICAL | ||
} | ||
|
||
func (t TemplateData) IsWarning() bool { | ||
return t.SystemStatus == SYSTEM_UNSTABLE | ||
} | ||
|
||
func (t TemplateData) IsPassing() bool { | ||
return t.SystemStatus == SYSTEM_HEALTHY | ||
} | ||
|
||
func renderTemplate(t TemplateData, templateFile string, defaultTemplate string) (string, error) { | ||
var tmpl *template.Template | ||
var err error | ||
if templateFile == "" { | ||
tmpl, err = template.New("base").Parse(defaultTemplate) | ||
} else { | ||
tmpl, err = template.ParseFiles(templateFile) | ||
} | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var body bytes.Buffer | ||
if err := tmpl.Execute(&body, t); err != nil { | ||
return "", err | ||
} | ||
|
||
return body.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package notifier | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestRenderTemplateWithInvalidFile(t *testing.T) { | ||
templateData := TemplateData{ | ||
ClusterName: "some cluster", | ||
SystemStatus: "some status", | ||
FailCount: 1, | ||
WarnCount: 2, | ||
PassCount: 3, | ||
Nodes: make(map[string]Messages), | ||
} | ||
|
||
renderedTemplate, err := renderTemplate(templateData, "some-file-that-does-not-exist", "") | ||
|
||
if err == nil { | ||
t.Errorf("Expected error but rendered something: %s", renderedTemplate) | ||
} else if err.Error() != "open some-file-that-does-not-exist: no such file or directory" { | ||
t.Errorf("Expected error in opening file but got: %s", err.Error()) | ||
} | ||
} | ||
|
||
func TestRenderTemplate(t *testing.T) { | ||
tmpfile, err := templateFile("{{ .SystemStatus }} - {{ .FailCount }}") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(tmpfile.Name()) // clean up | ||
|
||
templateData := TemplateData{ | ||
ClusterName: "some cluster", | ||
SystemStatus: "some status", | ||
FailCount: 1, | ||
WarnCount: 2, | ||
PassCount: 3, | ||
Nodes: make(map[string]Messages), | ||
} | ||
|
||
renderedTemplate, err := renderTemplate(templateData, tmpfile.Name(), "") | ||
|
||
if err != nil { | ||
t.Errorf("Rendering failed: %v", err) | ||
} else if renderedTemplate != "some status - 1" { | ||
t.Errorf("Expected 'temporary file' but was '%v'", renderedTemplate) | ||
} | ||
} | ||
|
||
func TestRenderDefaultTemplate(t *testing.T) { | ||
templateData := TemplateData{ | ||
ClusterName: "some cluster", | ||
SystemStatus: "some status", | ||
FailCount: 1, | ||
WarnCount: 2, | ||
PassCount: 3, | ||
Nodes: make(map[string]Messages), | ||
} | ||
|
||
renderedTemplate, err := renderTemplate(templateData, "", "{{ .SystemStatus }} - {{ .FailCount }}") | ||
|
||
if err != nil { | ||
t.Errorf("Rendering failed: %v", err) | ||
} else if renderedTemplate != "some status - 1" { | ||
t.Errorf("Expected 'temporary file' but was '%v'", renderedTemplate) | ||
} | ||
} | ||
|
||
func templateFile(content string) (*os.File, error) { | ||
tmpfile, err := ioutil.TempFile("", "consulAlertsTest") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if _, err := tmpfile.Write([]byte(content)); err != nil { | ||
return nil, err | ||
} | ||
if err := tmpfile.Close(); err != nil { | ||
return nil, err | ||
} | ||
return tmpfile, nil | ||
} |