Replace mox with mock in novaclient and compute

Replace mox with mock in tests.test_novaclient and tests.compute.test_manager.

Change-Id: Ic01fe3fd7510312e1930e87306ccc352d443fb35
This commit is contained in:
Victor Stinner 2013-11-15 19:07:05 +01:00
parent fe158993bb
commit b6636897b1
2 changed files with 31 additions and 23 deletions

View File

@ -21,7 +21,7 @@ import mock
from ceilometer.compute import manager
from ceilometer import nova_client
from ceilometer.openstack.common.fixture import moxstubout
from ceilometer.openstack.common.fixture import mockpatch
from ceilometer.openstack.common import test
from ceilometer.tests import agentbase
@ -57,9 +57,14 @@ class TestRunTasks(agentbase.BaseAgentManagerTestCase):
# of instances to poll we can control the results.
self.instance = self._fake_instance('faux', 'active')
stillborn_instance = self._fake_instance('stillborn', 'error')
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
self.stubs.Set(nova_client.Client, 'instance_get_all_by_host',
lambda *x: [self.instance, stillborn_instance])
def instance_get_all_by_host(*args):
return [self.instance, stillborn_instance]
self.useFixture(mockpatch.PatchObject(
nova_client.Client,
'instance_get_all_by_host',
side_effect=lambda *x: [self.instance, stillborn_instance]))
def test_notifier_task(self):
self.mgr.setup_notifier_task()
@ -79,8 +84,8 @@ class TestRunTasks(agentbase.BaseAgentManagerTestCase):
def test_manager_exception_persistency(self):
super(TestRunTasks, self).test_manager_exception_persistency()
self.stubs.Set(nova_client.Client, 'instance_get_all_by_host',
lambda *x: self._raise_exception())
mgr = manager.AgentManager()
polling_task = manager.PollingTask(mgr)
polling_task.poll_and_publish()
with mock.patch.object(nova_client.Client, 'instance_get_all_by_host',
side_effect=lambda *x: self._raise_exception()):
mgr = manager.AgentManager()
polling_task = manager.PollingTask(mgr)
polling_task.poll_and_publish()

View File

@ -18,10 +18,11 @@
# under the License.
import mock
from mock import patch
import novaclient
from ceilometer import nova_client
from ceilometer.openstack.common.fixture import moxstubout
from ceilometer.openstack.common.fixture import mockpatch
from ceilometer.openstack.common import test
@ -30,10 +31,12 @@ class TestNovaClient(test.BaseTestCase):
def setUp(self):
super(TestNovaClient, self).setUp()
self.nv = nova_client.Client()
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
self.stubs.Set(self.nv.nova_client.flavors, 'get',
self.fake_flavors_get)
self.stubs.Set(self.nv.nova_client.images, 'get', self.fake_images_get)
self.useFixture(mockpatch.PatchObject(
self.nv.nova_client.flavors, 'get',
side_effect=self.fake_flavors_get))
self.useFixture(mockpatch.PatchObject(
self.nv.nova_client.images, 'get',
side_effect=self.fake_images_get))
@staticmethod
def fake_flavors_get(*args, **kwargs):
@ -87,10 +90,10 @@ class TestNovaClient(test.BaseTestCase):
return [a]
def test_instance_get_all_by_host(self):
self.stubs.Set(self.nv.nova_client.servers, 'list',
self.fake_servers_list)
with patch.object(self.nv.nova_client.servers, 'list',
side_effect=self.fake_servers_list):
instances = self.nv.instance_get_all_by_host('foobar')
instances = self.nv.instance_get_all_by_host('foobar')
self.assertEqual(len(instances), 1)
self.assertEqual(instances[0].flavor['name'], 'm1.tiny')
self.assertEqual(instances[0].image['name'], 'ubuntu-12.04-x86')
@ -106,10 +109,10 @@ class TestNovaClient(test.BaseTestCase):
return [a]
def test_instance_get_all_by_host_unknown_flavor(self):
self.stubs.Set(self.nv.nova_client.servers, 'list',
self.fake_servers_list_unknown_flavor)
with patch.object(self.nv.nova_client.servers, 'list',
side_effect=self.fake_servers_list_unknown_flavor):
instances = self.nv.instance_get_all_by_host('foobar')
instances = self.nv.instance_get_all_by_host('foobar')
self.assertEqual(len(instances), 1)
self.assertEqual(instances[0].flavor['name'], 'unknown-id-666')
@ -130,10 +133,10 @@ class TestNovaClient(test.BaseTestCase):
return [a]
def test_instance_get_all_by_host_unknown_image(self):
self.stubs.Set(self.nv.nova_client.servers, 'list',
self.fake_servers_list_unknown_image)
with patch.object(self.nv.nova_client.servers, 'list',
side_effect=self.fake_servers_list_unknown_image):
instances = self.nv.instance_get_all_by_host('foobar')
instances = self.nv.instance_get_all_by_host('foobar')
self.assertEqual(len(instances), 1)
self.assertEqual(instances[0].image['name'], 'unknown-id-666')