1
"""Tests for assess_perf_test_simple module."""
3
from contextlib import contextmanager
4
from datetime import datetime
6
from mock import call, patch, Mock, mock_open
8
import pprof_collector as pc
9
from tests import TestCase
12
class TestActiveCollector(TestCase):
14
def test_installs_introspection_charm(self):
17
pc, 'install_introspection_charm', autospec=True) as iic:
18
pc.ActiveCollector(client, 'test')
19
iic.assert_called_once_with(client, 'test')
21
def test__collect_profile_uses_correct_profile_url(self):
23
expected_url = pc.get_profile_url(
24
'123.123.123.123', 'test', 'test_profile', '12')
26
pc, 'install_introspection_charm',
27
return_value='123.123.123.123',
29
with patch.object(pc, 'get_profile_reading', autospec=True) as gpr:
30
collector = pc.ActiveCollector(client, 'test')
31
collector._collect_profile('test_profile', '/tmp/test', '12')
32
gpr.assert_called_once_with(expected_url, '/tmp/test')
35
def mocked_collector(self, machine_id='test'):
36
"""Create an ActiveCollector with patched _collect_profile.
38
Used to test collect_* methods.
42
pc, 'install_introspection_charm',
43
return_value='123.123.123.123',
45
collector = pc.ActiveCollector(client, machine_id)
46
with patch.object(collector, '_collect_profile'):
49
def test_collect_profile(self):
50
with self.mocked_collector() as mc:
51
mc.collect_profile('/path/profile', 42)
52
mc._collect_profile.assert_called_once_with(
53
'profile', '/path/profile', 42)
55
def test_collect_heap(self):
56
with self.mocked_collector() as mc:
57
mc.collect_heap('/path/heap', 42)
58
mc._collect_profile.assert_called_once_with(
59
'heap', '/path/heap', 42)
61
def test_collect_goroutines(self):
62
with self.mocked_collector() as mc:
63
mc.collect_goroutines('/path/goroutines', 42)
64
mc._collect_profile.assert_called_once_with(
65
'goroutines', '/path/goroutines', 42)
68
class TestHelperFunctions(TestCase):
70
def test_get_profile_url(self):
71
url = pc.get_profile_url('1.1.1.1', 'test', 'test_profile', 123)
74
'http://1.1.1.1:19090/agents/machine-test/debug/pprof/test_profile'
78
def test_get_profile_reading(self):
80
open_patch = '{}.open'.format(pc.__name__)
81
mock_response = Mock()
82
mock_response.content = 'This is some content.'
83
with patch.object(pc, 'requests', autospect=True) as pr:
84
pr.get.return_value = mock_response
85
with patch(open_patch, m, create=True):
86
pc.get_profile_reading('test/url', '/tmp/testing/url')
87
m.assert_called_once_with('/tmp/testing/url', 'wb')
89
handle.write.assert_called_once_with('This is some content.')
91
def test_installs_introspection_charm(self):
93
with patch.object(pc, 'get_unit_ipaddress', autospec=True) as p_pui:
94
pc.install_introspection_charm(client, 'test')
96
client.deploy.assert_called_once_with(
97
'cs:~axwalk/juju-introspection', to='test')
98
client.wait_for_started.assert_called_once_with()
99
client.wait_for_workloads.assert_called_once_with()
100
client.juju.assert_called_once_with('expose', 'juju-introspection')
101
p_pui.assert_called_once_with(client, 'juju-introspection/0')
104
class TestPPROFCollector(TestCase):
106
def test_creates_profile_directories(self):
108
log_dir = '/test/logs/dir'
109
with patch.object(pc.os, 'makedirs', autospec=True) as p_md:
110
pc.PPROFCollector(client, ['test'], log_dir)
112
self.assertListEqual(
115
call(os.path.join(log_dir, 'cpu_profile')),
116
call(os.path.join(log_dir, 'heap_profile')),
117
call(os.path.join(log_dir, 'goroutines_profile'))
121
def test_defaults_to_non_active_collector(self):
123
log_dir = '/test/logs/dir'
124
with patch.object(pc.os, 'makedirs', autospec=True):
125
collector = pc.PPROFCollector(client, ['test'], log_dir)
127
self.assertIs(type(collector._collectors[0]), pc.NoopCollector)
128
self.assertListEqual(collector._active_collectors, [])
130
def test_creates_active_collector(self):
132
log_dir = '/test/logs/dir'
133
with patch.object(pc.os, 'makedirs', autospec=True):
135
pc, 'install_introspection_charm',
137
collector = pc.PPROFCollector(
138
client, ['test'], log_dir, active=True)
140
type(collector._collectors[0]),
142
self.assertListEqual(collector._noop_collectors, [])
144
def test_initiates_active_collector_after_creation(self):
146
log_dir = '/test/logs/dir'
147
with patch.object(pc.os, 'makedirs', autospec=True):
148
collector = pc.PPROFCollector(client, ['test'], log_dir)
149
self.assertIs(type(collector._collectors[0]), pc.NoopCollector)
151
pc, 'install_introspection_charm',
153
self.assertListEqual(collector._active_collectors, [])
154
collector.set_active()
156
type(collector._collectors[0]), pc.ActiveCollector)
158
def test_collect_profile(self):
160
log_dir = '/test/logs/dir'
161
file_name = 'machine-42-170130-092626.pprof'
162
profile_log = os.path.join(log_dir, 'cpu_profile', file_name)
163
with patch.object(pc.os, 'makedirs', autospec=True):
164
with patch.object(pc, 'NoopCollector', autospec=True) as m_nc:
166
mock_noop.machine_id = 42
167
m_nc.return_value = mock_noop
168
with patch.object(pc, 'datetime') as p_dt:
169
p_dt.utcnow.return_value = datetime(
170
2017, 1, 30, 9, 26, 26, 587930)
171
collector = pc.PPROFCollector(client, ['test'], log_dir)
172
collector.collect_profile()
173
collector._collectors[0].collect_profile.assert_called_once_with(
177
def test_collect_heap(selfm):
179
log_dir = '/test/logs/dir'
180
file_name = 'machine-42-170130-092626.pprof'
181
profile_log = os.path.join(log_dir, 'heap_profile', file_name)
182
with patch.object(pc.os, 'makedirs', autospec=True):
183
with patch.object(pc, 'NoopCollector', autospec=True) as m_nc:
185
mock_noop.machine_id = 42
186
m_nc.return_value = mock_noop
187
with patch.object(pc, 'datetime') as p_dt:
188
p_dt.utcnow.return_value = datetime(
189
2017, 1, 30, 9, 26, 26, 587930)
190
collector = pc.PPROFCollector(client, ['test'], log_dir)
191
collector.collect_heap()
192
collector._collectors[0].collect_heap.assert_called_once_with(
196
def test_collect_goroutines(selfm):
198
log_dir = '/test/logs/dir'
199
file_name = 'machine-42-170130-092626.pprof'
200
profile_log = os.path.join(log_dir, 'goroutines_profile', file_name)
201
with patch.object(pc.os, 'makedirs', autospec=True):
202
with patch.object(pc, 'NoopCollector', autospec=True) as m_nc:
204
mock_noop.machine_id = 42
205
m_nc.return_value = mock_noop
206
with patch.object(pc, 'datetime') as p_dt:
207
p_dt.utcnow.return_value = datetime(
208
2017, 1, 30, 9, 26, 26, 587930)
209
collector = pc.PPROFCollector(client, ['test'], log_dir)
210
collector.collect_goroutines()
211
collector._collectors[0].collect_goroutines.assert_called_once_with(