102
('show-status', '--format', 'yaml'): status,
105
('juju', '--show-log', 'status', '-e', 'foo'): status,
104
107
return output[args]
105
client = EnvJujuClient(JujuData('foo', {}), '1.25.0', '/foo/juju')
106
with patch.object(client, 'get_juju_output', side_effect=output,
107
autospec=True) as gjo_mock:
108
client = EnvJujuClient(SimpleEnvironment('foo', {}), None, '/foo/juju')
109
with patch('subprocess.check_output', side_effect=output,
110
autospec=True) as co_mock:
108
111
with patch('subprocess.check_call', autospec=True) as cc_mock:
109
112
monkey_runner = MonkeyRunner('foo', client, service='ser1')
110
with patch('jujupy.GroupReporter._write', autospec=True):
113
with patch('sys.stdout', autospec=True):
111
114
monkey_runner.deploy_chaos_monkey()
116
('juju', '--show-log', 'deploy',
117
'-m', 'foo:foo', 'local:chaos-monkey'),
115
assert_juju_call(self, cc_mock, client, (
116
'juju', '--show-log', 'deploy', '-e', 'foo', 'local:chaos-monkey'),
119
118
assert_juju_call(self, cc_mock, client, (
120
'juju', '--show-log', 'add-relation', '-m', 'foo:foo', 'ser1',
119
'juju', '--show-log', 'add-relation', '-e', 'foo', 'ser1',
121
120
'chaos-monkey'), 1)
122
121
self.assertEqual(cc_mock.call_count, 2)
123
self.assertEqual(gjo_mock.call_count, 2)
122
self.assertEqual(co_mock.call_count, 2)
125
124
def test_iter_chaos_monkey_units(self):
126
def output(*args, **kwargs):
125
def output(args, **kwargs):
127
126
status = yaml.safe_dump({
129
128
'0': {'agent-state': 'started'}
203
('config', 'chaos-monkey'): charm_config,
201
('juju', '--show-log', 'status', '-e', 'foo'): status,
202
('juju', '--show-log', 'get', '-e', 'foo', 'chaos-monkey'
205
205
return output[args]
206
client = EnvJujuClient(JujuData('foo', {}), None, '/foo')
206
client = EnvJujuClient(SimpleEnvironment('foo', {}), None, '/foo')
207
207
monkey_runner = MonkeyRunner('foo', client, service='jenkins')
208
208
monkey_runner.monkey_ids = {
209
209
'chaos-monkey/0': 'workspace0',
210
210
'chaos-monkey/1': 'workspace1'
212
with patch.object(client, 'get_juju_output', side_effect=output,
212
with patch('subprocess.check_output', side_effect=output,
214
214
with patch('subprocess.call', autospec=True,
215
215
return_value=0) as call_mock:
216
216
for unit_name in ['chaos-monkey/1', 'chaos-monkey/0']:
217
self.assertEqual(monkey_runner.get_unit_status(unit_name),
217
with patch('sys.stdout', autospec=True):
219
monkey_runner.get_unit_status(unit_name),
219
221
self.assertEqual(call_mock.call_count, 2)
220
with patch.object(client, 'get_juju_output', side_effect=output,
222
with patch('subprocess.check_output', side_effect=output,
222
224
with patch('subprocess.call', autospec=True,
223
225
return_value=1) as call_mock:
224
226
for unit_name in ['chaos-monkey/1', 'chaos-monkey/0']:
225
self.assertEqual(monkey_runner.get_unit_status(unit_name),
227
with patch('sys.stdout', autospec=True):
229
monkey_runner.get_unit_status(unit_name),
227
231
self.assertEqual(call_mock.call_count, 2)
230
class TestUnleashOnce(FakeHomeTestCase):
234
class TestUnleashOnce(TestCase):
232
236
def test_unleash_once(self):
233
def output(*args, **kwargs):
237
def output(args, **kwargs):
234
238
status = yaml.safe_dump({
236
240
'0': {'agent-state': 'started'}
269
('show-status', '--format', 'yaml'): status,
270
('get', 'jenkins'): charm_config,
271
('run-action', 'chaos-monkey/0', 'start', 'mode=single',
272
'enablement-timeout=120'
273
): ('Action queued with id: '
274
'abcdabcdabcdabcdabcdabcdabcdabcdabcd'),
275
('run-action', 'chaos-monkey/0', 'start', 'mode=single',
276
'enablement-timeout=120',
277
'monkey-id=abcdabcdabcdabcdabcdabcdabcdabcdabcd'
278
): ('Action queued with id: '
279
'efabefabefabefabefabefabefabefabefab'),
280
('run-action', 'chaos-monkey/1', 'start', 'mode=single',
281
'enablement-timeout=120'
282
): ('Action queued with id: '
283
'123412341234123412341234123412341234'),
284
('run-action', 'chaos-monkey/1', 'start', 'mode=single',
285
'enablement-timeout=120',
286
'monkey-id=123412341234123412341234123412341234'
287
): ('Action queued with id: '
288
'567856785678567856785678567856785678'),
273
('juju', '--show-log', 'status', '-e', 'foo'): status,
274
('juju', '--show-log', 'get', '-e', 'foo', 'jenkins'
276
('juju', '--show-log', 'action', 'do', '-e', 'foo',
277
'chaos-monkey/0', 'start', 'mode=single',
278
'enablement-timeout=120'
279
): 'Action queued with id: abcd',
280
('juju', '--show-log', 'action', 'do', '-e', 'foo',
281
'chaos-monkey/0', 'start', 'mode=single',
282
'enablement-timeout=120', 'monkey-id=abcd'
283
): 'Action queued with id: efgh',
284
('juju', '--show-log', 'action', 'do', '-e', 'foo',
285
'chaos-monkey/1', 'start', 'mode=single',
286
'enablement-timeout=120'
287
): 'Action queued with id: 1234',
288
('juju', '--show-log', 'action', 'do', '-e', 'foo',
289
'chaos-monkey/1', 'start', 'mode=single',
290
'enablement-timeout=120', 'monkey-id=1234'
291
): 'Action queued with id: 5678',
290
293
return output[args]
292
def output2(*args, **kwargs):
295
def output2(args, **kwargs):
293
296
status = yaml.safe_dump({
295
298
'0': {'agent-state': 'started'}
310
('show-status', '--format', 'yaml'): status,
311
('run-action', 'chaos-monkey/1', 'start', 'mode=single',
312
'enablement-timeout=120',
313
'monkey-id=123412341234123412341234123412341234'
314
): ('Action queued with id: '
315
'abcdabcdabcdabcdabcdabcdabcdabcdabcd'),
313
('juju', '--show-log', 'status', '-e', 'foo'): status,
314
('juju', '--show-log', 'action', 'do', '-e', 'foo',
315
'chaos-monkey/1', 'start', 'mode=single',
316
'enablement-timeout=120', 'monkey-id=1234'
317
): 'Action queued with id: abcd',
317
319
return output[args]
318
client = EnvJujuClient(JujuData('foo', {}), None, '/foo/juju')
320
client = EnvJujuClient(SimpleEnvironment('foo', {}), None, '/foo/juju')
319
321
monkey_runner = MonkeyRunner('foo', client, service='jenkins')
320
with patch.object(client, 'get_juju_output', side_effect=output,
321
autospec=True) as gjo_mock:
322
with patch('subprocess.check_output', side_effect=output,
323
autospec=True) as co_mock:
322
324
returned = monkey_runner.unleash_once()
323
expected = ['abcd' * 9, '1234' * 9]
326
call('show-status', '--format', 'yaml', controller=False),
327
call('run-action', 'chaos-monkey/1', 'start', 'mode=single',
328
'enablement-timeout=120'),
329
call('run-action', 'chaos-monkey/0', 'start', 'mode=single',
330
'enablement-timeout=120'),
332
gjo_mock.call_args_list)
325
expected = ['abcd', '1234']
326
assert_juju_call(self, co_mock, client, (
327
'juju', '--show-log', 'action', 'do', '-e', 'foo',
328
'chaos-monkey/1', 'start', 'mode=single',
329
'enablement-timeout=120'), 1, True)
330
assert_juju_call(self, co_mock, client, (
331
'juju', '--show-log', 'action', 'do', '-e', 'foo',
332
'chaos-monkey/0', 'start', 'mode=single',
333
'enablement-timeout=120'), 2, True)
333
334
self.assertEqual(['chaos-monkey/1', 'chaos-monkey/0'],
334
335
monkey_runner.monkey_ids.keys())
335
336
self.assertEqual(len(monkey_runner.monkey_ids), 2)
337
self.assertEqual(co_mock.call_count, 3)
336
338
self.assertItemsEqual(returned, expected)
337
with patch.object(client, 'get_juju_output', side_effect=output,
338
autospec=True) as gjo_mock:
339
with patch('subprocess.check_output', side_effect=output,
340
autospec=True) as co_mock:
339
341
monkey_runner.unleash_once()
342
call('show-status', '--format', 'yaml', controller=False),
344
'chaos-monkey/1', 'start', 'mode=single',
345
'enablement-timeout=120',
346
'monkey-id=123412341234123412341234123412341234'),
348
'chaos-monkey/0', 'start', 'mode=single',
349
'enablement-timeout=120',
350
'monkey-id=abcdabcdabcdabcdabcdabcdabcdabcdabcd'),
352
gjo_mock.call_args_list)
342
assert_juju_call(self, co_mock, client, (
343
'juju', '--show-log', 'action', 'do', '-e', 'foo',
344
'chaos-monkey/1', 'start', 'mode=single', 'enablement-timeout=120',
345
'monkey-id=1234'), 1, True)
346
assert_juju_call(self, co_mock, client, (
347
'juju', '--show-log', 'action', 'do', '-e', 'foo',
348
'chaos-monkey/0', 'start', 'mode=single', 'enablement-timeout=120',
349
'monkey-id=abcd'), 2, True)
353
350
self.assertTrue('1234', monkey_runner.monkey_ids['chaos-monkey/1'])
354
351
# Test monkey_ids.get(unit_name) does not change on second call to
356
with patch.object(client, 'get_juju_output', side_effect=output2,
353
with patch('subprocess.check_output', side_effect=output2,
358
355
monkey_runner.unleash_once()
359
self.assertEqual('1234' * 9,
360
monkey_runner.monkey_ids['chaos-monkey/1'])
356
self.assertEqual('1234', monkey_runner.monkey_ids['chaos-monkey/1'])
362
358
def test_unleash_once_raises_for_unexpected_action_output(self):
363
def output(*args, **kwargs):
359
def output(args, **kwargs):
364
360
status = yaml.safe_dump({
366
362
'0': {'agent-state': 'started'}
381
('show-status', '--format', 'yaml'): status,
382
('run-action', 'chaos-monkey/0', 'start', 'mode=single',
377
('juju', '--show-log', 'status', '-e', 'foo'): status,
378
('juju', '--show-log', 'action', 'do', '-e', 'foo',
379
'chaos-monkey/0', 'start', 'mode=single',
383
380
'enablement-timeout=120'
384
381
): 'Action fail',
386
383
return output[args]
387
client = EnvJujuClient(JujuData('foo', {}), None, '/foo/juju')
384
client = EnvJujuClient(SimpleEnvironment('foo', {}), None, '/foo/juju')
388
385
monkey_runner = MonkeyRunner('foo', client, service='jenkins')
389
with patch.object(client, 'get_juju_output', side_effect=output,
386
with patch('subprocess.check_output', side_effect=output,
391
388
with self.assertRaisesRegexp(
392
Exception, 'Action id not found in output: Action fail'):
389
Exception, 'Unexpected output from "juju action do":'):
393
390
monkey_runner.unleash_once()
396
class TestIsHealthy(unittest.TestCase):
393
class TestIsHealthy(TestCase):
398
395
def test_is_healthy(self):
399
396
SCRIPT = """#!/bin/bash\necho -n 'PASS'\nexit 0"""
400
client = EnvJujuClient(JujuData('foo', {}), None, '/foo/juju')
397
client = EnvJujuClient(SimpleEnvironment('foo', {}), None, '/foo/juju')
401
398
with NamedTemporaryFile(delete=False) as health_script:
402
399
health_script.write(SCRIPT)
403
400
os.fchmod(health_script.fileno(), stat.S_IEXEC | stat.S_IREAD)