"Fossies" - the Fresh Open Source Software Archive

Member "horizon-14.0.4/openstack_dashboard/test/unit/api/test_glance.py" (22 Oct 2019, 18060 Bytes) of package /linux/misc/openstack/horizon-14.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "test_glance.py": 15.1.0_vs_16.0.0.

    1 # Copyright 2012 United States Government as represented by the
    2 # Administrator of the National Aeronautics and Space Administration.
    3 # All Rights Reserved.
    4 #
    5 # Copyright 2012 Nebula, Inc.
    6 #
    7 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    8 #    not use this file except in compliance with the License. You may obtain
    9 #    a copy of the License at
   10 #
   11 #         http://www.apache.org/licenses/LICENSE-2.0
   12 #
   13 #    Unless required by applicable law or agreed to in writing, software
   14 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   15 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   16 #    License for the specific language governing permissions and limitations
   17 #    under the License.
   18 
   19 from django.conf import settings
   20 from django.test.utils import override_settings
   21 import mock
   22 
   23 from openstack_dashboard import api
   24 from openstack_dashboard.api import base
   25 from openstack_dashboard.test import helpers as test
   26 
   27 
   28 class GlanceApiTests(test.APIMockTestCase):
   29     def setUp(self):
   30         super(GlanceApiTests, self).setUp()
   31         api.glance.VERSIONS.clear_active_cache()
   32 
   33     @override_settings(API_RESULT_PAGE_SIZE=2)
   34     @mock.patch.object(api.glance, 'glanceclient')
   35     def test_image_list_detailed_no_pagination(self, mock_glanceclient):
   36         # Verify that all images are returned even with a small page size
   37         api_images = self.images_api.list()
   38         expected_images = self.images.list()  # Wrapped Images
   39         filters = {}
   40         limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
   41 
   42         glanceclient = mock_glanceclient.return_value
   43         mock_images_list = glanceclient.images.list
   44         mock_images_list.return_value = iter(api_images)
   45 
   46         images, has_more, has_prev = api.glance.image_list_detailed(
   47             self.request)
   48 
   49         mock_images_list.assert_called_once_with(page_size=limit,
   50                                                  limit=limit,
   51                                                  filters=filters,
   52                                                  sort_dir='desc',
   53                                                  sort_key='created_at')
   54         self.assertListEqual(images, expected_images)
   55         self.assertFalse(has_more)
   56         self.assertFalse(has_prev)
   57 
   58     @override_settings(API_RESULT_PAGE_SIZE=2)
   59     @mock.patch.object(api.glance, 'glanceclient')
   60     def test_image_list_detailed_sort_options(self, mock_glanceclient):
   61         # Verify that sort_dir and sort_key work
   62         api_images = self.images_api.list()
   63         expected_images = self.images.list()  # Wrapped Images
   64         filters = {}
   65         limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
   66         sort_dir = 'asc'
   67         sort_key = 'min_disk'
   68 
   69         glanceclient = mock_glanceclient.return_value
   70         mock_images_list = glanceclient.images.list
   71         mock_images_list.return_value = iter(api_images)
   72 
   73         images, has_more, has_prev = api.glance.image_list_detailed(
   74             self.request,
   75             sort_dir=sort_dir,
   76             sort_key=sort_key)
   77 
   78         mock_images_list.assert_called_once_with(page_size=limit,
   79                                                  limit=limit,
   80                                                  filters=filters,
   81                                                  sort_dir=sort_dir,
   82                                                  sort_key=sort_key)
   83         self.assertListEqual(images, expected_images)
   84         self.assertFalse(has_more)
   85         self.assertFalse(has_prev)
   86 
   87     @override_settings(API_RESULT_PAGE_SIZE=2)
   88     @mock.patch.object(api.glance, 'glanceclient')
   89     def test_image_list_detailed_pagination_more_page_size(self,
   90                                                            mock_glanceclient):
   91         # The total snapshot count is over page size, should return
   92         # page_size images.
   93         filters = {}
   94         page_size = settings.API_RESULT_PAGE_SIZE
   95         limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
   96 
   97         api_images = self.images_api.list()
   98         expected_images = self.images.list()  # Wrapped Images
   99         images_iter = iter(api_images)
  100 
  101         glanceclient = mock_glanceclient.return_value
  102         mock_images_list = glanceclient.images.list
  103         mock_images_list.return_value = images_iter
  104 
  105         # Pass back all images, ignoring filters
  106         images, has_more, has_prev = api.glance.image_list_detailed(
  107             self.request,
  108             marker=None,
  109             filters=filters,
  110             paginate=True)
  111 
  112         expected_images = expected_images[:page_size]
  113 
  114         mock_images_list.assert_called_once_with(limit=limit,
  115                                                  page_size=page_size + 1,
  116                                                  filters=filters,
  117                                                  sort_dir='desc',
  118                                                  sort_key='created_at')
  119         self.assertListEqual(images, expected_images)
  120         self.assertTrue(has_more)
  121         self.assertFalse(has_prev)
  122         # Ensure that only the needed number of images are consumed
  123         # from the iterator (page_size + 1).
  124         self.assertEqual(len(list(images_iter)),
  125                          len(api_images) - len(expected_images) - 1)
  126 
  127     @override_settings(API_RESULT_PAGE_SIZE=20)
  128     @mock.patch.object(api.glance, 'glanceclient')
  129     def test_image_list_detailed_pagination_less_page_size(self,
  130                                                            mock_glanceclient):
  131         # The total image count is less than page size, should return images
  132         # more, prev should return False.
  133         filters = {}
  134         page_size = settings.API_RESULT_PAGE_SIZE
  135         limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
  136 
  137         api_images = self.images_api.list()
  138         expected_images = self.images.list()  # Wrapped Images
  139 
  140         glanceclient = mock_glanceclient.return_value
  141         mock_images_list = glanceclient.images.list
  142         mock_images_list.return_value = iter(api_images)
  143 
  144         # Pass back all images, ignoring filters
  145         images, has_more, has_prev = api.glance.image_list_detailed(
  146             self.request,
  147             filters=filters,
  148             paginate=True)
  149 
  150         expected_images = expected_images[:page_size]
  151 
  152         mock_images_list.assert_called_once_with(limit=limit,
  153                                                  page_size=page_size + 1,
  154                                                  filters=filters,
  155                                                  sort_dir='desc',
  156                                                  sort_key='created_at')
  157         self.assertListEqual(images, expected_images)
  158         self.assertFalse(has_more)
  159         self.assertFalse(has_prev)
  160 
  161     @override_settings(API_RESULT_PAGE_SIZE=9)
  162     @mock.patch.object(api.glance, 'glanceclient')
  163     def test_image_list_detailed_pagination_equal_page_size(self,
  164                                                             mock_glanceclient):
  165         # The total image count equals page size, should return
  166         # page_size images. more, prev should return False
  167         filters = {}
  168         page_size = settings.API_RESULT_PAGE_SIZE
  169         limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
  170 
  171         api_images = self.images_api.list()
  172         expected_images = self.images.list()  # Wrapped Images
  173 
  174         glanceclient = mock_glanceclient.return_value
  175         mock_images_list = glanceclient.images.list
  176         mock_images_list.return_value = iter(api_images)
  177 
  178         images, has_more, has_prev = api.glance.image_list_detailed(
  179             self.request,
  180             filters=filters,
  181             paginate=True)
  182 
  183         expected_images = expected_images[:page_size]
  184 
  185         mock_images_list.assert_called_once_with(limit=limit,
  186                                                  page_size=page_size + 1,
  187                                                  filters=filters,
  188                                                  sort_dir='desc',
  189                                                  sort_key='created_at')
  190         self.assertListEqual(images, expected_images)
  191         self.assertFalse(has_more)
  192         self.assertFalse(has_prev)
  193         self.assertEqual(len(expected_images), len(images))
  194 
  195     @override_settings(API_RESULT_PAGE_SIZE=2)
  196     @mock.patch.object(api.glance, 'glanceclient')
  197     def test_image_list_detailed_pagination_marker(self, mock_glanceclient):
  198         # Tests getting a second page with a marker.
  199         filters = {}
  200         page_size = settings.API_RESULT_PAGE_SIZE
  201         limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
  202         marker = 'nonsense'
  203 
  204         api_images = self.images_api.list()[page_size:]
  205         expected_images = self.images.list()[page_size:]  # Wrapped Images
  206         images_iter = iter(api_images)
  207 
  208         glanceclient = mock_glanceclient.return_value
  209         mock_images_list = glanceclient.images.list
  210         mock_images_list.return_value = images_iter
  211 
  212         # Pass back all images, ignoring filters
  213         images, has_more, has_prev = api.glance.image_list_detailed(
  214             self.request,
  215             marker=marker,
  216             filters=filters,
  217             paginate=True)
  218 
  219         expected_images = expected_images[:page_size]
  220 
  221         mock_images_list.assert_called_once_with(limit=limit,
  222                                                  page_size=page_size + 1,
  223                                                  filters=filters,
  224                                                  marker=marker,
  225                                                  sort_dir='desc',
  226                                                  sort_key='created_at')
  227         self.assertListEqual(images, expected_images)
  228         self.assertTrue(has_more)
  229         self.assertTrue(has_prev)
  230         self.assertEqual(len(list(images_iter)),
  231                          len(api_images) - len(expected_images) - 1)
  232 
  233     @override_settings(API_RESULT_PAGE_SIZE=2)
  234     @mock.patch.object(api.glance, 'glanceclient')
  235     def test_image_list_detailed_pagination_marker_prev(self,
  236                                                         mock_glanceclient):
  237         # Tests getting previous page with a marker.
  238         filters = {}
  239         page_size = settings.API_RESULT_PAGE_SIZE
  240         limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
  241         marker = 'nonsense'
  242 
  243         api_images = self.images_api.list()[page_size:]
  244         expected_images = self.images.list()[page_size:]  # Wrapped Images
  245         images_iter = iter(api_images)
  246 
  247         glanceclient = mock_glanceclient.return_value
  248         mock_images_list = glanceclient.images.list
  249         mock_images_list.return_value = images_iter
  250 
  251         # Pass back all images, ignoring filters
  252         images, has_more, has_prev = api.glance.image_list_detailed(
  253             self.request,
  254             marker=marker,
  255             filters=filters,
  256             sort_dir='asc',
  257             paginate=True)
  258 
  259         expected_images = expected_images[:page_size]
  260 
  261         mock_images_list.assert_called_once_with(limit=limit,
  262                                                  page_size=page_size + 1,
  263                                                  marker=marker,
  264                                                  filters=filters,
  265                                                  sort_dir='asc',
  266                                                  sort_key='created_at')
  267         self.assertListEqual(images, expected_images)
  268         self.assertTrue(has_more)
  269         self.assertTrue(has_prev)
  270         self.assertEqual(len(list(images_iter)),
  271                          len(api_images) - len(expected_images) - 1)
  272 
  273     @mock.patch.object(api.glance, 'glanceclient')
  274     def test_get_image_empty_name(self, mock_glanceclient):
  275         glanceclient = mock_glanceclient.return_value
  276         mock_images_get = glanceclient.images.get
  277         mock_images_get.return_value = self.empty_name_image
  278 
  279         image = api.glance.image_get(self.request, 'empty')
  280 
  281         mock_images_get.assert_called_once_with('empty')
  282         self.assertIsNone(image.name)
  283 
  284     @mock.patch.object(api.glance, 'glanceclient')
  285     def test_metadefs_namespace_list(self, mock_glanceclient):
  286         metadata_defs = self.metadata_defs.list()
  287         limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
  288 
  289         glanceclient = mock_glanceclient.return_value
  290         mock_metadefs_list = glanceclient.metadefs_namespace.list
  291         mock_metadefs_list.return_value = metadata_defs
  292 
  293         defs, more, prev = api.glance.metadefs_namespace_list(self.request)
  294 
  295         mock_metadefs_list.assert_called_once_with(page_size=limit,
  296                                                    limit=limit,
  297                                                    filters={},
  298                                                    sort_dir='asc',
  299                                                    sort_key='namespace')
  300         self.assertEqual(len(metadata_defs), len(defs))
  301         for i in range(len(metadata_defs)):
  302             self.assertEqual(metadata_defs[i].namespace, defs[i].namespace)
  303         self.assertFalse(more)
  304         self.assertFalse(prev)
  305 
  306     @mock.patch.object(api.glance, 'glanceclient')
  307     def test_metadefs_namespace_list_with_properties_target(self,
  308                                                             mock_glanceclient):
  309         metadata_defs = self.metadata_defs.list()
  310         limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
  311         filters = {'resource_types': ['OS::Cinder::Volume'],
  312                    'properties_target': 'user'}
  313 
  314         glanceclient = mock_glanceclient.return_value
  315         mock_metadefs_list = glanceclient.metadefs_namespace.list
  316         mock_metadefs_list.return_value = metadata_defs
  317 
  318         defs = api.glance.metadefs_namespace_list(self.request,
  319                                                   filters=filters)[0]
  320 
  321         mock_metadefs_list.assert_called_once_with(page_size=limit,
  322                                                    limit=limit,
  323                                                    filters=filters,
  324                                                    sort_dir='asc',
  325                                                    sort_key='namespace')
  326         self.assertEqual(1, len(defs))
  327         self.assertEqual('namespace_4', defs[0].namespace)
  328 
  329     @mock.patch.object(api.glance, 'get_version', return_value=1)
  330     def test_metadefs_namespace_list_v1(self, mock_version):
  331         defs, more, prev = api.glance.metadefs_namespace_list(self.request)
  332         self.assertItemsEqual(defs, [])
  333         self.assertFalse(more)
  334         self.assertFalse(prev)
  335 
  336     @mock.patch.object(api.glance, 'get_version', return_value=1)
  337     def test_metadefs_resource_types_list_v1(self, mock_version):
  338         res_types = api.glance.metadefs_resource_types_list(self.request)
  339         self.assertItemsEqual(res_types, [])
  340 
  341     @mock.patch.object(api.glance, 'glanceclient')
  342     def _test_image_create_external_upload(self, mock_glanceclient,
  343                                            api_version=2):
  344         expected_image = self.images.first()
  345         service = base.get_service_from_catalog(self.service_catalog, 'image')
  346         base_url = base.get_url_for_service(service, 'RegionOne', 'publicURL')
  347         if api_version == 1:
  348             url_template = '%s/v1/images/%s'
  349         else:
  350             url_template = '%s/v2/images/%s/file'
  351         upload_url = url_template % (base_url, expected_image.id)
  352 
  353         glanceclient = mock_glanceclient.return_value
  354         mock_image_create = glanceclient.images.create
  355         mock_image_create.return_value = expected_image
  356 
  357         actual_image = api.glance.image_create(self.request, data='sample.iso')
  358 
  359         mock_image_create.assert_called_once()
  360         self.assertEqual(upload_url, actual_image.upload_url)
  361         self.assertEqual(self.request.user.token.id, actual_image.token_id)
  362 
  363     @override_settings(OPENSTACK_API_VERSIONS={"image": 1})
  364     def test_image_create_v1_external_upload(self):
  365         self._test_image_create_external_upload(api_version=1)
  366 
  367     def test_image_create_v2_external_upload(self):
  368         self._test_image_create_external_upload()
  369 
  370     @override_settings(OPENSTACK_API_VERSIONS={'image': 1})
  371     def test_create_image_metadata_docker_v1(self):
  372         form_data = {
  373             'name': u'Docker image',
  374             'description': u'Docker image test',
  375             'source_type': u'url',
  376             'image_url': u'/',
  377             'disk_format': u'docker',
  378             'architecture': u'x86-64',
  379             'min_disk': 15,
  380             'min_ram': 512,
  381             'is_public': False,
  382             'protected': False,
  383             'is_copying': False
  384         }
  385         meta = api.glance.create_image_metadata(form_data)
  386         self.assertEqual(meta['disk_format'], 'raw')
  387         self.assertEqual(meta['container_format'], 'docker')
  388         self.assertIn('properties', meta)
  389         self.assertNotIn('description', meta)
  390         self.assertNotIn('architecture', meta)
  391         self.assertEqual(meta['properties']['description'],
  392                          form_data['description'])
  393         self.assertEqual(meta['properties']['architecture'],
  394                          form_data['architecture'])
  395 
  396     def test_create_image_metadata_docker_v2(self):
  397         form_data = {
  398             'name': u'Docker image',
  399             'description': u'Docker image test',
  400             'source_type': u'url',
  401             'image_url': u'/',
  402             'disk_format': u'docker',
  403             'architecture': u'x86-64',
  404             'min_disk': 15,
  405             'min_ram': 512,
  406             'is_public': False,
  407             'protected': False,
  408             'is_copying': False
  409         }
  410         meta = api.glance.create_image_metadata(form_data)
  411         self.assertEqual(meta['disk_format'], 'raw')
  412         self.assertEqual(meta['container_format'], 'docker')
  413         self.assertNotIn('properties', meta)
  414         self.assertEqual(meta['description'], form_data['description'])
  415         self.assertEqual(meta['architecture'], form_data['architecture'])