1
from argparse import Namespace
2
from contextlib import contextmanager
8
from unittest import TestCase
14
from scale_out import (
23
def fake_EnvJujuClient(env, path=None, debug=None):
24
return EnvJujuClient(env=env, version='1.2.3.4', full_path=path)
27
class TestScaleOut(TestCase):
30
def fake_client_cxt(self):
31
env = JujuData('foo', {})
32
client = fake_EnvJujuClient(env)
33
bv_cxt = patch('scale_out.client_from_config',
35
with bv_cxt as bv_mock:
36
yield (client, env, bv_mock)
38
def test_parse_args(self):
40
['env', '/path/juju', 'logs', 'temp_name', 'foo', 'bar'])
45
charms=['foo', 'bar'],
48
juju_bin='/path/juju',
54
temp_env_name='temp_name',
59
self.assertEqual(args, expected)
61
@patch('scale_out.boot_context', autospec=True)
62
@patch('jujupy.EnvJujuClient.add_ssh_machines', autospec=True)
63
def test_scaleout_setup(
65
add_ssh_machines_func,
75
juju_bin='/path/juju',
81
temp_env_name='temp_name',
87
with self.fake_client_cxt() as (fake_client, fake_env, bv_mock):
88
with scaleout_setup(args) as client:
90
boot_context_func.assert_called_once_with(
102
bv_mock.assert_called_once_with('test_env', '/path/juju', False,
104
add_ssh_machines_func.assert_called_once_with(client, ['0'])
105
self.assertIs(client, fake_client)
107
def test_scaleout_setup_sets_series(self):
113
charms='ubuntu some_other_charm',
116
juju_bin='/path/juju',
122
temp_env_name='temp_name',
124
verbose=logging.INFO,
128
with self.fake_client_cxt():
129
with patch('scale_out.boot_context', autospec=True) as bc_mock:
130
with patch('jujupy.EnvJujuClient.add_ssh_machines',
132
with scaleout_setup(args) as client:
134
bc_mock.assert_called_once_with(
147
def test_deploy_charms(self):
148
with self.fake_client_cxt() as (client, env, bv_mock):
149
with patch.object(EnvJujuClient, 'deploy') as d_mock:
150
with patch.object(EnvJujuClient,
151
'wait_for_started') as wfs_mock:
152
deploy_charms(client, ['ubuntu', 'mysql'])
153
expected = [call('ubuntu', service='ubuntu'),
154
call('mysql', service='mysql')]
155
self.assertEqual(d_mock.mock_calls, expected)
156
wfs_mock.assert_called_once_with()
158
def test_deploy_charms_local(self):
159
with self.fake_client_cxt() as (client, env, bv_mock):
160
with patch.object(EnvJujuClient, 'deploy') as d_mock:
161
with patch.object(EnvJujuClient,
162
'wait_for_started') as wfs_mock:
163
deploy_charms(client, ['local:foo', 'local:bar'])
164
expected = [call('local:foo', service='foo'),
165
call('local:bar', service='bar')]
166
self.assertEqual(d_mock.mock_calls, expected)
167
wfs_mock.assert_called_once_with()
169
def test_scale_out(self):
170
with self.fake_client_cxt() as (client, env, bv_mock):
171
with patch.object(EnvJujuClient, 'juju') as j_mock:
172
with patch.object(EnvJujuClient,
173
'wait_for_started') as wfs_mock:
174
scale_out(client, 'ubuntu')
175
j_mock.assert_called_once_with('add-unit', ('ubuntu', '-n', '5'))
176
wfs_mock.assert_called_once_with()
178
def test_get_service_name(self):
179
charms = [('charm-name', 'charm-name'),
180
('charm-name-21', 'charm-name-21'),
181
('series/charm-name-13', 'charm-name-13'),
182
('local:charm-name', 'charm-name'),
183
('cs:~user/charm-name', 'charm-name'),
184
('cs:charm-name-2-1', 'charm-name-2-1'),
185
('lp:~user/some/path/to/charm-name', 'charm-name')]
186
for charm, expected in charms:
187
result = get_service_name(charm)
188
self.assertEqual(result, expected)