diff --git a/Makefile b/Makefile index bf58b92..d93320b 100644 --- a/Makefile +++ b/Makefile @@ -100,6 +100,7 @@ tidy-deps: ## Cleans up dependencies make-reports-dir: mkdir -p $(REPORTS_DIR) +.PHONY: test test: ## Run tests with the "unit" build tag KUBECONFIG=/cluster/connections/not/allowed CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) --tags=unit -failfast -short ./... $(TEST_BUILDFLAGS) diff --git a/go.mod b/go.mod index 738a95f..78611c5 100644 --- a/go.mod +++ b/go.mod @@ -1,15 +1,19 @@ module github.com/jenkins-x/logrus-stackdriver-formatter -go 1.19 +go 1.22.3 require ( + github.com/TV4/logrus-stackdriver-formatter v0.1.0 github.com/go-stack/stack v1.8.1 - github.com/kr/pretty v0.3.0 github.com/sirupsen/logrus v1.9.0 + github.com/stretchr/testify v1.7.0 ) require ( - github.com/kr/text v0.2.0 // indirect - github.com/rogpeppe/go-internal v1.6.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/pretty v0.3.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect + gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index e5e3bb2..f4adc4b 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/TV4/logrus-stackdriver-formatter v0.1.0 h1:nFea8RiX7ecTnWPM+9FIqwZYJdcGo58CHMGIVdYzMXg= +github.com/TV4/logrus-stackdriver-formatter v0.1.0/go.mod h1:wwS7hOiBvP6SBD0UXCa767+VhHkaXrfX0MzUojYcN0Q= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -23,6 +25,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/pkg/stackdriver/formatter_test.go b/pkg/stackdriver/formatter_test.go index a76676f..a26fe52 100644 --- a/pkg/stackdriver/formatter_test.go +++ b/pkg/stackdriver/formatter_test.go @@ -1,3 +1,4 @@ +//go:build unit // +build unit package stackdriver @@ -6,12 +7,10 @@ import ( "bytes" "encoding/json" "errors" - "reflect" "testing" - "github.com/kr/pretty" - "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" ) func TestFormatter(t *testing.T) { @@ -32,9 +31,7 @@ func TestFormatter(t *testing.T) { var got map[string]interface{} json.Unmarshal(out.Bytes(), &got) - if !reflect.DeepEqual(got, tt.out) { - t.Errorf("unexpected output = %# v; want = %# v", pretty.Formatter(got), pretty.Formatter(tt.out)) - } + assert.Equal(t, tt.out, got) } } @@ -73,8 +70,8 @@ var formatterTests = []struct { }, "reportLocation": map[string]interface{}{ "filePath": "github.com/jenkins-x/logrus-stackdriver-formatter/pkg/stackdriver/formatter_test.go", - "lineNumber": 61.0, - "functionName": "glob..func2", + "lineNumber": 58.0, + "functionName": "init.func2", }, }, }, @@ -99,8 +96,8 @@ var formatterTests = []struct { }, "reportLocation": map[string]interface{}{ "filePath": "github.com/jenkins-x/logrus-stackdriver-formatter/pkg/stackdriver/formatter_test.go", - "lineNumber": 87.0, - "functionName": "glob..func3", + "lineNumber": 84.0, + "functionName": "init.func3", }, }, }, @@ -132,8 +129,8 @@ var formatterTests = []struct { }, "reportLocation": map[string]interface{}{ "filePath": "github.com/jenkins-x/logrus-stackdriver-formatter/pkg/stackdriver/formatter_test.go", - "lineNumber": 117.0, - "functionName": "glob..func4", + "lineNumber": 114.0, + "functionName": "init.func4", }, }, }, diff --git a/pkg/stackdriver/stackskip_test.go b/pkg/stackdriver/stackskip_test.go index ce1e15d..32db86f 100644 --- a/pkg/stackdriver/stackskip_test.go +++ b/pkg/stackdriver/stackskip_test.go @@ -1,26 +1,15 @@ -//go:build unit -// +build unit - package stackdriver import ( "bytes" "encoding/json" - "reflect" "testing" - "github.com/kr/pretty" + "github.com/TV4/logrus-stackdriver-formatter/test" "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" ) -type logWrapper struct { - Logger *logrus.Logger -} - -func (l *logWrapper) error(msg string) { - l.Logger.Error(msg) -} - func TestStackSkip(t *testing.T) { var out bytes.Buffer @@ -29,37 +18,33 @@ func TestStackSkip(t *testing.T) { logger.Formatter = NewFormatter( WithService("test"), WithVersion("0.1"), - WithStackSkip("github.com/jenkins-x/logrus-stackdriver-formatter/pkg/stackdriver"), + WithStackSkip("github.com/TV4/logrus-stackdriver-formatter/test"), ) - mylog := logWrapper{ + mylog := test.LogWrapper{ Logger: logger, } - mylog.error("my log entry") + mylog.Error("my log entry") var got map[string]interface{} - json.Unmarshal(out.Bytes(), &got) - got["timestamp"] = "2020-01-01T00:00:00.000000Z" + _ = json.Unmarshal(out.Bytes(), &got) want := map[string]interface{}{ - "severity": "ERROR", - "message": "my log entry", - "timestamp": "2020-01-01T00:00:00.000000Z", + "severity": "ERROR", + "message": "my log entry", "serviceContext": map[string]interface{}{ "service": "test", "version": "0.1", }, "context": map[string]interface{}{ "reportLocation": map[string]interface{}{ - "filePath": "testing/testing.go", - "lineNumber": float64(1446), - "functionName": "tRunner", + "filePath": "github.com/jenkins-x/logrus-stackdriver-formatter/pkg/stackdriver/stackskip_test.go", + "lineNumber": 28.0, + "functionName": "TestStackSkip", }, }, } - if !reflect.DeepEqual(got, want) { - t.Errorf("unexpected output = %# v; want = %# v", pretty.Formatter(got), pretty.Formatter(want)) - } + assert.Equal(t, want, got) }