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
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

97 lines
2.0 KiB
Go

//go:build unit
// +build unit
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)
}
})
}
}