-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_leadbutt.py
116 lines (99 loc) · 3.64 KB
/
test_leadbutt.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
# -*- coding: UTF-8 -*-
"""
Tests for Cloudwatch to Graphite (leadbutt)
WISHLIST: supress chatty stderr and stdout in tests
"""
from __future__ import unicode_literals
from subprocess import call
import datetime
import os
import unittest
import mock
import leadbutt
class get_configTest(unittest.TestCase):
def test_example_config_loads(self):
config = leadbutt.get_config('config.yaml.example')
self.assertIn('metrics', config)
@mock.patch('sys.stdin')
def test_config_can_be_stdin(self, mock_stdin):
# simulate reading stdin
mock_stdin.read.side_effect = ['test: "123"\n', '']
# mock_stdin.name = 'oops'
config = leadbutt.get_config('-')
self.assertIn('test', config)
@mock.patch('sys.stderr')
@mock.patch('sys.stdin')
def test_config_handles_malformed_yaml(self, mock_stdin, mock_stderr):
mock_stdin.read.side_effect = ['-\nmalformed yaml', '']
mock_stdin.name = 'oops'
with self.assertRaises(SystemExit) as e:
leadbutt.get_config('-')
self.assertEqual(e.exception.code, 1)
self.assertTrue(mock_stderr.write.called)
class output_results(unittest.TestCase):
@mock.patch('sys.stdout')
def test_default_formatter_used(self, mock_sysout):
mock_results = [{
'Timestamp': datetime.datetime.utcnow(),
'Sum': 1337.0,
}]
metric = {
'Namespace': 'AWS/Foo',
'MetricName': 'RequestCount',
'Statistics': 'Sum',
'Unit': 'Count',
'Dimensions': {'Krang': 'X'},
}
leadbutt.output_results(mock_results, metric)
self.assertTrue(mock_sysout.write.called)
out = mock_sysout.write.call_args[0][0]
name, value, timestamp = out.split()
# assert default formatter was used
self.assertEqual(name, 'cloudwatch.aws.foo.x.requestcount.sum.count')
self.assertEqual(value, '1337.0')
@mock.patch('sys.stdout')
def test_custom_formatter_used(self, mock_sysout):
mock_results = [{
'Timestamp': datetime.datetime.utcnow(),
'Sum': 1337.0,
}]
metric = {
'Namespace': 'AWS/Foo',
'MetricName': 'RequestCount',
'Statistics': 'Sum',
'Unit': 'Count',
'Dimensions': {'Krang': 'X'},
'Options': {'Formatter': 'tmnt.%(dimension)s'}
}
leadbutt.output_results(mock_results, metric)
self.assertTrue(mock_sysout.write.called)
out = mock_sysout.write.call_args[0][0]
name, value, timestamp = out.split()
# assert custom formatter was used
self.assertEqual(name, 'tmnt.x')
self.assertEqual(value, '1337.0')
@mock.patch('sys.stdout')
def test_multiple_statistics_get_multiple_lines(self, mock_sysout):
mock_results = [{
'Timestamp': datetime.datetime.utcnow(),
'Maximum': 9001.0,
'Average': 1337.0,
}]
metric = {
'Namespace': 'AWS/Foo',
'MetricName': 'RequestCount',
'Statistics': ['Maximum', 'Average'],
'Unit': 'Count',
'Dimensions': {'Krang': 'X'},
}
leadbutt.output_results(mock_results, metric)
self.assertEqual(
mock_sysout.write.call_count, len(metric['Statistics']))
@unittest.skipUnless('TOX_TEST_ENTRYPOINT' in os.environ,
'This is only applicable if leadbutt is installed')
class mainTest(unittest.TestCase):
def test_entry_point(self):
# assert this does not raise an exception
call(['leadbutt', '--help'])
if __name__ == '__main__':
unittest.main()