From a5c26aed9f9544d1a290b59d138f5d884f8e6ad1 Mon Sep 17 00:00:00 2001 From: jichenjc Date: Sat, 19 Dec 2015 11:09:37 +0800 Subject: [PATCH] Use stub_out and mock to remove mox: part 1 This change replaces the use of stubs in a few files with stub_out, which was introduced in test.py. test_quota.py Part of bp:remove-mox Change-Id: I87508b934a2002edc4f79b85f151ebdccd733ec0 --- nova/tests/unit/test_quota.py | 49 +++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/nova/tests/unit/test_quota.py b/nova/tests/unit/test_quota.py index 6534cf260475..c1e5d56f185b 100644 --- a/nova/tests/unit/test_quota.py +++ b/nova/tests/unit/test_quota.py @@ -866,7 +866,7 @@ class DbQuotaDriverTestCase(test.TestCase): metadata_items=64, injected_file_content_bytes=5 * 1024, ) - self.stubs.Set(db, 'quota_class_get_default', fake_qcgd) + self.stub_out('nova.db.quota_class_get_default', fake_qcgd) def _stub_quota_class_get_all_by_name(self): # Stub out quota_class_get_all_by_name @@ -879,7 +879,7 @@ class DbQuotaDriverTestCase(test.TestCase): metadata_items=64, injected_file_content_bytes=5 * 1024, ) - self.stubs.Set(db, 'quota_class_get_all_by_name', fake_qcgabn) + self.stub_out('nova.db.quota_class_get_all_by_name', fake_qcgabn) def test_get_class_quotas(self): self._stub_quota_class_get_all_by_name() @@ -952,9 +952,10 @@ class DbQuotaDriverTestCase(test.TestCase): injected_file_path_bytes=dict(in_use=0, reserved=0), ) - self.stubs.Set(db, 'quota_get_all_by_project_and_user', fake_qgabpau) - self.stubs.Set(db, 'quota_get_all_by_project', fake_qgabp) - self.stubs.Set(db, 'quota_usage_get_all_by_project_and_user', + self.stub_out('nova.db.quota_get_all_by_project_and_user', + fake_qgabpau) + self.stub_out('nova.db.quota_get_all_by_project', fake_qgabp) + self.stub_out('nova.db.quota_usage_get_all_by_project_and_user', fake_qugabpau) self._stub_quota_class_get_all_by_name() @@ -1054,7 +1055,7 @@ class DbQuotaDriverTestCase(test.TestCase): return dict( test_resource=dict(in_use=20, reserved=10), ) - self.stubs.Set(db, 'quota_get', fake_quota_get) + self.stub_out('nova.db.quota_get', fake_quota_get) def test_get_by_project_and_user(self): self._stub_get_by_project_and_user_specific() @@ -1099,9 +1100,9 @@ class DbQuotaDriverTestCase(test.TestCase): sqa_models.ProjectUserQuota(resource='cores', hard_limit=2)] - self.stubs.Set(db, 'quota_get_all_by_project', fake_qgabp) - self.stubs.Set(db, 'quota_usage_get_all_by_project', fake_qugabp) - self.stubs.Set(db, 'quota_get_all', fake_quota_get_all) + self.stub_out('nova.db.quota_get_all_by_project', fake_qgabp) + self.stub_out('nova.db.quota_usage_get_all_by_project', fake_qugabp) + self.stub_out('nova.db.quota_get_all', fake_quota_get_all) self._stub_quota_class_get_all_by_name() self._stub_quota_class_get_default() @@ -1815,7 +1816,7 @@ class DbQuotaDriverTestCase(test.TestCase): self.calls.append('quota_get_all_by_project') return {'floating_ips': 20} - def fake_get_project_quotas(context, resources, project_id, + def fake_get_project_quotas(dbdrv, context, resources, project_id, quota_class=None, defaults=True, usages=True, remains=False, project_quotas=None): @@ -1842,7 +1843,7 @@ class DbQuotaDriverTestCase(test.TestCase): 'reserved': reserved, 'remains': remains} return result - def fake_process_quotas_in_get_user_quotas(context, resources, + def fake_process_quotas_in_get_user_quotas(dbdrv, context, resources, project_id, quotas, quota_class=None, defaults=True, usages=None, @@ -1866,13 +1867,13 @@ class DbQuotaDriverTestCase(test.TestCase): self.calls.append('quota_get_all_by_project_and_user') return {'instances': 2, 'cores': -1} - self.stubs.Set(db, 'quota_get_all_by_project', + self.stub_out('nova.db.quota_get_all_by_project', fake_quota_get_all_by_project) - self.stubs.Set(self.driver, 'get_project_quotas', + self.stub_out('nova.quota.DbQuotaDriver.get_project_quotas', fake_get_project_quotas) - self.stubs.Set(self.driver, '_process_quotas', + self.stub_out('nova.quota.DbQuotaDriver._process_quotas', fake_process_quotas_in_get_user_quotas) - self.stubs.Set(db, 'quota_get_all_by_project_and_user', + self.stub_out('nova.db.quota_get_all_by_project_and_user', fake_qgabpau) def test_get_settable_quotas_with_user(self): @@ -2087,14 +2088,14 @@ class DbQuotaDriverTestCase(test.TestCase): }) def _stub_get_project_quotas(self): - def fake_get_project_quotas(context, resources, project_id, + def fake_get_project_quotas(dbdrv, context, resources, project_id, quota_class=None, defaults=True, usages=True, remains=False, project_quotas=None): self.calls.append('get_project_quotas') return {k: dict(limit=v.default) for k, v in resources.items()} - self.stubs.Set(self.driver, 'get_project_quotas', + self.stub_out('nova.quota.DbQuotaDriver.get_project_quotas', fake_get_project_quotas) def test_get_quotas_has_sync_unknown(self): @@ -2217,7 +2218,7 @@ class DbQuotaDriverTestCase(test.TestCase): self.calls.append(('quota_reserve', expire, until_refresh, max_age)) return ['resv-1', 'resv-2', 'resv-3'] - self.stubs.Set(db, 'quota_reserve', fake_quota_reserve) + self.stub_out('nova.db.quota_reserve', fake_quota_reserve) def test_reserve_bad_expire(self): self._stub_get_project_quotas() @@ -2325,7 +2326,7 @@ class DbQuotaDriverTestCase(test.TestCase): resource, kwargs)) if resource == 'nonexist': raise exception.QuotaUsageNotFound(project_id=project_id) - self.stubs.Set(db, 'quota_usage_update', fake_quota_usage_update) + self.stub_out('nova.db.quota_usage_update', fake_quota_usage_update) ctx = FakeContext('test_project', 'test_class') resources = ['res1', 'res2', 'nonexist', 'res4'] @@ -2473,11 +2474,13 @@ class QuotaReserveSqlAlchemyTestCase(test.TestCase): return reservation_ref - self.stubs.Set(sqa_api, 'get_session', fake_get_session) - self.stubs.Set(sqa_api, '_get_project_user_quota_usages', + self.stub_out('nova.db.sqlalchemy.api.get_session', fake_get_session) + self.stub_out('nova.db.sqlalchemy.api._get_project_user_quota_usages', fake_get_project_user_quota_usages) - self.stubs.Set(sqa_api, '_quota_usage_create', fake_quota_usage_create) - self.stubs.Set(sqa_api, '_reservation_create', fake_reservation_create) + self.stub_out('nova.db.sqlalchemy.api._quota_usage_create', + fake_quota_usage_create) + self.stub_out('nova.db.sqlalchemy.api._reservation_create', + fake_reservation_create) self.useFixture(test.TimeOverride())