~abentley/juju-ci-tools/client-from-config-4

« back to all changes in this revision

Viewing changes to tests/test_quickstart_deploy.py

  • Committer: Martin Packman
  • Date: 2015-11-17 14:19:06 UTC
  • mto: This revision was merged to the branch mainline in revision 1155.
  • Revision ID: martin.packman@canonical.com-20151117141906-a4zmuqre72s7fyf1
Sample script for generating image streams for rackspace

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from contextlib import contextmanager
1
2
from mock import (
2
3
    ANY,
3
 
    MagicMock,
4
 
    patch,
 
4
    patch
5
5
)
 
6
from unittest import TestCase
6
7
 
7
 
from deploy_stack import BootstrapManager
8
8
from jujupy import (
9
9
    EnvJujuClient,
10
 
    JujuData,
 
10
    SimpleEnvironment,
11
11
    )
12
12
from quickstart_deploy import QuickstartTest
13
 
from tests import (
14
 
    FakeHomeTestCase,
15
 
    use_context,
16
 
)
17
 
from tests.test_deploy_stack import FakeBootstrapManager
18
 
from tests.test_jujupy import fake_juju_client
19
 
from utility import temp_dir
20
 
 
21
 
 
22
 
def make_bootstrap_manager(client, log_dir='log_dir'):
23
 
    return BootstrapManager(
24
 
        'env', client, client, None, [], 'series', 'agent_url',
25
 
        'agent_stream', 'region', log_dir, 'keep_env', 'permanent',
26
 
        'jes_enabled')
27
 
 
28
 
 
29
 
class TestQuickstartTest(FakeHomeTestCase):
 
13
 
 
14
 
 
15
class TestQuickstartTest(TestCase):
 
16
 
 
17
    @contextmanager
 
18
    def from_args_cxt(self):
 
19
        with patch('jujupy.EnvJujuClient.get_version',
 
20
                   side_effect=lambda x, juju_path=None: ''):
 
21
            with patch('jujupy.SimpleEnvironment.from_config',
 
22
                       side_effect=lambda x: SimpleEnvironment(x, {})):
 
23
                yield
 
24
 
 
25
    def test_from_args(self):
 
26
        def side_effect(x, y=None, debug=False):
 
27
            return x, y
 
28
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
 
29
            with patch('jujupy.SimpleEnvironment.from_config',
 
30
                       side_effect=lambda x: SimpleEnvironment(x, {})):
 
31
                quickstart = QuickstartTest.from_args(
 
32
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
 
33
                    '/tmp/bundle.yaml', 2
 
34
                )
 
35
        self.assertIs(type(quickstart), QuickstartTest)
 
36
        self.assertEqual(quickstart.client[0].environment, 'temp_env_name')
 
37
        self.assertEqual(quickstart.client[0].config, {
 
38
            'name': 'temp_env_name',
 
39
            })
 
40
        self.assertIs(quickstart.client[1], '/foo/bin/juju')
 
41
        self.assertEqual(quickstart.bundle_path, '/tmp/bundle.yaml')
 
42
        self.assertEqual(quickstart.log_dir, '/tmp/tmp')
 
43
        self.assertEqual(quickstart.service_count, 2)
 
44
 
 
45
    def test_from_args_agent_url(self):
 
46
        def side_effect(x, y=None, debug=False):
 
47
            return (x, y)
 
48
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
 
49
            with patch('jujupy.SimpleEnvironment.from_config',
 
50
                       side_effect=lambda x: SimpleEnvironment(x, {})):
 
51
                quickstart = QuickstartTest.from_args(
 
52
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
 
53
                    '/tmp/bundle.yaml', 2, agent_url='http://agent_url.com'
 
54
                )
 
55
        self.assertEqual(quickstart.client[0].config['tools-metadata-url'],
 
56
                         'http://agent_url.com')
 
57
 
 
58
    def test_from_args_series(self):
 
59
        def side_effect(x, y=None, debug=False):
 
60
            return (x, y)
 
