-
Notifications
You must be signed in to change notification settings - Fork 3
/
config_test.go
126 lines (122 loc) · 3.08 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package logging
import (
"encoding/json"
"reflect"
"testing"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
func TestUnmarshalJSON(t *testing.T) {
type expected struct {
wantErr bool
level logrus.Level
formatter logrus.Formatter
}
tests := [...]struct {
name string
jsonRaw []byte
expect expected
}{
{
"debug level json format",
[]byte(`{"level": "debug", "formatter":{"format": "json", "data": {"dataKey":"hodor"}}}`),
expected{false, logrus.DebugLevel, &logrus.JSONFormatter{}},
},
{
"warn level text format",
[]byte(`{"level": "warn", "formatter":{"format": "text", "data": null}}`),
expected{false, logrus.WarnLevel, &logrus.TextFormatter{}},
},
{
"warn level text format forceColor",
[]byte(`{"level": "warn", "formatter":{"format": "text", "data": {"forceColors": true}}}`),
expected{false, logrus.WarnLevel, &logrus.TextFormatter{ForceColors: true}},
},
{
"warn level default format",
[]byte(`{"level": "error"}`),
expected{false, logrus.ErrorLevel, &logrus.TextFormatter{}},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
config := Config{}
err := json.Unmarshal(test.jsonRaw, &config)
if !test.expect.wantErr && err != nil {
t.Fatalf("no error expected: %s", err)
}
if log.Level != test.expect.level {
t.Errorf("expected level \"%s\" got \"%s\"", test.expect.level, config.Level)
}
formatterType := reflect.TypeOf(log.Formatter).Elem()
expectedType := reflect.TypeOf(test.expect.formatter).Elem()
if formatterType.String() != expectedType.String() {
t.Errorf("expected formatter \"%s\" got \"%s\"", expectedType, formatterType)
}
})
}
}
func TestUnmarshalYAML(t *testing.T) {
type expected struct {
wantErr bool
level logrus.Level
formatter logrus.Formatter
}
tests := [...]struct {
name string
yamlRaw []byte
expect expected
}{
{
"debug level json format",
[]byte(`
level: debug
formatter:
format: json
data:
dataKey: 'hodor'
`),
expected{false, logrus.DebugLevel, &logrus.JSONFormatter{DataKey: "hodor"}},
},
{
"warn level text format",
[]byte(`
level: warn
formatter:
format: text
`),
expected{false, logrus.WarnLevel, &logrus.TextFormatter{}},
},
{
"warn level text format forceColor",
[]byte(`
level: warn
formatter:
format: text
data:
forceColors: true
`),
expected{false, logrus.WarnLevel, &logrus.TextFormatter{ForceColors: true}},
},
{
"warn level default format",
[]byte(`level: error`),
expected{false, logrus.ErrorLevel, &logrus.TextFormatter{}},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
c := Config{}
err := yaml.Unmarshal(test.yamlRaw, &c)
if !test.expect.wantErr && err != nil {
t.Fatalf("no error expected: %s", err)
}
if log.Level != test.expect.level {
t.Errorf("expected level \"%s\" got \"%s\"", test.expect.level, log.Level)
}
if !reflect.DeepEqual(log.Formatter, test.expect.formatter) {
t.Errorf("expected formatter \"%v\" got \"%v\"", test.expect.formatter, log.Formatter)
}
})
}
}