19
19
from __future__ import unicode_literals
21
from contextlib import contextmanager
23
30
from quickstart.models import apiinfo
24
31
from quickstart.tests import helpers
27
class TestInfo(unittest.TestCase, helpers.CallTestsMixin):
35
unittest.TestCase, helpers.CallTestsMixin, helpers.JenvFileTestsMixin):
38
# Instantiate an Info object.
39
self.juju_command = '/path/tp/juju'
40
self.info = apiinfo.Info(self.juju_command)
43
def make_juju_home(self, envs=()):
44
"""Create a Juju home with the given optional environments.
46
An empty jenv file is created for each environment name in envs.
47
The "environments" subdir of the Juju home is provided in the context
50
home = tempfile.mkdtemp()
51
self.addCleanup(shutil.rmtree, home)
52
envs_dir = os.path.join(home, 'environments')
55
jenv = os.path.join(envs_dir, env + '.jenv')
56
open(jenv, 'a').close()
57
with mock.patch('quickstart.settings.JUJU_HOME', home):
29
60
def test_get(self):
30
61
# The environment info is returned correctly.
64
'password': 'Secret!',
65
'environ-uuid': 'env-uuid',
66
'state-servers': ['localhost:17070', '10.0.3.1:17070'],
68
with self.patch_call(0, output, '') as mock_call:
69
info = self.info.get('ec2')
70
mock_call.assert_called_once_with(
71
self.juju_command, 'api-info', '-e', 'ec2', '--password',
76
'password': 'Secret!',
78
'state-servers': ['localhost:17070', '10.0.3.1:17070'],
80
self.assertEqual(expected_info, info)
33
82
def test_get_error(self):
34
83
# An empty dict is returned if the environment info cannot be
85
expected_message = 'unable to get API info for ec2: bad wolf'
86
with self.patch_call(1, '', 'bad wolf'):
87
with helpers.assert_logs([expected_message], level='debug'):
88
info = self.info.get('ec2')
89
self.assertEqual({}, info)
38
91
def test_all(self):
39
92
# The active environments database is properly returned.
93
output1 = '\n'.join(['local', 'ec2'])
94
output2 = json.dumps({
97
'environ-uuid': 'local-uuid',
98
'state-servers': ['localhost:17070', '10.0.3.1:17070'],
100
output3 = json.dumps({
103
'environ-uuid': 'ec2-uuid',
104
'state-servers': ['1.2.3.4:17070'],
107
# First call to retrieve the list of environments.
109
# Second call to retrieve info on the local environment.
111
# Third call to retrieve info on the ec2 environment.
114
with self.patch_multiple_calls(side_effect) as mock_call:
116
self.assertEqual(3, mock_call.call_count)
117
mock_call.assert_has_calls([
118
mock.call(self.juju_command, 'system', 'list'),
119
mock.call(self.juju_command, 'api-info', '-e', 'local',
120
'--password', '--format', u'json'),
121
mock.call(self.juju_command, 'api-info', '-e', 'ec2', '--password',
122
'--format', u'json'),
124
expected_db = {'environments': {
130
'state-servers': ['1.2.3.4:17070'],
134
'user': 'local-user',
136
'uuid': 'local-uuid',
137
'state-servers': ['localhost:17070', '10.0.3.1:17070'],
140
self.assertEqual(expected_db, db)
42
142
def test_all_empty(self):
43
143
# An empty active environments database is returned when there are no
44
144
# active environments.
145
with self.patch_call(0, '', '') as mock_call:
147
mock_call.assert_called_once_with(self.juju_command, 'system', 'list')
148
self.assertEqual({'environments': {}}, db)
47
150
def test_all_legacy(self):
48
151
# Active environments are detected even when using an old version of
49
152
# Juju not supporting controllers.
153
output1 = json.dumps({
154
'user': 'local-user',
156
'environ-uuid': 'local-uuid',
157
'state-servers': ['localhost:17070', '10.0.3.1:17070'],
159
output2 = json.dumps({
162
'environ-uuid': 'ec2-uuid',
163
'state-servers': ['1.2.3.4:17070'],
166
# First call to retrieve the list of environments.
167
(1, '', 'not implemented'),
168
# Second call to retrieve info on the local environment.
170
# Third call to retrieve info on the ec2 environment.
173
with self.make_juju_home(envs=('local', 'ec2')):
174
with self.patch_multiple_calls(side_effect) as mock_call:
176
self.assertEqual(3, mock_call.call_count)
177
mock_call.assert_has_calls([
178
mock.call(self.juju_command, 'system', 'list'),
179
mock.call(self.juju_command, 'api-info', '-e', 'local',
180
'--password', '--format', u'json'),
181
mock.call(self.juju_command, 'api-info', '-e', 'ec2', '--password',
182
'--format', u'json'),
184
expected_db = {'environments': {
190
'state-servers': ['1.2.3.4:17070'],
194
'user': 'local-user',
196
'uuid': 'local-uuid',
197
'state-servers': ['localhost:17070', '10.0.3.1:17070'],
200
self.assertEqual(expected_db, db)
52
202
def test_all_legacy_empty(self):
53
203
# An empty active environments database is returned when there are no
54
204
# active environments and an old version of Juju is in use.
205
with self.make_juju_home() as envs_dir:
206
# Directories and non-jenv files are ignored.
207
os.mkdir(os.path.join(envs_dir, 'dir'))
208
open(os.path.join(envs_dir, 'ec2.ext'), 'a').close()
209
with self.patch_call(2, '', 'not implemented'):
211
self.assertEqual({'environments': {}}, db)
213
def test_all_legacy_no_juju_home(self):
214
# An empty active environments database is returned when the Juju home
215
# does not exist and an old version of Juju is in use.
216
with self.make_juju_home() as envs_dir:
217
# Remove the environments directory.
219
with self.patch_call(2, '', 'not implemented'):
221
self.assertEqual({'environments': {}}, db)
58
224
class TestGetEnvDetails(unittest.TestCase):