mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
c5b83de7e1
* Add ans implementation * Remove todo comment * Rename test function Co-authored-by: Linda Siebert <39100394+LindaSieb@users.noreply.github.com> * Better wording Co-authored-by: Linda Siebert <39100394+LindaSieb@users.noreply.github.com> * Add reading of response body function * Use http pkg ReadResponseBody * Check read error * Better test case description * Fix formatting * Create own package for read response body * Omit empty nested resource struct * Separate Resource struct from Event struct * Merge and unmarshall instead of only unmarshalling * Improve status code error message * Remove unchangeable event fields * Separate event parts * Change log level setter function * Restructure ans send test * Revert exporting readResponseBody function Instead the code is duplicated in the xsuaa and ans package * Add check correct ans setup request * Add set options function for mocking * Review fixes * Correct function name * Use strict unmarshalling * Validate event * Move functions * Add documentation comments * improve test * Validate event * Add logrus hook for ans * Set defaults on new hook creation * Fix log level on error * Don't alter entry log level * Set severity fatal on 'fatal error' log message * Ensure that log entries don't affect each other * Remove unnecessary correlationID * Use file path instead of event template string * Improve warning messages * Add empty log message check * Allow configuration from file and string * Add sourceEventId to tags * Change resourceType to Pipeline * Use structured config approach * Use new log level set function * Check correct setup and return error * Mock http requests * Only send log level warning or higher * Use new function name * One-liner ifs * Improve test name * Fix tests * Prevent double firing * Reduce Fire test size * Add error message to test * Reduce newANSHook test size * Further check error * Rename to defaultEvent in hook struct * Reduce ifs further * Fix set error category test The ansHook Fire test cannot run in parallel, as it would affect the other tests that use the error category. * Change function name to SetServiceKey * Validate event * Rename to eventTemplate in hook struct * Move copy to event.go * Fix function mix * Remove unnecessary cleanup * Remove parallel test The translation fails now and again when parallel is on. * Remove prefix test * Remove unused copyEvent function * Fix ifs * Add docu comment * Register ans hook from pkg * register hook and setup event template seperately * Exclusively read eventTemplate from environment * setupEventTemplate tests * adjust hook levels test * sync tests- wlill still fail * migrate TestANSHook_registerANSHook test * fixes * review - cleanup, reuse poke * Apply suggestions from code review * Change subject * Review fixes * Set stepName 'n/a' if not available * Fix fire tests Co-authored-by: Linda Siebert <39100394+LindaSieb@users.noreply.github.com> Co-authored-by: Roland Stengel <r.stengel@sap.com>
194 lines
5.2 KiB
Go
194 lines
5.2 KiB
Go
package ans
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"testing"
|
|
)
|
|
|
|
func TestEvent_MergeWithJSON(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
eventJSON string
|
|
existingEvent Event
|
|
wantEvent Event
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Proper event JSON yields correct event",
|
|
eventJSON: `{"eventType": "my event","eventTimestamp":1647526655}`,
|
|
wantEvent: Event{
|
|
EventType: "my event",
|
|
EventTimestamp: 1647526655,
|
|
},
|
|
},
|
|
{
|
|
name: "Merging events includes all parts",
|
|
eventJSON: `{"eventType": "my event", "eventTimestamp": 1647526655, "tags": {"we": "were", "here": "first"}, "resource": {"resourceInstance": "myResourceInstance", "resourceName": "was changed"}}`,
|
|
existingEvent: Event{
|
|
EventType: "test",
|
|
Subject: "test",
|
|
Tags: map[string]interface{}{"Some": 1.0, "Additional": "a string", "Tags": true},
|
|
Resource: &Resource{
|
|
ResourceType: "myResourceType",
|
|
ResourceName: "myResourceName",
|
|
},
|
|
},
|
|
wantEvent: Event{
|
|
EventType: "my event",
|
|
EventTimestamp: 1647526655,
|
|
Subject: "test",
|
|
Tags: map[string]interface{}{"we": "were", "here": "first", "Some": 1.0, "Additional": "a string", "Tags": true},
|
|
Resource: &Resource{
|
|
ResourceType: "myResourceType",
|
|
ResourceName: "was changed",
|
|
ResourceInstance: "myResourceInstance",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Faulty JSON yields error",
|
|
eventJSON: `faulty json`,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Non-existent field yields error",
|
|
eventJSON: `{"unknownKey": "yields error"}`,
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotEvent := tt.existingEvent
|
|
err := gotEvent.MergeWithJSON([]byte(tt.eventJSON))
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("MergeWithJSON() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEvent_SetLogLevel(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
level logrus.Level
|
|
wantSeverity string
|
|
wantCategory string
|
|
}{
|
|
{
|
|
name: "InfoLevel yields INFO and NOTIFICATION",
|
|
level: logrus.InfoLevel,
|
|
wantSeverity: infoSeverity,
|
|
wantCategory: notificationCategory,
|
|
},
|
|
{
|
|
name: "DebugLevel yields INFO and NOTIFICATION",
|
|
level: logrus.DebugLevel,
|
|
wantSeverity: infoSeverity,
|
|
wantCategory: notificationCategory,
|
|
},
|
|
{
|
|
name: "WarnLevel yields WARNING and ALERT",
|
|
level: logrus.WarnLevel,
|
|
wantSeverity: warningSeverity,
|
|
wantCategory: alertCategory,
|
|
},
|
|
{
|
|
name: "ErrorLevel yields ERROR and EXCEPTION",
|
|
level: logrus.ErrorLevel,
|
|
wantSeverity: errorSeverity,
|
|
wantCategory: exceptionCategory,
|
|
},
|
|
{
|
|
name: "FatalLevel yields FATAL and EXCEPTION",
|
|
level: logrus.FatalLevel,
|
|
wantSeverity: fatalSeverity,
|
|
wantCategory: exceptionCategory,
|
|
},
|
|
{
|
|
name: "PanicLevel yields FATAL and EXCEPTION",
|
|
level: logrus.PanicLevel,
|
|
wantSeverity: fatalSeverity,
|
|
wantCategory: exceptionCategory,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
event := &Event{}
|
|
event.SetSeverityAndCategory(tt.level)
|
|
assert.Equal(t, tt.wantSeverity, event.Severity, "Got wrong severity")
|
|
assert.Equal(t, tt.wantCategory, event.Category, "Got wrong category")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEvent_Validate(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
eventJSON string
|
|
errMsg string
|
|
}{
|
|
{
|
|
errMsg: "Category must be one of [EXCEPTION ALERT NOTIFICATION]",
|
|
eventJSON: `{"category": "WRONG_CATEGORY"}`,
|
|
},
|
|
{
|
|
errMsg: "Severity must be one of [INFO NOTICE WARNING ERROR FATAL]",
|
|
eventJSON: `{"severity": "WRONG_SEVERITY"}`,
|
|
},
|
|
{
|
|
errMsg: "Priority must be 1,000 or less",
|
|
eventJSON: `{"priority": 1001}`,
|
|
},
|
|
{
|
|
errMsg: "Priority must be 1 or greater",
|
|
eventJSON: `{"priority": -1}`,
|
|
},
|
|
{
|
|
errMsg: "EventTimestamp must be 0 or greater",
|
|
eventJSON: `{"eventTimestamp": -1}`,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.errMsg, func(t *testing.T) {
|
|
event := defaultEvent()
|
|
require.NoError(t, event.MergeWithJSON([]byte(tt.eventJSON)))
|
|
assert.EqualError(t, event.Validate(), fmt.Sprintf("%s: %s", tt.errMsg, standardErrMsg))
|
|
})
|
|
}
|
|
}
|
|
|
|
const standardErrMsg = "event JSON failed the validation"
|
|
|
|
func defaultEvent() Event {
|
|
return Event{
|
|
EventType: "MyEvent",
|
|
EventTimestamp: 1653485928,
|
|
Severity: "INFO",
|
|
Category: "NOTIFICATION",
|
|
Subject: "mySubject",
|
|
Body: "myBody",
|
|
Priority: 123,
|
|
Resource: &Resource{
|
|
ResourceName: "myResourceName",
|
|
ResourceType: "myResourceType",
|
|
ResourceInstance: "myResourceInstance",
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestEvent_Copy(t *testing.T) {
|
|
t.Parallel()
|
|
t.Run("good", func(t *testing.T) {
|
|
originalEvent := defaultEvent()
|
|
newEvent, err := originalEvent.Copy()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, originalEvent, newEvent, "Events should be the same after copying.")
|
|
newEvent.Resource.ResourceType = "different"
|
|
assert.NotEqual(t, originalEvent, newEvent, "Events should not affect each other after copying")
|
|
})
|
|
}
|