"Fossies" - the Fresh Open Source Software Archive

Member "cinder-13.0.7/cinder/tests/unit/api/v3/test_group_types.py" (4 Oct 2019, 27768 Bytes) of package /linux/misc/openstack/cinder-13.0.7.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.

    1 # Copyright 2016 EMC Corporation
    2 # All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 
   16 import uuid
   17 
   18 import ddt
   19 import mock
   20 from oslo_utils import strutils
   21 from oslo_utils import timeutils
   22 import six
   23 import webob
   24 
   25 from cinder.api import microversions as mv
   26 from cinder.api.v3 import group_specs as v3_group_specs
   27 from cinder.api.v3 import group_types as v3_group_types
   28 from cinder.api.v3.views import group_types as views_types
   29 from cinder import context
   30 from cinder import exception
   31 from cinder import test
   32 from cinder.tests.unit.api import fakes
   33 from cinder.tests.unit import fake_constants as fake
   34 from cinder.volume import group_types
   35 
   36 IN_USE_GROUP_TYPE = fake.GROUP_TYPE3_ID
   37 
   38 
   39 def stub_group_type(id):
   40     specs = {
   41         "key1": "value1",
   42         "key2": "value2",
   43         "key3": "value3",
   44         "key4": "value4",
   45         "key5": "value5"
   46     }
   47     return dict(
   48         id=id,
   49         name='group_type_%s' % six.text_type(id),
   50         description='group_type_desc_%s' % six.text_type(id),
   51         group_specs=specs,
   52     )
   53 
   54 
   55 def return_group_types_get_all_types(context, filters=None, marker=None,
   56                                      limit=None, sort_keys=None,
   57                                      sort_dirs=None, offset=None,
   58                                      list_result=False):
   59     result = dict(group_type_1=stub_group_type(1),
   60                   group_type_2=stub_group_type(2),
   61                   group_type_3=stub_group_type(3)
   62                   )
   63     if list_result:
   64         return list(result.values())
   65     return result
   66 
   67 
   68 def return_empty_group_types_get_all_types(context, filters=None, marker=None,
   69                                            limit=None, sort_keys=None,
   70                                            sort_dirs=None, offset=None,
   71                                            list_result=False):
   72     if list_result:
   73         return []
   74     return {}
   75 
   76 
   77 def return_group_types_get_group_type(context, id):
   78     if id == fake.WILL_NOT_BE_FOUND_ID:
   79         raise exception.GroupTypeNotFound(group_type_id=id)
   80     return stub_group_type(id)
   81 
   82 
   83 def return_group_types_get_default():
   84     return stub_group_type(1)
   85 
   86 
   87 def return_group_types_get_default_not_found():
   88     return {}
   89 
   90 
   91 def return_group_types_with_groups_destroy(context, id):
   92     if id == IN_USE_GROUP_TYPE:
   93         raise exception.GroupTypeInUse(group_type_id=id)
   94 
   95 
   96 @ddt.ddt
   97 class GroupTypesApiTest(test.TestCase):
   98 
   99     def _create_group_type(self, group_type_name, group_specs=None,
  100                            is_public=True, projects=None):
  101         return group_types.create(self.ctxt, group_type_name, group_specs,
  102                                   is_public, projects).get('id')
  103 
  104     def setUp(self):
  105         super(GroupTypesApiTest, self).setUp()
  106         self.controller = v3_group_types.GroupTypesController()
  107         self.specs_controller = v3_group_specs.GroupTypeSpecsController()
  108         self.ctxt = context.RequestContext(user_id=fake.USER_ID,
  109                                            project_id=fake.PROJECT_ID,
  110                                            is_admin=True)
  111         self.user_ctxt = context.RequestContext(user_id=fake.USER2_ID,
  112                                                 project_id=fake.PROJECT2_ID,
  113                                                 is_admin=False)
  114         self.type_id1 = self._create_group_type('group_type1',
  115                                                 {'key1': 'value1'})
  116         self.type_id2 = self._create_group_type('group_type2',
  117                                                 {'key2': 'value2'})
  118         self.type_id3 = self._create_group_type('group_type3',
  119                                                 {'key3': 'value3'}, False,
  120                                                 [fake.PROJECT_ID])
  121         self.type_id0 = group_types.get_default_cgsnapshot_type()['id']
  122 
  123     @ddt.data('0', 'f', 'false', 'off', 'n', 'no', '1', 't', 'true', 'on',
  124               'y', 'yes')
  125     @mock.patch.object(group_types, "get_group_type_by_name")
  126     @mock.patch.object(group_types, "create")
  127     @mock.patch("cinder.api.openstack.wsgi.Request.cache_resource")
  128     @mock.patch("cinder.api.views.types.ViewBuilder.show")
  129     def test_create_group_type_with_valid_is_public_in_string(
  130             self, is_public, mock_show, mock_cache_resource,
  131             mock_create, mock_get):
  132         boolean_is_public = strutils.bool_from_string(is_public)
  133         req = fakes.HTTPRequest.blank('/v3/%s/types' % fake.PROJECT_ID,
  134                                       version=mv.GROUP_TYPE)
  135         req.environ['cinder.context'] = self.ctxt
  136 
  137         body = {"group_type": {"is_public": is_public, "name": "group_type1",
  138                                "description": None}}
  139         self.controller.create(req, body=body)
  140         mock_create.assert_called_once_with(
  141             self.ctxt, 'group_type1', {},
  142             boolean_is_public, description=None)
  143 
  144     @mock.patch.object(group_types, "get_group_type_by_name")
  145     @mock.patch.object(group_types, "create")
  146     @mock.patch("cinder.api.openstack.wsgi.Request.cache_resource")
  147     @mock.patch("cinder.api.views.types.ViewBuilder.show")
  148     def test_create_group_type_with_group_specs_null(
  149             self, mock_show, mock_cache_resource,
  150             mock_create, mock_get):
  151 
  152         req = fakes.HTTPRequest.blank('/v3/%s/types' % fake.PROJECT_ID,
  153                                       version=mv.GROUP_TYPE)
  154         req.environ['cinder.context'] = self.ctxt
  155 
  156         body = {"group_type": {"name": "group_type1",
  157                                "group_specs": None}}
  158         self.controller.create(req, body=body)
  159         mock_create.assert_called_once_with(
  160             self.ctxt, 'group_type1', None, True, description=None)
  161 
  162     @ddt.data(fake.GROUP_TYPE_ID, IN_USE_GROUP_TYPE)
  163     def test_group_type_destroy(self, grp_type_id):
  164         grp_type = {'id': grp_type_id, 'name': 'grp' + grp_type_id}
  165         self.mock_object(group_types, 'get_group_type',
  166                          return_value=grp_type)
  167         self.mock_object(group_types, 'destroy',
  168                          return_group_types_with_groups_destroy)
  169         mock_notify_info = self.mock_object(
  170             v3_group_types.GroupTypesController,
  171             '_notify_group_type_info')
  172         mock_notify_error = self.mock_object(
  173             v3_group_types.GroupTypesController,
  174             '_notify_group_type_error')
  175         req = fakes.HTTPRequest.blank('/v3/%s/group_types/%s' % (
  176             fake.PROJECT_ID, grp_type_id),
  177             version=mv.GROUP_TYPE)
  178         req.environ['cinder.context'] = self.ctxt
  179         if grp_type_id == IN_USE_GROUP_TYPE:
  180             self.assertRaises(webob.exc.HTTPBadRequest,
  181                               self.controller.delete,
  182                               req, grp_type_id)
  183             mock_notify_error.assert_called_once_with(
  184                 self.ctxt, 'group_type.delete', mock.ANY,
  185                 group_type=grp_type)
  186         else:
  187             self.controller.delete(req, grp_type_id)
  188             mock_notify_info.assert_called_once_with(
  189                 self.ctxt, 'group_type.delete', grp_type)
  190 
  191     def test_group_types_index(self):
  192         self.mock_object(group_types, 'get_all_group_types',
  193                          return_group_types_get_all_types)
  194 
  195         req = fakes.HTTPRequest.blank('/v3/%s/group_types' % fake.PROJECT_ID,
  196                                       use_admin_context=True,
  197                                       version=mv.GROUP_TYPE)
  198         res_dict = self.controller.index(req)
  199 
  200         self.assertEqual(3, len(res_dict['group_types']))
  201 
  202         expected_names = ['group_type_1', 'group_type_2', 'group_type_3']
  203         actual_names = map(lambda e: e['name'], res_dict['group_types'])
  204         self.assertEqual(set(expected_names), set(actual_names))
  205         for entry in res_dict['group_types']:
  206             self.assertEqual('value1', entry['group_specs']['key1'])
  207 
  208     def test_group_types_index_no_data(self):
  209         self.mock_object(group_types, 'get_all_group_types',
  210                          return_empty_group_types_get_all_types)
  211 
  212         req = fakes.HTTPRequest.blank('/v3/%s/group_types' % fake.PROJECT_ID,
  213                                       version=mv.GROUP_TYPE)
  214         res_dict = self.controller.index(req)
  215 
  216         self.assertEqual(0, len(res_dict['group_types']))
  217 
  218     def test_group_types_index_with_limit(self):
  219         req = fakes.HTTPRequest.blank('/v3/%s/group_types?limit=1' %
  220                                       fake.PROJECT_ID,
  221                                       version=mv.GROUP_TYPE)
  222         req.environ['cinder.context'] = self.ctxt
  223         res = self.controller.index(req)
  224 
  225         self.assertEqual(1, len(res['group_types']))
  226         self.assertEqual(self.type_id3, res['group_types'][0]['id'])
  227 
  228         expect_next_link = ('http://localhost/v3/%s/group_types?limit=1'
  229                             '&marker=%s' %
  230                             (fake.PROJECT_ID, res['group_types'][0]['id']))
  231         self.assertEqual(expect_next_link, res['group_type_links'][0]['href'])
  232 
  233     def test_group_types_index_with_offset(self):
  234         req = fakes.HTTPRequest.blank(
  235             '/v3/%s/group_types?offset=1' % fake.PROJECT_ID,
  236             version=mv.GROUP_TYPE)
  237         req.environ['cinder.context'] = self.ctxt
  238         res = self.controller.index(req)
  239 
  240         self.assertEqual(3, len(res['group_types']))
  241 
  242     def test_group_types_index_with_offset_out_of_range(self):
  243         url = '/v3/%s/group_types?offset=424366766556787' % fake.PROJECT_ID
  244         req = fakes.HTTPRequest.blank(url, version=mv.GROUP_TYPE)
  245         self.assertRaises(webob.exc.HTTPBadRequest,
  246                           self.controller.index, req)
  247 
  248     def test_group_types_index_with_limit_and_offset(self):
  249         req = fakes.HTTPRequest.blank(
  250             '/v3/%s/group_types?limit=2&offset=1' % fake.PROJECT_ID,
  251             version=mv.GROUP_TYPE)
  252         req.environ['cinder.context'] = self.ctxt
  253         res = self.controller.index(req)
  254 
  255         self.assertEqual(2, len(res['group_types']))
  256         self.assertEqual(self.type_id2, res['group_types'][0]['id'])
  257         self.assertEqual(self.type_id1, res['group_types'][1]['id'])
  258 
  259     def test_group_types_index_with_limit_and_marker(self):
  260         req = fakes.HTTPRequest.blank('/v3/%s/group_types?limit=1'
  261                                       '&marker=%s' %
  262                                       (fake.PROJECT_ID,
  263                                        self.type_id2),
  264                                       version=mv.GROUP_TYPE)
  265         req.environ['cinder.context'] = self.ctxt
  266         res = self.controller.index(req)
  267 
  268         self.assertEqual(1, len(res['group_types']))
  269         self.assertEqual(self.type_id1, res['group_types'][0]['id'])
  270 
  271     def test_group_types_index_with_valid_filter(self):
  272         req = fakes.HTTPRequest.blank(
  273             '/v3/%s/group_types?is_public=True' % fake.PROJECT_ID,
  274             version=mv.GROUP_TYPE)
  275         req.environ['cinder.context'] = self.ctxt
  276         res = self.controller.index(req)
  277 
  278         self.assertEqual(4, len(res['group_types']))
  279         self.assertEqual(self.type_id3, res['group_types'][0]['id'])
  280         self.assertEqual(self.type_id2, res['group_types'][1]['id'])
  281         self.assertEqual(self.type_id1, res['group_types'][2]['id'])
  282         self.assertEqual(self.type_id0, res['group_types'][3]['id'])
  283 
  284     def test_group_types_index_with_invalid_filter(self):
  285         req = fakes.HTTPRequest.blank(
  286             '/v3/%s/group_types?id=%s' % (fake.PROJECT_ID, self.type_id1),
  287             version=mv.GROUP_TYPE)
  288         req.environ['cinder.context'] = self.ctxt
  289         res = self.controller.index(req)
  290 
  291         self.assertEqual(4, len(res['group_types']))
  292 
  293     def test_group_types_index_with_sort_keys(self):
  294         req = fakes.HTTPRequest.blank('/v3/%s/group_types?sort=id' %
  295                                       fake.PROJECT_ID,
  296                                       version=mv.GROUP_TYPE)
  297         req.environ['cinder.context'] = self.ctxt
  298         res = self.controller.index(req)
  299         expect_result = [self.type_id0, self.type_id1, self.type_id2,
  300                          self.type_id3]
  301         expect_result.sort(reverse=True)
  302 
  303         self.assertEqual(4, len(res['group_types']))
  304         self.assertEqual(expect_result[0], res['group_types'][0]['id'])
  305         self.assertEqual(expect_result[1], res['group_types'][1]['id'])
  306         self.assertEqual(expect_result[2], res['group_types'][2]['id'])
  307         self.assertEqual(expect_result[3], res['group_types'][3]['id'])
  308 
  309     def test_group_types_index_with_sort_and_limit(self):
  310         req = fakes.HTTPRequest.blank(
  311             '/v3/%s/group_types?sort=id&limit=2' % fake.PROJECT_ID,
  312             version=mv.GROUP_TYPE)
  313         req.environ['cinder.context'] = self.ctxt
  314         res = self.controller.index(req)
  315         expect_result = [self.type_id0, self.type_id1, self.type_id2,
  316                          self.type_id3]
  317         expect_result.sort(reverse=True)
  318 
  319         self.assertEqual(2, len(res['group_types']))
  320         self.assertEqual(expect_result[0], res['group_types'][0]['id'])
  321         self.assertEqual(expect_result[1], res['group_types'][1]['id'])
  322 
  323     def test_group_types_index_with_sort_keys_and_sort_dirs(self):
  324         req = fakes.HTTPRequest.blank(
  325             '/v3/%s/group_types?sort=id:asc' % fake.PROJECT_ID,
  326             version=mv.GROUP_TYPE)
  327         req.environ['cinder.context'] = self.ctxt
  328         res = self.controller.index(req)
  329         expect_result = [self.type_id0, self.type_id1, self.type_id2,
  330                          self.type_id3]
  331         expect_result.sort()
  332 
  333         self.assertEqual(4, len(res['group_types']))
  334         self.assertEqual(expect_result[0], res['group_types'][0]['id'])
  335         self.assertEqual(expect_result[1], res['group_types'][1]['id'])
  336         self.assertEqual(expect_result[2], res['group_types'][2]['id'])
  337         self.assertEqual(expect_result[3], res['group_types'][3]['id'])
  338 
  339     @ddt.data('0', 'f', 'false', 'off', 'n', 'no', '1', 't', 'true', 'on',
  340               'y', 'yes')
  341     @mock.patch.object(group_types, "get_group_type")
  342     @mock.patch.object(group_types, "update")
  343     @mock.patch("cinder.api.openstack.wsgi.Request.cache_resource")
  344     @mock.patch("cinder.api.views.types.ViewBuilder.show")
  345     def test_update_group_type_with_valid_is_public_in_string(
  346             self, is_public, mock_show, mock_cache_resource,
  347             mock_update, mock_get):
  348         type_id = six.text_type(uuid.uuid4())
  349         req = fakes.HTTPRequest.blank(
  350             '/v3/%s/types/%s' % (fake.PROJECT_ID, type_id),
  351             version=mv.GROUP_TYPE)
  352         req.environ['cinder.context'] = self.ctxt
  353         boolean_is_public = strutils.bool_from_string(is_public)
  354         body = {"group_type": {"is_public": is_public, "name": "group_type1"}}
  355         self.controller.update(req, type_id, body=body)
  356         mock_update.assert_called_once_with(
  357             self.ctxt, type_id, 'group_type1', None,
  358             is_public=boolean_is_public)
  359 
  360     def test_update_group_type_with_name_null(self):
  361         req = fakes.HTTPRequest.blank(
  362             '/v3/%s/types/%s' % (fake.PROJECT_ID, fake.GROUP_TYPE_ID),
  363             version=mv.GROUP_TYPE)
  364         req.environ['cinder.context'] = self.ctxt
  365         body = {"group_type": {"name": None}}
  366         self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  367                           req, fake.GROUP_TYPE_ID, body=body)
  368 
  369     @ddt.data({"group_type": {"name": None,
  370                               "description": "description"}},
  371               {"group_type": {"name": "test",
  372                               "is_public": True}},
  373               {"group_type": {"description": None,
  374                               "is_public": True}})
  375     def test_update_group_type(self, body):
  376         req = fakes.HTTPRequest.blank(
  377             '/v3/%s/types/%s' % (fake.PROJECT_ID, fake.GROUP_TYPE_ID),
  378             version=mv.GROUP_TYPE)
  379 
  380         group_type_1 = group_types.create(self.ctxt, 'group_type')
  381 
  382         req.environ['cinder.context'] = self.ctxt
  383         res = self.controller.update(req, group_type_1.get('id'), body=body)
  384 
  385         expected_name = body['group_type'].get('name')
  386         if expected_name is not None:
  387             self.assertEqual(expected_name, res['group_type']['name'])
  388 
  389         expected_is_public = body['group_type'].get('is_public')
  390         if expected_is_public is not None:
  391             self.assertEqual(expected_is_public,
  392                              res['group_type']['is_public'])
  393 
  394         self.assertEqual(body['group_type'].get('description'),
  395                          res['group_type']['description'])
  396 
  397     def test_group_types_show(self):
  398         self.mock_object(group_types, 'get_group_type',
  399                          return_group_types_get_group_type)
  400 
  401         type_id = six.text_type(uuid.uuid4())
  402         req = fakes.HTTPRequest.blank('/v3/%s/group_types/' % fake.PROJECT_ID
  403                                       + type_id,
  404                                       version=mv.GROUP_TYPE)
  405         res_dict = self.controller.show(req, type_id)
  406 
  407         self.assertEqual(1, len(res_dict))
  408         self.assertEqual(type_id, res_dict['group_type']['id'])
  409         type_name = 'group_type_' + type_id
  410         self.assertEqual(type_name, res_dict['group_type']['name'])
  411 
  412     def test_group_types_show_pre_microversion(self):
  413         self.mock_object(group_types, 'get_group_type',
  414                          return_group_types_get_group_type)
  415 
  416         type_id = uuid.uuid4()
  417         req = fakes.HTTPRequest.blank(
  418             '/v3/%s/group_types/%s' % (fake.PROJECT_ID, type_id),
  419             version=mv.get_prior_version(mv.GROUP_TYPE))
  420 
  421         self.assertRaises(exception.VersionNotFoundForAPIMethod,
  422                           self.controller.show, req, type_id)
  423 
  424     def test_group_types_show_not_found(self):
  425         self.mock_object(group_types, 'get_group_type',
  426                          return_group_types_get_group_type)
  427 
  428         req = fakes.HTTPRequest.blank('/v3/%s/group_types/%s' %
  429                                       (fake.PROJECT_ID,
  430                                        fake.WILL_NOT_BE_FOUND_ID),
  431                                       version=mv.GROUP_TYPE)
  432         self.assertRaises(webob.exc.HTTPNotFound, self.controller.show,
  433                           req, fake.WILL_NOT_BE_FOUND_ID)
  434 
  435     def test_get_default(self):
  436         self.mock_object(group_types, 'get_default_group_type',
  437                          return_group_types_get_default)
  438         req = fakes.HTTPRequest.blank('/v3/%s/group_types/default' %
  439                                       fake.PROJECT_ID,
  440                                       version=mv.GROUP_TYPE)
  441         req.method = 'GET'
  442         res_dict = self.controller.show(req, 'default')
  443         self.assertEqual(1, len(res_dict))
  444         self.assertEqual('group_type_1', res_dict['group_type']['name'])
  445         self.assertEqual('group_type_desc_1',
  446                          res_dict['group_type']['description'])
  447 
  448     def test_get_default_not_found(self):
  449         self.mock_object(group_types, 'get_default_group_type',
  450                          return_group_types_get_default_not_found)
  451         req = fakes.HTTPRequest.blank('/v3/%s/group_types/default' %
  452                                       fake.PROJECT_ID,
  453                                       version=mv.GROUP_TYPE)
  454         req.method = 'GET'
  455 
  456         self.assertRaises(webob.exc.HTTPNotFound,
  457                           self.controller.show, req, 'default')
  458 
  459     def test_view_builder_show(self):
  460         view_builder = views_types.ViewBuilder()
  461 
  462         now = timeutils.utcnow().isoformat()
  463         raw_group_type = dict(
  464             name='new_type',
  465             description='new_type_desc',
  466             is_public=True,
  467             deleted=False,
  468             created_at=now,
  469             updated_at=now,
  470             group_specs={},
  471             deleted_at=None,
  472             id=42,
  473         )
  474 
  475         request = fakes.HTTPRequest.blank("/v3",
  476                                           version=mv.GROUP_TYPE)
  477         output = view_builder.show(request, raw_group_type)
  478 
  479         self.assertIn('group_type', output)
  480         expected_group_type = dict(
  481             name='new_type',
  482             description='new_type_desc',
  483             is_public=True,
  484             id=42,
  485         )
  486         self.assertDictEqual(expected_group_type, output['group_type'])
  487 
  488     def test_view_builder_show_admin(self):
  489         view_builder = views_types.ViewBuilder()
  490 
  491         now = timeutils.utcnow().isoformat()
  492         raw_group_type = dict(
  493             name='new_type',
  494             description='new_type_desc',
  495             is_public=True,
  496             deleted=False,
  497             created_at=now,
  498             updated_at=now,
  499             group_specs={},
  500             deleted_at=None,
  501             id=42,
  502         )
  503 
  504         request = fakes.HTTPRequest.blank("/v3", use_admin_context=True,
  505                                           version=mv.GROUP_TYPE)
  506         output = view_builder.show(request, raw_group_type)
  507 
  508         self.assertIn('group_type', output)
  509         expected_group_type = dict(
  510             name='new_type',
  511             description='new_type_desc',
  512             is_public=True,
  513             group_specs={},
  514             id=42,
  515         )
  516         self.assertDictEqual(expected_group_type, output['group_type'])
  517 
  518     def __test_view_builder_show_qos_specs_id_policy(self):
  519         with mock.patch.object(context.RequestContext,
  520                                'authorize',
  521                                side_effect=[False, True]):
  522             view_builder = views_types.ViewBuilder()
  523             now = timeutils.utcnow().isoformat()
  524             raw_group_type = dict(
  525                 name='new_type',
  526                 description='new_type_desc',
  527                 is_public=True,
  528                 deleted=False,
  529                 created_at=now,
  530                 updated_at=now,
  531                 deleted_at=None,
  532                 id=42,
  533             )
  534 
  535             request = fakes.HTTPRequest.blank("/v3",
  536                                               version=mv.GROUP_TYPE)
  537             output = view_builder.show(request, raw_group_type)
  538 
  539             self.assertIn('group_type', output)
  540             expected_group_type = dict(
  541                 name='new_type',
  542                 description='new_type_desc',
  543                 is_public=True,
  544                 id=42,
  545             )
  546             self.assertDictEqual(expected_group_type, output['group_type'])
  547 
  548     def test_view_builder_show_group_specs_policy(self):
  549         with mock.patch.object(context.RequestContext,
  550                                'authorize',
  551                                side_effect=[True, False]):
  552             view_builder = views_types.ViewBuilder()
  553             now = timeutils.utcnow().isoformat()
  554             raw_group_type = dict(
  555                 name='new_type',
  556                 description='new_type_desc',
  557                 is_public=True,
  558                 deleted=False,
  559                 created_at=now,
  560                 updated_at=now,
  561                 group_specs={},
  562                 deleted_at=None,
  563                 id=42,
  564             )
  565 
  566             request = fakes.HTTPRequest.blank("/v3",
  567                                               version=mv.GROUP_TYPE)
  568             output = view_builder.show(request, raw_group_type)
  569 
  570             self.assertIn('group_type', output)
  571             expected_group_type = dict(
  572                 name='new_type',
  573                 description='new_type_desc',
  574                 group_specs={},
  575                 is_public=True,
  576                 id=42,
  577             )
  578             self.assertDictEqual(expected_group_type, output['group_type'])
  579 
  580     def test_view_builder_show_pass_all_policy(self):
  581         with mock.patch.object(context.RequestContext,
  582                                'authorize',
  583                                side_effect=[True, False]):
  584             view_builder = views_types.ViewBuilder()
  585             now = timeutils.utcnow().isoformat()
  586             raw_group_type = dict(
  587                 name='new_type',
  588                 description='new_type_desc',
  589                 is_public=True,
  590                 deleted=False,
  591                 created_at=now,
  592                 updated_at=now,
  593                 group_specs={},
  594                 deleted_at=None,
  595                 id=42,
  596             )
  597 
  598             request = fakes.HTTPRequest.blank("/v3",
  599                                               version=mv.GROUP_TYPE)
  600             output = view_builder.show(request, raw_group_type)
  601 
  602             self.assertIn('group_type', output)
  603             expected_group_type = dict(
  604                 name='new_type',
  605                 description='new_type_desc',
  606                 group_specs={},
  607                 is_public=True,
  608                 id=42,
  609             )
  610             self.assertDictEqual(expected_group_type, output['group_type'])
  611 
  612     def test_view_builder_list(self):
  613         view_builder = views_types.ViewBuilder()
  614 
  615         now = timeutils.utcnow().isoformat()
  616         raw_group_types = []
  617         for i in range(0, 10):
  618             raw_group_types.append(
  619                 dict(
  620                     name='new_type',
  621                     description='new_type_desc',
  622                     is_public=True,
  623                     deleted=False,
  624                     created_at=now,
  625                     updated_at=now,
  626                     group_specs={},
  627                     deleted_at=None,
  628                     id=42 + i
  629                 )
  630             )
  631 
  632         request = fakes.HTTPRequest.blank("/v3",
  633                                           version=mv.GROUP_TYPE)
  634         output = view_builder.index(request, raw_group_types)
  635 
  636         self.assertIn('group_types', output)
  637         for i in range(0, 10):
  638             expected_group_type = dict(
  639                 name='new_type',
  640                 description='new_type_desc',
  641                 is_public=True,
  642                 id=42 + i
  643             )
  644             self.assertDictEqual(expected_group_type,
  645                                  output['group_types'][i])
  646 
  647     def test_view_builder_list_admin(self):
  648         view_builder = views_types.ViewBuilder()
  649 
  650         now = timeutils.utcnow().isoformat()
  651         raw_group_types = []
  652         for i in range(0, 10):
  653             raw_group_types.append(
  654                 dict(
  655                     name='new_type',
  656                     description='new_type_desc',
  657                     is_public=True,
  658                     deleted=False,
  659                     created_at=now,
  660                     updated_at=now,
  661                     group_specs={},
  662                     deleted_at=None,
  663                     id=42 + i
  664                 )
  665             )
  666 
  667         request = fakes.HTTPRequest.blank("/v3", use_admin_context=True,
  668                                           version=mv.GROUP_TYPE)
  669         output = view_builder.index(request, raw_group_types)
  670 
  671         self.assertIn('group_types', output)
  672         for i in range(0, 10):
  673             expected_group_type = dict(
  674                 name='new_type',
  675                 description='new_type_desc',
  676                 is_public=True,
  677                 group_specs={},
  678                 id=42 + i
  679             )
  680             self.assertDictEqual(expected_group_type,
  681                                  output['group_types'][i])