Replace mox with mock in energy and objectstore

Replace mox with mock in:

* tests.energy.test_kwapi
* tests.objectstore.test_swift
* tests.objectstore.test_swift_middleware

Change-Id: If1168c6a4884f4a01f33587a14e60ccb3819dbd1
This commit is contained in:
Victor Stinner 2013-11-15 19:50:36 +01:00
parent 4a638fab63
commit a976ae746c
3 changed files with 42 additions and 43 deletions

View File

@ -22,7 +22,7 @@ import mock
from ceilometer.central import manager from ceilometer.central import manager
from ceilometer.energy import kwapi from ceilometer.energy import kwapi
from ceilometer.openstack.common import context from ceilometer.openstack.common import context
from ceilometer.openstack.common.fixture import moxstubout from ceilometer.openstack.common.fixture.mockpatch import PatchObject
from ceilometer.openstack.common import test from ceilometer.openstack.common import test
@ -59,19 +59,19 @@ class TestKwapi(test.BaseTestCase):
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def setUp(self): def setUp(self):
super(TestKwapi, self).setUp() super(TestKwapi, self).setUp()
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
self.context = context.get_admin_context() self.context = context.get_admin_context()
self.manager = TestManager() self.manager = TestManager()
@staticmethod @staticmethod
def fake_get_kwapi_client(self, ksclient): def fake_get_kwapi_client(ksclient):
raise exceptions.EndpointNotFound("fake keystone exception") raise exceptions.EndpointNotFound("fake keystone exception")
def test_endpoint_not_exist(self): def test_endpoint_not_exist(self):
self.stubs.Set(kwapi._Base, 'get_kwapi_client', with PatchObject(kwapi._Base, 'get_kwapi_client',
self.fake_get_kwapi_client) side_effect=self.fake_get_kwapi_client):
pollster = kwapi.EnergyPollster()
samples = list(pollster.get_samples(self.manager, {}))
samples = list(kwapi.EnergyPollster().get_samples(self.manager, {}))
self.assertEqual(len(samples), 0) self.assertEqual(len(samples), 0)
@ -80,14 +80,13 @@ class TestEnergyPollster(test.BaseTestCase):
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def setUp(self): def setUp(self):
super(TestEnergyPollster, self).setUp() super(TestEnergyPollster, self).setUp()
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
self.context = context.get_admin_context() self.context = context.get_admin_context()
self.manager = TestManager() self.manager = TestManager()
self.stubs.Set(kwapi._Base, '_iter_probes', self.useFixture(PatchObject(kwapi._Base, '_iter_probes',
self.fake_iter_probes) side_effect=self.fake_iter_probes))
@staticmethod @staticmethod
def fake_iter_probes(self, ksclient, cache): def fake_iter_probes(ksclient, cache):
probes = PROBE_DICT['probes'] probes = PROBE_DICT['probes']
for key, value in probes.iteritems(): for key, value in probes.iteritems():
probe_dict = value probe_dict = value
@ -141,14 +140,13 @@ class TestPowerPollster(test.BaseTestCase):
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def setUp(self): def setUp(self):
super(TestPowerPollster, self).setUp() super(TestPowerPollster, self).setUp()
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
self.context = context.get_admin_context() self.context = context.get_admin_context()
self.manager = TestManager() self.manager = TestManager()
self.stubs.Set(kwapi._Base, '_iter_probes', self.useFixture(PatchObject(kwapi._Base, '_iter_probes',
self.fake_iter_probes) side_effect=self.fake_iter_probes))
@staticmethod @staticmethod
def fake_iter_probes(self, ksclient, cache): def fake_iter_probes(ksclient, cache):
probes = PROBE_DICT['probes'] probes = PROBE_DICT['probes']
for key, value in probes.iteritems(): for key, value in probes.iteritems():
probe_dict = value probe_dict = value

View File

