~natefinch/juju-ci-tools/logrot

« back to all changes in this revision

Viewing changes to test_run_chaos_monkey.py

  • Committer: Nate Finch
  • Date: 2015-06-02 03:47:22 UTC
  • mfrom: (953.2.15 origin/trunk)
  • Revision ID: nate.finch@canonical.com-20150602034722-cr3lzq2yb6xdh7gz
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from argparse import Namespace
 
2
from unittest import TestCase
 
3
 
 
4
from mock import (
 
5
    patch
 
6
    )
 
7
import yaml
 
8
 
 
9
from jujupy import (
 
10
    EnvJujuClient,
 
11
    SimpleEnvironment,
 
12
    )
 
13
from run_chaos_monkey import (
 
14
    get_args,
 
15
    MonkeyRunner,
 
16
    )
 
17
from test_jujupy import (
 
18
    assert_juju_call,
 
19
    )
 
20
 
 
21
 
 
22
def fake_EnvJujuClient_by_version(env, path=None, debug=None):
 
23
    return EnvJujuClient(env=env, version='1.2.3.4', full_path=path)
 
24
 
 
25
 
 
26
def fake_SimpleEnvironment_from_config(name):
 
27
    return SimpleEnvironment(name, {})
 
28
 
 
29
 
 
30
class TestRunChaosMonkey(TestCase):
 
31
 
 
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')
 
39
 
 
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)
 
53
 
 
54
    def test_deploy_chaos_monkey(self):
 
55
        def output(args, **kwargs):
 
56
            status = yaml.safe_dump({
 
57
                'machines': {'0': {'agent-state': 'started'}},
 
58
                'services': {}})
 
59
            output = {
 
60
                ('juju', '--show-log', 'status', '-e', 'foo'): status,
 
61
                }
 
62
            return output[args]
 
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'),
 
72
            0)
 
73
        assert_juju_call(self, cc_mock, client, (
 
74
            'juju', '--show-log', 'add-relation', '-e', 'foo', 'bar',
 
75
            'chaos-monkey'), 1)
 
76
        self.assertEqual(cc_mock.call_count, 2)
 
77
        self.assertEqual(co_mock.call_count, 1)