-
Notifications
You must be signed in to change notification settings - Fork 490
/
test_conf.py
432 lines (386 loc) · 15.6 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import os
from tempfile import mkdtemp, mkstemp
from shutil import rmtree
from os import makedirs
from os.path import dirname, join
from unittest import TestCase
from carbon.conf import get_default_parser, parse_options, read_config
from carbon.exceptions import CarbonConfigException
class FakeParser(object):
def __init__(self):
self.called = []
def parse_args(self, args):
return object(), args
def print_usage(self):
self.called.append("print_usage")
class FakeOptions(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __getitem__(self, name):
return self.__dict__[name]
def __setitem__(self, name, value):
self.__dict__[name] = value
class DefaultParserTest(TestCase):
def test_default_parser(self):
"""Check default parser settings."""
parser = get_default_parser()
self.assertTrue(parser.has_option("--debug"))
self.assertEqual(None, parser.defaults["debug"])
self.assertTrue(parser.has_option("--profile"))
self.assertEqual(None, parser.defaults["profile"])
self.assertTrue(parser.has_option("--pidfile"))
self.assertEqual(None, parser.defaults["pidfile"])
self.assertTrue(parser.has_option("--config"))
self.assertEqual(None, parser.defaults["config"])
self.assertTrue(parser.has_option("--logdir"))
self.assertEqual(None, parser.defaults["logdir"])
self.assertTrue(parser.has_option("--instance"))
self.assertEqual("a", parser.defaults["instance"])
class ParseOptionsTest(TestCase):
def test_no_args_prints_usage_and_exit(self):
"""
If no arguments are provided, the usage help will be printed and a
SystemExit exception will be raised.
"""
parser = FakeParser()
self.assertRaises(SystemExit, parse_options, parser, ())
self.assertEqual(["print_usage"], parser.called)
def test_no_valid_args_prints_usage_and_exit(self):
"""
If an argument which isn't a valid command was provided, 'print_usage'
will be called and a SystemExit exception will be raised.
"""
parser = FakeParser()
self.assertRaises(SystemExit, parse_options, parser, ("bazinga!",))
self.assertEqual(["print_usage"], parser.called)
def test_valid_args(self):
"""
If a valid argument is provided, it will be returned along with
options.
"""
parser = FakeParser()
options, args = parser.parse_args(("start",))
self.assertEqual(("start",), args)
class ReadConfigTest(TestCase):
def makeFile(self, content=None, basename=None, dirname=None):
"""
Create a temporary file with content
Deletes the file after tests
"""
if basename is not None:
path = join(dirname, basename)
else:
fd, path = mkstemp(dir=dirname)
os.close(fd)
self.addCleanup(os.unlink, path)
if content is not None:
with open(path, "w") as f:
f.write(content)
return path
def test_root_dir_is_required(self):
"""
At minimum, the caller must provide a 'ROOT_DIR' setting.
"""
try:
read_config("carbon-foo", FakeOptions(config=None))
except CarbonConfigException as e:
self.assertEqual("Either ROOT_DIR or GRAPHITE_ROOT "
"needs to be provided.", str(e))
else:
self.fail("Did not raise exception.")
def test_config_is_not_required(self):
"""
If the '--config' option is not provided, it defaults to
ROOT_DIR/conf/carbon.conf.
"""
root_dir = mkdtemp()
self.addCleanup(rmtree, root_dir)
conf_dir = join(root_dir, "conf")
makedirs(conf_dir)
self.makeFile(content="[foo]",
basename="carbon.conf",
dirname=conf_dir)
options = FakeOptions(config=None, instance=None,
pidfile=None, logdir=None)
read_config("carbon-foo", options, ROOT_DIR=root_dir)
self.assertEqual(join(root_dir, "conf", "carbon.conf"),
options["config"])
def test_config_dir_from_environment(self):
"""
If the 'GRAPHITE_CONFIG_DIR' variable is set in the environment, then
'CONFIG_DIR' will be set to that directory.
"""
root_dir = mkdtemp()
self.addCleanup(rmtree, root_dir)
conf_dir = join(root_dir, "configs", "production")
makedirs(conf_dir)
self.makeFile(content="[foo]",
basename="carbon.conf",
dirname=conf_dir)
orig_value = os.environ.get("GRAPHITE_CONF_DIR", None)
if orig_value is not None:
self.addCleanup(os.environ.__setitem__,
"GRAPHITE_CONF_DIR",
orig_value)
else:
self.addCleanup(os.environ.__delitem__, "GRAPHITE_CONF_DIR")
os.environ["GRAPHITE_CONF_DIR"] = conf_dir
settings = read_config("carbon-foo",
FakeOptions(config=None, instance=None,
pidfile=None, logdir=None),
ROOT_DIR=root_dir)
self.assertEqual(conf_dir, settings.CONF_DIR)
def test_conf_dir_defaults_to_config_dirname(self):
"""
The 'CONF_DIR' setting defaults to the parent directory of the
provided configuration file.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual(dirname(config), settings.CONF_DIR)
def test_storage_dir_relative_to_root_dir(self):
"""
The 'STORAGE_DIR' setting defaults to the 'storage' directory relative
to the 'ROOT_DIR' setting.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual(join("foo", "storage"), settings.STORAGE_DIR)
def test_log_dir_relative_to_storage_dir(self):
"""
The 'LOG_DIR' setting defaults to a program-specific directory relative
to the 'STORAGE_DIR' setting.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual(join("foo", "storage", "log", "carbon-foo"),
settings.LOG_DIR)
def test_log_dir_relative_to_provided_storage_dir(self):
"""
Providing a different 'STORAGE_DIR' in defaults overrides the default
of being relative to 'ROOT_DIR'.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo", STORAGE_DIR="bar")
self.assertEqual(join("bar", "log", "carbon-foo"),
settings.LOG_DIR)
def test_log_dir_for_instance_relative_to_storage_dir(self):
"""
The 'LOG_DIR' setting defaults to a program-specific directory relative
to the 'STORAGE_DIR' setting. In the case of an instance, the instance
name is appended to the directory.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance="x",
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual(join("foo", "storage", "log",
"carbon-foo", "carbon-foo-x"),
settings.LOG_DIR)
def test_log_dir_for_instance_relative_to_provided_storage_dir(self):
"""
Providing a different 'STORAGE_DIR' in defaults overrides the default
of being relative to 'ROOT_DIR'. In the case of an instance, the
instance name is appended to the directory.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance="x",
pidfile=None, logdir=None),
ROOT_DIR="foo", STORAGE_DIR="bar")
self.assertEqual(join("bar", "log", "carbon-foo", "carbon-foo-x"),
settings.LOG_DIR)
def test_pidfile_relative_to_storage_dir(self):
"""
The 'pidfile' setting defaults to a program-specific filename relative
to the 'STORAGE_DIR' setting.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual(join("foo", "storage", "carbon-foo.pid"),
settings.pidfile)
def test_pidfile_in_options_has_precedence(self):
"""
The 'pidfile' option from command line overrides the default setting.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile="foo.pid", logdir=None),
ROOT_DIR="foo")
self.assertEqual("foo.pid", settings.pidfile)
def test_pidfile_for_instance_in_options_has_precedence(self):
"""
The 'pidfile' option from command line overrides the default setting
for the instance, if one is specified.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance="x",
pidfile="foo.pid", logdir=None),
ROOT_DIR="foo")
self.assertEqual("foo.pid", settings.pidfile)
def test_storage_dir_as_provided(self):
"""
Providing a 'STORAGE_DIR' in defaults overrides the root-relative
default.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo", STORAGE_DIR="bar")
self.assertEqual("bar", settings.STORAGE_DIR)
def test_log_dir_as_provided(self):
"""
Providing a 'LOG_DIR' in defaults overrides the storage-relative
default.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo", STORAGE_DIR="bar", LOG_DIR='baz')
self.assertEqual("baz", settings.LOG_DIR)
def test_log_dir_from_options(self):
"""
Providing a 'LOG_DIR' in the command line overrides the
storage-relative default.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir="baz"),
ROOT_DIR="foo")
self.assertEqual("baz", settings.LOG_DIR)
def test_log_dir_for_instance_from_options(self):
"""
Providing a 'LOG_DIR' in the command line overrides the
storage-relative default for the instance.
"""
config = self.makeFile(content="[foo]")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance="x",
pidfile=None, logdir="baz"),
ROOT_DIR="foo")
self.assertEqual("baz", settings.LOG_DIR)
def test_storage_dir_from_config(self):
"""
Providing a 'STORAGE_DIR' in the configuration file overrides the
root-relative default.
"""
config = self.makeFile(content="[foo]\nSTORAGE_DIR = bar")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual("bar", settings.STORAGE_DIR)
def test_log_dir_from_config(self):
"""
Providing a 'LOG_DIR' in the configuration file overrides the
storage-relative default.
"""
config = self.makeFile(content="[foo]\nLOG_DIR = baz")
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual("baz", settings.LOG_DIR)
def test_log_dir_from_instance_config(self):
"""
Providing a 'LOG_DIR' for the specific instance in the configuration
file overrides the storage-relative default. The actual value will have
the instance name appended to it and ends with a forward slash.
"""
config = self.makeFile(
content=("[foo]\nLOG_DIR = baz\n"
"[foo:x]\nLOG_DIR = boo"))
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance="x",
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual("boo/carbon-foo-x", settings.LOG_DIR)
def test_pid_dir_depends_on_storage_dir(self):
"""
Tests 'STORAGE_DIR' dependency 'PID_DIR'
"""
config = self.makeFile(
content=("[foo]\n"
"STORAGE_DIR = bar"))
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual("bar", settings.PID_DIR)
def test_log_dir_depends_on_storage_dir(self):
"""
Tests 'STORAGE_DIR' dependency 'LOG_DIR'
"""
config = self.makeFile(
content=("[foo]\n"
"STORAGE_DIR = bar"))
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual(join("bar", "log", "carbon-foo"), settings.LOG_DIR)
def test_local_data_dir_depends_on_storage_dir(self):
"""
Tests 'STORAGE_DIR' dependency 'LOCAL_DATA_DIR'
"""
config = self.makeFile(
content=("[foo]\n"
"STORAGE_DIR = bar"))
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual(join("bar", "whisper"), settings.LOCAL_DATA_DIR)
def test_whitelists_dir_depends_on_storage_dir(self):
"""
Tests 'STORAGE_DIR' dependency 'WHITELISTS_DIR'
"""
config = self.makeFile(
content=("[foo]\n"
"STORAGE_DIR = bar"))
settings = read_config(
"carbon-foo",
FakeOptions(config=config, instance=None,
pidfile=None, logdir=None),
ROOT_DIR="foo")
self.assertEqual(join("bar", "lists"), settings.WHITELISTS_DIR)