Skip to content

Commit

Permalink
Athens: add log tests to cloud formatters (gomods#289)
Browse files Browse the repository at this point in the history
* Athens: add log tests to cloud formatters

* log: consistent variable mutation in tests

* log: add testify instead of standard library
  • Loading branch information
marwan-at-work authored Jul 17, 2018
1 parent eb0768f commit a033789
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package log

import (
"bytes"
"fmt"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/sirupsen/logrus"
)

type input struct {
name string
cloudProvider string
level string
fields logrus.Fields
logFunc func(e Entry)
output string
}

var testCases = []input{
{
name: "gcp_debug",
cloudProvider: "GCP",
level: "debug",
fields: logrus.Fields{},
logFunc: func(e Entry) { e.Infof("info message") },
output: `{"message":"info message","severity":"info","timestamp":"%v"}` + "\n",
},
{
name: "gcp_error",
cloudProvider: "GCP",
level: "debug",
fields: logrus.Fields{},
logFunc: func(e Entry) { e.Errorf("err message") },
output: `{"message":"err message","severity":"error","timestamp":"%v"}` + "\n",
},
{
name: "gcp_empty",
cloudProvider: "GCP",
level: "error",
fields: logrus.Fields{},
logFunc: func(e Entry) { e.Infof("info message") },
output: ``,
},
{
name: "gcp_fields",
cloudProvider: "GCP",
level: "debug",
fields: logrus.Fields{"field1": "value1", "field2": 2},
logFunc: func(e Entry) { e.Debugf("debug message") },
output: `{"field1":"value1","field2":2,"message":"debug message","severity":"debug","timestamp":"%v"}` + "\n",
},
{
name: "gcp_logs",
cloudProvider: "GCP",
level: "debug",
fields: logrus.Fields{},
logFunc: func(e Entry) { e.Warnf("warn message") },
output: `{"message":"warn message","severity":"warning","timestamp":"%v"}` + "\n",
},
{
name: "default",
cloudProvider: "default",
level: "debug",
fields: logrus.Fields{"xyz": "abc", "abc": "xyz"},
logFunc: func(e Entry) { e.Warnf("warn message") },
output: `{"abc":"xyz","level":"warning","msg":"warn message","time":"%v","xyz":"abc"}` + "\n",
},
}

func TestCloudLogger(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
lggr := New(tc.cloudProvider, tc.level)
var buf bytes.Buffer
lggr.Out = &buf
e := lggr.WithFields(tc.fields)
tc.logFunc(e)
out := buf.String()
expected := tc.output
if strings.Contains(expected, "%v") {
expected = fmt.Sprintf(expected, time.Now().Format(time.RFC3339))
}

require.Equal(t, expected, out, "expected the logged entry to match the testCase output")
})
}
}

0 comments on commit a033789

Please sign in to comment.