mirror of
https://github.com/gomods/athens
synced 2026-02-08 05:28:11 +00:00
Athens: add log tests to cloud formatters (#289)
* Athens: add log tests to cloud formatters * log: consistent variable mutation in tests * log: add testify instead of standard library
This commit is contained in:
@@ -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")
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user