Merge "Replace mox with mocks in test_inspector"

This commit is contained in:
Jenkins 2013-11-12 15:07:54 +00:00 committed by Gerrit Code Review
commit 4e9f5bf11d

View File

@ -23,7 +23,8 @@ from ceilometer.compute.virt import inspector as virt_inspector
from ceilometer.compute.virt.libvirt import inspector as libvirt_inspector from ceilometer.compute.virt.libvirt import inspector as libvirt_inspector
import fixtures import fixtures
from ceilometer.openstack.common import test from ceilometer.openstack.common import test
from ceilometer.openstack.common.fixture import moxstubout import mock
from contextlib import nested
class TestLibvirtInspection(test.BaseTestCase): class TestLibvirtInspection(test.BaseTestCase):
@ -32,9 +33,9 @@ class TestLibvirtInspection(test.BaseTestCase):
super(TestLibvirtInspection, self).setUp() super(TestLibvirtInspection, self).setUp()
self.instance_name = 'instance-00000001' self.instance_name = 'instance-00000001'
self.inspector = libvirt_inspector.LibvirtInspector() self.inspector = libvirt_inspector.LibvirtInspector()
self.mox = self.useFixture(moxstubout.MoxStubout()).mox self.inspector.connection = mock.Mock()
self.inspector.connection = self.mox.CreateMockAnything() self.domain = mock.Mock()
self.domain = self.mox.CreateMockAnything() self.addCleanup(mock.patch.stopall)
def test_inspect_instances(self): def test_inspect_instances(self):
class FakeDomain(object): class FakeDomain(object):
@ -45,14 +46,13 @@ class TestLibvirtInspection(test.BaseTestCase):
return 'uuid' return 'uuid'
fake_domain = FakeDomain() fake_domain = FakeDomain()
self.inspector.connection.getCapabilities() connection = self.inspector.connection
self.inspector.connection.numOfDomains().AndReturn(1) with nested(mock.patch.object(connection, 'numOfDomains',
self.inspector.connection.getCapabilities() return_value=1),
self.inspector.connection.listDomainsID().AndReturn([42]) mock.patch.object(connection, 'listDomainsID',
self.inspector.connection.getCapabilities() return_value=[42]),
self.inspector.connection.lookupByID(42).AndReturn(fake_domain) mock.patch.object(connection, 'lookupByID',
self.mox.ReplayAll() return_value=fake_domain)):
inspected_instances = list(self.inspector.inspect_instances()) inspected_instances = list(self.inspector.inspect_instances())
self.assertEqual(len(inspected_instances), 1) self.assertEqual(len(inspected_instances), 1)
inspected_instance = inspected_instances[0] inspected_instance = inspected_instances[0]
@ -60,12 +60,11 @@ class TestLibvirtInspection(test.BaseTestCase):
self.assertEqual(inspected_instance.UUID, 'uuid') self.assertEqual(inspected_instance.UUID, 'uuid')
def test_inspect_cpus(self): def test_inspect_cpus(self):
self.inspector.connection.getCapabilities() with nested(mock.patch.object(self.inspector.connection,
self.inspector.connection.lookupByName(self.instance_name).AndReturn( 'lookupByName',
self.domain) return_value=self.domain),
self.domain.info().AndReturn((0L, 0L, 0L, 2L, 999999L)) mock.patch.object(self.domain, 'info',
self.mox.ReplayAll() return_value=(0L, 0L, 0L, 2L, 999999L))):
cpu_info = self.inspector.inspect_cpus(self.instance_name) cpu_info = self.inspector.inspect_cpus(self.instance_name)
self.assertEqual(cpu_info.number, 2L) self.assertEqual(cpu_info.number, 2L)
self.assertEqual(cpu_info.time, 999999L) self.assertEqual(cpu_info.time, 999999L)
@ -128,18 +127,20 @@ class TestLibvirtInspection(test.BaseTestCase):
</domain> </domain>
""" """
self.inspector.connection.getCapabilities() interface_stats = {
self.inspector.connection.lookupByName(self.instance_name).AndReturn( 'vnet0': (1L, 2L, 0L, 0L, 3L, 4L, 0L, 0L),
self.domain) 'vnet1': (5L, 6L, 0L, 0L, 7L, 8L, 0L, 0L),
self.domain.XMLDesc(0).AndReturn(dom_xml) 'vnet2': (9L, 10L, 0L, 0L, 11L, 12L, 0L, 0L),
self.domain.interfaceStats('vnet0').AndReturn((1L, 2L, 0L, 0L, }
3L, 4L, 0L, 0L)) interfaceStats = interface_stats.__getitem__
self.domain.interfaceStats('vnet1').AndReturn((5L, 6L, 0L, 0L,
7L, 8L, 0L, 0L))
self.domain.interfaceStats('vnet2').AndReturn((9L, 10L, 0L, 0L,
11L, 12L, 0L, 0L))
self.mox.ReplayAll()
connection = self.inspector.connection
with nested(mock.patch.object(connection, 'lookupByName',
return_value=self.domain),
mock.patch.object(self.domain, 'XMLDesc',
return_value=dom_xml),
mock.patch.object(self.domain, 'interfaceStats',
side_effect=interfaceStats)):
interfaces = list(self.inspector.inspect_vnics(self.instance_name)) interfaces = list(self.inspector.inspect_vnics(self.instance_name))
self.assertEqual(len(interfaces), 3) self.assertEqual(len(interfaces), 3)
@ -197,14 +198,13 @@ class TestLibvirtInspection(test.BaseTestCase):
</domain> </domain>
""" """
self.inspector.connection.getCapabilities() with nested(mock.patch.object(self.inspector.connection,
self.inspector.connection.lookupByName(self.instance_name).AndReturn( 'lookupByName',
self.domain) return_value=self.domain),
self.domain.XMLDesc(0).AndReturn(dom_xml) mock.patch.object(self.domain, 'XMLDesc',
return_value=dom_xml),
self.domain.blockStats('vda').AndReturn((1L, 2L, 3L, 4L, -1)) mock.patch.object(self.domain, 'blockStats',
self.mox.ReplayAll() return_value=(1L, 2L, 3L, 4L, -1))):
disks = list(self.inspector.inspect_disks(self.instance_name)) disks = list(self.inspector.inspect_disks(self.instance_name))
self.assertEqual(len(disks), 1) self.assertEqual(len(disks), 1)