summaryrefslogtreecommitdiff
path: root/tuskar_ui/infrastructure/overview/tests.py
blob: 20155c86790c874d2d63f3b61c7c2a05d61ce78e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# -*- coding: utf8 -*-
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import contextlib

from django.core import urlresolvers
from mock import patch, call  # noqa
from openstack_dashboard.test.test_data import utils

from tuskar_ui import api
from tuskar_ui.infrastructure.overview import forms
from tuskar_ui.infrastructure.overview import views
from tuskar_ui.test import helpers as test
from tuskar_ui.test.test_data import heat_data
from tuskar_ui.test.test_data import tuskar_data


INDEX_URL = urlresolvers.reverse(
    'horizon:infrastructure:overview:index')
DEPLOY_URL = urlresolvers.reverse(
    'horizon:infrastructure:overview:deploy_confirmation')
DELETE_URL = urlresolvers.reverse(
    'horizon:infrastructure:overview:undeploy_confirmation')
POST_DEPLOY_INIT_URL = urlresolvers.reverse(
    'horizon:infrastructure:overview:post_deploy_init')
TEST_DATA = utils.TestDataContainer()
heat_data.data(TEST_DATA)
tuskar_data.data(TEST_DATA)


@contextlib.contextmanager
def _mock_plan(**kwargs):
    plan = None

    params = {
        'spec_set': [
            'create',
            'delete',
            'get',
            'get_the_plan',
            'id',
            'uuid',
            'patch',
            'parameters',
            'role_list',
            'parameter_value',
            'get_role_by_name',
            'get_role_node_count',
            'list_generated_parameters',
            'make_generated_parameters',
            'parameter_list',
        ],
        'create.side_effect': lambda *args, **kwargs: plan,
        'delete.return_value': None,
        'get.side_effect': lambda *args, **kwargs: plan,
        'get_the_plan.side_effect': lambda *args, **kwargs: plan,
        'id': 'plan-1',
        'uuid': 'plan-1',
        'patch.side_effect': lambda *args, **kwargs: plan,
        'role_list': [],
        'parameter_list.return_value': [],
        'parameter_value.return_value': None,
        'get_role_by_name.side_effect': KeyError,
        'get_role_node_count.return_value': 0,
        'list_generated_parameters.return_value': {},
        'make_generated_parameters.return_value': {},
    }
    params.update(kwargs)
    with patch(
            'tuskar_ui.api.tuskar.Plan', **params) as Plan:
        plan = Plan
        yield Plan


