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
|
# 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 oslo_utils import uuidutils
import testtools
from webob import exc
from neutron_lib.api import attributes
from neutron_lib.api import converters
from neutron_lib.api.definitions import network
from neutron_lib.api.definitions import port
from neutron_lib.api.definitions import subnet
from neutron_lib.api.definitions import subnetpool
from neutron_lib import constants
from neutron_lib import context
from neutron_lib import exceptions
from neutron_lib.tests import _base as base
class TestPopulateProjectInfo(base.BaseTestCase):
def test_populate_project_info_add_project(self):
attrs_in = {'tenant_id': uuidutils.generate_uuid()}
attrs_out = attributes.populate_project_info(attrs_in)
self.assertIn('project_id', attrs_out)
self.assertEqual(attrs_in['tenant_id'], attrs_out['project_id'])
self.assertEqual(2, len(attrs_out))
def test_populate_project_info_add_tenant(self):
attrs_in = {'project_id': uuidutils.generate_uuid()}
attrs_out = attributes.populate_project_info(attrs_in)
self.assertIn('tenant_id', attrs_out)
self.assertEqual(attrs_in['project_id'], attrs_out['tenant_id'])
self.assertEqual(2, len(attrs_out))
def test_populate_project_info_ids_match(self):
project_id = uuidutils.generate_uuid()
attrs_in = {'project_id': project_id, 'tenant_id': project_id}
attrs_out = attributes.populate_project_info(attrs_in)
self.assertEqual(attrs_in, attrs_out)
def test_populate_project_info_id_mismatch(self):
attrs = {
'project_id': uuidutils.generate_uuid(),
'tenant_id': uuidutils.generate_uuid()
}
self.assertRaises(exc.HTTPBadRequest,
attributes.populate_project_info, attrs)
class TestAttributeInfo(base.BaseTestCase):
class _MyException(Exception):
pass
_EXC_CLS = _MyException
_RESOURCE_NAME = 'thing'
_RESOURCE_ATTRS = {'name': {}, 'type': {}}
_RESOURCE_MAP = {_RESOURCE_NAME: _RESOURCE_ATTRS}
_ATTRS_INSTANCE = attributes.AttributeInfo(_RESOURCE_MAP)
def test_create_from_attribute_info_instance(self):
cloned_attrs = attributes.AttributeInfo(
TestAttributeInfo._ATTRS_INSTANCE)
self.assertEqual(TestAttributeInfo._ATTRS_INSTANCE.attributes,
cloned_attrs.attributes)
def test_create_from_api_def(self):
self.assertEqual(
port.RESOURCE_ATTRIBUTE_MAP,
attributes.AttributeInfo(port.RESOURCE_ATTRIBUTE_MAP).attributes)
def _test_fill_default_value(self, attr_inst, expected, res_dict,
check_allow_post=True):
attr_inst.fill_post_defaults(
res_dict, check_allow_post=check_allow_post)
self.assertEqual(expected, res_dict)
def test_fill_default_value_ok(self):
attr_info = {
'key': {
'allow_post': True,
'default': constants.ATTR_NOT_SPECIFIED,
},
}
attr_inst = attributes.AttributeInfo(attr_info)
self._test_fill_default_value(attr_inst, {'key': 'X'}, {'key': 'X'})
self._test_fill_default_value(
attr_inst, {'key': constants.ATTR_NOT_SPECIFIED}, {})
def test_override_no_allow_post(self):
attr_info = {
'key': {
'allow_post': False,
'default': constants.ATTR_NOT_SPECIFIED,
},
}
attr_inst = attributes.AttributeInfo(attr_info)
self._test_fill_default_value(attr_inst, {'key': 'X'}, {'key': 'X'},
check_allow_post=False)
def test_fill_no_default_value_allow_post(self):
attr_info = {
'key': {
'allow_post': True,
},
}
attr_inst = attributes.AttributeInfo(attr_info)
self._test_fill_default_value(attr_inst, {'key': 'X'}, {'key': 'X'})
self.assertRaises(exceptions.InvalidInput,
self._test_fill_default_value,
attr_inst, {'key': 'X'}, {})
self.assertRaises(self._EXC_CLS, attr_inst.fill_post_defaults,
{}, self._EXC_CLS)
def test_fill_no_default_value_no_allow_post(self):
attr_info = {
'key': {
'allow_post': False,
},
}
attr_inst = attributes.AttributeInfo(attr_info)
self.assertRaises(exceptions.InvalidInput,
self._test_fill_default_value,
attr_inst, {'key': 'X'}, {'key': 'X'})
self._test_fill_default_value(attr_inst, {}, {})
self.assertRaises(self._EXC_CLS, attr_inst.fill_post_defaults,
{'key': 'X'}, self._EXC_CLS)
def test_fill_none_overridden_by_default(self):
attr_info = {
'key': {
'allow_post': True,
'default': 42,
'default_overrides_none': True,
},
}
attr_inst = attributes.AttributeInfo(attr_info)
self._test_fill_default_value(attr_inst, {'key': 42}, {'key': None})
def _test_convert_value(self, attr_inst, expected, res_dict):
attr_inst.convert_values(res_dict)
self.assertEqual(expected, res_dict)
def test_convert_value(self):
attr_info = {
'key': {
},
}
attr_inst = attributes.AttributeInfo(attr_info)
self._test_convert_value(attr_inst,
{'key': constants.ATTR_NOT_SPECIFIED},
{'key': constants.ATTR_NOT_SPECIFIED})
self._test_convert_value(attr_inst, {'key': 'X'}, {'key': 'X'})
self._test_convert_value(attr_inst,
{'other_key': 'X'}, {'other_key': 'X'})
attr_info = {
'key': {
'convert_to': converters.convert_to_int,
},
}
attr_inst = attributes.AttributeInfo(attr_info)
self._test_convert_value(attr_inst,
{'key': constants.ATTR_NOT_SPECIFIED},
{'key': constants.ATTR_NOT_SPECIFIED})
self._test_convert_value(attr_inst, {'key': 1}, {'key': '1'})
self._test_convert_value(attr_inst, {'key': 1}, {'key': 1})
self.assertRaises(exceptions.InvalidInput, self._test_convert_value,
attr_inst, {'key': 1}, {'key': 'a'})
attr_info = {
'key': {
'validate': {'type:uuid': None},
},
}
attr_inst = attributes.AttributeInfo(attr_info)
self._test_convert_value(attr_inst,
{'key': constants.ATTR_NOT_SPECIFIED},
{'key': constants.ATTR_NOT_SPECIFIED})
uuid_str = '01234567-1234-1234-1234-1234567890ab'
self._test_convert_value(attr_inst,
{'key': uuid_str}, {'key': uuid_str})
self.assertRaises(exceptions.InvalidInput, self._test_convert_value,
attr_inst, {'key': 1}, {'key': 1})
self.assertRaises(self._EXC_CLS, attr_inst.convert_values,
{'key': 1}, self._EXC_CLS)
def test_populate_project_id_admin_req(self):
project_id_1 = uuidutils.generate_uuid()
project_id_2 = uuidutils.generate_uuid()
# non-admin users can't create a res on behalf of another project
ctx = context.Context(user_id=None, project_id=project_id_1)
res_dict = {'project_id': project_id_2}
attr_inst = attributes.AttributeInfo({})
self.assertRaises(exc.HTTPBadRequest,
attr_inst.populate_project_id,
ctx, res_dict, None)
# but admin users can
ctx.is_admin = True
attr_inst.populate_project_id(ctx, res_dict, is_create=False)
def test_populate_project_id_from_context(self):
project_id = uuidutils.generate_uuid()
ctx = context.Context(user_id=None, project_id=project_id)
# for each create request, for the resources which require project_id,
# it should be added to the req body
res_dict = {}
attr_inst = attributes.AttributeInfo(
{'project_id': {'allow_post': True}})
attr_inst.populate_project_id(ctx, res_dict, is_create=True)
self.assertEqual(
{'tenant_id': ctx.project_id, 'project_id': ctx.project_id},
res_dict)
def test_populate_project_id_mandatory_not_specified(self):
project_id = uuidutils.generate_uuid()
ctx = context.Context(user_id=None, project_id=project_id)
# if the project_id is mandatory for the resource and not specified
# in the request nor in the context, an exception should be raised
res_dict = {}
attr_info = {'project_id': {'allow_post': True}}
ctx.project_id = None
attr_inst = attributes.AttributeInfo(attr_info)
self.assertRaises(exc.HTTPBadRequest,
attr_inst.populate_project_id,
ctx, res_dict, True)
def test_populate_project_id_not_mandatory(self):
ctx = context.Context(user_id=None)
# if the project_id is not mandatory for the resource it should be
# OK if it is not in the request.
res_dict = {'name': 'test_port'}
attr_inst = attributes.AttributeInfo({})
ctx.project_id = None
attr_inst.populate_project_id(ctx, res_dict, True)
self.assertEqual({'name': 'test_port'}, res_dict)
def test_verify_attributes_null(self):
attributes.AttributeInfo({}).verify_attributes({})
def test_verify_attributes_ok_with_project_id(self):
attributes.AttributeInfo(
{'tenant_id': 'foo', 'project_id': 'foo'}).verify_attributes(
{'project_id': 'foo'})
def test_verify_attributes_ok_subset(self):
attributes.AttributeInfo(
{'attr1': 'foo', 'attr2': 'bar'}).verify_attributes(
{'attr1': 'foo'})
def test_verify_attributes_unrecognized(self):
with testtools.ExpectedException(exc.HTTPBadRequest) as bad_req:
attributes.AttributeInfo(
{'attr1': 'foo'}).verify_attributes(
{'attr1': 'foo', 'attr2': 'bar'})
self.assertEqual(bad_req.message,
"Unrecognized attribute(s) 'attr2'")
class TestCoreResources(base.BaseTestCase):
CORE_DEFS = [network, port, subnet, subnetpool]
def test_core_resource_names(self):
self.assertEqual(
sorted([r.COLLECTION_NAME for r in TestCoreResources.CORE_DEFS]),
sorted(attributes.RESOURCES.keys()))
def test_core_resource_attrs(self):
for r in TestCoreResources.CORE_DEFS:
self.assertIs(r.RESOURCE_ATTRIBUTE_MAP[r.COLLECTION_NAME],
attributes.RESOURCES[r.COLLECTION_NAME])
class TestValidatePriviliges(base.BaseTestCase):
def test__validate_privileges_same_tenant(self):
project_id = 'fake_project'
ctx = context.Context(project_id=project_id)
res_dict = {'project_id': project_id}
try:
attributes._validate_privileges(ctx, res_dict)
except exc.HTTPBadRequest:
self.fail("HTTPBadRequest exception should not be raised.")
def test__validate_privileges_user_other_tenant(self):
project_id = 'fake_project'
ctx = context.Context(project_id='fake_project2')
res_dict = {'project_id': project_id}
self.assertRaises(
exc.HTTPBadRequest,
attributes._validate_privileges,
ctx, res_dict)
def test__validate_privileges_admin_other_tenant(self):
project_id = 'fake_project'
ctx = context.Context(project_id='fake_project2',
is_admin=True)
res_dict = {'project_id': project_id}
try:
attributes._validate_privileges(ctx, res_dict)
except exc.HTTPBadRequest:
self.fail("HTTPBadRequest exception should not be raised.")
def test__validate_privileges_advsvc_other_tenant(self):
project_id = 'fake_project'
ctx = context.Context(project_id='fake_project2',
is_advsvc=True)
res_dict = {'project_id': project_id}
try:
attributes._validate_privileges(ctx, res_dict)
except exc.HTTPBadRequest:
self.fail("HTTPBadRequest exception should not be raised.")
def test__validate_privileges_service_role_other_tenant(self):
project_id = 'fake_project'
ctx = context.Context(project_id='fake_project2',
roles=['service'])
res_dict = {'project_id': project_id}
try:
attributes._validate_privileges(ctx, res_dict)
except exc.HTTPBadRequest:
self.fail("HTTPBadRequest exception should not be raised.")
class TestRetrieveValidSortKeys(base.BaseTestCase):
def test_retrieve_valid_sort_keys(self):
attr_info = {
"id": {
"visible": True,
"is_sort_key": True
},
"name": {
"is_sort_key": True
},
"created_at": {
"is_sort_key": False
},
"tenant_id": {
"visible": True,
},
"project_id": {
"visible": True,
}
}
expect_val = {"id", "name"}
actual_val = attributes.retrieve_valid_sort_keys(attr_info)
self.assertEqual(expect_val, actual_val)
|