~andrewjbeach/juju-ci-tools/make-local-patcher

908.2.2 by John George
Add additional tests for quickstart_deploy.py
1
from mock import (
2
    ANY,
3
    patch
4
)
908.2.1 by John George
Re-write quickstart_deploy.py to use EnvJujuClient.
5
from unittest import TestCase
6
7
from jujupy import (
8
    EnvJujuClient,
9
    SimpleEnvironment,
10
    )
11
from quickstart_deploy import QuickstartTest
12
13
14
class TestQuickstartTest(TestCase):
15
16
    def test_from_args(self):
17
        side_effect = lambda x, y=None, debug=False: (x, y)
18
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
19
            with patch('jujupy.SimpleEnvironment.from_config',
20
                       side_effect=lambda x: SimpleEnvironment(x, {})):
21
                quickstart = QuickstartTest.from_args(
22
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
23
                    '/tmp/bundle.yaml', 2
24
                )
25
        self.assertIs(type(quickstart), QuickstartTest)
26
        self.assertEqual(quickstart.client[0].environment, 'temp_env_name')
27
        self.assertIs(quickstart.client[1], '/foo/bin/juju')
28
        self.assertEqual(quickstart.bundle_path, '/tmp/bundle.yaml')
29
        self.assertEqual(quickstart.log_dir, '/tmp/tmp')
30
        self.assertEqual(quickstart.service_count, 2)
31
32
    def test_from_args_agent_url(self):
33
        side_effect = lambda x, y=None, debug=False: (x, y)
34
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
35
            with patch('jujupy.SimpleEnvironment.from_config',
36
                       side_effect=lambda x: SimpleEnvironment(x, {})):
37
                quickstart = QuickstartTest.from_args(
38
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
39
                    '/tmp/bundle.yaml', 2, agent_url='http://agent_url.com'
40
                )
41
        self.assertEqual(quickstart.client[0].config['agent_url'],
42
                         'http://agent_url.com')
43
44
    def test_from_args_series(self):
45
        side_effect = lambda x, y=None, debug=False: (x, y)
46
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
47
            with patch('jujupy.SimpleEnvironment.from_config',
48
                       side_effect=lambda x: SimpleEnvironment(x, {})):
49
                quickstart = QuickstartTest.from_args(
50
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
51
                    '/tmp/bundle.yaml', 2, series='precise'
52
                )
53
        self.assertEqual(quickstart.client[0].config['series'],
54
                         'precise')
55
56
    def test_from_args_debug(self):
57
        with patch('jujupy.EnvJujuClient.get_version',
58
                   side_effect=lambda x, juju_path=None: ''):
59
            with patch('jujupy.SimpleEnvironment.from_config',
60
                       side_effect=lambda x: SimpleEnvironment(x, {})):
61
                quickstart = QuickstartTest.from_args(
62
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
63
                    '/tmp/bundle.yaml', 2, debug_flag=True
64
                )
65
        self.assertEqual(quickstart.client.debug, True)
66
908.2.2 by John George
Add additional tests for quickstart_deploy.py
67
    def test_run_finally(self):
915.1.2 by John George
Adjust test_quickstart_deploy to handle raise.
68
        def fake_iter_steps():
69
            yield {'bootstrap_host': 'foo'}
