From 98e446b3f0c53c80814cb2456edf5553408f912a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 31 Oct 2013 18:48:47 +0100 Subject: [PATCH] Replace mox with mocks in tests.compute.pollsters Change-Id: Iea844b86e64e7bf4a231406cd7532dd5c28002dc --- tests/compute/pollsters/base.py | 15 ++++++++------ tests/compute/pollsters/test_cpu.py | 25 +++++++++++++----------- tests/compute/pollsters/test_diskio.py | 3 +-- tests/compute/pollsters/test_instance.py | 4 ---- tests/compute/pollsters/test_net.py | 7 +------ 5 files changed, 25 insertions(+), 29 deletions(-) diff --git a/tests/compute/pollsters/base.py b/tests/compute/pollsters/base.py index 59b49e6d9..7cceb1718 100644 --- a/tests/compute/pollsters/base.py +++ b/tests/compute/pollsters/base.py @@ -21,18 +21,16 @@ import mock from ceilometer.openstack.common import test -from ceilometer.openstack.common.fixture import moxstubout -from ceilometer.compute.virt import inspector as virt_inspector class TestPollsterBase(test.BaseTestCase): def setUp(self): super(TestPollsterBase, self).setUp() - self.mox = self.useFixture(moxstubout.MoxStubout()).mox - self.mox.StubOutWithMock(virt_inspector, 'get_hypervisor_inspector') - self.inspector = self.mox.CreateMock(virt_inspector.Inspector) - virt_inspector.get_hypervisor_inspector().AndReturn(self.inspector) + + self.addCleanup(mock.patch.stopall) + + self.inspector = mock.Mock() self.instance = mock.MagicMock() self.instance.name = 'instance-00000001' setattr(self.instance, 'OS-EXT-SRV-ATTR:instance_name', @@ -40,3 +38,8 @@ class TestPollsterBase(test.BaseTestCase): self.instance.id = 1 self.instance.flavor = {'name': 'm1.small', 'id': 2, 'vcpus': 1, 'ram': 512, 'disk': 20, 'ephemeral': 0} + + patch_virt = mock.patch('ceilometer.compute.virt.inspector' + '.get_hypervisor_inspector', + mock.Mock(return_value=self.inspector)) + patch_virt.start() diff --git a/tests/compute/pollsters/test_cpu.py b/tests/compute/pollsters/test_cpu.py index 47002f8dc..3d8881810 100644 --- a/tests/compute/pollsters/test_cpu.py +++ b/tests/compute/pollsters/test_cpu.py @@ -21,6 +21,7 @@ import time import mock +import six from ceilometer.compute import manager from ceilometer.compute.pollsters import cpu @@ -36,14 +37,17 @@ class TestCPUPollster(base.TestPollsterBase): @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) def test_get_samples(self): - self.inspector.inspect_cpus(self.instance.name).AndReturn( - virt_inspector.CPUStats(time=1 * (10 ** 6), number=2)) - self.inspector.inspect_cpus(self.instance.name).AndReturn( - virt_inspector.CPUStats(time=3 * (10 ** 6), number=2)) - # cpu_time resets on instance restart - self.inspector.inspect_cpus(self.instance.name).AndReturn( - virt_inspector.CPUStats(time=2 * (10 ** 6), number=2)) - self.mox.ReplayAll() + next_value = iter(( + virt_inspector.CPUStats(time=1 * (10 ** 6), number=2), + virt_inspector.CPUStats(time=3 * (10 ** 6), number=2), + # cpu_time resets on instance restart + virt_inspector.CPUStats(time=2 * (10 ** 6), number=2), + )) + + def inspect_cpus(name): + return six.next(next_value) + + self.inspector.inspect_cpus = mock.Mock(side_effect=inspect_cpus) mgr = manager.AgentManager() pollster = cpu.CPUPollster() @@ -65,9 +69,8 @@ class TestCPUPollster(base.TestPollsterBase): @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) def test_get_samples_no_caching(self): - self.inspector.inspect_cpus(self.instance.name).AndReturn( - virt_inspector.CPUStats(time=1 * (10 ** 6), number=2)) - self.mox.ReplayAll() + cpu_stats = virt_inspector.CPUStats(time=1 * (10 ** 6), number=2) + self.inspector.inspect_cpus = mock.Mock(return_value=cpu_stats) mgr = manager.AgentManager() pollster = cpu.CPUPollster() diff --git a/tests/compute/pollsters/test_diskio.py b/tests/compute/pollsters/test_diskio.py index e0dcc8592..37c4dc8cd 100644 --- a/tests/compute/pollsters/test_diskio.py +++ b/tests/compute/pollsters/test_diskio.py @@ -38,8 +38,7 @@ class TestDiskPollsters(base.TestPollsterBase): def setUp(self): super(TestDiskPollsters, self).setUp() - self.inspector.inspect_disks(self.instance.name).AndReturn(self.DISKS) - self.mox.ReplayAll() + self.inspector.inspect_disks = mock.Mock(return_value=self.DISKS) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) def _check_get_samples(self, factory, name, expected_volume): diff --git a/tests/compute/pollsters/test_instance.py b/tests/compute/pollsters/test_instance.py index 23dfcde23..acdaead1a 100644 --- a/tests/compute/pollsters/test_instance.py +++ b/tests/compute/pollsters/test_instance.py @@ -33,8 +33,6 @@ class TestInstancePollster(base.TestPollsterBase): @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) def test_get_samples_instance(self): - self.mox.ReplayAll() - mgr = manager.AgentManager() pollster = pollsters_instance.InstancePollster() samples = list(pollster.get_samples(mgr, {}, self.instance)) @@ -48,8 +46,6 @@ class TestInstancePollster(base.TestPollsterBase): @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) def test_get_samples_instance_flavor(self): - self.mox.ReplayAll() - mgr = manager.AgentManager() pollster = pollsters_instance.InstanceFlavorPollster() samples = list(pollster.get_samples(mgr, {}, self.instance)) diff --git a/tests/compute/pollsters/test_net.py b/tests/compute/pollsters/test_net.py index 1d1c5afd1..6e846c287 100644 --- a/tests/compute/pollsters/test_net.py +++ b/tests/compute/pollsters/test_net.py @@ -67,8 +67,7 @@ class TestNetPollster(base.TestPollsterBase): (self.vnic1, stats1), (self.vnic2, stats2), ] - self.inspector.inspect_vnics(self.instance.name).AndReturn(vnics) - self.mox.ReplayAll() + self.inspector.inspect_vnics = mock.Mock(return_value=vnics) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) def _check_get_samples(self, factory, expected): @@ -138,10 +137,6 @@ class TestNetPollster(base.TestPollsterBase): class TestNetPollsterCache(base.TestPollsterBase): - def setUp(self): - super(TestNetPollsterCache, self).setUp() - self.mox.ReplayAll() - @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) def _check_get_samples_cache(self, factory): vnic0 = virt_inspector.Interface(