forked from reddit/monitors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.py
56 lines (47 loc) · 1.59 KB
/
testing.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
import tempfile
import wessex
import alerts
def stub(namespace, name):
'''Allows a function to override a value and restore it upon returning.'''
def decorator(f):
def wrapped(*args, **kwargs):
try:
orig_value = getattr(namespace, name)
has_orig_value = True
except AttributeError:
has_orig_value = False
try:
return f(*args, **kwargs)
finally:
if has_orig_value:
setattr(namespace, name, orig_value)
else:
try:
delattr(namespace, name)
except AttributeError:
pass
return wrapped
return decorator
class TestingHarold(wessex.Harold):
def __init__(self, *args, **kwargs):
super(TestingHarold, self).__init__(*args, **kwargs)
self.reset_for_testing()
def _post_to_harold(self, path, data):
self.post_log.append((path, data))
def reset_for_testing(self):
self.post_log = []
@stub(wessex, 'Harold')
def init_alerts(**sections):
wessex.Harold = TestingHarold
config = dict(
harold=dict(host='localhost', port=8888, secret='secret'),
)
config.update(sections)
with tempfile.NamedTemporaryFile() as f:
for section, data in config.iteritems():
f.write('[%s]\n' % section)
for name, value in data.iteritems():
f.write('%s = %s\n' % (name, value))
f.write('\n')
f.flush()
alerts.init(config_path=f.name)