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/errors_test.go
ffeldmann 19a05a9c70
Reporting: Variable change to deprecate step Splunk Hook; logging in telemetry (#3539)
* Adds fatalError variable with helper setter and getter helper functions
* Adds logging function to telemetry package (always executed)
2022-02-14 08:56:11 +01:00

91 lines
2.0 KiB
Go

package log
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSetErrorCategory(t *testing.T) {
SetErrorCategory(ErrorCustom)
assert.Equal(t, errorCategory, ErrorCustom)
assert.Equal(t, "custom", fmt.Sprint(errorCategory))
}
func TestGetErrorCategory(t *testing.T) {
errorCategory = ErrorCompliance
assert.Equal(t, GetErrorCategory(), errorCategory)
}
func TestSetFatalErrorDetail(t *testing.T) {
sampleError := logrus.Fields{"Message": "Error happened"}
errDetails, _ := json.Marshal(&sampleError)
tests := []struct {
name string
error []byte
want []byte
errPresent bool
}{
{
name: "set fatal error",
error: errDetails,
want: errDetails,
errPresent: false,
},
{
name: "set fatal error - override",
error: errDetails,
want: errDetails,
errPresent: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.errPresent {
SetFatalErrorDetail(tt.error)
}
SetFatalErrorDetail(tt.error)
assert.Equalf(t, tt.want, fatalError, "GetFatalErrorDetail()")
fatalError = nil // reset error
})
}
}
func TestGetFatalErrorDetail(t *testing.T) {
sampleError := logrus.Fields{"Message": "Error happened"}
errDetails, _ := json.Marshal(&sampleError)
tests := []struct {
name string
errDetails []byte
want string
}{
{
name: "returns fatal error",
errDetails: errDetails,
want: "{\"Message\":\"Error happened\"}",
},
{
name: "no fatal error set - returns empty",
errDetails: nil,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.errDetails != nil {
SetFatalErrorDetail(tt.errDetails)
}
str := ""
data := []byte(str)
fmt.Println(data)
fatalErrorDetail := string(GetFatalErrorDetail())
assert.Equalf(t, tt.want, fatalErrorDetail, "GetFatalErrorDetail()")
fatalError = nil // resets fatal error
})
}
}