1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/log/collectorHook_test.go
ffeldmann b88ebdad6c
feat(splunk) Sending telemetry and logging information to Splunk (#2796)
* Adds inital splunk hook and logCollector

* Adds documentation of the Splunk hook

* Fixes markdown lint issues and removes comment from telemetry.go file

* Fixes markdown lint issues and adds missing generated file

* Markdown linting

* Changes documentation according to review, adds Splunk token automatically during init

* Adds error handling for marshalling hook config

* Markdown lint und correct Splunk token in httpclient

* Registeres Splunk token as secret and adjusts test cases

* Adds missing error handling and removes unnecessary comments

* Creates new function readPipelineEnvironment, adds tests

* Moves MonitoringData struct, edits defaults for json fields

* Adds gitRepository and gitOwner to telemetry information

* Simplifies readCommonPipelineEnvironment function, adds more descriptive errors, adds automated adding of Splunk prefix token

* Adjusts error handling

* Cleaner error logging
2021-05-17 12:14:04 +02:00

94 lines
2.0 KiB
Go

package log
import (
"reflect"
"testing"
"time"
"github.com/sirupsen/logrus"
)
func TestCollectorHook_Fire(t *testing.T) {
type fields struct {
CorrelationID string
Messages []Message
}
type args struct {
entry *logrus.Entry
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{"Test Fire",
fields{
CorrelationID: "123",
Messages: []Message{},
},
args{entry: &logrus.Entry{
Time: time.Now(),
Data: logrus.Fields{"test": "test value"},
Message: "Test Message",
},
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
initialMessageLength := len(tt.fields.Messages)
f := &CollectorHook{
CorrelationID: tt.fields.CorrelationID,
Messages: tt.fields.Messages,
}
// Check if hook was triggered
if err := f.Fire(tt.args.entry); (err != nil) != tt.wantErr {
t.Errorf("Fire() error = %v, wantErr %v", err, tt.wantErr)
}
// Check if the message was successfully added
if len(f.Messages) != initialMessageLength+1 {
t.Errorf("Fire() error - Messages not added to array - Message count %v", len(f.Messages))
}
})
}
}
func TestCollectorHook_Levels(t *testing.T) {
type fields struct {
CorrelationID string
Messages []Message
}
tests := []struct {
name string
fields fields
want []logrus.Level
}{
{"Test Levels",
fields{
CorrelationID: "123",
Messages: []Message{
{
Time: time.Now(),
Level: logrus.DebugLevel,
Message: "Test Message",
},
},
},
[]logrus.Level{logrus.InfoLevel, logrus.DebugLevel, logrus.WarnLevel, logrus.ErrorLevel, logrus.PanicLevel, logrus.FatalLevel},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &CollectorHook{
CorrelationID: tt.fields.CorrelationID,
Messages: tt.fields.Messages,
}
if got := f.Levels(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Levels() = %v, want %v", got, tt.want)
}
})
}
}