summaryrefslogtreecommitdiff
path: root/openstack_dashboard/dashboards/project/images/tests.py
blob: 0de38198edf5e413da8ec821408ece631a5f6b5c (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# 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 2012 OpenStack Foundation
#
#    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 os
from socket import timeout as socket_timeout
import tempfile
import unittest
from unittest import mock

from django.urls import reverse

from horizon import exceptions

from openstack_dashboard import api
from openstack_dashboard.dashboards.project.images import utils
from openstack_dashboard.test import helpers as test


INDEX_TEMPLATE = 'horizon/common/_data_table_view.html'
INDEX_URL = reverse('horizon:project:images:index')
CREATE_URL = reverse('horizon:project:images:images:create')


class BaseImagesTestCase(test.TestCase):
    def setUp(self):
        super().setUp()
        self.patcher = mock.patch.object(api.glance, 'image_list_detailed')
        self.mock_image_list = self.patcher.start()


class ImagesAndSnapshotsTests(BaseImagesTestCase):
    def test_index(self):
        images = self.images.list()
        self.mock_image_list.return_value = [images, False, False]

        res = self.client.get(INDEX_URL)
        self.assertTemplateUsed(res, INDEX_TEMPLATE)
        self.assertContains(res, 'help_text="Deleted images'
                                 ' are not recoverable."')
        self.assertIn('images_table', res.context)
        images_table = res.context['images_table']
        images = images_table.data

        self.assertEqual(len(images), 10)
        row_actions = images_table.get_row_actions(images[0])
        self.assertEqual(len(row_actions), 5)
        row_actions = images_table.get_row_actions(images[1])
        self.assertEqual(len(row_actions), 3)
        self.assertNotIn('delete_image',
                         [a.name for a in row_actions])
        row_actions = images_table.get_row_actions(images[2])
        self.assertEqual(len(row_actions), 4)

        self.mock_image_list.assert_called_once_with(test.IsHttpRequest(),
                                                     marker=None,
                                                     paginate=True,
                                                     sort_dir='asc',
                                                     sort_key='name',
                                                     reversed_order=False)

    def test_index_no_images(self):
        self.mock_image_list.return_value = [(), False, False]

        res = self.client.get(INDEX_URL)

        self.mock_image_list.assert_called_once_with(test.IsHttpRequest(),
                                                     marker=None,
                                                     paginate=True,
                                                     sort_dir='asc',
                                                     sort_key='name',
                                                     reversed_order=False)
        self.assertTemplateUsed(res, INDEX_TEMPLATE)
        self.assertContains(res, 'No items to display')

    def test_index_error(self):
        self.mock_image_list.side_effect = self.exceptions.glance

        res = self.client.get(INDEX_URL)

        self.mock_image_list.assert_called_once_with(test.IsHttpRequest(),
                                                     marker=None,
                                                     paginate=True,
                                                     sort_dir='asc',
                                                     sort_key='name',
                                                     reversed_order=False)
        self.assertTemplateUsed(res, INDEX_TEMPLATE)

    def test_snapshot_actions(self):
        snapshots = self.snapshots.list()
        self.mock_image_list.return_value = [snapshots, False, False]

        res = self.client.get(INDEX_URL)
        self.assertTemplateUsed(res, INDEX_TEMPLATE)
        self.assertIn('images_table', res.context)
        snaps = res.context['images_table']
        self.assertEqual(len(snaps.get_rows()), 4)

        row_actions = snaps.get_row_actions(snaps.data[0])

        # first instance - status active, owned
        self.assertEqual(len(row_actions), 5)
        self.assertEqual(row_actions[0].verbose_name, "Launch")
        self.assertEqual(row_actions[1].verbose_name, "Create Volume")
        self.assertEqual(row_actions[2].verbose_name, "Edit Image")
        self.assertEqual(row_actions[3].verbose_name, "Update Metadata")
        self.assertEqual(row_actions[4].verbose_name, "Delete Image")

        row_actions = snaps.get_row_actions(snaps.data[1])

        # second instance - status active, not owned
        self.assertEqual(len(row_actions), 2)
        self.assertEqual(row_actions[0].verbose_name, "Launch")
        self.assertEqual(row_actions[1].verbose_name, "Create Volume")

        row_actions = snaps.get_row_actions(snaps.data[2])
        # third instance - status queued, only delete is available
        self.assertEqual(len(row_actions), 1)
        self.assertEqual(row_actions[0].verbose_name, "Delete Image")
        self.assertEqual(str(row_actions[0]), "<DeleteImage: delete>")

        self.mock_image_list.assert_called_once_with(test.IsHttpRequest(),
                                                     marker=None,
                                                     paginate=True,
                                                     sort_dir='asc',
                                                     sort_key='name',
                                                     reversed_order=False)


class ImagesAndSnapshotsUtilsTests(BaseImagesTestCase):
    def test_list_image(self):
        public_images = [image for image in self.images.list()
                         if image.status == 'active' and image.is_public]
        private_images = [image for image in self.images.list()
                          if (image.status == 'active' and
                              not image.is_public)]
        shared_images = [image for image in self.imagesV2.list()
                         if (image.status == 'active' and
                             image.visibility == 'shared')]
        community_images = [image for image in self.imagesV2.list()
                            if (image.status == 'active' and
                                image.visibility == 'community')]
        self.mock_image_list.side_effect = [
            [public_images, False, False],
            [private_images, False, False],
            [community_images, False, False],
            [shared_images, False, False]
        ]

        image_calls = [
            mock.call(test.IsHttpRequest(),
                      filters={'is_public': True, 'status': 'active'}),
            mock.call(test.IsHttpRequest(),
                      filters={'property-owner_id': self.tenant.id,
                               'status': 'active'}),
            mock.call(test.IsHttpRequest(),
                      filters={'visibility': 'community', 'status': 'active'}),
            mock.call(test.IsHttpRequest(),
                      filters={'visibility': 'shared', 'status': 'active'})
        ]

        ret = utils.get_available_images(self.request, self.tenant.id)

        expected_images = [image for image in self.images.list()
                           if (image.status == 'active' and
                               image.container_format not in ('ami', 'aki'))]

        self.mock_image_list.assert_has_calls(image_calls)
        self.assertEqual(len(expected_images), len(ret))

    def test_list_image_using_cache(self):
        public_images = [image for image in self.images.list()
                         if image.status == 'active' and image.is_public]
        private_images = [image for image in self.images.list()
                          if (image.status == 'active' and
                              not image.is_public)]
        community_images = [image for image in self.imagesV2.list()
                            if (image.status == 'active' and
                                image.visibility == 'community')]
        shared_images = [image for image in self.imagesV2.list()
                         if (image.status == 'active' and
                             image.visibility == 'shared')]

        self.mock_image_list.side_effect = [
            [public_images, False, False],
            [private_images, False, False],
            [community_images, False, False],
            [shared_images, False, False],
            [private_images, False, False]
        ]

        image_calls = [
            mock.call(test.IsHttpRequest(),
                      filters={'is_public': True, 'status': 'active'}),
            mock.call(test.IsHttpRequest(),
                      filters={'property-owner_id': self.tenant.id,
                               'status': 'active'}),
            mock.call(test.IsHttpRequest(),
                      filters={'visibility': 'community', 'status': 'active'}),
            mock.call(test.IsHttpRequest(),
                      filters={'visibility': 'shared', 'status': 'active'}),
            mock.call(test.IsHttpRequest(),
                      filters={'property-owner_id': 'other-tenant',
                               'status': 'active'})
        ]

        expected_images = [image for image in self.images.list()
                           if (image.status == 'active' and
                               image.container_format not in ('ari', 'aki'))]

        images_cache = {}
        ret = utils.get_available_images(self.request, self.tenant.id,
                                         images_cache)
        self.assertEqual(len(expected_images), len(ret))
        self.assertEqual(
            len(public_images),
            len(images_cache['public_images']))
        self.assertEqual(1, len(images_cache['images_by_project']))
        self.assertEqual(
            len(private_images),
            len(images_cache['images_by_project'][self.tenant.id]))
        self.assertEqual(
            len(community_images),
            len(images_cache['community_images']))
        self.assertEqual(
            len(shared_images),
            len(images_cache['shared_images']))

        ret = utils.get_available_images(self.request, self.tenant.id,
                                         images_cache)
        self.assertEqual(len(expected_images), len(ret))

        # image list for other-tenant
        ret = utils.get_available_images(self.request, 'other-tenant',
                                         images_cache)
        self.assertEqual(len(expected_images), len(ret))
        self.assertEqual(
            len(public_images),
            len(images_cache['public_images']))
        self.assertEqual(2, len(images_cache['images_by_project']))
        self.assertEqual(
            len(private_images),
            len(images_cache['images_by_project']['other-tenant']))

        self.mock_image_list.assert_has_calls(image_calls)

    @mock.patch.object(exceptions, 'handle')
    def test_list_image_error_public_image_list(self, mock_exception_handle):
        private_images = [image for image in self.images.list()
                          if (image.status == 'active' and
                              not image.is_public)]
        community_images = [image for image in self.imagesV2.list()
                            if (image.status == 'active' and
                                image.visibility == 'community')]
        shared_images = [image for image in self.imagesV2.list()
                         if (image.status == 'active' and
                             image.visibility == 'shared')]

        self.mock_image_list.side_effect = [
            self.exceptions.glance,
            [private_images, False, False],
            [community_images, False, False],
            [shared_images, False, False]
        ]

        images_cache = {}
        ret = utils.get_available_images(self.request, self.tenant.id,
                                         images_cache)
        image_calls = [
            mock.call(test.IsHttpRequest(),
                      filters={'is_public': True, 'status': 'active'}),
            mock.call(test.IsHttpRequest(),
                      filters={'status': 'active', 'property-owner_id': '1'}),
            mock.call(test.IsHttpRequest(),
                      filters={'visibility': 'community', 'status': 'active'}),
            mock.call(test.IsHttpRequest(),
                      filters={'visibility': 'shared', 'status': 'active'})
        ]
        self.mock_image_list.assert_has_calls(image_calls)
        mock_exception_handle.assert_called_once_with(
            test.IsHttpRequest(),
            "Unable to retrieve public images.")

        expected_images = [image for image in private_images
                           if image.container_format not in ('ami', 'aki')]
        self.assertEqual(len(expected_images), len(ret))
        self.assertNotIn('public_images', images_cache)
        self.assertEqual(1, len(images_cache['images_by_project']))
        self.assertEqual(
            len(private_images),
            len(images_cache['images_by_project'][self.tenant.id]))
        self.assertEqual(
            len(community_images),
            len(images_cache['community_images']))
        self.assertEqual(
            len(shared_images),
            len(images_cache['shared_images']))

    @mock.patch.object(exceptions, 'handle')
    def test_list_image_error_private_image_list(self, mock_exception_handle):
        public_images = [image for image in self.images.list()
                         if image.status == 'active' and image.is_public]
        private_images = [image for image in self.images.list()
                          if (image.status == 'active' and
                              not image.is_public)]
        community_images = [image for image in self.imagesV2.list()
                            if (image.status == 'active' and
                                image.visibility == 'community')]
        shared_images = [image for image in self.imagesV2.list()
                         if (image.status == 'active' and
                             image.visibility == 'shared')]

        self.mock_image_list.side_effect = [
            [public_images, False, False],
            self.exceptions.glance,
            [community_images, False, False],
            [shared_images, False, False],
            [private_images, False, False]
        ]
        images_cache = {}
        ret = utils.get_available_images(self.request, self.tenant.id,
                                         images_cache)
        expected_images = [image for image in public_images
                           if image.container_format not in ('ami', 'aki')]
        self.assertEqual(len(expected_images), len(ret))
        self.assertEqual(
            len(public_images),
            len(images_cache['public_images']))
        self.assertFalse(len(images_cache['images_by_project']))
        self.assertEqual(
            len(community_images),
            len(images_cache['community_images']))
        self.assertEqual(
            len(shared_images),
            len(images_cache['shared_images']))

        ret = utils.get_available_images(self.request, self.tenant.id,
                                         images_cache)

        expected_images = [image for image in self.images.list()
                           if image.container_format not in ('ami', 'aki')]
        self.assertEqual(len(expected_images), len(ret))
        self.assertEqual(
            len(public_images),
            len(images_cache['public_images']))
        self.assertEqual(1, len(images_cache['images_by_project']))
        self.assertEqual(
            len(private_images),
            len(images_cache['images_by_project'][self.tenant.id]))
        self.assertEqual(
            len(community_images),
            len(images_cache['community_images']))
        self.assertEqual(
            len(shared_images),
            len(images_cache['shared_images']))

        image_calls = [
            mock.call(test.IsHttpRequest(),
                      filters={'status': 'active', 'is_public': True}),
            mock.call(test.IsHttpRequest(),
                      filters={'status': 'active', 'property-owner_id': '1'}),
            mock.call(test.IsHttpRequest(),
                      filters={'status': 'active', 'visibility': 'community'}),
            mock.call(test.IsHttpRequest(),
                      filters={'status': 'active', 'visibility': 'shared'}),
            mock.call(test.IsHttpRequest(),
                      filters={'status': 'active', 'property-owner_id': '1'})
        ]
        self.mock_image_list.assert_has_calls(image_calls)
        mock_exception_handle.assert_called_once_with(
            test.IsHttpRequest(),
            "Unable to retrieve images for the current project.")


class SeleniumTests(test.SeleniumTestCase):
    @test.create_mocks({api.glance: ('image_list_detailed',)})
    def test_modal_create_image_from_url(self):
        driver = self.selenium
        images = self.images.list()
        self.mock_image_list_detailed.return_value = [images, False, False]

        driver.get("%s%s" % (self.live_server_url, INDEX_URL))

        # Open the modal menu
        driver.find_element_by_id("images__action_create").click()
        wait = self.ui.WebDriverWait(self.selenium, 10,
                                     ignored_exceptions=[socket_timeout])
        wait.until(lambda x: driver.find_element_by_id("id_disk_format"))

        driver.find_element_by_xpath('//a[@data-select-value="url"]').click()
        copyfrom = driver.find_element_by_id("id_image_url")
        copyfrom.send_keys("http://www.test.com/test.iso")
        formats = self.ui.Select(driver.find_element_by_id("id_disk_format"))
        body = formats.first_selected_option
        self.assertIn("ISO", body.text,
                      "ISO should be selected when the extension is *.iso")

        self.assertEqual(3, self.mock_image_list_detailed.call_count)
        self.mock_image_list_detailed.assert_has_calls([
            mock.call(test.IsHttpRequest(), marker=None, paginate=True,
                      reversed_order=False, sort_dir='asc', sort_key='name'),
            mock.call(test.IsHttpRequest(), filters={'disk_format': 'aki'}),
            mock.call(test.IsHttpRequest(), filters={'disk_format': 'ari'}),
        ])

    @unittest.skipIf(os.environ.get('SELENIUM_PHANTOMJS'),
                     "PhantomJS cannot test file upload widgets.")
    @test.create_mocks({api.glance: ('image_list_detailed',)})
    def test_modal_create_image_from_file(self):
        driver = self.selenium
        images = self.images.list()
        self.mock_image_list_detailed.return_value = [images, False, False]

        driver.get("%s%s" % (self.live_server_url, INDEX_URL))

        # Open the modal menu
        driver.find_element_by_id("images__action_create").click()
        wait = self.ui.WebDriverWait(driver, 10,
                                     ignored_exceptions=[socket_timeout])
        wait.until(lambda x: driver.find_element_by_id("id_disk_format"))

        driver.find_element_by_xpath('//a[@data-select-value="file"]').click()
        with tempfile.NamedTemporaryFile() as tmp:
            driver.find_element_by_id("id_image_file").send_keys(tmp.name)
        formats = self.ui.Select(driver.find_element_by_id("id_disk_format"))
        formats.select_by_visible_text('ISO - Optical Disk Image')
        body = formats.first_selected_option
        self.assertIn("ISO", body.text,
                      "ISO should be selected when the extension is *.iso")

        self.assertEqual(3, self.mock_image_list_detailed.call_count)
        self.mock_image_list_detailed.assert_has_calls([
            mock.call(test.IsHttpRequest(), marker=None, paginate=True,
                      reversed_order=False, sort_dir='asc', sort_key='name'),
            mock.call(test.IsHttpRequest(), filters={'disk_format': 'aki'}),
            mock.call(test.IsHttpRequest(), filters={'disk_format': 'ari'}),
        ])

    @test.create_mocks({api.glance: ('image_list_detailed',)})
    def test_create_image_from_url(self):
        driver = self.selenium
        self.mock_image_list_detailed.return_value = [self.images.list(),
                                                      False, False]

        driver.get("%s%s" % (self.live_server_url, CREATE_URL))
        wait = self.ui.WebDriverWait(driver, 10,
                                     ignored_exceptions=[socket_timeout])
        wait.until(lambda x: driver.find_element_by_id("id_disk_format"))

        driver.find_element_by_xpath('//a[@data-select-value="url"]').click()
        copyfrom = driver.find_element_by_id("id_image_url")
        copyfrom.send_keys("http://www.test.com/test.iso")
        formats = self.ui.Select(driver.find_element_by_id("id_disk_format"))
        formats.select_by_visible_text('ISO - Optical Disk Image')
        body = formats.first_selected_option
        self.assertIn("ISO", body.text,
                      "ISO should be selected when the extension is *.iso")

        self.assertEqual(2, self.mock_image_list_detailed.call_count)
        self.mock_image_list_detailed.assert_has_calls([
            mock.call(test.IsHttpRequest(), filters={'disk_format': 'aki'}),
            mock.call(test.IsHttpRequest(), filters={'disk_format': 'ari'}),
        ])

    @unittest.skipIf(os.environ.get('SELENIUM_PHANTOMJS'),
                     "PhantomJS cannot test file upload widgets.")
    @test.create_mocks({api.glance: ('image_list_detailed',)})
    def test_create_image_from_file(self):
        driver = self.selenium
        self.mock_image_list_detailed.return_value = [self.images.list(),
                                                      False, False]

        driver.get("%s%s" % (self.live_server_url, CREATE_URL))
        wait = self.ui.WebDriverWait(driver, 10,
                                     ignored_exceptions=[socket_timeout])
        wait.until(lambda x: driver.find_element_by_id("id_disk_format"))

        driver.find_element_by_xpath('//a[@data-select-value="file"]').click()
        with tempfile.NamedTemporaryFile() as tmp:
            driver.find_element_by_id("id_image_file").send_keys(tmp.name)
        formats = self.ui.Select(driver.find_element_by_id("id_disk_format"))
        formats.select_by_visible_text('ISO - Optical Disk Image')
        body = formats.first_selected_option

        self.assertIn("ISO", body.text,
                      "ISO should be selected when the extension is *.iso")

        self.assertEqual(2, self.mock_image_list_detailed.call_count)
        self.mock_image_list_detailed.assert_has_calls([
            mock.call(test.IsHttpRequest(), filters={'disk_format': 'aki'}),
            mock.call(test.IsHttpRequest(), filters={'disk_format': 'ari'}),
        ])