1
3
from argparse import Namespace
3
5
from unittest import TestCase
10
9
from assess_heterogeneous_control import (
15
test_control_heterogeneous,
17
from fakejuju import (
19
fake_juju_client_optional_jes,
21
15
from jujupy import (
24
from tests.test_deploy_stack import FakeBootstrapManager
22
class TestDumping_env(TestCase):
24
def test_dumping_env_exception(self):
25
client = EnvJujuClient(SimpleEnvironment('env'), '5', '4')
26
with patch.object(client, 'destroy_environment') as de_mock:
27
with patch('subprocess.check_call') as cc_mock:
28
with patch('deploy_stack.copy_remote_logs') as crl_mock:
29
with self.assertRaises(ValueError):
30
with dumping_env(client, 'foo', 'bar'):
32
crl_mock.assert_called_once_with('foo', 'bar')
33
cc_mock.assert_called_once_with(['gzip', '-f'])
34
de_mock.assert_called_once_with()
36
def test_dumping_env_success(self):
37
client = EnvJujuClient(SimpleEnvironment('env'), '5', '4')
38
with patch.object(client, 'destroy_environment') as de_mock:
39
with patch('subprocess.check_call') as cc_mock:
40
with patch('deploy_stack.copy_remote_logs') as crl_mock:
41
with dumping_env(client, 'foo', 'bar'):
43
crl_mock.assert_called_once_with('foo', 'bar')
44
cc_mock.assert_called_once_with(['gzip', '-f'])
45
self.assertEqual(de_mock.call_count, 0)
29
48
class TestParseArgs(TestCase):
44
63
self.assertEqual(args.user, 'my name')
45
64
self.assertEqual(args.password, 'fake pass')
47
def test_parse_args_agent_stream(self):
48
args = parse_args(['a', 'b', 'c', 'd', 'e',
49
'--agent-stream', 'proposed',
50
'--user', 'my name', '--password', 'fake pass'])
51
self.assertEqual(args.agent_stream, 'proposed')
52
self.assertEqual(args.user, 'my name')
53
self.assertEqual(args.password, 'fake pass')
55
def test_parse_args_series(self):
56
args = parse_args(['a', 'b', 'c', 'd', 'e', '--series', 'trusty',
57
'--user', 'my name', '--password', 'fake pass'])
58
self.assertEqual(args.series, 'trusty')
59
self.assertEqual(args.user, 'my name')
60
self.assertEqual(args.password, 'fake pass')
63
67
class TestGetClients(TestCase):
72
76
with _temp_env({'environments': {'baz': {}}}):
73
77
with patch('subprocess.check_output', lambda x: boo[x]):
74
78
initial, other, released = get_clients('foo', 'bar', 'baz',
76
80
self.assertEqual(initial.env, other.env)
77
81
self.assertEqual(initial.env, released.env)
78
self.assertNotIn('tools-metadata-url', initial.env.config)
82
self.assertEqual(initial.env.config['tools-metadata-url'], 'quxx')
79
83
self.assertEqual(initial.full_path, os.path.abspath('foo'))
80
84
self.assertEqual(other.full_path, os.path.abspath('bar'))
81
85
self.assertEqual(released.full_path, '/usr/bun/juju')
83
def test_get_clients_different_env(self):
85
('foo', '--version'): '1.18.73',
86
('bar', '--version'): '1.18.74',
87
('juju', '--version'): '2.0',
88
('which', 'juju'): '/usr/bun/juju'
90
with _temp_env({'environments': {'baz': {}}}):
91
with patch('subprocess.check_output', lambda x: boo[x]):
92
with patch('jujupy.JujuData.load_yaml'):
93
initial, other, teardown = get_clients('foo', 'bar', 'baz',
95
self.assertIs(initial, teardown)
97
def test_old_released(self):
99
('foo', '--version'): '2.1',
100
('bar', '--version'): '2.0',
101
('juju', '--version'): '1.18',
102
('which', 'juju'): '/usr/bun/juju'
104
with _temp_env({'environments': {'baz': {}}}):
105
with patch('subprocess.check_output', lambda x: boo[x]):
106
with patch('jujupy.JujuData.load_yaml'):
107
initial, other, teardown = get_clients('foo', 'bar', 'baz',
109
self.assertIs(initial, teardown)
111
87
def test_get_clients_no_agent(self):
112
88
with _temp_env({'environments': {'baz': {}}}):
113
89
with patch('subprocess.check_output', return_value='1.18.73'):
114
90
initial, other, released = get_clients('foo', 'bar', 'baz',
116
92
self.assertTrue('tools-metadata-url' not in initial.env.config)
119
class TestAssessHeterogeneous(TestCase):
121
@patch('assess_heterogeneous_control.BootstrapManager')
122
@patch('assess_heterogeneous_control.test_control_heterogeneous',
124
@patch('assess_heterogeneous_control.get_clients', autospec=True)
125
def test_assess_heterogeneous(self, gc_mock, ch_mock, bm_mock):
126
initial = MagicMock()
127
gc_mock.return_value = (
128
initial, 'other_client', 'released_client')
129
assess_heterogeneous(
130
'initial', 'other', 'base_env', 'environment_name', 'log_dir',
131
False, False, 'agent_url', 'agent_stream', 'series')
132
gc_mock.assert_called_once_with(
133
'initial', 'other', 'base_env', False, 'agent_url')
134
is_jes_enabled = initial.is_jes_enabled.return_value
135
bm_mock.assert_called_once_with(
136
'environment_name', initial, 'released_client',
137
agent_stream='agent_stream', agent_url='agent_url',
138
bootstrap_host=None, jes_enabled=is_jes_enabled, keep_env=False,
139
log_dir='log_dir', machines=[], permanent=is_jes_enabled,
140
region=None, series='series')
141
ch_mock.assert_called_once_with(
142
bm_mock.return_value, 'other_client', False)
145
class TestTestControlHeterogeneous(TestCase):
147
def test_test_control_heterogeneous(self):
148
client = fake_juju_client_optional_jes(jes_enabled=False)
149
bs_manager = FakeBootstrapManager(client)
151
bs_manager.tear_down_client = MagicMock()
152
bs_manager.tear_down_client.destroy_environment.return_value = 0
153
with patch.object(client, 'kill_controller'):
154
test_control_heterogeneous(bs_manager, client, True)
155
models = client._backend.controller_state.models
156
model_state = models[client.model_name]
157
self.assertEqual(model_state.exposed, {'sink2', 'dummy-sink'})
158
self.assertEqual(model_state.machines, {'0', '1', '2'})
159
self.assertEqual(client.env.juju_home, 'foo')
161
def test_same_home(self):
162
initial_client = fake_juju_client(version='1.25')
163
other_client = fake_juju_client(
164
env=initial_client.env,
165
_backend=initial_client._backend.clone(version='2.0.0'))
166
bs_manager = FakeBootstrapManager(initial_client)
167
bs_manager.permanent = True
168
test_control_heterogeneous(bs_manager, other_client, True)
169
self.assertEqual(initial_client.env.juju_home,
170
other_client.env.juju_home)
173
class TestCheckSeries(TestCase):
175
def test_check_series(self):
176
client = fake_juju_client()
180
def test_check_series_xenial(self):
181
client = MagicMock(spec=["get_juju_output"])
182
client.get_juju_output.return_value = "Codename: xenial"
183
check_series(client, 1, 'xenial')
185
def test_check_series_calls(self):
186
client = MagicMock(spec=["get_juju_output"])
187
with patch.object(client, 'get_juju_output',
188
return_value="Codename: xenial") as gjo_mock:
189
check_series(client, 2, 'xenial')
190
gjo_mock.assert_called_once_with('ssh', 2, 'lsb_release', '-c')
192
def test_check_series_exceptionl(self):
193
client = fake_juju_client()
195
with self.assertRaisesRegexp(
196
AssertionError, 'Series is angsty, not xenial'):
197
check_series(client, '0', 'xenial')
95
class TestUploadHeterogeneous(TestCase):
97
def test_upload_heterogeneous(self):
98
args = Namespace(user='foo', password='bar')
100
'upload_hetero_control.HUploader.upload_by_env_build_number')
101
with patch('upload_hetero_control.get_s3_access',
102
return_value=('name', 'pass')) as a_mock:
103
with patch('upload_hetero_control.S3Connection') as c_mock:
104
with env_ctx as u_mock:
105
upload_heterogeneous(args)
106
a_mock.assert_called_once_with()
107
c_mock.assert_called_once_with('name', 'pass')
108
u_mock.assert_called_once_with()