traefik/pkg/middlewares/accesslog/logger_formatters_test.go

164 lines
4.2 KiB
Go
Raw Normal View History

2017-09-07 08:54:03 +00:00
package accesslog
import (
"net/http"
"os"
2017-09-07 08:54:03 +00:00
"testing"
"time"
2018-01-22 11:16:03 +00:00
"github.com/sirupsen/logrus"
2017-09-07 08:54:03 +00:00
"github.com/stretchr/testify/assert"
)
func TestCommonLogFormatter_Format(t *testing.T) {
clf := CommonLogFormatter{}
testCases := []struct {
name string
data map[string]interface{}
expectedLog string
}{
{
name: "OriginStatus & OriginContentSize are nil",
data: map[string]interface{}{
2018-03-14 13:12:04 +00:00
StartUTC: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
Duration: 123 * time.Second,
ClientHost: "10.0.0.1",
ClientUsername: "Client",
RequestMethod: http.MethodGet,
RequestPath: "/foo",
RequestProtocol: "http",
OriginStatus: nil,
OriginContentSize: nil,
RequestRefererHeader: "",
RequestUserAgentHeader: "",
RequestCount: 0,
2018-11-14 09:18:03 +00:00
RouterName: "",
ServiceURL: "",
2017-09-07 08:54:03 +00:00
},
2018-01-11 09:04:03 +00:00
expectedLog: `10.0.0.1 - Client [10/Nov/2009:23:00:00 +0000] "GET /foo http" - - "-" "-" 0 - - 123000ms
2017-09-07 08:54:03 +00:00
`,
},
{
name: "all data",
data: map[string]interface{}{
2018-03-14 13:12:04 +00:00
StartUTC: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
Duration: 123 * time.Second,
ClientHost: "10.0.0.1",
ClientUsername: "Client",
RequestMethod: http.MethodGet,
RequestPath: "/foo",
RequestProtocol: "http",
OriginStatus: 123,
OriginContentSize: 132,
RequestRefererHeader: "referer",
RequestUserAgentHeader: "agent",
RequestCount: nil,
2018-11-14 09:18:03 +00:00
RouterName: "foo",
ServiceURL: "http://10.0.0.2/toto",
2017-09-07 08:54:03 +00:00
},
expectedLog: `10.0.0.1 - Client [10/Nov/2009:23:00:00 +0000] "GET /foo http" 123 132 "referer" "agent" - "foo" "http://10.0.0.2/toto" 123000ms
`,
},
{
name: "all data with local time",
data: map[string]interface{}{
StartLocal: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
Duration: 123 * time.Second,
ClientHost: "10.0.0.1",
ClientUsername: "Client",
RequestMethod: http.MethodGet,
RequestPath: "/foo",
RequestProtocol: "http",
OriginStatus: 123,
OriginContentSize: 132,
RequestRefererHeader: "referer",
RequestUserAgentHeader: "agent",
RequestCount: nil,
RouterName: "foo",
ServiceURL: "http://10.0.0.2/toto",
},
expectedLog: `10.0.0.1 - Client [10/Nov/2009:14:00:00 -0900] "GET /foo http" 123 132 "referer" "agent" - "foo" "http://10.0.0.2/toto" 123000ms
2017-09-07 08:54:03 +00:00
`,
},
}
// Set timezone to Alaska to have a constant behavior
os.Setenv("TZ", "US/Alaska")
2017-09-07 08:54:03 +00:00
for _, test := range testCases {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
entry := &logrus.Entry{Data: test.data}
raw, err := clf.Format(entry)
assert.NoError(t, err)
assert.Equal(t, test.expectedLog, string(raw))
})
}
}
func Test_toLog(t *testing.T) {
testCases := []struct {
2018-03-14 13:12:04 +00:00
desc string
fields logrus.Fields
fieldName string
defaultValue string
quoted bool
expectedLog interface{}
2017-09-07 08:54:03 +00:00
}{
{
2018-03-14 13:12:04 +00:00
desc: "Should return int 1",
fields: logrus.Fields{
"Powpow": 1,
},
fieldName: "Powpow",
defaultValue: defaultValue,
quoted: false,
expectedLog: 1,
2017-09-07 08:54:03 +00:00
},
{
2018-03-14 13:12:04 +00:00
desc: "Should return string foo",
fields: logrus.Fields{
"Powpow": "foo",
},
fieldName: "Powpow",
defaultValue: defaultValue,
quoted: true,
expectedLog: `"foo"`,
2017-09-07 08:54:03 +00:00
},
{
2018-03-14 13:12:04 +00:00
desc: "Should return defaultValue if fieldName does not exist",
fields: logrus.Fields{
"Powpow": "foo",
},
fieldName: "",
defaultValue: defaultValue,
quoted: false,
expectedLog: "-",
},
{
desc: "Should return defaultValue if fields is nil",
fields: nil,
fieldName: "",
defaultValue: defaultValue,
quoted: false,
expectedLog: "-",
2017-09-07 08:54:03 +00:00
},
}
for _, test := range testCases {
test := test
2018-03-14 13:12:04 +00:00
t.Run(test.desc, func(t *testing.T) {
2017-09-07 08:54:03 +00:00
t.Parallel()
2018-03-14 13:12:04 +00:00
lg := toLog(test.fields, test.fieldName, defaultValue, test.quoted)
2017-09-07 08:54:03 +00:00
assert.Equal(t, test.expectedLog, lg)
})
}
}