github.com/SAP/jenkins-library@v1.362.0/pkg/log/sentryHook_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package log
     5  
     6  import (
     7  	"errors"
     8  	"testing"
     9  
    10  	"github.com/getsentry/sentry-go"
    11  	"github.com/sirupsen/logrus"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestSentryHookLevels(t *testing.T) {
    16  	hook := NewSentryHook("", "")
    17  	assert.Equal(t, []logrus.Level{logrus.PanicLevel, logrus.FatalLevel}, hook.Levels())
    18  }
    19  
    20  func TestSentryHookDsn(t *testing.T) {
    21  	sentryDsn := "http://publickey@url.to.sentry/1"
    22  	hook := NewSentryHook(sentryDsn, "")
    23  
    24  	assert.Equal(t, hook.Hub.Client().Options().Dsn, sentryDsn)
    25  }
    26  
    27  func TestSentryHookNoErrorOnInvalidDsn(t *testing.T) {
    28  	hook := NewSentryHook("invalid sentry dsn", "")
    29  
    30  	entry := logrus.Entry{}
    31  	err := hook.Fire(&entry)
    32  	assert.NoError(t, err)
    33  }
    34  
    35  func TestSentryHookEventLevel(t *testing.T) {
    36  	sentryDsn := "http://publickey@url.to.sentry/1"
    37  	hook := NewSentryHook(sentryDsn, "")
    38  
    39  	entry := logrus.Entry{}
    40  	err := hook.Fire(&entry)
    41  
    42  	assert.NoError(t, err)
    43  	assert.Equal(t, sentry.LevelFatal, hook.Event.Level)
    44  }
    45  
    46  func TestSentryHookManualTag(t *testing.T) {
    47  	sentryDsn := "http://publickey@url.to.sentry/1"
    48  	hook := NewSentryHook(sentryDsn, "")
    49  	key := "testkey"
    50  	value := "testValue"
    51  	hook.tags[key] = value
    52  
    53  	assert.NotNil(t, hook.tags[key])
    54  	assert.Equal(t, value, hook.tags[key])
    55  
    56  	entry := logrus.Entry{}
    57  	err := hook.Fire(&entry)
    58  
    59  	assert.NoError(t, err)
    60  	assert.Equal(t, value, hook.tags[key])
    61  }
    62  
    63  func TestSentryHookAutomaticTags(t *testing.T) {
    64  	sentryDsn := "http://publickey@url.to.sentry/1"
    65  	corrId := "correlationId"
    66  	hook := NewSentryHook(sentryDsn, corrId)
    67  	stepName := "stepName"
    68  	category := "category"
    69  	value := "testValue"
    70  
    71  	entry := logrus.Entry{
    72  		Data: logrus.Fields{
    73  			stepName: value,
    74  			category: value,
    75  		},
    76  	}
    77  	err := hook.Fire(&entry)
    78  
    79  	assert.NoError(t, err)
    80  	assert.Equal(t, value, hook.tags[stepName])
    81  	assert.Equal(t, value, hook.tags[category])
    82  	assert.Equal(t, corrId, hook.tags[corrId])
    83  }
    84  
    85  func TestSentryHookException(t *testing.T) {
    86  	sentryDsn := "http://publickey@url.to.sentry/1"
    87  	hook := NewSentryHook(sentryDsn, "")
    88  	hook.Event = sentry.NewEvent()
    89  	errVal := errors.New("Exception value")
    90  	errorMessage := "actual error message"
    91  	exception := sentry.Exception{
    92  		Type:  errorMessage,
    93  		Value: errVal.Error(),
    94  	}
    95  
    96  	entry := logrus.Entry{
    97  		Data: logrus.Fields{
    98  			logrus.ErrorKey: errVal,
    99  		},
   100  		Message: errorMessage,
   101  	}
   102  	err := hook.Fire(&entry)
   103  
   104  	assert.NoError(t, err)
   105  	assert.Equal(t, []sentry.Exception{exception}, hook.Event.Exception)
   106  }