Skip to content

Commit

Permalink
move summary and details to their own functions and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Campbell <todd@sensu.io>
  • Loading branch information
Todd Campbell committed Aug 21, 2020
1 parent f0f4ed4 commit 4058188
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
52 changes: 36 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,21 @@ func checkArgs(event *corev2.Event) error {
}

func manageIncident(event *corev2.Event) error {
var details interface{}

severity, err := getPagerDutySeverity(event, config.statusMapJson)
if err != nil {
return err
}
log.Printf("Incident severity: %s", severity)

summary, err := templates.EvalTemplate("summary", config.summaryTemplate, event)
summary, err := getSummary(event)
if err != nil {
return fmt.Errorf("failed to evaluate template %s: %v", config.summaryTemplate, err)
}
if len(config.detailsTemplate) > 0 {
details, err = templates.EvalTemplate("details", config.detailsTemplate, event)
if err != nil {
return fmt.Errorf("failed to evaluate template %s: %v", config.detailsTemplate, err)
}
} else {
details = event
return err
}
// "The maximum permitted length of this property is 1024 characters."
if len(summary) > 1024 {
summary = summary[:1024]

details, err := getDetails(event)
if err != nil {
return err
}
log.Printf("Incident Summary: %s", summary)

// "The maximum permitted length of PG event is 512 KB. Let's limit check output to 256KB to prevent triggering a failed send"
if len(event.Check.Output) > 256000 {
Expand Down Expand Up @@ -238,3 +228,33 @@ func parseStatusMap(statusMapJson string) (map[uint32]string, error) {

return statusToSeverityMap, nil
}

func getSummary(event *corev2.Event) (string, error) {
summary, err := templates.EvalTemplate("summary", config.summaryTemplate, event)
if err != nil {
return "", fmt.Errorf("failed to evaluate template %s: %v", config.summaryTemplate, err)
}
// "The maximum permitted length of this property is 1024 characters."
if len(summary) > 1024 {
summary = summary[:1024]
}
log.Printf("Incident Summary: %s", summary)
return summary, nil
}

func getDetails(event *corev2.Event) (interface{}, error) {
var (
details interface{}
err error
)

if len(config.detailsTemplate) > 0 {
details, err = templates.EvalTemplate("details", config.detailsTemplate, event)
if err != nil {
return "", fmt.Errorf("failed to evaluate template %s: %v", config.detailsTemplate, err)
}
} else {
details = event
}
return details, nil
}
36 changes: 36 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"encoding/json"

corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/stretchr/testify/assert"
"testing"
Expand Down Expand Up @@ -103,3 +105,37 @@ func Test_GetPagerDutyDedupKey(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "foo-bar", dedupKey)
}

func Test_GetSummary(t *testing.T) {
event := corev2.FixtureEvent("foo", "bar")
config.summaryTemplate = "{{.Entity.Name}}-{{.Check.Name}}"

summary, err := getSummary(event)
assert.Nil(t, err)
assert.Equal(t, "foo-bar", summary)
}

func Test_GetDetailsJSON(t *testing.T) {
event := corev2.FixtureEvent("foo", "bar")
config.detailsTemplate = ""

details, err := getDetails(event)
assert.Nil(t, err)
b, err := json.Marshal(details)
assert.Nil(t, err)
j := &corev2.Event{}
err = json.Unmarshal(b, &j)
assert.Nil(t, err)
assert.Equal(t, "foo", j.Entity.Name)
assert.Equal(t, "bar", j.Check.Name)
}

func Test_GetDetailsTemplate(t *testing.T) {
event := corev2.FixtureEvent("foo", "bar")
config.detailsTemplate = "{{.Entity.Name}}-{{.Check.Name}}"

details, err := getDetails(event)
assert.Nil(t, err)
assert.Equal(t, "foo-bar", details)
}

0 comments on commit 4058188

Please sign in to comment.