/
test_conf.py
315 lines (255 loc) · 10 KB
/
test_conf.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# Copyright 2012 Gregory Holt
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import ConfigParser
from StringIO import StringIO
from unittest import main, TestCase
from uuid import uuid4
from brim import conf
class TestConf(TestCase):
def test_true_values(self):
self.assertEquals(conf.TRUE_VALUES,
[v.lower() for v in conf.TRUE_VALUES])
def test_false_values(self):
self.assertEquals(conf.FALSE_VALUES,
[v.lower() for v in conf.FALSE_VALUES])
def test_true_false_values_distinct(self):
self.assertEquals(
set(), set(conf.TRUE_VALUES).intersection(set(conf.FALSE_VALUES)))
def test_direct_store(self):
d = {'s1': {'o1': '1.1', 'o2': '1.2'},
's2': {'o1': '2.1', 'o2': '2.2'}}
self.assertEquals(d, conf.Conf(d).store)
def test_files(self):
f = ['one.conf', 'two.conf']
self.assertEquals(f, conf.Conf({}, f).files)
def test_get(self):
self.assertEquals(
'1.1', conf.Conf({'s1': {'o1': '1.1'}}).get('s1', 'o1'))
def test_get_default(self):
self.assertEquals('d', conf.Conf({}).get('s1', 'o1', 'd'))
def test_get_default_orig_is_none(self):
self.assertEquals(
'd', conf.Conf({'s1': {'o1': None}}).get('s1', 'o1', 'd'))
def test_get_default_orig_is_empty(self):
self.assertEquals(
'd', conf.Conf({'s1': {'o1': ''}}).get('s1', 'o1', 'd'))
def test_get_default_orig_is_something(self):
self.assertEquals(
's', conf.Conf({'s1': {'o1': 's'}}).get('s1', 'o1', 'd'))
def test_get_bool(self):
self.assertTrue(
conf.Conf({'s1': {'o1': 'True'}}).get_bool('s1', 'o1', False))
self.assertFalse(
conf.Conf({'s1': {'o1': 'False'}}).get_bool('s1', 'o1', True))
def test_get_bool_default(self):
self.assertTrue(conf.Conf({}).get_bool('s1', 'o1', True))
self.assertFalse(conf.Conf({}).get_bool('s1', 'o1', False))
def test_get_bool_error(self):
calls = []
def _exit(v):
calls.append(v)
orig_exit = conf.exit
try:
conf.exit = _exit
conf.Conf({'s1': {'o1': 'z'}}).get_bool('s1', 'o1', True)
finally:
conf.exit = orig_exit
self.assertEquals(calls, ["Configuration value [s1] o1 of 'z' cannot "
"be converted to boolean."])
def test_get_int(self):
self.assertEquals(
1, conf.Conf({'s1': {'o1': '1'}}).get_int('s1', 'o1', -2))
self.assertEquals(
-2, conf.Conf({'s1': {'o1': '-2'}}).get_int('s1', 'o1', 1))
def test_get_int_default(self):
self.assertEquals(1, conf.Conf({}).get_int('s1', 'o1', 1))
def test_get_int_error(self):
calls = []
def _exit(v):
calls.append(v)
orig_exit = conf.exit
try:
conf.exit = _exit
conf.Conf({'s1': {'o1': 'z'}}).get_int('s1', 'o1', 1)
finally:
conf.exit = orig_exit
self.assertEquals(
calls,
["Configuration value [s1] o1 of 'z' cannot be converted to int."])
def test_get_float(self):
self.assertEquals(
1.1, conf.Conf({'s1': {'o1': '1.1'}}).get_float('s1', 'o1', -2.3))
self.assertEquals(
-2.3, conf.Conf({'s1': {'o1': '-2.3'}}).get_float('s1', 'o1', 1.1))
def test_get_float_default(self):
self.assertEquals(1.1, conf.Conf({}).get_float('s1', 'o1', 1.1))
def test_get_float_error(self):
calls = []
def _exit(v):
calls.append(v)
orig_exit = conf.exit
try:
conf.exit = _exit
conf.Conf({'s1': {'o1': 'z'}}).get_float('s1', 'o1', 1.1)
finally:
conf.exit = orig_exit
self.assertEquals(calls, ["Configuration value [s1] o1 of 'z' cannot "
"be converted to float."])
def test_str(self):
d = {'s1': {'o1': '1.1', 'o2': '1.2'},
's2': {'o1': '2.1', 'o2': '2.2'}}
self.assertEquals("Conf based on 'unknown files': " + str(d),
str(conf.Conf(d)))
f = ['one.conf', 'two.conf']
self.assertEquals('Conf based on ' + str(f) + ': ' + str(d),
str(conf.Conf(d, f)))
def test_repr(self):
d = {'s1': {'o1': '1.1', 'o2': '1.2'},
's2': {'o1': '2.1', 'o2': '2.2'}}
self.assertEquals("Conf based on 'unknown files': " + str(d),
repr(conf.Conf(d)))
f = ['one.conf', 'two.conf']
self.assertEquals('Conf based on ' + str(f) + ': ' + str(d),
repr(conf.Conf(d, f)))
def test_read_conf_calls_expanduser(self):
calls = []
def _expanduser(path):
calls.append(path)
return path
u1 = uuid4().hex
u2 = uuid4().hex
orig_expanduser = conf.expanduser
try:
conf.expanduser = _expanduser
conf.read_conf([u1, u2])
finally:
conf.expanduser = orig_expanduser
self.assertEquals(calls, [u1, u2])
def test_read_conf_exits_on_read_exception(self):
exc = None
calls = []
def _exit(v):
calls.append(v)
raise Exception('end test')
class _SafeConfigParser(object):
def read(self, files):
raise ConfigParser.Error('SafeConfigParser error')
orig_exit = conf.exit
orig_SafeConfigParser = conf.SafeConfigParser
try:
conf.exit = _exit
conf.SafeConfigParser = _SafeConfigParser
conf.read_conf(['test.conf'])
except Exception, err:
exc = err
finally:
conf.exit = orig_exit
conf.SafeConfigParser = orig_SafeConfigParser
self.assertEquals(['SafeConfigParser error'], [str(v) for v in calls])
self.assertEquals('end test', str(exc))
def test_read_conf_raises_read_exception_if_asked(self):
exc = None
class _SafeConfigParser(object):
def read(self, files):
raise ConfigParser.Error('SafeConfigParser error')
orig_SafeConfigParser = conf.SafeConfigParser
try:
conf.SafeConfigParser = _SafeConfigParser
conf.read_conf(['test.conf'], exit_on_read_exception=False)
except Exception, err:
exc = err
finally:
conf.SafeConfigParser = orig_SafeConfigParser
self.assertEquals('SafeConfigParser error', str(exc))
def test_read_conf(self):
class _SafeConfigParser(ConfigParser.SafeConfigParser):
def read(self, files):
ConfigParser.SafeConfigParser.readfp(self, StringIO('''
[DEFAULT]
default1 = 1
[section1]
option1 = 1.1
option2 = 1.2
[section2]
option1 = 2.1
option2 = 2.2
'''))
return files
orig_SafeConfigParser = conf.SafeConfigParser
try:
conf.SafeConfigParser = _SafeConfigParser
c = conf.read_conf(['test1.conf', 'test2.conf'],
exit_on_read_exception=False)
self.assertEquals(
c.store,
{'section1': {'default1': '1', 'option1': '1.1',
'option2': '1.2'},
'section2': {'default1': '1', 'option1': '2.1',
'option2': '2.2'}})
self.assertEquals(c.files, ['test1.conf', 'test2.conf'])
finally:
conf.SafeConfigParser = orig_SafeConfigParser
def test_read_conf_stops_after_50(self):
class _SafeConfigParser(ConfigParser.SafeConfigParser):
def read(self, files):
ConfigParser.SafeConfigParser.readfp(self, StringIO('''
[brim]
additional_confs = same_file
'''))
return files
exc = None
orig_SafeConfigParser = conf.SafeConfigParser
try:
conf.SafeConfigParser = _SafeConfigParser
conf.read_conf(['test.conf'], exit_on_read_exception=False)
except Exception, err:
exc = err
finally:
conf.SafeConfigParser = orig_SafeConfigParser
self.assertTrue(str(exc).startswith(
'Tried to read more than 50 conf files.\n'
'Recursion with [brim_conf] additional_confs?\n'
'Files read so far: '))
def test_read_conf_stops_after_50_with_exit(self):
exc = None
calls = []
def _exit(v):
calls.append(v)
raise Exception('end test')
class _SafeConfigParser(ConfigParser.SafeConfigParser):
def read(self, files):
ConfigParser.SafeConfigParser.readfp(self, StringIO('''
[brim]
additional_confs = same_file
'''))
return files
orig_exit = conf.exit
orig_SafeConfigParser = conf.SafeConfigParser
try:
conf.exit = _exit
conf.SafeConfigParser = _SafeConfigParser
conf.read_conf(['test.conf'])
except Exception, err:
exc = err
finally:
conf.exit = orig_exit
conf.SafeConfigParser = orig_SafeConfigParser
self.assertEquals(str(exc), 'end test')
self.assertEquals(len(calls), 1)
self.assertTrue(str(calls[0]).startswith(
'Tried to read more than 50 conf files.\n'
'Recursion with [brim_conf] additional_confs?\n'
'Files read so far: '))
if __name__ == '__main__':
main()