-
Notifications
You must be signed in to change notification settings - Fork 28
/
test_leadbutt.py
180 lines (155 loc) · 6.04 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
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
# -*- 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)
@mock.patch('sys.stderr')
def test_config_handles_missing_file(self, mock_stderr):
with self.assertRaises(SystemExit) as e:
leadbutt.get_config('whatever_the_default_config_is')
self.assertEqual(e.exception.code, 2)
self.assertTrue(mock_stderr.write.called)
class get_optionsTest(unittest.TestCase):
def test_get_options_returns_right_option(self):
# only have the defaults
options = leadbutt.get_options(None, None, None)
self.assertEqual(options['Period'], 1)
self.assertEqual(options['Count'], 5)
# config options were specified
config_options = {
'Period': 2,
}
options = leadbutt.get_options(config_options, None, None)
self.assertEqual(options['Period'], 2)
self.assertEqual(options['Count'], 5)
# local_options were specified
local_options = {
'Period': 3,
}
options = leadbutt.get_options(config_options, local_options, None)
self.assertEqual(options['Period'], 3)
self.assertEqual(options['Count'], 5)
# cli_options were specified
cli_options = {
'Period': 4,
'Count': 10,
}
options = leadbutt.get_options(config_options, local_options, cli_options)
self.assertEqual(options['Period'], 4)
self.assertEqual(options['Count'], 10)
class output_resultsTest(unittest.TestCase):
@mock.patch('sys.stdout')
def test_default_formatter_used(self, mock_sysout):
mock_results = [{
'Timestamp': datetime.datetime.utcnow(),
'Unit': 'Count',
'Sum': 1337.0,
}]
metric = {
'Namespace': 'AWS/Foo',
'MetricName': 'RequestCount',
'Statistics': 'Sum',
'Unit': 'Count',
'Dimensions': {'Krang': 'X'},
}
options = leadbutt.get_options(None, metric.get('Options'), None)
leadbutt.output_results(mock_results, metric, options)
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(),
'Unit': 'Count',
'Sum': 1337.0,
}]
metric = {
'Namespace': 'AWS/Foo',
'MetricName': 'RequestCount',
'Statistics': 'Sum',
'Unit': 'Count',
'Dimensions': {'Krang': 'X'},
'Options': {'Formatter': 'tmnt.%(dimension)s'}
}
options = leadbutt.get_options(None, metric.get('Options'), None)
leadbutt.output_results(mock_results, metric, options)
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,
'Unit': 'Count',
}]
metric = {
'Namespace': 'AWS/Foo',
'MetricName': 'RequestCount',
'Statistics': ['Maximum', 'Average'],
'Unit': 'Count',
'Dimensions': {'Krang': 'X'},
}
options = leadbutt.get_options(None, metric.get('Options'), None)
leadbutt.output_results(mock_results, metric, options)
self.assertEqual(
mock_sysout.write.call_count, len(metric['Statistics']))
class leadbuttTest(unittest.TestCase):
@mock.patch('boto.ec2.cloudwatch.connect_to_region')
@mock.patch('leadbutt.get_config')
def test_can_get_auth_from_config(self, mock_get_config, mock_connect):
mock_get_config.return_value = {
'Metrics': [],
'Auth': {
'aws_access_key_id': 'foo',
'aws_secret_access_key': 'bar',
}
}
leadbutt.leadbutt('dummy_config_file', {'Count': 1, 'Period': 5})
self.assertTrue(mock_connect.called)
args, kwargs = mock_connect.call_args
self.assertEqual(kwargs['aws_access_key_id'], 'foo')
self.assertEqual(kwargs['aws_secret_access_key'], 'bar')
@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()