61
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
 
62
            with patch('jujupy.SimpleEnvironment.from_config',
 
63
                       side_effect=lambda x: SimpleEnvironment(x, {})):
 
64
                quickstart = QuickstartTest.from_args(
 
65
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
 
66
                    '/tmp/bundle.yaml', 2, series='precise'
 
67
                )
 
68
        self.assertEqual(quickstart.client[0].config['default-series'],
 
69
                         'precise')
 
70
 
 
71
    def test_from_args_debug(self):
 
72
        with self.from_args_cxt():
 
73
            quickstart = QuickstartTest.from_args(
 
74
                'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
 
75
                '/tmp/bundle.yaml', 2, debug_flag=True
 
76
            )
 
77
        self.assertEqual(quickstart.client.debug, True)
 
78
 
 
79
    def test_from_args_region(self):
 
80
        with self.from_args_cxt():
 
81
            quickstart = QuickstartTest.from_args(
 
82
                'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
 
83
                '/tmp/bundle.yaml', 2, region='region-foo')
 
84
        self.assertEqual(quickstart.client.env.config['region'], 'region-foo')
 
85
 
 
86
    def test_from_args_agent_stream(self):
 
87
        with self.from_args_cxt():
 
88
            quickstart = QuickstartTest.from_args(
 
89
                'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
 
90
                '/tmp/bundle.yaml', 2, agent_stream='agent-stream-foo')
 
91
        self.assertEqual(quickstart.client.env.config['agent-stream'],
 
92
                         'agent-stream-foo')
 
93
 
 
94
    def test_from_args_bootstrap_host(self):
 
95
        with self.from_args_cxt():
 
96
            quickstart = QuickstartTest.from_args(
 
97
                'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
 
98
                '/tmp/bundle.yaml', 2, bootstrap_host='host-foo')
 
99
        self.assertEqual(quickstart.client.env.config['bootstrap-host'],
 
100
                         'host-foo')
30
101
 
31
102
    def test_run_finally(self):
32
 
        do_finally = MagicMock()
33
 
 
34
 
        def fake_iter_steps():
35
 
            try:
36
 
                yield {'bootstrap_host': 'foo'}
37
 
            finally:
38
 
                do_finally()
39
 
 
40
 
        client = EnvJujuClient(
41
 
            JujuData('foo', {'type': 'local'}), '1.234-76', None)
42
 
        bs_manager = make_bootstrap_manager(client)
43
 
        quickstart = QuickstartTest(bs_manager, '/tmp/bundle.yaml', 2)
44
 
        with patch.object(quickstart, 'iter_steps',
45
 
                          side_effect=fake_iter_steps):
46
 
            quickstart.run()
47
 
        do_finally.assert_called_once_with()
48
 
 
49
 
    def test_run_exception(self):
50
 
        tear_down = MagicMock()
51
 
 
52
 
        def fake_iter_steps():
53
 
            try:
54
 
                yield {'bootstrap_host': 'foo'}
55
 
            except:
56
 
                tear_down()
57
 
 
58
 
        client = EnvJujuClient(
59
 
            JujuData('foo', {'type': 'local'}), '1.234-76', None)
60
 
        bs_manager = make_bootstrap_manager(client)
61
 
        quickstart = QuickstartTest(bs_manager, '/tmp/bundle.yaml', 2)
62
 
        with patch.object(quickstart, 'iter_steps',
63
 
                          side_effect=fake_iter_steps):
64
 
            with self.assertRaises(BaseException):
65
 
                with patch('logging.info', side_effect=Exception):
66
 
                    quickstart.run()
67
 
        tear_down.assert_called_once_with()
 
103
        def fake_iter_steps():
 
104
            yield {'bootstrap_host': 'foo'}
 
