Fix uuid warnings in various api contrib unit tests

This commit fixes invalid uuid usage in various
unit tests under cinder/tests/unit/api/contrib,
cleaning up FutureWarnings from oslo_versioned_objects [1].

[1] http://docs.openstack.org/developer/oslo.versionedobjects/api/fields.html#oslo_versionedobjects.fields.UUIDField

Change-Id: Ieb2f63b16d3d2ca264409a37863da47eeaa66206
This commit is contained in:
Tom Barron 2016-04-12 05:59:56 -04:00
parent 93be776287
commit 1fe4ed1a0e
7 changed files with 95 additions and 75 deletions

View File

@ -22,6 +22,7 @@ from cinder import context
from cinder import exception
from cinder import test
from cinder.tests.unit.api import fakes
from cinder.tests.unit import fake_constants as fake
def rpcapi_get_capabilities(self, context, host, discover):
@ -63,7 +64,7 @@ class CapabilitiesAPITest(test.TestCase):
super(CapabilitiesAPITest, self).setUp()
self.flags(host='fake')
self.controller = capabilities.CapabilitiesController()
self.ctxt = context.RequestContext('admin', 'fake', True)
self.ctxt = context.RequestContext(fake.USER_ID, fake.PROJECT_ID, True)
@mock.patch('cinder.db.service_get_all')
@mock.patch('cinder.volume.rpcapi.VolumeAPI.get_capabilities',

View File

@ -30,8 +30,10 @@ from cinder import db
from cinder import exception
from cinder import quota
from cinder import test
from cinder.tests.unit import fake_constants as fake
from cinder.tests.unit import test_db_api
from keystonemiddleware import auth_token
from oslo_config import cfg
from oslo_config import fixture as config_fixture
@ -42,7 +44,7 @@ CONF = cfg.CONF
def make_body(root=True, gigabytes=1000, snapshots=10,
volumes=10, backups=10, backup_gigabytes=1000,
tenant_id='foo', per_volume_gigabytes=-1):
tenant_id=fake.PROJECT_ID, per_volume_gigabytes=-1):
resources = {'gigabytes': gigabytes,
'snapshots': snapshots,
'volumes': volumes,
@ -68,7 +70,7 @@ def make_body(root=True, gigabytes=1000, snapshots=10,
def make_subproject_body(root=True, gigabytes=0, snapshots=0,
volumes=0, backups=0, backup_gigabytes=0,
tenant_id='foo', per_volume_gigabytes=0):
tenant_id=fake.PROJECT_ID, per_volume_gigabytes=0):
return make_body(root=root, gigabytes=gigabytes, snapshots=snapshots,
volumes=volumes, backups=backups,
backup_gigabytes=backup_gigabytes, tenant_id=tenant_id,
@ -79,7 +81,7 @@ class QuotaSetsControllerTestBase(test.TestCase):
class FakeProject(object):
def __init__(self, id='foo', parent_id=None):
def __init__(self, id=fake.PROJECT_ID, parent_id=None):
self.id = id
self.parent_id = parent_id
self.subtree = None
@ -164,36 +166,36 @@ class QuotaSetsControllerTest(QuotaSetsControllerTestBase):
super(QuotaSetsControllerTest, self).setUp()
def test_defaults(self):
result = self.controller.defaults(self.req, 'foo')
result = self.controller.defaults(self.req, fake.PROJECT_ID)
self.assertDictMatch(make_body(), result)
def test_show(self):
result = self.controller.show(self.req, 'foo')
result = self.controller.show(self.req, fake.PROJECT_ID)
self.assertDictMatch(make_body(), result)
def test_show_not_authorized(self):
self.req.environ['cinder.context'].is_admin = False
self.req.environ['cinder.context'].user_id = 'bad_user'
self.req.environ['cinder.context'].project_id = 'bad_project'
self.req.environ['cinder.context'].user_id = fake.USER_ID
self.req.environ['cinder.context'].project_id = fake.PROJECT_ID
self.assertRaises(webob.exc.HTTPForbidden, self.controller.show,
self.req, 'foo')
self.req, fake.PROJECT2_ID)
def test_show_non_admin_user(self):
self.controller._get_quotas = mock.Mock(side_effect=
self.controller._get_quotas)
result = self.controller.show(self.req, 'foo')
result = self.controller.show(self.req, fake.PROJECT_ID)
self.assertDictMatch(make_body(), result)
self.controller._get_quotas.assert_called_with(
self.req.environ['cinder.context'], 'foo', False)
self.req.environ['cinder.context'], fake.PROJECT_ID, False)
def test_update(self):
body = make_body(gigabytes=2000, snapshots=15,
volumes=5, backups=5, tenant_id=None)
result = self.controller.update(self.req, 'foo', body)
result = self.controller.update(self.req, fake.PROJECT_ID, body)
self.assertDictMatch(body, result)
body = make_body(gigabytes=db.MAX_INT, tenant_id=None)
result = self.controller.update(self.req, 'foo', body)
result = self.controller.update(self.req, fake.PROJECT_ID, body)
self.assertDictMatch(body, result)
def test_update_subproject_not_in_hierarchy_non_nested(self):
@ -227,7 +229,7 @@ class QuotaSetsControllerTest(QuotaSetsControllerTestBase):
mock_validate_integer.return_value = 10
body = {'quota_set': {'volumes': 10}}
result = self.controller.update(self.req, 'foo', body)
result = self.controller.update(self.req, fake.PROJECT_ID, body)
self.assertEqual(10, result['quota_set']['volumes'])
self.assertTrue(mock_validate.called)
@ -236,105 +238,106 @@ class QuotaSetsControllerTest(QuotaSetsControllerTestBase):
def test_update_wrong_key(self):
body = {'quota_set': {'bad': 'bad'}}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
def test_update_invalid_value_key_value(self):
body = {'quota_set': {'gigabytes': "should_be_int"}}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
def test_update_invalid_type_key_value(self):
body = {'quota_set': {'gigabytes': None}}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
def test_update_multi_value_with_bad_data(self):
orig_quota = self.controller.show(self.req, 'foo')
orig_quota = self.controller.show(self.req, fake.PROJECT_ID)
body = make_body(gigabytes=2000, snapshots=15, volumes="should_be_int",
backups=5, tenant_id=None)
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
# Verify that quota values are not updated in db
new_quota = self.controller.show(self.req, 'foo')
new_quota = self.controller.show(self.req, fake.PROJECT_ID)
self.assertDictMatch(orig_quota, new_quota)
def test_update_bad_quota_limit(self):
body = {'quota_set': {'gigabytes': -1000}}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
body = {'quota_set': {'gigabytes': db.MAX_INT + 1}}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
def test_update_no_admin(self):
self.req.environ['cinder.context'].is_admin = False
self.req.environ['cinder.context'].project_id = 'foo'
self.req.environ['cinder.context'].project_id = fake.PROJECT_ID
self.req.environ['cinder.context'].user_id = 'foo_user'
self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
self.req, 'foo', make_body(tenant_id=None))
self.req, fake.PROJECT_ID, make_body(tenant_id=None))
def test_update_without_quota_set_field(self):
body = {'fake_quota_set': {'gigabytes': 100}}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
def test_update_empty_body(self):
body = {}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
def _commit_quota_reservation(self):
# Create simple quota and quota usage.
ctxt = context.get_admin_context()
res = test_db_api._quota_reserve(ctxt, 'foo')
db.reservation_commit(ctxt, res, 'foo')
expected = {'project_id': 'foo',
res = test_db_api._quota_reserve(ctxt, fake.PROJECT_ID)
db.reservation_commit(ctxt, res, fake.PROJECT_ID)
expected = {'project_id': fake.PROJECT_ID,
'volumes': {'reserved': 0, 'in_use': 1},
'gigabytes': {'reserved': 0, 'in_use': 2},
}
self.assertEqual(expected,
db.quota_usage_get_all_by_project(ctxt, 'foo'))
db.quota_usage_get_all_by_project(ctxt,
fake.PROJECT_ID))
def test_update_lower_than_existing_resources_when_skip_false(self):
self._commit_quota_reservation()
body = {'quota_set': {'volumes': 0},
'skip_validation': 'false'}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
# Ensure that validation works even if some resources are valid
body = {'quota_set': {'gigabytes': 1, 'volumes': 10},
'skip_validation': 'false'}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
def test_update_lower_than_existing_resources_when_skip_true(self):
self._commit_quota_reservation()
body = {'quota_set': {'volumes': 0},
'skip_validation': 'true'}
result = self.controller.update(self.req, 'foo', body)
result = self.controller.update(self.req, fake.PROJECT_ID, body)
self.assertEqual(body['quota_set']['volumes'],
result['quota_set']['volumes'])
def test_update_lower_than_existing_resources_without_skip_argument(self):
self._commit_quota_reservation()
body = {'quota_set': {'volumes': 0}}
result = self.controller.update(self.req, 'foo', body)
result = self.controller.update(self.req, fake.PROJECT_ID, body)
self.assertEqual(body['quota_set']['volumes'],
result['quota_set']['volumes'])
def test_delete(self):
result_show = self.controller.show(self.req, 'foo')
result_show = self.controller.show(self.req, fake.PROJECT_ID)
self.assertDictMatch(make_body(), result_show)
body = make_body(gigabytes=2000, snapshots=15,
volumes=5, backups=5,
backup_gigabytes=1000, tenant_id=None)
result_update = self.controller.update(self.req, 'foo', body)
result_update = self.controller.update(self.req, fake.PROJECT_ID, body)
self.assertDictMatch(body, result_update)
self.controller.delete(self.req, 'foo')
self.controller.delete(self.req, fake.PROJECT_ID)
result_show_after = self.controller.show(self.req, 'foo')
result_show_after = self.controller.show(self.req, fake.PROJECT_ID)
self.assertDictMatch(result_show, result_show_after)
def test_delete_with_allocated_quota_different_from_zero(self):
@ -361,7 +364,7 @@ class QuotaSetsControllerTest(QuotaSetsControllerTestBase):
def test_delete_no_admin(self):
self.req.environ['cinder.context'].is_admin = False
self.assertRaises(webob.exc.HTTPForbidden, self.controller.delete,
self.req, 'foo')
self.req, fake.PROJECT_ID)
def test_subproject_show_not_using_nested_quotas(self):
# Current roles say for non-nested quotas, an admin should be able to

