diff --git a/event.go b/event.go index 8c44de1..1c2366a 100644 --- a/event.go +++ b/event.go @@ -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 { return nil, err } diff --git a/http_test.go b/http_test.go index e7ad4c9..a5b9c0b 100644 --- a/http_test.go +++ b/http_test.go @@ -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 + } + + 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 @@ -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() diff --git a/main.go b/main.go index 13799d9..e56758f 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/main_test.go b/main_test.go index 7e610df..9d18377 100644 --- a/main_test.go +++ b/main_test.go @@ -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) @@ -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("", "")