Merge "db: Final cleanup for context-based enginefacade"

This commit is contained in:
Zuul 2022-06-09 16:48:46 +00:00 committed by Gerrit Code Review
commit cf786fbd37
7 changed files with 411 additions and 413 deletions

File diff suppressed because it is too large Load Diff

View File

@ -135,9 +135,12 @@ class TransfersTableTestCase(test.TestCase):
db.transfer_accept(nctxt.elevated(), xfer_id, fake.USER2_ID,
fake.PROJECT2_ID)
xfer = db_api.model_query(
nctxt.elevated(), models.Transfer, read_deleted='yes'
).filter_by(id=xfer_id).first()
nctxt_admin = nctxt.elevated()
with db_api.main_context_manager.reader.using(nctxt_admin):
xfer = db_api.model_query(
nctxt_admin, models.Transfer, read_deleted='yes'
).filter_by(id=xfer_id).first()
self.assertEqual(volume.project_id, xfer['source_project_id'])
self.assertTrue(xfer['accepted'])

View File

@ -561,8 +561,9 @@ class DBAPIVolumeTestCase(BaseTest):
'volume_type_id': fake.VOLUME_TYPE_ID,
'use_quota': False})
result = sqlalchemy_api._volume_data_get_for_project(
self.ctxt, 'project', skip_internal=skip_internal)
with sqlalchemy_api.main_context_manager.reader.using(self.ctxt):
result = sqlalchemy_api._volume_data_get_for_project(
self.ctxt, 'project', skip_internal=skip_internal)
self.assertEqual((count, gigabytes), result)
@ddt.data((True, THREE_HUNDREDS, THREE),
@ -583,8 +584,9 @@ class DBAPIVolumeTestCase(BaseTest):
'volume_type_id': fake.VOLUME_TYPE_ID,
'admin_metadata': {'temporary': 'True'}})
result = sqlalchemy_api._volume_data_get_for_project(
self.ctxt, 'project', skip_internal=skip_internal)
with sqlalchemy_api.main_context_manager.reader.using(self.ctxt):
result = sqlalchemy_api._volume_data_get_for_project(
self.ctxt, 'project', skip_internal=skip_internal)
self.assertEqual((count, gigabytes), result)
def test_volume_data_get_for_project_with_host(self):
@ -1521,7 +1523,7 @@ class DBAPIVolumeTestCase(BaseTest):
@ddt.data(common.METADATA_TYPES.user, common.METADATA_TYPES.image)
@mock.patch.object(timeutils, 'utcnow')
@mock.patch.object(sqlalchemy_api, 'resource_exists')
@mock.patch.object(sqlalchemy_api, 'conditional_update')
@mock.patch.object(sqlalchemy_api, '_conditional_update')
@mock.patch.object(sqlalchemy_api, '_volume_x_metadata_get_query')
def test_volume_metadata_delete_deleted_at_updated(self,
meta_type,
@ -2187,8 +2189,9 @@ class DBAPISnapshotTestCase(BaseTest):
'use_quota': False,
'volume_size': ONE_HUNDREDS})
result = sqlalchemy_api._snapshot_data_get_for_project(
self.ctxt, 'project', skip_internal=skip_internal)
with sqlalchemy_api.main_context_manager.reader.using(self.ctxt):
result = sqlalchemy_api._snapshot_data_get_for_project(
self.ctxt, 'project', skip_internal=skip_internal)
self.assertEqual(expected, result)
@ -2411,7 +2414,8 @@ class DBAPIVolumeTypeTestCase(BaseTest):
'extra_specs': vt_extra_specs})
volume_ref = db.volume_create(self.ctxt, {'volume_type_id': vt.id})
volume = sqlalchemy_api._volume_get(self.ctxt, volume_ref.id)
with sqlalchemy_api.main_context_manager.reader.using(self.ctxt):
volume = sqlalchemy_api._volume_get(self.ctxt, volume_ref.id)
actual_specs = {}
for spec in volume.volume_type.extra_specs:
@ -2568,8 +2572,9 @@ class DBAPIReservationTestCase(BaseTest):
def test__get_reservation_resources(self):
reservations = _quota_reserve(self.ctxt, 'project1')
expected = ['gigabytes', 'volumes']
resources = sqlalchemy_api._get_reservation_resources(
self.ctxt, reservations)
with sqlalchemy_api.main_context_manager.reader.using(self.ctxt):
resources = sqlalchemy_api._get_reservation_resources(
self.ctxt, reservations)
self.assertEqual(expected, sorted(resources))
def test_reservation_commit(self):
@ -2908,15 +2913,17 @@ class DBAPIQuotaTestCase(BaseTest):
def test__get_quota_usages(self):
_quota_reserve(self.ctxt, 'project1')
quota_usage = sqlalchemy_api._get_quota_usages(
self.ctxt, 'project1')
with sqlalchemy_api.main_context_manager.reader.using(self.ctxt):
quota_usage = sqlalchemy_api._get_quota_usages(
self.ctxt, 'project1')
self.assertEqual(['gigabytes', 'volumes'],
sorted(quota_usage.keys()))
def test__get_quota_usages_with_resources(self):
_quota_reserve(self.ctxt, 'project1')
quota_usage = sqlalchemy_api._get_quota_usages(
self.ctxt, 'project1', resources=['volumes'])
with sqlalchemy_api.main_context_manager.reader.using(self.ctxt):
quota_usage = sqlalchemy_api._get_quota_usages(
self.ctxt, 'project1', resources=['volumes'])
self.assertEqual(['volumes'], list(quota_usage.keys()))
@mock.patch('oslo_utils.timeutils.utcnow', return_value=UTC_NOW)

