14
15
from fakejuju import fake_juju_client
15
16
import generate_perfscale_results as gpr
16
17
import perf_graphing
17
from test_perfscale_deployment import get_default_args
18
18
from test_quickstart_deploy import make_bootstrap_manager
19
19
from tests import TestCase
20
20
from utility import temp_dir
23
def get_default_args(**kwargs):
28
temp_env_name='an-env-mod',
41
default_args.update(kwargs)
43
return argparse.Namespace(**default_args)
46
class TestAddBasicPerfscaleArguments(TestCase):
48
def test_adds_perfscale_arguments(self):
49
parser = argparse.ArgumentParser()
50
gpr.add_basic_perfscale_arguments(parser)
51
parsed_args = parser.parse_args()
52
self.assertEqual(parsed_args.enable_ha, False)
54
parsed_args = parser.parse_args(['--enable-ha'])
55
self.assertEqual(parsed_args.enable_ha, True)
57
def test_includes_basic_default_arguments(self):
58
parser = argparse.ArgumentParser()
59
gpr.add_basic_perfscale_arguments(parser)
60
parsed_args = parser.parse_args([
66
self.assertEqual(parsed_args, get_default_args())
69
class TestMaybeEnableHA(TestCase):
71
def test_must_not_be_enabled_by_default(self):
72
parser = argparse.ArgumentParser()
73
gpr.add_basic_perfscale_arguments(parser)
74
args = parser.parse_args([])
77
gpr.maybe_enable_ha(client, args)
79
self.assertEqual(0, client.enable_ha.call_count)
80
self.assertEqual(0, client.wait_for_ha.call_count)
82
def test_must_be_enabled_when_requested(self):
83
parser = argparse.ArgumentParser()
84
gpr.add_basic_perfscale_arguments(parser)
85
args = parser.parse_args(['--enable-ha'])
88
gpr.maybe_enable_ha(client, args)
90
client.enable_ha.assert_called_once_with()
91
client.wait_for_ha.assert_called_once_with()
23
94
class TestRunPerfscaleTest(TestCase):
25
96
def test_calls_provided_test(self):
43
114
noop_test.assert_called_once_with(client, get_default_args())
117
class TestGetControllerMachines(TestCase):
119
def test_returns_machine_id_for_non_ha(self):
120
client = fake_juju_client()
122
self.assertListEqual(
124
gpr.get_controller_machines(client.get_controller_client()))
126
def test_returns_machine_id_for_ha_enabled(self):
127
client = fake_juju_client()
130
self.assertListEqual(
132
gpr.get_controller_machines(client.get_controller_client()))
135
class TestSetupSystemMonitoring(TestCase):
137
def test_setup_for_non_ha(self):
138
client = fake_juju_client()
140
admin_client = client.get_controller_client()
143
gpr, '_setup_system_monitoring', autospec=True) as m_ssm:
145
gpr, '_enable_monitoring', autospec=True) as m_em:
146
self.assertListEqual(
148
gpr.setup_system_monitoring(admin_client))
149
m_ssm.assert_called_once_with(admin_client, '0')
150
m_em.assert_called_once_with(admin_client, '0')
152
def test_setup_for_ha_enabled(self):
153
client = fake_juju_client()
156
admin_client = client.get_controller_client()
159
gpr, '_setup_system_monitoring', autospec=True) as m_ssm:
161
gpr, '_enable_monitoring', autospec=True) as m_em:
162
self.assertListEqual(
164
gpr.setup_system_monitoring(admin_client))
165
self.assertListEqual(
166
m_ssm.call_args_list,
168
call(admin_client, '0'),
169
call(admin_client, '1'),
170
call(admin_client, '2')])
171
self.assertListEqual(
174
call(admin_client, '0'),
175
call(admin_client, '1'),
176
call(admin_client, '2')])
178
def test__system_monitoring_installs_on_requested_machine(self):
179
admin_client = Mock()
180
static_setup_path = '/foo/bar/setup'
181
static_config_path = '/baz/bang/config'
184
call('scp', (static_config_path, '2:/tmp/collectd.config')),
185
call('scp', (static_setup_path, '2:/tmp/installer.sh')),
186
call('ssh', ('2', 'chmod +x /tmp/installer.sh')),
189
with patch.object(gpr, 'SetupPaths', autospec=True) as m_sp:
190
m_sp.installer_script_path = static_setup_path
191
m_sp.collectd_config_path = static_config_path
192
m_sp.collectd_config_dest_file = '/tmp/collectd.config'
193
m_sp.installer_script_dest_path = '/tmp/installer.sh'
194
gpr._setup_system_monitoring(admin_client, '2')
196
self.assertListEqual(
197
admin_client.juju.call_args_list,
200
def test__enable_monitoring_enables_script_on_controller_machine(self):
201
admin_client = Mock()
202
gpr._enable_monitoring(admin_client, '3')
207
'/tmp/installer.sh /tmp/collectd.config /tmp/runner.sh')),
209
'ssh', ('3', '--', 'daemon --respawn /tmp/runner.sh'))]
210
self.assertListEqual(
211
admin_client.juju.call_args_list,
46
216
class TestDumpPerformanceMetricsLogs(TestCase):
48
def test_pulls_rrd_and_mongostats_logs(self):
218
def test_pulls_rrd_and_mongostats_logs_with_single_controller(self):
50
220
res_dir = '/foo/performance_results/'
221
expected_dir = '{}machine-0'.format(res_dir)
52
224
with patch.object(gpr.os, 'makedirs', autospec=True) as m_makedirs:
55
gpr.dump_performance_metrics_logs('/foo', client))
56
m_makedirs.assert_called_once_with(res_dir)
227
gpr.dump_performance_metrics_logs('/foo', client, machine_ids))
228
m_makedirs.assert_called_once_with(expected_dir)
57
229
expected_calls = [
60
('--', '-r', '0:/var/lib/collectd/rrd/localhost/*', res_dir)),
233
'0:/var/lib/collectd/rrd/localhost/*', expected_dir)),
63
('0:/tmp/mongodb-stats.log', res_dir)
236
('0:/tmp/mongodb-stats.log', expected_dir)
66
239
client.juju.call_args_list,
242
def test_pulls_rrd_and_mongostats_logs_when_ha_enabled(self):
244
res_dir = '/foo/performance_results/'
245
machine_ids = ['0', '1', '2']
247
with patch.object(gpr.os, 'makedirs', autospec=True) as m_makedirs:
250
gpr.dump_performance_metrics_logs('/foo', client, machine_ids))
253
call('/foo/performance_results/machine-0'),
254
call('/foo/performance_results/machine-1'),
255
call('/foo/performance_results/machine-2')]
256
self.assertListEqual(
257
m_makedirs.call_args_list,
261
for m_id in machine_ids:
262
expected_dir = '{}machine-{}'.format(res_dir, m_id)
263
expected_calls.append(
267
'{}:/var/lib/collectd/rrd/localhost/*'.format(m_id),
269
expected_calls.append(
272
('{}:/tmp/mongodb-stats.log'.format(m_id), expected_dir)))
275
client.juju.call_args_list,
279
class TestGetControllerLogMessageChunks(TestCase):
281
def test_retains_backwards_compatiable_naming(self):
282
# Previous to the HA addtions the stored data was expected to have a
283
# single machines log under the name 'log_message_chunks'.
284
log_dir = '/foo/logs'
287
bootstrap='bootstrap', cleanup='cleanup', deploys='deploys')
290
gpr, 'breakdown_log_by_events_timeframe',
291
autospec=True) as m_blbet:
292
results = gpr._get_controller_log_message_chunks(
293
log_dir, machine_ids, deployments)
295
self.assertListEqual(
297
['log_message_chunks']
300
source_log = '/foo/logs/controller/machine-0/machine-0.log.gz'
301
m_blbet.assert_called_once_with(
302
source_log, 'bootstrap', 'cleanup', 'deploys')
304
def test_creates_log_chunks_for_each_controller(self):
305
# Must chunk logs for each controller.
306
log_dir = '/foo/logs'
307
machine_ids = ['0', '1', '2']
309
bootstrap='bootstrap', cleanup='cleanup', deploys='deploys')
312
gpr, 'breakdown_log_by_events_timeframe',
313
autospec=True) as m_blbet:
314
results = gpr._get_controller_log_message_chunks(
315
log_dir, machine_ids, deployments)
317
self.assertListEqual(
318
sorted(results.keys()),
319
['log_message_chunks',
320
'log_message_chunks_1',
321
'log_message_chunks_2']
325
call('/foo/logs/controller/machine-0/machine-0.log.gz',
326
'bootstrap', 'cleanup', 'deploys'),
327
call('/foo/logs/controller/machine-1/machine-1.log.gz',
328
'bootstrap', 'cleanup', 'deploys'),
329
call('/foo/logs/controller/machine-2/machine-2.log.gz',
330
'bootstrap', 'cleanup', 'deploys')]
331
self.assertListEqual(
332
m_blbet.call_args_list,
70
336
class TestJsonSerialisation(TestCase):