908.2.3 by John George
Drop use of FakeEnvJujuClient in test_quickstart_deploy.py and add an exception test for QuickstartTest.run().
70
        client = EnvJujuClient(
71
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
908.2.2 by John George
Add additional tests for quickstart_deploy.py
72
        quickstart = QuickstartTest(client, '/tmp/bundle.yaml', '/tmp/logs', 2)
73
        with patch.object(client, 'destroy_environment') as qs_mock:
74
            with patch('quickstart_deploy.safe_print_status') as ps_mock:
990.1.3 by Curtis Hovey
Always collect logs from quickstart and deployer.
75
                with patch('quickstart_deploy.dump_env_logs') as dl_mock:
76
                    with patch.object(quickstart, 'iter_steps',
77
                                      side_effect=fake_iter_steps):
78
                        quickstart.run()
908.2.2 by John George
Add additional tests for quickstart_deploy.py
79
        qs_mock.assert_called_once_with(delete_jenv=True)
80
        ps_mock.assert_called_once_with(client)
990.1.3 by Curtis Hovey
Always collect logs from quickstart and deployer.
81
        dl_mock.assert_called_once_with(client, 'foo', '/tmp/logs')
908.2.2 by John George
Add additional tests for quickstart_deploy.py
82
990.1.3 by Curtis Hovey
Always collect logs from quickstart and deployer.
83
    @patch('sys.stderr')
84
    def test_run_exception(self, se_mock):
908.2.3 by John George
Drop use of FakeEnvJujuClient in test_quickstart_deploy.py and add an exception test for QuickstartTest.run().
85
        def fake_iter_steps():
86
            yield {'bootstrap_host': 'foo'}
87
            raise Exception()
88
        client = EnvJujuClient(
89
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
90
        quickstart = QuickstartTest(client, '/tmp/bundle.yaml', '/tmp/logs', 2)
91
        with patch.object(client, 'destroy_environment') as qs_mock:
92
            with patch('quickstart_deploy.safe_print_status') as ps_mock:
93
                with patch('quickstart_deploy.dump_env_logs') as dl_mock:
94
                    with patch.object(quickstart, 'iter_steps',
95
                                      side_effect=fake_iter_steps):
915.1.2 by John George
Adjust test_quickstart_deploy to handle raise.
96
                        with self.assertRaises(BaseException):
97
                            quickstart.run()
908.2.3 by John George
Drop use of FakeEnvJujuClient in test_quickstart_deploy.py and add an exception test for QuickstartTest.run().
98
        dl_mock.assert_called_once_with(client, 'foo', '/tmp/logs')
99
        qs_mock.assert_called_once_with(delete_jenv=True)
100
        ps_mock.assert_called_once_with(client)
101
908.2.2 by John George
Add additional tests for quickstart_deploy.py
102
    def test_iter_steps(self):
908.2.3 by John George
Drop use of FakeEnvJujuClient in test_quickstart_deploy.py and add an exception test for QuickstartTest.run().
103
        client = EnvJujuClient(
104
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
908.2.1 by John George
Re-write quickstart_deploy.py to use EnvJujuClient.
105
        quickstart = QuickstartTest(client, '/tmp/bundle.yaml', '/tmp/logs', 2)
106
        steps = quickstart.iter_steps()
107
        with patch.object(client, 'quickstart') as qs_mock:
108
            # Test first yield
109
            step = steps.next()
110
        qs_mock.assert_called_once_with('/tmp/bundle.yaml')
111
        expected = {'juju-quickstart': 'Returned from quickstart'}
112
        self.assertEqual(expected, step)
908.2.2 by John George
Add additional tests for quickstart_deploy.py
113
        with patch('quickstart_deploy.get_machine_dns_name',
114
                   return_value='mocked_name') as dns_mock:
908.2.1 by John George
Re-write quickstart_deploy.py to use EnvJujuClient.
115
            # Test second yield
116
            step = steps.next()
908.2.2 by John George
Add additional tests for quickstart_deploy.py
117
        dns_mock.assert_called_once_with(client, 0)
908.2.1 by John George
Re-write quickstart_deploy.py to use EnvJujuClient.
118
        self.assertEqual('mocked_name', step['bootstrap_host'])
908.2.2 by John George
Add additional tests for quickstart_deploy.py
119
        with patch.object(client, 'wait_for_deploy_started') as wds_mock:
120
            # Test third yield
121
            step = steps.next()
122
        wds_mock.assert_called_once_with(2)
123
        self.assertEqual('Deploy stated', step['deploy_started'])
124
        with patch.object(client, 'wait_for_started') as ws_mock:
125
            # Test forth yield
126
            step = steps.next()
127
        ws_mock.assert_called_once_with(ANY)
128
        self.assertEqual('All Agents started', step['agents_started'])