class OverviewTests(test.BaseAdminViewTests):
    def test_index_stack_not_created(self):
        with contextlib.nested(
            _mock_plan(),
            patch('tuskar_ui.api.heat.Stack.list', return_value=[]),
            patch('tuskar_ui.api.node.Node.list', return_value=[]),
            patch('tuskar_ui.api.flavor.Flavor.list', return_value=[]),
        ):
            res = self.client.get(INDEX_URL)
            get_the_plan = api.tuskar.Plan.get_the_plan
            request = get_the_plan.call_args_list[0][0][0]
            self.assertListEqual(get_the_plan.call_args_list, [
                call(request),
                call(request),
                call(request),
            ])
            self.assertListEqual(api.heat.Stack.list.call_args_list, [
                call(request),
            ])
            self.assertListEqual(api.node.Node.list.call_args_list, [
                call(request, associated=False, maintenance=False),
            ])
            self.assertListEqual(api.flavor.Flavor.list.call_args_list, [
                call(request),
            ])
        self.assertTemplateUsed(
            res, 'infrastructure/overview/index.html')
        self.assertTemplateUsed(
            res, 'infrastructure/overview/role_nodes_edit.html')

    def test_index_stack_not_created_post(self):
        with contextlib.nested(
            _mock_plan(),
            patch('tuskar_ui.api.heat.Stack.list', return_value=[]),
            patch('tuskar_ui.api.node.Node.list', return_value=[]),
            patch('tuskar_ui.api.flavor.Flavor.list', return_value=[]),
        ) as (plan, _stack_list, _node_list, _flavor_list):
            data = {
                'role-1-count': 1,
                'role-2-count': 0,
                'role-3-count': 0,
                'role-4-count': 0,
            }
            res = self.client.post(INDEX_URL, data)
            self.assertNoFormErrors(res)
            self.assertRedirectsNoFollow(res, INDEX_URL)
            get_the_plan = api.tuskar.Plan.get_the_plan
            request = get_the_plan.call_args_list[0][0][0]
            self.assertListEqual(get_the_plan.call_args_list, [
                call(request),
            ])
            self.assertListEqual(
                api.tuskar.Plan.patch.call_args_list,
                [call(request, plan.id, {})],
            )

    def test_index_stack_deployed(self):
        stack = api.heat.Stack(TEST_DATA.heatclient_stacks.first())
        roles = [api.tuskar.Role(role)
                 for role in self.tuskarclient_roles.list()]

        with contextlib.nested(
                _mock_plan(**{'get_role_by_name.side_effect': None,
                              'get_role_by_name.return_value': roles[0]}),
                patch('tuskar_ui.api.heat.Stack.get_by_plan',
                      return_value=stack),
                patch('tuskar_ui.api.heat.Stack.events',
                      return_value=[]),
        ) as (Plan, stack_get_mock, stack_events_mock):
            res = self.client.get(INDEX_URL)
            request = Plan.get_the_plan.call_args_list[0][0][0]
            self.assertListEqual(
                Plan.get_the_plan.call_args_list,
                [
                    call(request),
                    call(request),
                    call(request),
                ])

        self.assertTemplateUsed(
            res, 'infrastructure/overview/index.html')
        self.assertTemplateUsed(
            res, 'infrastructure/overview/deployment_live.html')

    def test_index_stack_undeploy_in_progress(self):
        stack = api.heat.Stack(TEST_DATA.heatclient_stacks.first())

        with contextlib.nested(
                _mock_plan(),
                patch('tuskar_ui.api.heat.Stack.get_by_plan',
                      return_value=stack),
                patch('tuskar_ui.api.heat.Stack.is_deleting',
                      return_value=True),
                patch('tuskar_ui.api.heat.Stack.is_deployed',
                      return_value=False),
                patch('tuskar_ui.api.heat.Stack.resources',
                      return_value=[]),
                patch('tuskar_ui.api.heat.Stack.events',
                      return_value=[]),
        ):
            res = self.client.get(INDEX_URL)

        self.assertTemplateUsed(
            res, 'infrastructure/overview/index.html')
        self.assertTemplateUsed(
            res, 'infrastructure/overview/deployment_progress.html')

    def test_deploy_get(self):
        with _mock_plan():
            res = self.client.get(DEPLOY_URL)
        self.assertTemplateUsed(
            res, 'infrastructure/overview/deploy_confirmation.html')

    def test_delete_get(self):
        stack = api.heat.Stack(TEST_DATA.heatclient_stacks.first())

        with contextlib.nested(
                _mock_plan(),
                patch('tuskar_ui.api.heat.Stack.get_by_plan',
                      return_value=stack),
        ):
            res = self.client.get(DELETE_URL)
        self.assertTemplateUsed(
            res, 'infrastructure/overview/undeploy_confirmation.html')

    def test_delete_post(self):
        stack = api.heat.Stack(TEST_DATA.heatclient_stacks.first())

        with contextlib.nested(
                _mock_plan(),
                patch('tuskar_ui.api.heat.Stack.get_by_plan',
                      return_value=stack),
                patch('tuskar_ui.api.heat.Stack.delete',
                      return_value=None),
        ):
            res = self.client.post(DELETE_URL)
        self.assertRedirectsNoFollow(res, INDEX_URL)

    def test_post_deploy_init_get(self):
        stack = api.heat.Stack(TEST_DATA.heatclient_stacks.first())

        with contextlib.nested(
            _mock_plan(),
            patch('tuskar_ui.api.heat.Stack.get_by_plan',
                  return_value=stack),
        ):
            res = self.client.get(POST_DEPLOY_INIT_URL)
        self.assertEqual(res.context['form']['admin_email'].value(), '')
        self.assertTemplateUsed(
            res, 'infrastructure/overview/post_deploy_init.html')

    def test_post_deploy_init_post(self):
        stack = api.heat.Stack(TEST_DATA.heatclient_stacks.first())
        roles = [api.tuskar.Role(role)
                 for role in self.tuskarclient_roles.list()]

        data = {
            'admin_email': "example@example.org",
            'public_host': '',
            'region': 'regionOne',
        }

        with contextlib.nested(
            _mock_plan(**{'get_role_by_name.side_effect': None,
                          'get_role_by_name.return_value': roles[0]}),
            patch('tuskar_ui.api.heat.Stack.get_by_plan',
                  return_value=stack),
            patch('os_cloud_config.keystone.initialize',
                  return_value=None),
            patch('os_cloud_config.keystone.setup_endpoints',
                  return_value=None),
            patch('os_cloud_config.utils.clients.get_keystone_client',
                  return_value='keystone_client'),
        ) as (mock_plan, mock_get_by_plan, mock_initialize,
              mock_setup_endpoints, mock_get_keystone_client):
            res = self.client.post(POST_DEPLOY_INIT_URL, data)

        self.assertNoFormErrors(res)
        self.assertEqual(res.status_code, 302)
        self.assertRedirectsNoFollow(res, INDEX_URL)

        mock_initialize.assert_called_once_with(
            '192.0.2.23', None, 'example@example.org', None, ssl=None,
            region='regionOne', user='heat-admin', public=None,
            pki_setup=False)
        mock_setup_endpoints.assert_called_once_with(
            {'nova': {'password': None},
             'heat': {'password': None},
             'ceilometer': {'password': None},
             'ec2': {'password': None},
             "horizon": {
                'port': '80',
                'path': '/',
                'admin_path': '/admin'},
             'cinder': {'password': None},
             'cinderv2': {'password': None},
             'glance': {'password': None},
             'swift': {'password': None,
                       'path': '/v1/AUTH_%(tenant_id)s',
                       'admin_path': '/v1'},
             'novav3': {'password': None},
             'neutron': {'password': None}},
            os_auth_url=stack.keystone_auth_url,
            client='keystone_client',
            region='regionOne',
            public_host='')
        mock_get_keystone_client.assert_called_once_with(
            'admin', None, 'admin', stack.keystone_auth_url)

    def test_get_role_data(self):
        plan = api.tuskar.Plan(self.tuskarclient_plans.first())
        stack = api.heat.Stack(self.heatclient_stacks.first())
        role = api.tuskar.Role(self.tuskarclient_roles.first())
        stack.resources = lambda *args, **kwargs: []
        ret = views._get_role_data(plan, stack, None, role)
        self.assertEqual(ret, {
            'deployed_node_count': 0,
            'deploying_node_count': 0,
            'error_node_count': 0,
            'field': '',
            'finished': False,
            'icon': 'fa-exclamation',
            'id': 'role-1',
            'name': 'Controller',
            'planned_node_count': 1,
            'role': role,
            'status': 'warning',
            'total_node_count': 0,
            'waiting_node_count': 0,
        })

    def test_validate_plan_empty(self):
        with (
            _mock_plan()
        ) as plan, (
            patch('tuskar_ui.api.node.Node.list', return_value=[])
        ), (
            patch('tuskar_ui.api.flavor.Flavor.list', return_value=[])
        ):
            ret = forms.validate_plan(None, plan)
        for m in ret:
            m['text'] = unicode(m['text'])
        self.assertEqual(ret, [
            {
                'is_critical': True,
                'text': u'Define Flavors.',
                'status': 'pending',
                'classes': 'fa-square-o text-info',
            }, {
                'is_critical': True,
                'text': u'Register Nodes.',
                'status': 'pending',
                'classes': 'fa-square-o text-info',
            }, {
                'status': 'ok',
                'text': u'Configure Roles.',
                'classes': 'fa-check-square-o text-success',
            }, {
                'status': 'ok',
                'text': u'Global Service Configuration.',
                'classes': 'fa-check-square-o text-success',
            }, {
                'status': 'pending',
                'text': u'Assign roles.',
                'classes': 'fa-square-o text-info',
            }, {
                'is_critical': True,
                'text': u'Controller Role Needed.',
                'status': 'error',
                'indent': 1,
                'classes': 'fa-exclamation-circle text-danger',
            }, {
                'is_critical': True,
                'text': u'Compute Role Needed.',
                'status': 'error',
                'indent': 1,
                'classes': 'fa-exclamation-circle text-danger',
            },
        ])