~viswesn/juju-ci-tools/ensure_provider_cleanup

« back to all changes in this revision

Viewing changes to tests/test_pprof_collector.py

  • Committer: viswesn
  • Date: 2017-03-16 11:05:43 UTC
  • mfrom: (1823.1.112 trunk)
  • Revision ID: viswesn@gmail.com-20170316110543-ay89t3dcl1cknp5o
Merge to trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Tests for assess_perf_test_simple module."""
 
2
 
 
3
from contextlib import contextmanager
 
4
from datetime import datetime
 
5
import os
 
6
from mock import call, patch, Mock, mock_open
 
7
 
 
8
import pprof_collector as pc
 
9
from tests import TestCase
 
10
 
 
11
 
 
12
class TestActiveCollector(TestCase):
 
13
 
 
14
    def test_installs_introspection_charm(self):
 
15
        client = Mock()
 
16
        with patch.object(
 
17
                pc, 'install_introspection_charm', autospec=True) as iic:
 
18
            pc.ActiveCollector(client, 'test')
 
19
        iic.assert_called_once_with(client, 'test')
 
20
 
 
21
    def test__collect_profile_uses_correct_profile_url(self):
 
22
        client = Mock()
 
23
        expected_url = pc.get_profile_url(
 
24
            '123.123.123.123', 'test', 'test_profile', '12')
 
25
        with patch.object(
 
26
                pc, 'install_introspection_charm',
 
27
                return_value='123.123.123.123',
 
28
                autospec=True):
 
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')
 
33
 
 
34
    @contextmanager
 
35
    def mocked_collector(self, machine_id='test'):
 
36
        """Create an ActiveCollector with patched _collect_profile.
 
37
 
 
38
        Used to test collect_* methods.
 
39
        """
 
40
        client = Mock()
 
41
        with patch.object(
 
42
                pc, 'install_introspection_charm',
 
43
                return_value='123.123.123.123',
 
44
                autospec=True):
 
45
            collector = pc.ActiveCollector(client, machine_id)
 
46
            with patch.object(collector, '_collect_profile'):
 
47
                yield collector
 
48
 
 
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)
 
54
 
 
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)
 
60
 
 
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)
 
66
 
 
67
 
 
68
class TestHelperFunctions(TestCase):
 
69
 
 
70
    def test_get_profile_url(self):
 
71
        url = pc.get_profile_url('1.1.1.1', 'test', 'test_profile', 123)
 
72
        self.assertEqual(
 
73
            url,
 
74
            'http://1.1.1.1:19090/agents/machine-test/debug/pprof/test_profile'
 
75
            '?seconds=123'
 
76
        )
 
77
 
 
78
    def test_get_profile_reading(self):
 
79
        m = mock_open()
 
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')
 
88
                handle = m()
 
89
                handle.write.assert_called_once_with('This is some content.')
 
90
 
 
91
    def test_installs_introspection_charm(self):
 
92
        client = Mock()
 
93
        with patch.object(pc, 'get_unit_ipaddress', autospec=True) as p_pui:
 
94
            pc.install_introspection_charm(client, 'test')
 
95
 
 
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')
 
102
 
 
103
 
 
104
class TestPPROFCollector(TestCase):
 
105
 
 
106
    def test_creates_profile_directories(self):
 
107
        client = Mock()
 
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)
 
111
 
 
112
        self.assertListEqual(
 
113
            p_md.call_args_list,
 
114
            [
 
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'))
 
118
            ]
 
119
        )
 
120
 
 
121
    def test_defaults_to_non_active_collector(self):
 
122
        client = Mock()
 
123
        log_dir = '/test/logs/dir'
 
124
        with patch.object(pc.os, 'makedirs', autospec=True):
 
125
            collector = pc.PPROFCollector(client, ['test'], log_dir)
 
126
 
 
127
            self.assertIs(type(collector._collectors[0]), pc.NoopCollector)
 
128
            self.assertListEqual(collector._active_collectors, [])
 
129
 
 
130
    def test_creates_active_collector(self):
 
131
        client = Mock()
 
132
        log_dir = '/test/logs/dir'
 
133
        with patch.object(pc.os, 'makedirs', autospec=True):
 
134
            with patch.object(
 
135
                    pc, 'install_introspection_charm',
 
136
                    autospec=True):
 
137
                collector = pc.PPROFCollector(
 
138
                    client, ['test'], log_dir, active=True)
 
139
                self.assertIs(
 
140
                    type(collector._collectors[0]),
 
141
                    pc.ActiveCollector)
 
142
                self.assertListEqual(collector._noop_collectors, [])
 
143
 
 
144
    def test_initiates_active_collector_after_creation(self):
 
145
        client = Mock()
 
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)
 
150
            with patch.object(
 
151
                    pc, 'install_introspection_charm',
 
152
                    autospec=True):
 
153
                self.assertListEqual(collector._active_collectors, [])
 
154
                collector.set_active()
 
155
                self.assertIs(
 
156
                    type(collector._collectors[0]), pc.ActiveCollector)
 
157
 
 
158
    def test_collect_profile(self):
 
159
        client = Mock()
 
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:
 
165
                mock_noop = Mock()
 
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(
 
174
            profile_log, 5
 
175
        )
 
176
 
 
177
    def test_collect_heap(selfm):
 
178
        client = Mock()
 
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:
 
184
                mock_noop = Mock()
 
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(
 
193
            profile_log, 5
 
194
        )
 
195
 
 
196
    def test_collect_goroutines(selfm):
 
197
        client = Mock()
 
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:
 
203
                mock_noop = Mock()
 
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(
 
212
            profile_log, 5
 
213
        )