View File

@ -27,6 +27,7 @@ from cinder.api.contrib import quota_classes
from cinder import context
from cinder import quota
from cinder import test
from cinder.tests.unit import fake_constants as fake
from cinder.volume import volume_types
@ -37,7 +38,7 @@ def make_body(root=True, gigabytes=1000, snapshots=10,
volumes=10, backups=10,
backup_gigabytes=1000, per_volume_gigabytes=-1,
volume_types_faked=None,
tenant_id='foo'):
tenant_id=fake.PROJECT_ID):
resources = {'gigabytes': gigabytes,
'snapshots': snapshots,
'volumes': volumes,
@ -61,7 +62,7 @@ def make_body(root=True, gigabytes=1000, snapshots=10,
def make_response_body(root=True, ctxt=None, quota_class='foo',
request_body=None, tenant_id='foo'):
request_body=None, tenant_id=fake.PROJECT_ID):
resources = {}
if not ctxt:
ctxt = context.get_admin_context()
@ -91,21 +92,21 @@ class QuotaClassSetsControllerTest(test.TestCase):
def test_show(self):
volume_types.create(self.ctxt, 'fake_type')
result = self.controller.show(self.req, 'foo')
result = self.controller.show(self.req, fake.PROJECT_ID)
self.assertDictMatch(make_body(), result)
def test_show_not_authorized(self):
self.req.environ['cinder.context'].is_admin = False
self.req.environ['cinder.context'].user_id = 'bad_user'
self.req.environ['cinder.context'].project_id = 'bad_project'
self.req.environ['cinder.context'].user_id = fake.USER_ID
self.req.environ['cinder.context'].project_id = fake.PROJECT_ID
self.assertRaises(webob.exc.HTTPForbidden, self.controller.show,
self.req, 'foo')
self.req, fake.PROJECT_ID)
def test_update(self):
volume_types.create(self.ctxt, 'fake_type')
body = make_body(gigabytes=2000, snapshots=15,
volumes=5, tenant_id=None)
result = self.controller.update(self.req, 'foo', body)
result = self.controller.update(self.req, fake.PROJECT_ID, body)
self.assertDictMatch(body, result)
@mock.patch('cinder.api.openstack.wsgi.Controller.validate_string_length')
@ -114,7 +115,7 @@ class QuotaClassSetsControllerTest(test.TestCase):
mock_validate_integer.return_value = 5
volume_types.create(self.ctxt, 'fake_type')
body = make_body(volumes=5)
result = self.controller.update(self.req, 'foo', body)
result = self.controller.update(self.req, fake.PROJECT_ID, body)
self.assertEqual(5, result['quota_class_set']['volumes'])
self.assertTrue(mock_validate.called)
self.assertTrue(mock_validate_integer.called)
@ -122,32 +123,32 @@ class QuotaClassSetsControllerTest(test.TestCase):
def test_update_wrong_key(self):
volume_types.create(self.ctxt, 'fake_type')
body = {'quota_class_set': {'bad': 'bad'}}
result = self.controller.update(self.req, 'foo', body)
result = self.controller.update(self.req, fake.PROJECT_ID, body)
self.assertDictMatch(make_body(tenant_id=None), result)
def test_update_invalid_key_value(self):
body = {'quota_class_set': {'gigabytes': "should_be_int"}}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
def test_update_bad_quota_limit(self):
body = {'quota_class_set': {'gigabytes': -1000}}
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
self.req, 'foo', body)
self.req, fake.PROJECT_ID, body)
def test_update_no_admin(self):
self.req.environ['cinder.context'].is_admin = False
self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
self.req, 'foo', make_body(tenant_id=None))
self.req, fake.PROJECT_ID, make_body(tenant_id=None))
def test_update_with_more_volume_types(self):
volume_types.create(self.ctxt, 'fake_type_1')
volume_types.create(self.ctxt, 'fake_type_2')
body = {'quota_class_set': {'gigabytes_fake_type_1': 1111,
'volumes_fake_type_2': 2222}}
result = self.controller.update(self.req, 'foo', body)
result = self.controller.update(self.req, fake.PROJECT_ID, body)
self.assertDictMatch(make_response_body(ctxt=self.ctxt,
quota_class='foo',
quota_class=fake.PROJECT_ID,
request_body=body,
tenant_id=None),
result)

