summaryrefslogtreecommitdiff
path: root/openstack_dashboard/dashboards/project/backups/tests.py
blob: a3616fe0b5b32e595c8016728fecce75e18c6c39 (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# 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 django.conf import settings
from django.test.utils import override_settings
from django.urls import reverse
from django.utils.http import urlencode
from django.utils.http import urlunquote

from openstack_dashboard import api
from openstack_dashboard.dashboards.project.backups \
    import tables as backup_tables
from openstack_dashboard.test import helpers as test


INDEX_URL = reverse('horizon:project:backups:index')


class VolumeBackupsViewTests(test.TestCase):

    @test.create_mocks({api.cinder: ('volume_list', 'volume_snapshot_list',
                                     'volume_backup_list_paged_with_page_menu')
                        })
    def _test_backups_index_paginated(self, page_number, backups,
                                      url, page_size, total_of_entries,
                                      number_of_pages, has_prev, has_more):
        self.mock_volume_backup_list_paged_with_page_menu.return_value = [
            backups, page_size, total_of_entries, number_of_pages]
        self.mock_volume_list.return_value = self.cinder_volumes.list()
        self.mock_volume_snapshot_list.return_value \
            = self.cinder_volume_snapshots.list()

        res = self.client.get(urlunquote(url))

        self.assertEqual(res.status_code, 200)
        self.assertTemplateUsed(res, 'horizon/common/_data_table_view.html')
        self.assertEqual(has_more,
                         res.context_data['view'].has_more_data(None))
        self.assertEqual(has_prev,
                         res.context_data['view'].has_prev_data(None))
        self.assertEqual(
            page_number, res.context_data['view'].current_page(None))
        self.assertEqual(
            number_of_pages, res.context_data['view'].number_of_pages(None))
        self.mock_volume_backup_list_paged_with_page_menu.\
            assert_called_once_with(test.IsHttpRequest(),
                                    page_number=page_number)
        self.mock_volume_list.assert_called_once_with(test.IsHttpRequest())
        self.mock_volume_snapshot_list.assert_called_once_with(
            test.IsHttpRequest())
        return res

    @override_settings(API_RESULT_PAGE_SIZE=1)
    def test_backups_index_paginated(self):
        backups = self.cinder_volume_backups.list()
        expected_snapshosts = self.cinder_volume_snapshots.list()
        size = settings.API_RESULT_PAGE_SIZE
        base_url = INDEX_URL
        number_of_pages = len(backups)
        pag = backup_tables.BackupsTable._meta.pagination_param
        page_number = 1

        # get first page
        expected_backups = backups[:size]
        res = self._test_backups_index_paginated(
            page_number=page_number, backups=expected_backups, url=base_url,
            has_more=True, has_prev=False, page_size=size,
            number_of_pages=number_of_pages, total_of_entries=number_of_pages)
        result = res.context['volume_backups_table'].data
        self.assertCountEqual(result, expected_backups)

        # get second page
        expected_backups = backups[size:2 * size]
        page_number = 2
        url = base_url + "?%s=%s" % (pag, page_number)
        res = self._test_backups_index_paginated(
            page_number=page_number, backups=expected_backups, url=url,
            has_more=True, has_prev=True, page_size=size,
            number_of_pages=number_of_pages, total_of_entries=number_of_pages)
        result = res.context['volume_backups_table'].data
        self.assertCountEqual(result, expected_backups)
        self.assertEqual(result[0].snapshot.id, expected_snapshosts[1].id)
        # get last page
        expected_backups = backups[-size:]
        page_number = 3
        url = base_url + "?%s=%s" % (pag, page_number)
        res = self._test_backups_index_paginated(
            page_number=page_number, backups=expected_backups, url=url,
            has_more=False, has_prev=True, page_size=size,
            number_of_pages=number_of_pages, total_of_entries=number_of_pages)
        result = res.context['volume_backups_table'].data
        self.assertCountEqual(result, expected_backups)

    @override_settings(API_RESULT_PAGE_SIZE=1)
    def test_backups_index_paginated_prev_page(self):
        backups = self.cinder_volume_backups.list()
        size = settings.API_RESULT_PAGE_SIZE
        number_of_pages = len(backups)
        base_url = INDEX_URL
        pag = backup_tables.BackupsTable._meta.pagination_param

        # prev from some page
        expected_backups = backups[size:2 * size]
        page_number = 2
        url = base_url + "?%s=%s" % (pag, page_number)
        res = self._test_backups_index_paginated(
            page_number=page_number, backups=expected_backups, url=url,
            has_more=True, has_prev=True, page_size=size,
            number_of_pages=number_of_pages, total_of_entries=number_of_pages)
        result = res.context['volume_backups_table'].data
        self.assertCountEqual(result, expected_backups)

        # back to first page
        expected_backups = backups[:size]
        page_number = 1
        url = base_url + "?%s=%s" % (pag, page_number)
        res = self._test_backups_index_paginated(
            page_number=page_number, backups=expected_backups, url=url,
            has_more=True, has_prev=False, page_size=size,
            number_of_pages=number_of_pages, total_of_entries=number_of_pages)
        result = res.context['volume_backups_table'].data
        self.assertCountEqual(result, expected_backups)

    @test.create_mocks({api.cinder: ('volume_backup_create',
                                     'volume_snapshot_list',
                                     'volume_get')})
    def test_create_backup_available(self):
        volume = self.cinder_volumes.first()
        backup = self.cinder_volume_backups.first()

        self.mock_volume_get.return_value = volume
        self.mock_volume_backup_create.return_value = backup

        formData = {'method': 'CreateBackupForm',
                    'tenant_id': self.tenant.id,
                    'volume_id': volume.id,
                    'container_name': backup.container_name,
                    'name': backup.name,
                    'description': backup.description}
        url = reverse('horizon:project:volumes:create_backup',
                      args=[volume.id])
        res = self.client.post(url, formData)

        self.assertNoFormErrors(res)
        self.assertMessageCount(error=0, warning=0)
        self.assertRedirectsNoFollow(res, INDEX_URL)
        self.mock_volume_snapshot_list.assert_called_once_with(
            test.IsHttpRequest(),
            search_opts={'volume_id': volume.id})
        self.mock_volume_get.assert_called_once_with(test.IsHttpRequest(),
                                                     volume.id)
        self.mock_volume_backup_create.assert_called_once_with(
            test.IsHttpRequest(),
            volume.id,
            backup.container_name,
            backup.name,
            backup.description,
            force=False,
            snapshot_id=None)

    @test.create_mocks(
        {api.cinder: ('volume_backup_create', 'volume_snapshot_get',
                      'volume_get')})
    def test_create_backup_from_snapshot_table(self):
        backup = self.cinder_volume_backups.list()[1]
        volume = self.cinder_volumes.list()[4]
        snapshot = self.cinder_volume_snapshots.list()[1]
        self.mock_volume_backup_create.return_value = backup
        self.mock_volume_get.return_value = volume
        self.mock_volume_snapshot_get.return_value = snapshot
        formData = {'method': 'CreateBackupForm',
                    'tenant_id': self.tenant.id,
                    'volume_id': volume.id,
                    'container_name': backup.container_name,
                    'name': backup.name,
                    'snapshot_id': backup.snapshot_id,
                    'description': backup.description}
        url = reverse('horizon:project:volumes:create_snapshot_backup',
                      args=[backup.volume_id, backup.snapshot_id])
        res = self.client.post(url, formData)

        self.assertNoFormErrors(res)
        self.assertMessageCount(error=0, warning=0)
        self.assertRedirectsNoFollow(res, INDEX_URL)

        self.mock_volume_get.assert_called_once_with(test.IsHttpRequest(),
                                                     backup.volume_id)
        self.mock_volume_backup_create.assert_called_once_with(
            test.IsHttpRequest(),
            backup.volume_id,
            backup.container_name,
            backup.name,
            backup.description,
            force=False,
            snapshot_id=backup.snapshot_id)

    @test.create_mocks(
        {api.cinder: ('volume_backup_create',
                      'volume_snapshot_list',
                      'volume_get')})
    def test_create_backup_from_snapshot_volume_table(self):
        volume = self.cinder_volumes.list()[4]
        backup = self.cinder_volume_backups.list()[1]
        snapshots = self.cinder_volume_snapshots.list()[1:3]
        self.mock_volume_backup_create.return_value = backup
        self.mock_volume_get.return_value = volume
        self.mock_volume_snapshot_list.return_value = snapshots
        formData = {'method': 'CreateBackupForm',
                    'tenant_id': self.tenant.id,
                    'volume_id': volume.id,
                    'container_name': backup.container_name,
                    'name': backup.name,
                    'snapshot_id': snapshots[0].id,
                    'description': backup.description}
        url = reverse('horizon:project:volumes:create_backup',
                      args=[backup.volume_id])

        res = self.client.post(url, formData)
        self.assertNoFormErrors(res)
        self.mock_volume_snapshot_list.assert_called_once_with(
            test.IsHttpRequest(),
            search_opts={'volume_id': volume.id})
        self.assertMessageCount(error=0, warning=0)
        self.assertRedirectsNoFollow(res, INDEX_URL)

        self.mock_volume_get.assert_called_once_with(test.IsHttpRequest(),
                                                     backup.volume_id)
        self.mock_volume_backup_create.assert_called_once_with(
            test.IsHttpRequest(),
            backup.volume_id,
            backup.container_name,
            backup.name,
            backup.description,
            force=False,
            snapshot_id=backup.snapshot_id)

    @test.create_mocks(
        {api.cinder: ('volume_backup_create', 'volume_snapshot_list',
                      'volume_get')})
    def test_create_backup_in_use(self):
        # The third volume in the cinder test volume data is in-use
        volume = self.cinder_volumes.list()[2]

        backup = self.cinder_volume_backups.first()
        snapshots = []
        self.mock_volume_get.return_value = volume
        self.mock_volume_backup_create.return_value = backup
        self.mock_volume_snapshot_list.return_value = snapshots
        formData = {'method': 'CreateBackupForm',
                    'tenant_id': self.tenant.id,
                    'volume_id': volume.id,
                    'container_name': backup.container_name,
                    'name': backup.name,
                    'description': backup.description}
        url = reverse('horizon:project:volumes:create_backup',
                      args=[volume.id])

        res = self.client.post(url, formData)
        self.mock_volume_snapshot_list.assert_called_once_with(
            test.IsHttpRequest(),
            search_opts={'volume_id': volume.id})
        self.assertNoFormErrors(res)
        self.assertMessageCount(error=0, warning=0)
        self.assertRedirectsNoFollow(res, INDEX_URL)
        self.mock_volume_get.assert_called_once_with(test.IsHttpRequest(),
                                                     volume.id)
        self.mock_volume_backup_create.assert_called_once_with(
            test.IsHttpRequest(),
            volume.id,
            backup.container_name,
            backup.name,
            backup.description,
            force=True,
            snapshot_id=None)

    @test.create_mocks({api.cinder: ('volume_list',
                                     'volume_snapshot_list',
                                     'volume_backup_list_paged_with_page_menu',
                                     'volume_backup_delete')})
    def test_delete_volume_backup(self):
        vol_backups = self.cinder_volume_backups.list()
        volumes = self.cinder_volumes.list()
        backup = self.cinder_volume_backups.first()
        snapshots = self.cinder_volume_snapshots.list()
        page_number = 1
        page_size = 1
        total_of_entries = 1
        number_of_pages = 1

        self.mock_volume_backup_list_paged_with_page_menu.return_value = [
            vol_backups, page_size, total_of_entries, number_of_pages]
        self.mock_volume_list.return_value = volumes
        self.mock_volume_backup_delete.return_value = None
        self.mock_volume_snapshot_list.return_value = snapshots
        formData = {'action':
                    'volume_backups__delete__%s' % backup.id}
        res = self.client.post(INDEX_URL, formData)

        self.assertRedirectsNoFollow(res, INDEX_URL)
        self.assertMessageCount(success=1)
        self.mock_volume_backup_list_paged_with_page_menu.\
            assert_called_once_with(test.IsHttpRequest(),
                                    page_number=page_number)
        self.mock_volume_list.assert_called_once_with(test.IsHttpRequest())
        self.mock_volume_snapshot_list.assert_called_once_with(
            test.IsHttpRequest())
        self.mock_volume_backup_delete.assert_called_once_with(
            test.IsHttpRequest(), backup.id)

    @test.create_mocks({api.cinder: ('volume_backup_get',
                                     'volume_get')})
    def test_volume_backup_detail_get(self):
        backup = self.cinder_volume_backups.first()
        volume = self.cinder_volumes.get(id=backup.volume_id)

        self.mock_volume_backup_get.return_value = backup
        self.mock_volume_get.return_value = volume

        url = reverse('horizon:project:backups:detail',
                      args=[backup.id])
        res = self.client.get(url)

        self.assertTemplateUsed(res, 'horizon/common/_detail.html')
        self.assertEqual(res.context['backup'].id, backup.id)
        self.mock_volume_backup_get.assert_called_once_with(
            test.IsHttpRequest(), backup.id)
        self.mock_volume_get.assert_called_once_with(
            test.IsHttpRequest(), backup.volume_id)

    @test.create_mocks({api.cinder: ('volume_backup_get',
                                     'volume_snapshot_get',
                                     'volume_get')})
    def test_volume_backup_detail_get_with_snapshot(self):
        backup = self.cinder_volume_backups.list()[1]
        volume = self.cinder_volumes.get(id=backup.volume_id)

        self.mock_volume_backup_get.return_value = backup
        self.mock_volume_get.return_value = volume
        self.mock_volume_snapshot_get.return_value \
            = self.cinder_volume_snapshots.list()[1]
        url = reverse('horizon:project:backups:detail',
                      args=[backup.id])
        res = self.client.get(url)

        self.assertTemplateUsed(res, 'horizon/common/_detail.html')
        self.assertEqual(res.context['backup'].id, backup.id)
        self.assertEqual(res.context['snapshot'].id, backup.snapshot_id)
        self.mock_volume_backup_get.assert_called_once_with(
            test.IsHttpRequest(), backup.id)
        self.mock_volume_get.assert_called_once_with(
            test.IsHttpRequest(), backup.volume_id)
        self.mock_volume_snapshot_get.assert_called_once_with(
            test.IsHttpRequest(), backup.snapshot_id)

    @test.create_mocks({api.cinder: ('volume_backup_get',)})
    def test_volume_backup_detail_get_with_exception(self):
        # Test to verify redirect if get volume backup fails
        backup = self.cinder_volume_backups.first()

        self.mock_volume_backup_get.side_effect = self.exceptions.cinder

        url = reverse('horizon:project:backups:detail',
                      args=[backup.id])
        res = self.client.get(url)

        self.assertNoFormErrors(res)
        self.assertMessageCount(error=1)
        self.assertRedirectsNoFollow(res, INDEX_URL)
        self.mock_volume_backup_get.assert_called_once_with(
            test.IsHttpRequest(), backup.id)

    @test.create_mocks({api.cinder: ('volume_backup_get',
                                     'volume_get')})
    def test_volume_backup_detail_with_missing_volume(self):
        # Test to check page still loads even if volume is deleted
        backup = self.cinder_volume_backups.first()

        self.mock_volume_backup_get.return_value = backup
        self.mock_volume_get.side_effect = self.exceptions.cinder

        url = reverse('horizon:project:backups:detail',
                      args=[backup.id])
        res = self.client.get(url)

        self.assertTemplateUsed(res, 'horizon/common/_detail.html')
        self.assertEqual(res.context['backup'].id, backup.id)
        self.mock_volume_backup_get.assert_called_once_with(
            test.IsHttpRequest(), backup.id)
        self.mock_volume_get.assert_called_once_with(
            test.IsHttpRequest(), backup.volume_id)

    @test.create_mocks({api.cinder: ('volume_list',
                                     'volume_backup_restore')})
    def test_restore_backup(self):
        mock_backup = self.cinder_volume_backups.first()
        volumes = self.cinder_volumes.list()
        expected_volumes = [vol for vol in volumes
                            if vol.status == 'available']

        self.mock_volume_list.return_value = expected_volumes
        self.mock_volume_backup_restore.return_value = mock_backup

        formData = {'method': 'RestoreBackupForm',
                    'backup_id': mock_backup.id,
                    'backup_name': mock_backup.name,
                    'volume_id': mock_backup.volume_id}
        url = reverse('horizon:project:backups:restore',
                      args=[mock_backup.id])
        url += '?%s' % urlencode({'backup_name': mock_backup.name,
                                  'volume_id': mock_backup.volume_id})
        res = self.client.post(url, formData)

        self.assertNoFormErrors(res)
        self.assertMessageCount(info=1)
        self.assertRedirectsNoFollow(res,
                                     reverse('horizon:project:volumes:index'))
        self.mock_volume_list.assert_called_once_with(test.IsHttpRequest(),
                                                      {'status': 'available'})
        self.mock_volume_backup_restore.assert_called_once_with(
            test.IsHttpRequest(), mock_backup.id, mock_backup.volume_id)