~frankban/juju-quickstart/support-cache-yaml

« back to all changes in this revision

Viewing changes to quickstart/tests/models/test_apiinfo.py

  • Committer: Francesco Banconi
  • Date: 2015-11-03 12:54:39 UTC
  • Revision ID: francesco.banconi@canonical.com-20151103125439-p0yv7vea5gf183m7
Add apiinfo module tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from __future__ import unicode_literals
20
20
 
 
21
from contextlib import contextmanager
 
22
import json
 
23
import os
 
24
import shutil
 
25
import tempfile
21
26
import unittest
22
27
 
 
28
import mock
 
29
 
23
30
from quickstart.models import apiinfo
24
31
from quickstart.tests import helpers
25
32
 
26
33
 
27
 
class TestInfo(unittest.TestCase, helpers.CallTestsMixin):
 
34
class TestInfo(
 
35
        unittest.TestCase, helpers.CallTestsMixin, helpers.JenvFileTestsMixin):
 
36
 
 
37
    def setUp(self):
 
38
        # Instantiate an Info object.
 
39
        self.juju_command = '/path/tp/juju'
 
40
        self.info = apiinfo.Info(self.juju_command)
 
41
 
 
42
    @contextmanager
 
43
    def make_juju_home(self, envs=()):
 
44
        """Create a Juju home with the given optional environments.
 
45
 
 
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
 
48
        block.
 
49
        """
 
50
        home = tempfile.mkdtemp()
 
51
        self.addCleanup(shutil.rmtree, home)
 
52
        envs_dir = os.path.join(home, 'environments')
 
53
        os.mkdir(envs_dir)
 
54
        for env in envs:
 
55
            jenv = os.path.join(envs_dir, env + '.jenv')
 
56
            open(jenv, 'a').close()
 
57
        with mock.patch('quickstart.settings.JUJU_HOME', home):
 
58
            yield envs_dir
28
59
 
29
60
    def test_get(self):
30
61
        # The environment info is returned correctly.
31
 
        pass
 
62
        output = json.dumps({
 
63
            'user': 'who',
 
64
            'password': 'Secret!',
 
65
            'environ-uuid': 'env-uuid',
 
66
            'state-servers': ['localhost:17070', '10.0.3.1:17070'],
 
67
        })
 
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',
 
72
            '--format', u'json')
 
73
        expected_info = {
 
74
            'name': 'ec2',
 
75
            'user': 'who',
 
76
            'password': 'Secret!',
 
77
            'uuid': 'env-uuid',
 
78
            'state-servers': ['localhost:17070', '10.0.3.1:17070'],
 
79
        }
 
80
        self.assertEqual(expected_info, info)
32
81
 
33
82
    def test_get_error(self):
34
83
        # An empty dict is returned if the environment info cannot be
35
84
        # retrieved.
36
 
        pass
 
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)
37
90
 
38
91
    def test_all(self):
39
92
        # The active environments database is properly returned.
40
 
        pass
 
93
        output1 = '\n'.join(['local', 'ec2'])
 
94
        output2 = json.dumps({
 
95
            'user': 'local-user',
 
96
            'password': 'pswd1',
 
97
            'environ-uuid': 'local-uuid',
 
98
            'state-servers': ['localhost:17070', '10.0.3.1:17070'],
 
99
        })
 
100
        output3 = json.dumps({
 
101
            'user': 'ec2-user',
 
102
            'password': 'pswd2',
 
103
            'environ-uuid': 'ec2-uuid',
 
104
            'state-servers': ['1.2.3.4:17070'],
 
105
        })
 
106
        side_effect = [
 
107
            # First call to retrieve the list of environments.
 
108
            (0, output1, ''),
 
109
            # Second call to retrieve info on the local environment.
 
110
            (0, output2, ''),
 
111
            # Third call to retrieve info on the ec2 environment.
 
112
            (0, output3, ''),
 
113
        ]
 
114
        with self.patch_multiple_calls(side_effect) as mock_call:
 
115
            db = self.info.all()
 
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'),
 
123
        ])
 
124
        expected_db = {'environments': {
 
125
            'ec2': {
 
126
                'name': 'ec2',
 
127
                'user': 'ec2-user',
 
128
                'password': 'pswd2',
 
129
                'uuid': 'ec2-uuid',
 
130
                'state-servers': ['1.2.3.4:17070'],
 
131
            },
 
132
            'local': {
 
133
                'name': 'local',
 
134
                'user': 'local-user',
 
135
                'password': 'pswd1',
 
136
                'uuid': 'local-uuid',
 
137
                'state-servers': ['localhost:17070', '10.0.3.1:17070'],
 
138
            },
 
139
        }}
 
140
        self.assertEqual(expected_db, db)
41
141
 
42
142
    def test_all_empty(self):
43
143
        # An empty active environments database is returned when there are no
44
144
        # active environments.
45
 
        pass
 
145
        with self.patch_call(0, '', '') as mock_call:
 
146
            db = self.info.all()
 
147
        mock_call.assert_called_once_with(self.juju_command, 'system', 'list')
 
148
        self.assertEqual({'environments': {}}, db)
46
149
 
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.
50
 
        pass
 
153
        output1 = json.dumps({
 
154
            'user': 'local-user',
 
155
            'password': 'pswd1',
 
156
            'environ-uuid': 'local-uuid',
 
157
            'state-servers': ['localhost:17070', '10.0.3.1:17070'],
 
158
        })
 
159
        output2 = json.dumps({
 
160
            'user': 'ec2-user',
 
161
            'password': 'pswd2',
 
162
            'environ-uuid': 'ec2-uuid',
 
163
            'state-servers': ['1.2.3.4:17070'],
 
164
        })
 
165
        side_effect = [
 
166
            # First call to retrieve the list of environments.
 
167
            (1, '', 'not implemented'),
 
168
            # Second call to retrieve info on the local environment.
 
169
            (0, output1, ''),
 
170
            # Third call to retrieve info on the ec2 environment.
 
171
            (0, output2, ''),
 
172
        ]
 
173
        with self.make_juju_home(envs=('local', 'ec2')):
 
174
            with self.patch_multiple_calls(side_effect) as mock_call:
 
175
                db = self.info.all()
 
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'),
 
183
        ])
 
184
        expected_db = {'environments': {
 
185
            'ec2': {
 
186
                'name': 'ec2',
 
187
                'user': 'ec2-user',
 
188
                'password': 'pswd2',
 
189
                'uuid': 'ec2-uuid',
 
190
                'state-servers': ['1.2.3.4:17070'],
 
191
            },
 
192
            'local': {
 
193
                'name': 'local',
 
194
                'user': 'local-user',
 
195
                'password': 'pswd1',
 
196
                'uuid': 'local-uuid',
 
197
                'state-servers': ['localhost:17070', '10.0.3.1:17070'],
 
198
            },
 
199
        }}
 
200
        self.assertEqual(expected_db, db)
51
201
 
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.
55
 
        pass
 
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'):
 
210
                db = self.info.all()
 
211
        self.assertEqual({'environments': {}}, db)
 
212
 
 
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.
 
218
            os.rmdir(envs_dir)
 
219
            with self.patch_call(2, '', 'not implemented'):
 
220
                db = self.info.all()
 
221
        self.assertEqual({'environments': {}}, db)
56
222
 
57
223
 
58
224
class TestGetEnvDetails(unittest.TestCase):