View File

@ -23,7 +23,8 @@ class TestPaginateQuery(test.TestCase):
self.ctxt = context.RequestContext(fake.USER_ID, fake.PROJECT_ID,
auth_token=True,
is_admin=True)
self.query = db_api._volume_get_query(self.ctxt)
with db_api.main_context_manager.reader.using(self.ctxt):
self.query = db_api._volume_get_query(self.ctxt)
self.model = models.Volume
def test_paginate_query_marker_null(self):

View File

@ -1861,54 +1861,75 @@ class QuotaVolumeTypeReservationTestCase(test.TestCase):
def setUp(self):
super(QuotaVolumeTypeReservationTestCase, self).setUp()
self.user_id = fake.USER_ID
self.project_id = fake.PROJECT_ID
self.context = context.RequestContext(
self.user_id, self.project_id, is_admin=True,
)
self.volume_type_name = CONF.default_volume_type
self.volume_type = db.volume_type_get_by_name(
context.get_admin_context(),
name=self.volume_type_name)
self.context, name=self.volume_type_name,
)
@mock.patch.object(quota.QUOTAS, 'reserve')
@mock.patch.object(quota.QUOTAS, 'add_volume_type_opts')
def test_volume_type_reservation(self,
mock_add_volume_type_opts,
mock_reserve):
my_context = FakeContext('MyProject', None)
volume = fake_volume.fake_volume_obj(my_context,
name= 'my_vol_name',
id= fake.VOLUME_ID,
size= 1,
project_id= 'vol_project_id')
quota_utils.get_volume_type_reservation(my_context,
volume,
self.volume_type['id'])
def test_volume_type_reservation(
self,
mock_add_volume_type_opts,
mock_reserve,
):
volume = fake_volume.fake_volume_obj(
self.context,
name='my_vol_name',
id=fake.VOLUME_ID,
size=1,
project_id='vol_project_id',
)
quota_utils.get_volume_type_reservation(
self.context,
volume,
self.volume_type['id'],
)
reserve_opts = {'volumes': 1, 'gigabytes': volume.size}
mock_add_volume_type_opts.assert_called_once_with(
my_context,
self.context,
reserve_opts,
self.volume_type['id'])
mock_reserve.assert_called_once_with(my_context,
project_id='vol_project_id',
gigabytes=1,
volumes=1)
self.volume_type['id'],
)
mock_reserve.assert_called_once_with(
self.context,
project_id='vol_project_id',
gigabytes=1,
volumes=1,
)
@mock.patch.object(quota.QUOTAS, 'reserve')
def test_volume_type_reservation_with_type_only(self, mock_reserve):
my_context = FakeContext('MyProject', None)
volume = fake_volume.fake_volume_obj(my_context,
name='my_vol_name',
id=fake.VOLUME_ID,
size=1,
project_id='vol_project_id')
quota_utils.get_volume_type_reservation(my_context,
volume,
self.volume_type['id'],
reserve_vol_type_only=True)
volume = fake_volume.fake_volume_obj(
self.context,
name='my_vol_name',
id=fake.VOLUME_ID,
size=1,
project_id='vol_project_id',
)
quota_utils.get_volume_type_reservation(
self.context,
volume,
self.volume_type['id'],
reserve_vol_type_only=True,
)
vtype_volume_quota = "%s_%s" % ('volumes', self.volume_type['name'])
vtype_size_quota = "%s_%s" % ('gigabytes', self.volume_type['name'])
reserve_opts = {vtype_volume_quota: 1,
vtype_size_quota: volume.size}
mock_reserve.assert_called_once_with(my_context,
project_id='vol_project_id',
**reserve_opts)
reserve_opts = {
vtype_volume_quota: 1,
vtype_size_quota: volume.size,
}
mock_reserve.assert_called_once_with(
self.context,
project_id='vol_project_id',
**reserve_opts,
)
@ddt.data({'count_snaps': True, 'negative': True},
{'count_snaps': True, 'negative': False},
@ -1929,21 +1950,22 @@ class QuotaVolumeTypeReservationTestCase(test.TestCase):
It should work for negative and positive quotas.
"""
self.override_config('no_snapshot_gb_quota', not count_snaps)
my_context = FakeContext('MyProject', None)
snaps = [fake_snapshot.fake_db_snapshot(volume_size=1),
fake_snapshot.fake_db_snapshot(volume_size=2)]
volume = fake_volume.fake_volume_obj(my_context,
expected_attrs=['snapshots'],
name='my_vol_name',
id=fake.VOLUME_ID,
size=1,
project_id=fake.PROJECT_ID,
snapshots=snaps)
quota_utils.get_volume_type_reservation(my_context,
volume,
self.volume_type['id'],
reserve_vol_type_only=True,
negative=negative)
volume = fake_volume.fake_volume_obj(
self.context,
expected_attrs=['snapshots'],
name='my_vol_name',
id=fake.VOLUME_ID,
size=1,
project_id=fake.PROJECT_ID,
snapshots=snaps)
quota_utils.get_volume_type_reservation(
self.context,
volume,
self.volume_type['id'],
reserve_vol_type_only=True,
negative=negative)
factor = -1 if negative else 1
if count_snaps:
@ -1953,10 +1975,12 @@ class QuotaVolumeTypeReservationTestCase(test.TestCase):
vtype_volume_quota = "volumes_%s" % self.volume_type['name']
vtype_snapshot_quota = "snapshots_%s" % self.volume_type['name']
vtype_size_quota = "%s_%s" % ('gigabytes', self.volume_type['name'])
reserve_opts = {vtype_volume_quota: factor * 1,
vtype_snapshot_quota: factor * 2,
vtype_size_quota: factor * (volume['size'] +
snaps_size)}
mock_reserve.assert_called_once_with(my_context,
project_id=fake.PROJECT_ID,
**reserve_opts)
reserve_opts = {
vtype_volume_quota: factor * 1,
vtype_snapshot_quota: factor * 2,
vtype_size_quota: factor * (volume['size'] + snaps_size),
}
mock_reserve.assert_called_once_with(
self.context,
project_id=fake.PROJECT_ID,
**reserve_opts)

View File

@ -446,9 +446,10 @@ class VolumeTransferTestCase(test.TestCase):
tx_api.accept(self.ctxt, transfer['id'], transfer['auth_key'])
xfer = db_api.model_query(self.ctxt, models.Transfer,
read_deleted='yes'
).filter_by(id=transfer['id']).first()
with db_api.main_context_manager.reader.using(self.ctxt):
xfer = db_api.model_query(
self.ctxt, models.Transfer, read_deleted='yes'
).filter_by(id=transfer['id']).first()
self.assertEqual(volume.project_id, xfer['source_project_id'])
self.assertTrue(xfer['accepted'])
self.assertEqual(fake.PROJECT2_ID, xfer['destination_project_id'])

View File

@ -121,8 +121,10 @@ class VolumeTypeTestCase(test.TestCase):
'drive type was not deleted')
# Assert that associated volume type access is deleted successfully
# on destroying the volume type
vol_type_access = db_api._volume_type_access_query(
self.ctxt).filter_by(volume_type_id=type_ref['id']).all()
with db_api.main_context_manager.reader.using(self.ctxt):
vol_type_access = db_api._volume_type_access_query(
self.ctxt
).filter_by(volume_type_id=type_ref['id']).all()
self.assertEqual([], vol_type_access)
@mock.patch('cinder.quota.VolumeTypeQuotaEngine.'