summaryrefslogtreecommitdiff
path: root/openstack_dashboard/dashboards/project/floating_ips/tests.py
blob: 12a259a9a671fe886e0bf1e2549ab6f5e236f228 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# Copyright 2012 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2012 Nebula, Inc.
# Copyright (c) 2012 X.commerce, a business unit of eBay Inc.
#
#    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.

from unittest import mock

from django.urls import reverse
from django.utils.http import urlencode

from openstack_dashboard import api
from openstack_dashboard.test import helpers as test
from openstack_dashboard.usage import quotas

from horizon.workflows import views


INDEX_URL = reverse('horizon:project:floating_ips:index')
NAMESPACE = "horizon:project:floating_ips"


class FloatingIpViewTests(test.TestCase):

    @test.create_mocks({api.neutron: ('floating_ip_target_list',
                                      'tenant_floating_ip_list')})
    def test_associate(self):
        self.mock_floating_ip_target_list.return_value = \
            self._get_fip_targets()
        self.mock_tenant_floating_ip_list.return_value = \
            self.floating_ips.list()

        url = reverse('%s:associate' % NAMESPACE)
        res = self.client.get(url)
        self.assertTemplateUsed(res, views.WorkflowView.template_name)
        workflow = res.context['workflow']
        choices = dict(workflow.steps[0].action.fields['ip_id'].choices)
        # Verify that our "associated" floating IP isn't in the choices list.
        self.assertNotIn(self.floating_ips.first(), choices)

        self.mock_floating_ip_target_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_tenant_floating_ip_list.assert_called_once_with(
            test.IsHttpRequest())

    @test.create_mocks({api.neutron: ('floating_ip_target_list_by_instance',
                                      'tenant_floating_ip_list')})
    def test_associate_with_instance_id(self):
        targets = self._get_fip_targets()
        target = targets[0]
        self.mock_floating_ip_target_list_by_instance.return_value = [target]
        self.mock_tenant_floating_ip_list.return_value = \
            self.floating_ips.list()

        base_url = reverse('%s:associate' % NAMESPACE)
        params = urlencode({'instance_id': target.instance_id})
        url = '?'.join([base_url, params])
        res = self.client.get(url)
        self.assertTemplateUsed(res, views.WorkflowView.template_name)
        workflow = res.context['workflow']
        choices = dict(workflow.steps[0].action.fields['ip_id'].choices)
        # Verify that our "associated" floating IP isn't in the choices list.
        self.assertNotIn(self.floating_ips.first(), choices)

        self.mock_floating_ip_target_list_by_instance.assert_called_once_with(
            test.IsHttpRequest(), target.instance_id)
        self.mock_tenant_floating_ip_list.assert_called_once_with(
            test.IsHttpRequest())

    def _get_compute_ports(self):
        return [p for p in self.ports.list()
                if not p.device_owner.startswith('network:')]

    def _get_fip_targets(self):
        server_dict = dict((s.id, s.name) for s in self.servers.list())
        targets = []
        for p in self._get_compute_ports():
            for ip in p.fixed_ips:
                targets.append(api.neutron.FloatingIpTarget(
                    p, ip['ip_address'], server_dict[p.device_id]))
        return targets

    @staticmethod
    def _get_target_id(port):
        return '%s_%s' % (port.id, port.fixed_ips[0]['ip_address'])

    @test.create_mocks({api.neutron: ('floating_ip_target_list',
                                      'tenant_floating_ip_list')})
    def test_associate_with_port_id(self):
        compute_port = self._get_compute_ports()[0]
        associated_fips = [fip.id for fip in self.floating_ips.list()
                           if fip.port_id]

        self.mock_floating_ip_target_list.return_value = \
            self._get_fip_targets()
        self.mock_tenant_floating_ip_list.return_value = \
            self.floating_ips.list()

        base_url = reverse('%s:associate' % NAMESPACE)
        params = urlencode({'port_id': compute_port.id})
        url = '?'.join([base_url, params])
        res = self.client.get(url)
        self.assertTemplateUsed(res, views.WorkflowView.template_name)
        workflow = res.context['workflow']
        choices = dict(workflow.steps[0].action.fields['ip_id'].choices)
        # Verify that our "associated" floating IP isn't in the choices list.
        self.assertFalse(set(associated_fips) & set(choices.keys()))

        self.mock_floating_ip_target_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_tenant_floating_ip_list.assert_called_once_with(
            test.IsHttpRequest())

    @test.create_mocks({api.neutron: ('floating_ip_associate',
                                      'floating_ip_target_list',
                                      'tenant_floating_ip_list')})
    def test_associate_post(self):
        floating_ip = [fip for fip in self.floating_ips.list()
                       if not fip.port_id][0]
        compute_port = self._get_compute_ports()[0]
        port_target_id = self._get_target_id(compute_port)

        self.mock_tenant_floating_ip_list.return_value = \
            self.floating_ips.list()
        self.mock_floating_ip_target_list.return_value = \
            self._get_fip_targets()
        self.mock_floating_ip_associate.return_value = None

        form_data = {'port_id': port_target_id,
                     'ip_id': floating_ip.id}
        url = reverse('%s:associate' % NAMESPACE)
        res = self.client.post(url, form_data)
        self.assertRedirectsNoFollow(res, INDEX_URL)

        self.mock_tenant_floating_ip_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_target_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_associate.assert_called_once_with(
            test.IsHttpRequest(), floating_ip.id, port_target_id)

    @test.create_mocks({api.neutron: ('floating_ip_associate',
                                      'floating_ip_target_list',
                                      'tenant_floating_ip_list')})
    def test_associate_post_with_redirect(self):
        floating_ip = [fip for fip in self.floating_ips.list()
                       if not fip.port_id][0]
        compute_port = self._get_compute_ports()[0]
        port_target_id = self._get_target_id(compute_port)

        self.mock_tenant_floating_ip_list.return_value = \
            self.floating_ips.list()
        self.mock_floating_ip_target_list.return_value = \
            self._get_fip_targets()
        self.mock_floating_ip_associate.return_value = None

        next = reverse("horizon:project:instances:index")
        form_data = {'port_id': port_target_id,
                     'next': next,
                     'ip_id': floating_ip.id}
        url = reverse('%s:associate' % NAMESPACE)
        res = self.client.post(url, form_data)
        self.assertRedirectsNoFollow(res, next)

        self.mock_tenant_floating_ip_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_target_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_associate.assert_called_once_with(
            test.IsHttpRequest(), floating_ip.id, port_target_id)

    @test.create_mocks({api.neutron: ('floating_ip_associate',
                                      'floating_ip_target_list',
                                      'tenant_floating_ip_list')})
    def test_associate_post_with_exception(self):
        floating_ip = [fip for fip in self.floating_ips.list()
                       if not fip.port_id][0]
        compute_port = self._get_compute_ports()[0]
        port_target_id = self._get_target_id(compute_port)

        self.mock_tenant_floating_ip_list.return_value = \
            self.floating_ips.list()
        self.mock_floating_ip_target_list.return_value = \
            self._get_fip_targets()
        self.mock_floating_ip_associate.side_effect = self.exceptions.nova

        form_data = {'port_id': port_target_id,
                     'ip_id': floating_ip.id}
        url = reverse('%s:associate' % NAMESPACE)
        res = self.client.post(url, form_data)
        self.assertRedirectsNoFollow(res, INDEX_URL)

        self.mock_tenant_floating_ip_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_target_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_associate.assert_called_once_with(
            test.IsHttpRequest(), floating_ip.id, port_target_id)

    @test.create_mocks({api.nova: ('server_list',),
                        api.neutron: ('floating_ip_disassociate',
                                      'floating_ip_pools_list',
                                      'is_extension_supported',
                                      'tenant_floating_ip_list')})
    def test_disassociate_post(self):
        floating_ip = self.floating_ips.first()

        self.mock_is_extension_supported.return_value = False
        self.mock_server_list.return_value = [self.servers.list(), False]
        self.mock_tenant_floating_ip_list.return_value = \
            self.floating_ips.list()
        self.mock_floating_ip_pools_list.return_value = self.pools.list()
        self.mock_floating_ip_disassociate.return_value = None

        action = "floating_ips__disassociate__%s" % floating_ip.id
        res = self.client.post(INDEX_URL, {"action": action})
        self.assertMessageCount(success=1)
        self.assertRedirectsNoFollow(res, INDEX_URL)

        self.mock_server_list.assert_called_once_with(test.IsHttpRequest(),
                                                      detailed=False)
        self.mock_tenant_floating_ip_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_pools_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_disassociate.assert_called_once_with(
            test.IsHttpRequest(), floating_ip.id)
        self.mock_is_extension_supported.assert_called_once_with(
            test.IsHttpRequest(), 'dns-integration')

    @test.create_mocks({api.nova: ('server_list',),
                        api.neutron: ('floating_ip_disassociate',
                                      'floating_ip_pools_list',
                                      'is_extension_supported',
                                      'tenant_floating_ip_list')})
    def test_disassociate_post_with_exception(self):
        floating_ip = self.floating_ips.first()

        self.mock_is_extension_supported.return_value = False
        self.mock_server_list.return_value = [self.servers.list(), False]
        self.mock_tenant_floating_ip_list.return_value = \
            self.floating_ips.list()
        self.mock_floating_ip_pools_list.return_value = self.pools.list()
        self.mock_floating_ip_disassociate.side_effect = self.exceptions.nova

        action = "floating_ips__disassociate__%s" % floating_ip.id
        res = self.client.post(INDEX_URL, {"action": action})
        self.assertRedirectsNoFollow(res, INDEX_URL)

        self.mock_server_list.assert_called_once_with(test.IsHttpRequest(),
                                                      detailed=False)
        self.mock_tenant_floating_ip_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_pools_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_disassociate.assert_called_once_with(
            test.IsHttpRequest(), floating_ip.id)
        self.mock_is_extension_supported.assert_called_once_with(
            test.IsHttpRequest(), 'dns-integration')

    @test.create_mocks({api.neutron: ('tenant_floating_ip_list',
                                      'is_extension_supported',
                                      'floating_ip_pools_list'),
                        api.nova: ('server_list',),
                        quotas: ('tenant_quota_usages',)})
    def test_allocate_button_attributes(self):
        floating_ips = self.floating_ips.list()
        floating_pools = self.pools.list()
        quota_data = self.neutron_quota_usages.first()

        self.mock_is_extension_supported.return_value = False
        self.mock_tenant_floating_ip_list.return_value = floating_ips
        self.mock_floating_ip_pools_list.return_value = floating_pools
        self.mock_server_list.return_value = [self.servers.list(), False]
        self.mock_tenant_quota_usages.return_value = quota_data

        res = self.client.get(INDEX_URL)

        allocate_action = self.getAndAssertTableAction(res, 'floating_ips',
                                                       'allocate')
        self.assertEqual(set(['ajax-modal']), set(allocate_action.classes))
        self.assertEqual('Allocate IP To Project', allocate_action.verbose_name)
        self.assertIsNone(allocate_action.policy_rules)

        url = 'horizon:project:floating_ips:allocate'
        self.assertEqual(url, allocate_action.url)

        self.mock_tenant_floating_ip_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_pools_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_server_list.assert_called_once_with(test.IsHttpRequest(),
                                                      detailed=False)
        self.assert_mock_multiple_calls_with_same_arguments(
            self.mock_tenant_quota_usages, 3,
            mock.call(test.IsHttpRequest(), targets=('floatingip', )))
        self.mock_is_extension_supported.assert_called_once_with(
            test.IsHttpRequest(), 'dns-integration',
        )

    @test.create_mocks({api.neutron: ('tenant_floating_ip_list',
                                      'is_extension_supported',
                                      'floating_ip_pools_list'),
                        api.nova: ('server_list',),
                        quotas: ('tenant_quota_usages',)})
    def test_allocate_button_disabled_when_quota_exceeded(self):
        floating_ips = self.floating_ips.list()
        floating_pools = self.pools.list()
        quota_data = self.neutron_quota_usages.first()
        quota_data['floatingip']['available'] = 0

        self.mock_is_extension_supported.return_value = False
        self.mock_tenant_floating_ip_list.return_value = floating_ips
        self.mock_floating_ip_pools_list.return_value = floating_pools
        self.mock_server_list.return_value = [self.servers.list(), False]
        self.mock_tenant_quota_usages.return_value = quota_data

        res = self.client.get(INDEX_URL)

        allocate_action = self.getAndAssertTableAction(res, 'floating_ips',
                                                       'allocate')
        self.assertIn('disabled', allocate_action.classes,
                      'The create button should be disabled')
        self.assertEqual('Allocate IP To Project (Quota exceeded)',
                         allocate_action.verbose_name)

        self.mock_tenant_floating_ip_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_pools_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_server_list.assert_called_once_with(test.IsHttpRequest(),
                                                      detailed=False)
        self.assert_mock_multiple_calls_with_same_arguments(
            self.mock_tenant_quota_usages, 3,
            mock.call(test.IsHttpRequest(), targets=('floatingip', )))
        self.mock_is_extension_supported.assert_called_once_with(
            test.IsHttpRequest(), 'dns-integration',
        )

    @test.create_mocks({api.neutron: ('floating_ip_pools_list',
                                      'tenant_floating_ip_list',
                                      'is_extension_supported',
                                      'is_router_enabled',
                                      'tenant_quota_get'),
                        api.base: ('is_service_enabled',)})
    @test.update_settings(OPENSTACK_NEUTRON_NETWORK={'enable_quotas': True})
    def test_correct_quotas_displayed(self):
        self.mock_is_service_enabled.return_value = True
        self.mock_is_extension_supported.side_effect = [False, True, False]
        self.mock_is_router_enabled.return_value = True
        self.mock_tenant_quota_get.return_value = self.neutron_quotas.first()
        self.mock_tenant_floating_ip_list.return_value = \
            self.floating_ips.list()
        self.mock_floating_ip_pools_list.return_value = self.pools.list()

        url = reverse('%s:allocate' % NAMESPACE)
        res = self.client.get(url)
        self.assertEqual(res.context['usages']['floatingip']['quota'],
                         self.neutron_quotas.first().get('floatingip').limit)

        self.mock_is_service_enabled.assert_called_once_with(
            test.IsHttpRequest(), 'network')
        self.assertEqual(3, self.mock_is_extension_supported.call_count)
        self.mock_is_extension_supported.assert_has_calls([
            mock.call(test.IsHttpRequest(), 'dns-integration'),
            mock.call(test.IsHttpRequest(), 'quotas'),
            mock.call(test.IsHttpRequest(), 'quota_details'),
        ])
        self.mock_is_router_enabled.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_tenant_quota_get.assert_called_once_with(
            test.IsHttpRequest(), self.tenant.id)
        self.mock_tenant_floating_ip_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_floating_ip_pools_list.assert_called_once_with(
            test.IsHttpRequest())