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/sentryHook_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

107 lines
2.4 KiB
Go

//go:build unit
// +build unit
package log
import (
"errors"
"testing"
"github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestSentryHookLevels(t *testing.T) {
hook := NewSentryHook("", "")
assert.Equal(t, []logrus.Level{logrus.PanicLevel, logrus.FatalLevel}, hook.Levels())
}
func TestSentryHookDsn(t *testing.T) {
sentryDsn := "http://publickey@url.to.sentry/1"
hook := NewSentryHook(sentryDsn, "")
assert.Equal(t, hook.Hub.Client().Options().Dsn, sentryDsn)
}
func TestSentryHookNoErrorOnInvalidDsn(t *testing.T) {
hook := NewSentryHook("invalid sentry dsn", "")
entry := logrus.Entry{}
err := hook.Fire(&entry)
assert.NoError(t, err)
}
func TestSentryHookEventLevel(t *testing.T) {
sentryDsn := "http://publickey@url.to.sentry/1"
hook := NewSentryHook(sentryDsn, "")
entry := logrus.Entry{}
err := hook.Fire(&entry)
assert.NoError(t, err)
assert.Equal(t, sentry.LevelFatal, hook.Event.Level)
}
func TestSentryHookManualTag(t *testing.T) {
sentryDsn := "http://publickey@url.to.sentry/1"
hook := NewSentryHook(sentryDsn, "")
key := "testkey"
value := "testValue"
hook.tags[key] = value
assert.NotNil(t, hook.tags[key])
assert.Equal(t, value, hook.tags[key])
entry := logrus.Entry{}
err := hook.Fire(&entry)
assert.NoError(t, err)
assert.Equal(t, value, hook.tags[key])
}
func TestSentryHookAutomaticTags(t *testing.T) {
sentryDsn := "http://publickey@url.to.sentry/1"
corrId := "correlationId"
hook := NewSentryHook(sentryDsn, corrId)
stepName := "stepName"
category := "category"
value := "testValue"
entry := logrus.Entry{
Data: logrus.Fields{
stepName: value,
category: value,
},
}
err := hook.Fire(&entry)
assert.NoError(t, err)
assert.Equal(t, value, hook.tags[stepName])
assert.Equal(t, value, hook.tags[category])
assert.Equal(t, corrId, hook.tags[corrId])
}
func TestSentryHookException(t *testing.T) {
sentryDsn := "http://publickey@url.to.sentry/1"
hook := NewSentryHook(sentryDsn, "")
hook.Event = sentry.NewEvent()
errVal := errors.New("Exception value")
errorMessage := "actual error message"
exception := sentry.Exception{
Type: errorMessage,
Value: errVal.Error(),
}
entry := logrus.Entry{
Data: logrus.Fields{
logrus.ErrorKey: errVal,
},
Message: errorMessage,
}
err := hook.Fire(&entry)
assert.NoError(t, err)
assert.Equal(t, []sentry.Exception{exception}, hook.Event.Exception)
}