1
from contextlib import contextmanager
6
from unittest import TestCase
7
from deploy_stack import BootstrapManager
12
12
from quickstart_deploy import QuickstartTest
17
from tests.test_deploy_stack import FakeBootstrapManager
18
from tests.test_jujupy import fake_juju_client
19
from utility import temp_dir
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',
29
class TestQuickstartTest(FakeHomeTestCase):
15
class TestQuickstartTest(TestCase):
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, {})):
25
def test_from_args(self):
26
def side_effect(x, y=None, debug=False):
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',
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',
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)
45
def test_from_args_agent_url(self):
46
def side_effect(x, y=None, debug=False):
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'
55
self.assertEqual(quickstart.client[0].config['tools-metadata-url'],
56
'http://agent_url.com')
58
def test_from_args_series(self):
59
def side_effect(x, y=None, debug=False):
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'
68
self.assertEqual(quickstart.client[0].config['default-series'],
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
77
self.assertEqual(quickstart.client.debug, True)
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')
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'],
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'],
31
102
def test_run_finally(self):
32
do_finally = MagicMock()
34
def fake_iter_steps():
36
yield {'bootstrap_host': 'foo'}
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):
47
do_finally.assert_called_once_with()
49
def test_run_exception(self):
50
tear_down = MagicMock()
52
def fake_iter_steps():
54
yield {'bootstrap_host': 'foo'}
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):
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):
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')
119
def test_run_exception(self, se_mock):
120
def fake_iter_steps():
121
yield {'bootstrap_host': 'foo'}
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):
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)
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:
81
td_mock.assert_called_once_with(client, 'jes_enabled', try_jes=True)
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:
90
dns_mock.assert_called_once_with(gcc_mock.return_value, '0')
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',
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)
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)
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:
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:
137
wfs_mock.assert_called_once_with(3600)
138
self.assertIs(False, bs_manager.exited_runtime)
139
with self.assertRaises(StopIteration):
141
self.assertIs(True, bs_manager.exited_runtime)
142
self.assertIs(True, bs_manager.exited_top)
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):
153
self.assertIs(False, bs_manager.entered_runtime)
154
self.assertIs(True, bs_manager.exited_bootstrap)
155
self.assertIs(True, bs_manager.exited_top)
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()
164
with patch.object(client, 'wait_for_deploy_started',
165
side_effect=Exception):
166
with self.assertRaises(Exception):
168
self.assertIs(True, bs_manager.exited_runtime)
169
self.assertIs(True, bs_manager.exited_bootstrap)
170
self.assertIs(True, bs_manager.exited_top)