105
        client = EnvJujuClient(
 
106
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
 
107
        quickstart = QuickstartTest(client, '/tmp/bundle.yaml', '/tmp/logs', 2)
 
108
        with patch.object(client, 'destroy_environment') as qs_mock:
 
109
            with patch('quickstart_deploy.safe_print_status') as ps_mock:
 
110
                with patch('quickstart_deploy.dump_env_logs') as dl_mock:
 
111
                    with patch.object(quickstart, 'iter_steps',
 
112
                                      side_effect=fake_iter_steps):
 
113
                        quickstart.run()
 
114
        qs_mock.assert_called_once_with(delete_jenv=True)
 
115
        ps_mock.assert_called_once_with(client)
 
116
        dl_mock.assert_called_once_with(client, 'foo', '/tmp/logs')
 
117
 
 
118
    @patch('sys.stderr')
 
119
    def test_run_exception(self, se_mock):
 
120
        def fake_iter_steps():
 
121
            yield {'bootstrap_host': 'foo'}
 
122
            raise Exception()
 
123
        client = EnvJujuClient(
 
124
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
 
125
        quickstart = QuickstartTest(client, '/tmp/bundle.yaml', '/tmp/logs', 2)
 
126
        with patch.object(client, 'destroy_environment') as qs_mock:
 
127
            with patch('quickstart_deploy.safe_print_status') as ps_mock:
 
128
                with patch('quickstart_deploy.dump_env_logs') as dl_mock:
 
129
                    with patch.object(quickstart, 'iter_steps',
 
130
                                      side_effect=fake_iter_steps):
 
131
                        with self.assertRaises(BaseException):
 
132
                            quickstart.run()
 
133
        dl_mock.assert_called_once_with(client, 'foo', '/tmp/logs')
 
134
        qs_mock.assert_called_once_with(delete_jenv=True)
 
135
        ps_mock.assert_called_once_with(client)
68
136
 
69
137
    def test_iter_steps(self):
70
 
        log_dir = use_context(self, temp_dir())
71
138
        client = EnvJujuClient(
72
 
            JujuData('foo', {'type': 'local'}), '1.234-76', None)
73
 
        bs_manager = make_bootstrap_manager(client, log_dir=log_dir)
74
 
        quickstart = QuickstartTest(bs_manager, '/tmp/bundle.yaml', 2)
 
139
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
 
140
        quickstart = QuickstartTest(client, '/tmp/bundle.yaml', '/tmp/logs', 2)
75
141
        steps = quickstart.iter_steps()
76
142
        with patch.object(client, 'quickstart') as qs_mock:
77
143
            # Test first yield
78
 
            with patch('jujupy.check_free_disk_space', autospec=True):
79
 
                with patch('deploy_stack.tear_down', autospec=True) as td_mock:
80
 
                    step = steps.next()
81
 
        td_mock.assert_called_once_with(client, 'jes_enabled', try_jes=True)
 
144
            step = steps.next()
82
145
        qs_mock.assert_called_once_with('/tmp/bundle.yaml')
83
146
        expected = {'juju-quickstart': 'Returned from quickstart'}
84
147
        self.assertEqual(expected, step)
85
 
        with patch('deploy_stack.get_machine_dns_name',
 
148
        with patch('quickstart_deploy.get_machine_dns_name',
86
149
                   return_value='mocked_name') as dns_mock:
87
150
            # Test second yield
88
 
            with patch.object(client, 'get_controller_client') as gcc_mock:
89
 
                step = steps.next()
90
 
        dns_mock.assert_called_once_with(gcc_mock.return_value, '0')
 
151
            step = steps.next()
 
152
        dns_mock.assert_called_once_with(client, 0)
91
153
        self.assertEqual('mocked_name', step['bootstrap_host'])
92
154
        with patch.object(client, 'wait_for_deploy_started') as wds_mock:
93
155
            # Test third yield
99
161
            step = steps.next()
100
162
        ws_mock.assert_called_once_with(ANY)
101
163
        self.assertEqual('All Agents started', step['agents_started'])
102
 
        with patch('deploy_stack.safe_print_status'):
103
 
            with patch('deploy_stack.tear_down'):
104
 
                with patch('quickstart_deploy.BootstrapManager.dump_all_logs'):
105
 
                    with patch('jujupy.EnvJujuClient.iter_model_clients',
106
 
                               return_value=[]):
107
 
                        steps.close()
108
 
 
109
 
    def test_iter_steps_context(self):
110
 
        client = fake_juju_client()
111
 
        bs_manager = FakeBootstrapManager(client)
112
 
        quickstart = QuickstartTest(bs_manager, '/tmp/bundle.yaml', 2)
113
 
        step_iter = quickstart.iter_steps()
114
 
        self.assertIs(False, bs_manager.entered_top)
115
 
        self.assertIs(False, bs_manager.exited_top)
116
 
        self.assertIs(False, bs_manager.entered_bootstrap)
117
 
        self.assertIs(False, bs_manager.exited_bootstrap)
118
 
        step_iter.next()
119
 
        models = client._backend.controller_state.models
120
 
        backing_state = models[client.model_name]
121
 
        self.assertEqual('/tmp/bundle.yaml', backing_state.current_bundle)
122
 
        self.assertIs(True, bs_manager.entered_top)
123
 
        self.assertIs(True, bs_manager.entered_bootstrap)
124
 
        self.assertIs(False, bs_manager.exited_bootstrap)
125
 
        self.assertIs(False, bs_manager.entered_runtime)
126
 
        self.assertIs(False, bs_manager.exited_runtime)
127
 
        step_iter.next()
128
 
        self.assertIs(True, bs_manager.exited_bootstrap)
129
 
        self.assertIs(True, bs_manager.entered_runtime)
130
 
        self.assertIs(False, bs_manager.exited_runtime)
131
 
        with patch.object(client, 'wait_for_deploy_started') as wfds_mock:
132
 
            step_iter.next()
133
 
        wfds_mock.assert_called_once_with(2)
134
 
        self.assertIs(False, bs_manager.exited_runtime)
135
 
        with patch.object(client, 'wait_for_started') as wfs_mock:
136
 
            step_iter.next()
137
 
        wfs_mock.assert_called_once_with(3600)
138
 
        self.assertIs(False, bs_manager.exited_runtime)
139
 
        with self.assertRaises(StopIteration):
140
 
            step_iter.next()
141
 
        self.assertIs(True, bs_manager.exited_runtime)
142
 
        self.assertIs(True, bs_manager.exited_top)
143
 
 
144
 
    def test_iter_steps_quickstart_fail(self):
145
 
        client = EnvJujuClient(
146
 
            JujuData('foo', {'type': 'local'}), '1.234-76', None)
147
 
        bs_manager = FakeBootstrapManager(client)
148
 
        quickstart = QuickstartTest(bs_manager, '/tmp/bundle.yaml', 2)
149
 
        step_iter = quickstart.iter_steps()
150
 
        with patch.object(client, 'quickstart', side_effect=Exception):
151
 
            with self.assertRaises(Exception):
152
 
                step_iter.next()
153
 
        self.assertIs(False, bs_manager.entered_runtime)
154
 
        self.assertIs(True, bs_manager.exited_bootstrap)
155
 
        self.assertIs(True, bs_manager.exited_top)
156
 
 
157
 
    def test_iter_steps_wait_fail(self):
158
 
        client = fake_juju_client()
159
 
        bs_manager = FakeBootstrapManager(client)
160
 
        quickstart = QuickstartTest(bs_manager, '/tmp/bundle.yaml', 2)
161
 
        step_iter = quickstart.iter_steps()
162
 
        step_iter.next()
163
 
        step_iter.next()
164
 
        with patch.object(client, 'wait_for_deploy_started',
165
 
                          side_effect=Exception):
166
 
            with self.assertRaises(Exception):
167
 
                step_iter.next()
168
 
        self.assertIs(True, bs_manager.exited_runtime)
169
 
        self.assertIs(True, bs_manager.exited_bootstrap)
170
 
        self.assertIs(True, bs_manager.exited_top)