1
from argparse import Namespace
2
from unittest import TestCase
13
from run_chaos_monkey import (
17
from test_jujupy import (
22
def fake_EnvJujuClient_by_version(env, path=None, debug=None):
23
return EnvJujuClient(env=env, version='1.2.3.4', full_path=path)
26
def fake_SimpleEnvironment_from_config(name):
27
return SimpleEnvironment(name, {})
30
class TestRunChaosMonkey(TestCase):
32
def test_get_args(self):
33
args = get_args(['foo', 'bar', 'baz'])
34
self.assertItemsEqual(['env', 'service', 'health_checker'],
35
[a for a in dir(args) if not a.startswith('_')])
36
self.assertEqual(args.env, 'foo')
37
self.assertEqual(args.service, 'bar')
38
self.assertEqual(args.health_checker, 'baz')
40
def test_from_config(self):
41
with patch('jujupy.EnvJujuClient.by_version',
42
side_effect=fake_EnvJujuClient_by_version):
43
with patch('jujupy.SimpleEnvironment.from_config',
44
side_effect=fake_SimpleEnvironment_from_config) as mock:
45
monkey_runner = MonkeyRunner.from_config(Namespace(
46
env='foo', service='bar', health_checker='checker'))
47
self.assertIsInstance(monkey_runner, MonkeyRunner)
48
self.assertEqual(monkey_runner.env, 'foo')
49
self.assertEqual(monkey_runner.service, 'bar')
50
self.assertEqual(monkey_runner.health_checker, 'checker')
51
mock.assert_called_once_with('foo')
52
self.assertIsInstance(monkey_runner.client, EnvJujuClient)
54
def test_deploy_chaos_monkey(self):
55
def output(args, **kwargs):
56
status = yaml.safe_dump({
57
'machines': {'0': {'agent-state': 'started'}},
60
('juju', '--show-log', 'status', '-e', 'foo'): status,
63
client = EnvJujuClient(SimpleEnvironment('foo', {}), None, '/foo/juju')
64
with patch('subprocess.check_output', side_effect=output,
65
autospec=True) as co_mock:
66
with patch('subprocess.check_call', autospec=True) as cc_mock:
67
monkey_runner = MonkeyRunner('foo', 'bar', 'checker', client)
68
with patch('sys.stdout', autospec=True):
69
monkey_runner.deploy_chaos_monkey()
70
assert_juju_call(self, cc_mock, client, (
71
'juju', '--show-log', 'deploy', '-e', 'foo', 'local:chaos-monkey'),
73
assert_juju_call(self, cc_mock, client, (
74
'juju', '--show-log', 'add-relation', '-e', 'foo', 'bar',
76
self.assertEqual(cc_mock.call_count, 2)
77
self.assertEqual(co_mock.call_count, 1)