Merge "Replace mox with mocks in tests.compute.pollsters"

This commit is contained in:
Jenkins 2013-11-08 10:04:10 +00:00 committed by Gerrit Code Review
commit c40b23f9ec
5 changed files with 25 additions and 29 deletions

View File

@ -21,18 +21,16 @@
import mock import mock
from ceilometer.openstack.common import test 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): class TestPollsterBase(test.BaseTestCase):
def setUp(self): def setUp(self):
super(TestPollsterBase, self).setUp() super(TestPollsterBase, self).setUp()
self.mox = self.useFixture(moxstubout.MoxStubout()).mox
self.mox.StubOutWithMock(virt_inspector, 'get_hypervisor_inspector') self.addCleanup(mock.patch.stopall)
self.inspector = self.mox.CreateMock(virt_inspector.Inspector)
virt_inspector.get_hypervisor_inspector().AndReturn(self.inspector) self.inspector = mock.Mock()
self.instance = mock.MagicMock() self.instance = mock.MagicMock()
self.instance.name = 'instance-00000001' self.instance.name = 'instance-00000001'
setattr(self.instance, 'OS-EXT-SRV-ATTR:instance_name', setattr(self.instance, 'OS-EXT-SRV-ATTR:instance_name',
@ -40,3 +38,8 @@ class TestPollsterBase(test.BaseTestCase):
self.instance.id = 1 self.instance.id = 1
self.instance.flavor = {'name': 'm1.small', 'id': 2, 'vcpus': 1, self.instance.flavor = {'name': 'm1.small', 'id': 2, 'vcpus': 1,
'ram': 512, 'disk': 20, 'ephemeral': 0} '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()

View File

@ -21,6 +21,7 @@
import time import time
import mock import mock
import six
from ceilometer.compute import manager from ceilometer.compute import manager
from ceilometer.compute.pollsters import cpu from ceilometer.compute.pollsters import cpu
@ -36,14 +37,17 @@ class TestCPUPollster(base.TestPollsterBase):
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def test_get_samples(self): def test_get_samples(self):
self.inspector.inspect_cpus(self.instance.name).AndReturn( next_value = iter((
virt_inspector.CPUStats(time=1 * (10 ** 6), number=2)) 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),
virt_inspector.CPUStats(time=3 * (10 ** 6), number=2))
# cpu_time resets on instance restart # cpu_time resets on instance restart
self.inspector.inspect_cpus(self.instance.name).AndReturn( virt_inspector.CPUStats(time=2 * (10 ** 6), number=2),
virt_inspector.CPUStats(time=2 * (10 ** 6), number=2)) ))
self.mox.ReplayAll()
def inspect_cpus(name):
return six.next(next_value)
self.inspector.inspect_cpus = mock.Mock(side_effect=inspect_cpus)
mgr = manager.AgentManager() mgr = manager.AgentManager()
pollster = cpu.CPUPollster() pollster = cpu.CPUPollster()
@ -65,9 +69,8 @@ class TestCPUPollster(base.TestPollsterBase):
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def test_get_samples_no_caching(self): def test_get_samples_no_caching(self):
self.inspector.inspect_cpus(self.instance.name).AndReturn( cpu_stats = virt_inspector.CPUStats(time=1 * (10 ** 6), number=2)
virt_inspector.CPUStats(time=1 * (10 ** 6), number=2)) self.inspector.inspect_cpus = mock.Mock(return_value=cpu_stats)
self.mox.ReplayAll()
mgr = manager.AgentManager() mgr = manager.AgentManager()
pollster = cpu.CPUPollster() pollster = cpu.CPUPollster()

View File

@ -38,8 +38,7 @@ class TestDiskPollsters(base.TestPollsterBase):
def setUp(self): def setUp(self):
super(TestDiskPollsters, self).setUp() super(TestDiskPollsters, self).setUp()
self.inspector.inspect_disks(self.instance.name).AndReturn(self.DISKS) self.inspector.inspect_disks = mock.Mock(return_value=self.DISKS)
self.mox.ReplayAll()
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def _check_get_samples(self, factory, name, expected_volume): def _check_get_samples(self, factory, name, expected_volume):

View File

@ -33,8 +33,6 @@ class TestInstancePollster(base.TestPollsterBase):
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def test_get_samples_instance(self): def test_get_samples_instance(self):
self.mox.ReplayAll()
mgr = manager.AgentManager() mgr = manager.AgentManager()
pollster = pollsters_instance.InstancePollster() pollster = pollsters_instance.InstancePollster()
samples = list(pollster.get_samples(mgr, {}, self.instance)) samples = list(pollster.get_samples(mgr, {}, self.instance))
@ -48,8 +46,6 @@ class TestInstancePollster(base.TestPollsterBase):
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def test_get_samples_instance_flavor(self): def test_get_samples_instance_flavor(self):
self.mox.ReplayAll()
mgr = manager.AgentManager() mgr = manager.AgentManager()
pollster = pollsters_instance.InstanceFlavorPollster() pollster = pollsters_instance.InstanceFlavorPollster()
samples = list(pollster.get_samples(mgr, {}, self.instance)) samples = list(pollster.get_samples(mgr, {}, self.instance))

View File

@ -67,8 +67,7 @@ class TestNetPollster(base.TestPollsterBase):
(self.vnic1, stats1), (self.vnic1, stats1),
(self.vnic2, stats2), (self.vnic2, stats2),
] ]
self.inspector.inspect_vnics(self.instance.name).AndReturn(vnics) self.inspector.inspect_vnics = mock.Mock(return_value=vnics)
self.mox.ReplayAll()
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def _check_get_samples(self, factory, expected): def _check_get_samples(self, factory, expected):
@ -138,10 +137,6 @@ class TestNetPollster(base.TestPollsterBase):
class TestNetPollsterCache(base.TestPollsterBase): class TestNetPollsterCache(base.TestPollsterBase):
def setUp(self):
super(TestNetPollsterCache, self).setUp()
self.mox.ReplayAll()
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def _check_get_samples_cache(self, factory): def _check_get_samples_cache(self, factory):
vnic0 = virt_inspector.Interface( vnic0 = virt_inspector.Interface(