@ -26,7 +26,7 @@ import testscenarios
from ceilometer.central import manager from ceilometer.central import manager
from ceilometer.objectstore import swift from ceilometer.objectstore import swift
from ceilometer.openstack.common.fixture import moxstubout from ceilometer.openstack.common.fixture.mockpatch import PatchObject
from ceilometer.openstack.common import test from ceilometer.openstack.common import test
load_tests = testscenarios.load_tests_apply_scenarios load_tests = testscenarios.load_tests_apply_scenarios
@ -72,17 +72,15 @@ class TestSwiftPollster(test.BaseTestCase):
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def setUp(self): def setUp(self):
super(TestSwiftPollster, self).setUp() super(TestSwiftPollster, self).setUp()
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
self.pollster = self.factory() self.pollster = self.factory()
self.manager = TestManager() self.manager = TestManager()
def test_iter_accounts_no_cache(self): def test_iter_accounts_no_cache(self):
def empty_account_info(obj, ksclient, cache):
return []
self.stubs.Set(self.factory, '_get_account_info',
empty_account_info)
cache = {} cache = {}
data = list(self.pollster._iter_accounts(mock.Mock(), cache)) with PatchObject(self.factory, '_get_account_info',
return_value=[]):
data = list(self.pollster._iter_accounts(mock.Mock(), cache))
self.assertTrue(self.pollster.CACHE_KEY_TENANT in cache) self.assertTrue(self.pollster.CACHE_KEY_TENANT in cache)
self.assertTrue(self.pollster.CACHE_KEY_HEAD in cache) self.assertTrue(self.pollster.CACHE_KEY_HEAD in cache)
self.assertEqual(data, []) self.assertEqual(data, [])
@ -95,15 +93,15 @@ class TestSwiftPollster(test.BaseTestCase):
ksclient.tenants.list.side_effect = AssertionError( ksclient.tenants.list.side_effect = AssertionError(
'should not be called', 'should not be called',
) )
self.stubs.Set(swift_client, 'head_account',
ksclient) with PatchObject(swift_client, 'head_account', new=ksclient):
self.stubs.Set(self.factory, '_neaten_url', with PatchObject(self.factory, '_neaten_url'):
mock.Mock()) Tenant = collections.namedtuple('Tenant', 'id')
Tenant = collections.namedtuple('Tenant', 'id') cache = {
cache = { self.pollster.CACHE_KEY_TENANT: [Tenant(ACCOUNTS[0][0])],
self.pollster.CACHE_KEY_TENANT: [Tenant(ACCOUNTS[0][0])], }
} data = list(self.pollster._iter_accounts(mock.Mock(), cache))
data = list(self.pollster._iter_accounts(mock.Mock(), cache))
self.assertTrue(self.pollster.CACHE_KEY_HEAD in cache) self.assertTrue(self.pollster.CACHE_KEY_HEAD in cache)
self.assertEqual(data[0][0], ACCOUNTS[0][0]) self.assertEqual(data[0][0], ACCOUNTS[0][0])
@ -126,20 +124,23 @@ class TestSwiftPollster(test.BaseTestCase):
test_tenant_id)) test_tenant_id))
def test_metering(self): def test_metering(self):
self.stubs.Set(self.factory, '_iter_accounts', with PatchObject(self.factory, '_iter_accounts',
self.fake_iter_accounts) side_effect=self.fake_iter_accounts):
samples = list(self.pollster.get_samples(self.manager, {})) samples = list(self.pollster.get_samples(self.manager, {}))
self.assertEqual(len(samples), 2) self.assertEqual(len(samples), 2)
def test_get_meter_names(self): def test_get_meter_names(self):
self.stubs.Set(self.factory, '_iter_accounts', with PatchObject(self.factory, '_iter_accounts',
self.fake_iter_accounts) side_effect=self.fake_iter_accounts):
samples = list(self.pollster.get_samples(self.manager, {})) samples = list(self.pollster.get_samples(self.manager, {}))
self.assertEqual(set([s.name for s in samples]), self.assertEqual(set([s.name for s in samples]),
set([samples[0].name])) set([samples[0].name]))
def test_endpoint_notfound(self): def test_endpoint_notfound(self):
self.stubs.Set(self.manager.keystone.service_catalog, 'url_for', with PatchObject(self.manager.keystone.service_catalog, 'url_for',
self.fake_ks_service_catalog_url_for) side_effect=self.fake_ks_service_catalog_url_for):
samples = list(self.pollster.get_samples(self.manager, {})) samples = list(self.pollster.get_samples(self.manager, {}))
self.assertEqual(len(samples), 0) self.assertEqual(len(samples), 0)

View File

@ -24,7 +24,7 @@ from webob import Request
from ceilometer.objectstore import swift_middleware from ceilometer.objectstore import swift_middleware
from ceilometer.openstack.common.fixture import config from ceilometer.openstack.common.fixture import config
from ceilometer.openstack.common.fixture import moxstubout from ceilometer.openstack.common.fixture.mockpatch import PatchObject
from ceilometer.openstack.common import test from ceilometer.openstack.common import test
from ceilometer import pipeline from ceilometer import pipeline
@ -60,14 +60,14 @@ class TestSwiftMiddleware(test.BaseTestCase):
def __init__(self): def __init__(self):
self.pipelines = [self._faux_pipeline(self)] self.pipelines = [self._faux_pipeline(self)]
def _faux_setup_pipeline(self, transformer_manager): def _fake_setup_pipeline(self, transformer_manager):
return self.pipeline_manager return self.pipeline_manager
def setUp(self): def setUp(self):
super(TestSwiftMiddleware, self).setUp() super(TestSwiftMiddleware, self).setUp()
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
self.pipeline_manager = self._faux_pipeline_manager() self.pipeline_manager = self._faux_pipeline_manager()
self.stubs.Set(pipeline, 'setup_pipeline', self._faux_setup_pipeline) self.useFixture(PatchObject(pipeline, 'setup_pipeline',
side_effect=self._fake_setup_pipeline))
self.CONF = self.useFixture(config.Config()).conf self.CONF = self.useFixture(config.Config()).conf
@staticmethod @staticmethod