-
Notifications
You must be signed in to change notification settings - Fork 34
/
config_test.go
166 lines (159 loc) · 3.94 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package loggo
import gc "gopkg.in/check.v1"
type ConfigSuite struct{}
var _ = gc.Suite(&ConfigSuite{})
func (*ConfigSuite) TestParseConfigValue(c *gc.C) {
for i, test := range []struct {
value string
module string
level Level
err string
}{{
err: `config value expected '=', found ""`,
}, {
value: "WARNING",
err: `config value expected '=', found "WARNING"`,
}, {
value: "=WARNING",
err: `config value "=WARNING" has missing module name`,
}, {
value: " name = WARNING ",
module: "name",
level: WARNING,
}, {
value: "name = foo",
err: `unknown severity level "foo"`,
}, {
value: "name=DEBUG=INFO",
err: `unknown severity level "DEBUG=INFO"`,
}, {
value: "<root> = info",
module: "",
level: INFO,
}, {
value: "#tag = info",
module: "#tag",
level: INFO,
}, {
value: "#TAG = info",
module: "#tag",
level: INFO,
}, {
value: "#tag.1 = info",
err: `config tag should not contain '.', found "#tag.1"`,
}} {
c.Logf("%d: %s", i, test.value)
module, level, err := parseConfigValue(test.value)
if test.err == "" {
c.Check(err, gc.IsNil)
c.Check(module, gc.Equals, test.module)
c.Check(level, gc.Equals, test.level)
} else {
c.Check(module, gc.Equals, "")
c.Check(level, gc.Equals, UNSPECIFIED)
c.Check(err.Error(), gc.Equals, test.err)
}
}
}
func (*ConfigSuite) TestPaarseConfigurationString(c *gc.C) {
for i, test := range []struct {
configuration string
expected Config
err string
}{{
configuration: "",
// nil Config, no error
}, {
configuration: "INFO",
expected: Config{"": INFO},
}, {
configuration: "=INFO",
err: `config value "=INFO" has missing module name`,
}, {
configuration: "<root>=UNSPECIFIED",
expected: Config{"": UNSPECIFIED},
}, {
configuration: "<root>=DEBUG",
expected: Config{"": DEBUG},
}, {
configuration: "#label=DEBUG",
expected: Config{"#label": DEBUG},
}, {
configuration: "test.module=debug",
expected: Config{"test.module": DEBUG},
}, {
configuration: "module=info; sub.module=debug; other.module=warning",
expected: Config{
"module": INFO,
"sub.module": DEBUG,
"other.module": WARNING,
},
}, {
// colons not semicolons
configuration: "module=info: sub.module=debug: other.module=warning",
expected: Config{
"module": INFO,
"sub.module": DEBUG,
"other.module": WARNING,
},
}, {
configuration: " foo.bar \t\r\n= \t\r\nCRITICAL \t\r\n; \t\r\nfoo \r\t\n = DEBUG",
expected: Config{
"foo": DEBUG,
"foo.bar": CRITICAL,
},
}, {
configuration: "foo;bar",
err: `config value expected '=', found "foo"`,
}, {
configuration: "foo=",
err: `unknown severity level ""`,
}, {
configuration: "foo=unknown",
err: `unknown severity level "unknown"`,
}} {
c.Logf("%d: %q", i, test.configuration)
config, err := ParseConfigString(test.configuration)
if test.err == "" {
c.Check(err, gc.IsNil)
c.Check(config, gc.DeepEquals, test.expected)
} else {
c.Check(config, gc.IsNil)
c.Check(err.Error(), gc.Equals, test.err)
}
}
}
func (*ConfigSuite) TestConfigString(c *gc.C) {
for i, test := range []struct {
config Config
expected string
}{{
config: nil,
expected: "",
}, {
config: Config{"": INFO},
expected: "<root>=INFO",
}, {
config: Config{"": UNSPECIFIED},
expected: "<root>=UNSPECIFIED",
}, {
config: Config{"": DEBUG},
expected: "<root>=DEBUG",
}, {
config: Config{"test.module": DEBUG},
expected: "test.module=DEBUG",
}, {
config: Config{
"": WARNING,
"module": INFO,
"sub.module": DEBUG,
"other.module": WARNING,
},
expected: "<root>=WARNING;module=INFO;other.module=WARNING;sub.module=DEBUG",
}} {
c.Logf("%d: %q", i, test.expected)
c.Check(test.config.String(), gc.Equals, test.expected)
}
}