github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/log/collectorHook_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package log 5 6 import ( 7 "reflect" 8 "testing" 9 "time" 10 11 "github.com/sirupsen/logrus" 12 ) 13 14 func TestCollectorHook_Fire(t *testing.T) { 15 type fields struct { 16 CorrelationID string 17 Messages []Message 18 } 19 type args struct { 20 entry *logrus.Entry 21 } 22 tests := []struct { 23 name string 24 fields fields 25 args args 26 wantErr bool 27 }{ 28 {"Test Fire", 29 fields{ 30 CorrelationID: "123", 31 Messages: []Message{}, 32 }, 33 args{entry: &logrus.Entry{ 34 Time: time.Now(), 35 Data: logrus.Fields{"test": "test value"}, 36 Message: "Test Message", 37 }, 38 }, 39 false, 40 }, 41 } 42 for _, tt := range tests { 43 t.Run(tt.name, func(t *testing.T) { 44 initialMessageLength := len(tt.fields.Messages) 45 f := &CollectorHook{ 46 CorrelationID: tt.fields.CorrelationID, 47 Messages: tt.fields.Messages, 48 } 49 // Check if hook was triggered 50 if err := f.Fire(tt.args.entry); (err != nil) != tt.wantErr { 51 t.Errorf("Fire() error = %v, wantErr %v", err, tt.wantErr) 52 } 53 54 // Check if the message was successfully added 55 if len(f.Messages) != initialMessageLength+1 { 56 t.Errorf("Fire() error - Messages not added to array - Message count %v", len(f.Messages)) 57 } 58 }) 59 } 60 } 61 func TestCollectorHook_Levels(t *testing.T) { 62 type fields struct { 63 CorrelationID string 64 Messages []Message 65 } 66 tests := []struct { 67 name string 68 fields fields 69 want []logrus.Level 70 }{ 71 {"Test Levels", 72 fields{ 73 CorrelationID: "123", 74 Messages: []Message{ 75 { 76 Time: time.Now(), 77 Level: logrus.DebugLevel, 78 Message: "Test Message", 79 }, 80 }, 81 }, 82 []logrus.Level{logrus.InfoLevel, logrus.DebugLevel, logrus.WarnLevel, logrus.ErrorLevel, logrus.PanicLevel, logrus.FatalLevel}, 83 }, 84 } 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 f := &CollectorHook{ 88 CorrelationID: tt.fields.CorrelationID, 89 Messages: tt.fields.Messages, 90 } 91 if got := f.Levels(); !reflect.DeepEqual(got, tt.want) { 92 t.Errorf("Levels() = %v, want %v", got, tt.want) 93 } 94 }) 95 } 96 }