Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty check name template was causing an error #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ func createEvent(inputEvent *corev2.Event, status int, checkNameTemplate string,
return nil, errors.New("negative status")
}
// Let's construct the check name from template
checkName, err := templates.EvalTemplate("check-name", checkNameTemplate, inputEvent)
var checkName = inputEvent.Check.Name
var err error
if len(checkNameTemplate) > 0 {
checkName, err = templates.EvalTemplate("check-name", checkNameTemplate, inputEvent)
}
if err != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the error check can be moved inside the if block as the error will always be null if we don't call EvalTemplate.

return nil, err
}
Expand Down
47 changes: 47 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,53 @@ type testHandler struct {
event *sensu.Event
}

func TestCreateEvent_TemplateEvaluation(t *testing.T) {
testCases := []struct {
name string
event *sensu.Event // Setup sensu event data here
checkNameTemplate string
expectedCheckName string
expectedError bool
}{
{
name: "Simple Template",
event: sensu.FixtureEvent("entity1", "check1"),
checkNameTemplate: "new-{{ .Check.Name }}",
expectedCheckName: "new-check1",
expectedError: false,
},
{
name: "Invalid Template",
event: sensu.FixtureEvent("entity1", "check1"),
checkNameTemplate: "{{ .Foo }}", // Assuming 'Foo' isn't a valid field
expectedCheckName: "", // Template evaluation should fail
expectedError: true,
},
// ... add more test cases with different scenarios
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want more test here? One I can think of is with an empty ``checkNameTemplate`.

}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
outputEvent, err := createEvent(tc.event, 1, tc.checkNameTemplate, "") // Status 0 here as it doesn't impact this test

if tc.expectedError {
if err == nil {
t.Error("Expected an error during template evaluation, but got none")
}
} else {
if err != nil {
t.Errorf("Unexpected error during template evaluation: %v", err)
}
if outputEvent.Check.Name != tc.expectedCheckName {
t.Errorf("Incorrect check name. Expected: %s, Got: %s", tc.expectedCheckName, outputEvent.Check.Name)
}
}
})
}
}

func (h testHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

if !strings.HasSuffix(req.URL.Path, "/events") {
http.Error(w, "not found", 404)
return
Expand Down Expand Up @@ -47,6 +93,7 @@ func (h testHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}

func TestSendEvent(t *testing.T) {

event := sensu.FixtureEvent("foo", "bar")
server := httptest.NewServer(testHandler{t: t, event: event})
defer server.Close()
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ func executeCheck(event *corev2.Event) (int, error) {
fmt.Printf("Error: Event API url not defined. Event generation aborted\n")
return sensu.CheckStateWarning, nil
}

outputEvent, err := createEvent(event, status, plugin.CheckNameTemplate, output)
if err != nil {
fmt.Printf("Error creating event: %s\n", err)
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func TestExecuteWithEvent(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 1, status)

// no name template error
//no name template error
td, err = os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
Expand All @@ -414,7 +414,7 @@ func TestExecuteWithEvent(t *testing.T) {
plugin.CheckNameTemplate = ""
status, err = executeCheck(event)
assert.NoError(t, err)
assert.Equal(t, 1, status)
assert.Equal(t, 0, status)

// 404 events api status error
td, err = os.MkdirTemp("", "")
Expand Down
Loading