View File

@ -19,9 +19,12 @@ from oslo_serialization import jsonutils
import cinder
from cinder.api.openstack import wsgi
from cinder import context
from cinder import test
from cinder.tests.unit.api import fakes
from cinder.tests.unit.api.v2 import stubs
from cinder.tests.unit import fake_constants as fake
UUID = fakes.FAKE_UUID
@ -30,7 +33,7 @@ class SchedulerHintsTestCase(test.TestCase):
def setUp(self):
super(SchedulerHintsTestCase, self).setUp()
self.fake_instance = stubs.stub_volume(1, uuid=UUID)
self.fake_instance = stubs.stub_volume(fake.VOLUME_ID, uuid=UUID)
self.fake_instance['created_at'] =\
datetime.datetime(2013, 1, 1, 1, 1, 1)
self.fake_instance['launched_at'] =\
@ -39,7 +42,9 @@ class SchedulerHintsTestCase(test.TestCase):
osapi_volume_extension=[
'cinder.api.contrib.select_extensions'],
osapi_volume_ext_list=['Scheduler_hints'])
self.app = fakes.wsgi_app()
self.user_ctxt = context.RequestContext(
fake.USER_ID, fake.PROJECT_ID, auth_token=True)
self.app = fakes.wsgi_app(fake_auth_context=self.user_ctxt)
def test_create_server_without_hints(self):
@ -51,12 +56,12 @@ class SchedulerHintsTestCase(test.TestCase):
self.stubs.Set(cinder.api.v2.volumes.VolumeController, 'create',
fake_create)
req = fakes.HTTPRequest.blank('/v2/fake/volumes')
req = fakes.HTTPRequest.blank('/v2/%s/volumes' % fake.PROJECT_ID)
req.method = 'POST'
req.content_type = 'application/json'
body = {'id': id,
'volume_type_id': 'cedef40a-ed67-4d10-800e-17455edce175',
'volume_id': '1', }
body = {'id': UUID,
'volume_type_id': fake.VOLUME_TYPE_ID,
'volume_id': fake.VOLUME_ID, }
req.body = jsonutils.dump_as_bytes(body)
res = req.get_response(self.app)
self.assertEqual(202, res.status_int)
@ -72,12 +77,12 @@ class SchedulerHintsTestCase(test.TestCase):
self.stubs.Set(cinder.api.v2.volumes.VolumeController, 'create',
fake_create)
req = fakes.HTTPRequest.blank('/v2/fake/volumes')
req = fakes.HTTPRequest.blank('/v2/%s/volumes' % fake.PROJECT_ID)
req.method = 'POST'
req.content_type = 'application/json'
body = {'id': id,
'volume_type_id': 'cedef40a-ed67-4d10-800e-17455edce175',
'volume_id': '1',
body = {'id': UUID,
'volume_type_id': fake.VOLUME_TYPE_ID,
'volume_id': fake.VOLUME_ID,
'scheduler_hints': {'a': 'b'}, }
req.body = jsonutils.dump_as_bytes(body)
@ -85,13 +90,13 @@ class SchedulerHintsTestCase(test.TestCase):
self.assertEqual(202, res.status_int)
def test_create_server_bad_hints(self):
req = fakes.HTTPRequest.blank('/v2/fake/volumes')
req = fakes.HTTPRequest.blank('/v2/%s/volumes' % fake.PROJECT_ID)
req.method = 'POST'
req.content_type = 'application/json'
body = {'volume': {
'id': id,
'volume_type_id': 'cedef40a-ed67-4d10-800e-17455edce175',
'volume_id': '1',
'id': UUID,
'volume_type_id': fake.VOLUME_TYPE_ID,
'volume_id': fake.VOLUME_ID,
'scheduler_hints': 'a', }}
req.body = jsonutils.dump_as_bytes(body)

View File

@ -20,6 +20,7 @@ from cinder.api.contrib import scheduler_stats
from cinder import context
from cinder import test
from cinder.tests.unit.api import fakes
from cinder.tests.unit import fake_constants as fake
def schedule_rpcapi_get_pools(self, context, filters=None):
@ -49,10 +50,11 @@ class SchedulerStatsAPITest(test.TestCase):
super(SchedulerStatsAPITest, self).setUp()
self.flags(host='fake')
self.controller = scheduler_stats.SchedulerStatsController()
self.ctxt = context.RequestContext('admin', 'fake', True)
self.ctxt = context.RequestContext(fake.USER_ID, fake.PROJECT_ID, True)
def test_get_pools_summery(self):
req = fakes.HTTPRequest.blank('/v2/fake/scheduler_stats')
req = fakes.HTTPRequest.blank('/v2/%s/scheduler_stats' %
fake.PROJECT_ID)
req.environ['cinder.context'] = self.ctxt
res = self.controller.get_pools(req)
@ -72,7 +74,8 @@ class SchedulerStatsAPITest(test.TestCase):
self.assertDictMatch(expected, res)
def test_get_pools_detail(self):
req = fakes.HTTPRequest.blank('/v2/fake/scheduler_stats?detail=True')
req = fakes.HTTPRequest.blank('/v2/%s/scheduler_stats?detail=True' %
fake.PROJECT_ID)
req.environ['cinder.context'] = self.ctxt
res = self.controller.get_pools(req)

View File

@ -28,6 +28,7 @@ from cinder import exception
from cinder import policy
from cinder import test
from cinder.tests.unit.api import fakes
from cinder.tests.unit import fake_constants as fake
fake_services_list = [
@ -594,27 +595,31 @@ class ServicesTest(test.TestCase):
def test_services_enable_with_service_key(self):
body = {'host': 'host1', 'service': 'cinder-volume'}
req = fakes.HTTPRequest.blank('/v2/fake/os-services/enable')
req = fakes.HTTPRequest.blank(
'/v2/%s/os-services/enable' % fake.PROJECT_ID)
res_dict = self.controller.update(req, "enable", body)
self.assertEqual('enabled', res_dict['status'])
def test_services_enable_with_binary_key(self):
body = {'host': 'host1', 'binary': 'cinder-volume'}
req = fakes.HTTPRequest.blank('/v2/fake/os-services/enable')
req = fakes.HTTPRequest.blank(
'/v2/%s/os-services/enable' % fake.PROJECT_ID)
res_dict = self.controller.update(req, "enable", body)
self.assertEqual('enabled', res_dict['status'])
def test_services_disable_with_service_key(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-services/disable')
req = fakes.HTTPRequest.blank(
'/v2/%s/os-services/disable' % fake.PROJECT_ID)
body = {'host': 'host1', 'service': 'cinder-volume'}
res_dict = self.controller.update(req, "disable", body)
self.assertEqual('disabled', res_dict['status'])
def test_services_disable_with_binary_key(self):
req = fakes.HTTPRequest.blank('/v2/fake/os-services/disable')
req = fakes.HTTPRequest.blank(
'/v2/%s/os-services/disable' % fake.PROJECT_ID)
body = {'host': 'host1', 'binary': 'cinder-volume'}
res_dict = self.controller.update(req, "disable", body)

View File

@ -20,6 +20,7 @@ from cinder.api.openstack import wsgi
from cinder import exception
from cinder import test
from cinder.tests.unit.api import fakes
from cinder.tests.unit import fake_constants as fake
class FakeRequest(object):
@ -36,7 +37,8 @@ class UsedLimitsTestCase(test.TestCase):
@mock.patch('cinder.quota.QUOTAS.get_project_quotas')
@mock.patch('cinder.policy.enforce')
def test_used_limits(self, _mock_policy_enforce, _mock_get_project_quotas):
fake_req = FakeRequest(fakes.FakeRequestContext('fake', 'fake'))
fake_req = FakeRequest(fakes.FakeRequestContext(fake.USER_ID,
fake.PROJECT_ID))
obj = {
"limits": {
"rate": [],