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
import fixtures
from ceilometer.openstack.common import test
from ceilometer.openstack.common.fixture import moxstubout
import mock
from contextlib import nested
class TestLibvirtInspection(test.BaseTestCase):
@ -32,9 +33,9 @@ class TestLibvirtInspection(test.BaseTestCase):
super(TestLibvirtInspection, self).setUp()
self.instance_name = 'instance-00000001'
self.inspector = libvirt_inspector.LibvirtInspector()
self.mox = self.useFixture(moxstubout.MoxStubout()).mox
self.inspector.connection = self.mox.CreateMockAnything()
self.domain = self.mox.CreateMockAnything()
self.inspector.connection = mock.Mock()
self.domain = mock.Mock()
self.addCleanup(mock.patch.stopall)
def test_inspect_instances(self):
class FakeDomain(object):
@ -45,30 +46,28 @@ class TestLibvirtInspection(test.BaseTestCase):
return 'uuid'
fake_domain = FakeDomain()
self.inspector.connection.getCapabilities()
self.inspector.connection.numOfDomains().AndReturn(1)
self.inspector.connection.getCapabilities()
self.inspector.connection.listDomainsID().AndReturn([42])
self.inspector.connection.getCapabilities()
self.inspector.connection.lookupByID(42).AndReturn(fake_domain)
self.mox.ReplayAll()
inspected_instances = list(self.inspector.inspect_instances())
self.assertEqual(len(inspected_instances), 1)
inspected_instance = inspected_instances[0]
self.assertEqual(inspected_instance.name, 'fake_name')
self.assertEqual(inspected_instance.UUID, 'uuid')
connection = self.inspector.connection
with nested(mock.patch.object(connection, 'numOfDomains',
return_value=1),
mock.patch.object(connection, 'listDomainsID',
return_value=[42]),
mock.patch.object(connection, 'lookupByID',
return_value=fake_domain)):
inspected_instances = list(self.inspector.inspect_instances())
self.assertEqual(len(inspected_instances), 1)
inspected_instance = inspected_instances[0]
self.assertEqual(inspected_instance.name, 'fake_name')
self.assertEqual(inspected_instance.UUID, 'uuid')
def test_inspect_cpus(self):
self.inspector.connection.getCapabilities()
self.inspector.connection.lookupByName(self.instance_name).AndReturn(
self.domain)
self.domain.info().AndReturn((0L, 0L, 0L, 2L, 999999L))
self.mox.ReplayAll()
cpu_info = self.inspector.inspect_cpus(self.instance_name)
self.assertEqual(cpu_info.number, 2L)
self.assertEqual(cpu_info.time, 999999L)
with nested(mock.patch.object(self.inspector.connection,
'lookupByName',
return_value=self.domain),
mock.patch.object(self.domain, 'info',
return_value=(0L, 0L, 0L, 2L, 999999L))):
cpu_info = self.inspector.inspect_cpus(self.instance_name)
self.assertEqual(cpu_info.number, 2L)
self.assertEqual(cpu_info.time, 999999L)
def test_inspect_vnics(self):
dom_xml = """
@ -128,58 +127,60 @@ class TestLibvirtInspection(test.BaseTestCase):
</domain>
"""
self.inspector.connection.getCapabilities()
self.inspector.connection.lookupByName(self.instance_name).AndReturn(
self.domain)
self.domain.XMLDesc(0).AndReturn(dom_xml)
self.domain.interfaceStats('vnet0').AndReturn((1L, 2L, 0L, 0L,
3L, 4L, 0L, 0L))
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()
interface_stats = {
'vnet0': (1L, 2L, 0L, 0L, 3L, 4L, 0L, 0L),
'vnet1': (5L, 6L, 0L, 0L, 7L, 8L, 0L, 0L),
'vnet2': (9L, 10L, 0L, 0L, 11L, 12L, 0L, 0L),
}
interfaceStats = interface_stats.__getitem__
interfaces = list(self.inspector.inspect_vnics(self.instance_name))
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))
self.assertEqual(len(interfaces), 3)
vnic0, info0 = interfaces[0]
self.assertEqual(vnic0.name, 'vnet0')
self.assertEqual(vnic0.mac, 'fa:16:3e:71:ec:6d')
self.assertEqual(vnic0.fref,
'nova-instance-00000001-fa163e71ec6d')
self.assertEqual(vnic0.parameters.get('projmask'), '255.255.255.0')
self.assertEqual(vnic0.parameters.get('ip'), '10.0.0.2')
self.assertEqual(vnic0.parameters.get('projnet'), '10.0.0.0')
self.assertEqual(vnic0.parameters.get('dhcpserver'), '10.0.0.1')
self.assertEqual(info0.rx_bytes, 1L)
self.assertEqual(info0.rx_packets, 2L)
self.assertEqual(info0.tx_bytes, 3L)
self.assertEqual(info0.tx_packets, 4L)
self.assertEqual(len(interfaces), 3)
vnic0, info0 = interfaces[0]
self.assertEqual(vnic0.name, 'vnet0')
self.assertEqual(vnic0.mac, 'fa:16:3e:71:ec:6d')
self.assertEqual(vnic0.fref,
'nova-instance-00000001-fa163e71ec6d')
self.assertEqual(vnic0.parameters.get('projmask'), '255.255.255.0')
self.assertEqual(vnic0.parameters.get('ip'), '10.0.0.2')
self.assertEqual(vnic0.parameters.get('projnet'), '10.0.0.0')
self.assertEqual(vnic0.parameters.get('dhcpserver'), '10.0.0.1')
self.assertEqual(info0.rx_bytes, 1L)
self.assertEqual(info0.rx_packets, 2L)
self.assertEqual(info0.tx_bytes, 3L)
self.assertEqual(info0.tx_packets, 4L)
vnic1, info1 = interfaces[1]
self.assertEqual(vnic1.name, 'vnet1')
self.assertEqual(vnic1.mac, 'fa:16:3e:71:ec:6e')
self.assertEqual(vnic1.fref,
'nova-instance-00000001-fa163e71ec6e')
self.assertEqual(vnic1.parameters.get('projmask'), '255.255.255.0')
self.assertEqual(vnic1.parameters.get('ip'), '192.168.0.2')
self.assertEqual(vnic1.parameters.get('projnet'), '192.168.0.0')
self.assertEqual(vnic1.parameters.get('dhcpserver'), '192.168.0.1')
self.assertEqual(info1.rx_bytes, 5L)
self.assertEqual(info1.rx_packets, 6L)
self.assertEqual(info1.tx_bytes, 7L)
self.assertEqual(info1.tx_packets, 8L)
vnic1, info1 = interfaces[1]
self.assertEqual(vnic1.name, 'vnet1')
self.assertEqual(vnic1.mac, 'fa:16:3e:71:ec:6e')
self.assertEqual(vnic1.fref,
'nova-instance-00000001-fa163e71ec6e')
self.assertEqual(vnic1.parameters.get('projmask'), '255.255.255.0')
self.assertEqual(vnic1.parameters.get('ip'), '192.168.0.2')
self.assertEqual(vnic1.parameters.get('projnet'), '192.168.0.0')
self.assertEqual(vnic1.parameters.get('dhcpserver'), '192.168.0.1')
self.assertEqual(info1.rx_bytes, 5L)
self.assertEqual(info1.rx_packets, 6L)
self.assertEqual(info1.tx_bytes, 7L)
self.assertEqual(info1.tx_packets, 8L)
vnic2, info2 = interfaces[2]
self.assertEqual(vnic2.name, 'vnet2')
self.assertEqual(vnic2.mac, 'fa:16:3e:96:33:f0')
self.assertEqual(vnic2.fref, None)
self.assertEqual(vnic2.parameters, dict())
self.assertEqual(info2.rx_bytes, 9L)
self.assertEqual(info2.rx_packets, 10L)
self.assertEqual(info2.tx_bytes, 11L)
self.assertEqual(info2.tx_packets, 12L)
vnic2, info2 = interfaces[2]
self.assertEqual(vnic2.name, 'vnet2')
self.assertEqual(vnic2.mac, 'fa:16:3e:96:33:f0')
self.assertEqual(vnic2.fref, None)
self.assertEqual(vnic2.parameters, dict())
self.assertEqual(info2.rx_bytes, 9L)
self.assertEqual(info2.rx_packets, 10L)
self.assertEqual(info2.tx_bytes, 11L)
self.assertEqual(info2.tx_packets, 12L)
def test_inspect_disks(self):
dom_xml = """
@ -197,23 +198,22 @@ class TestLibvirtInspection(test.BaseTestCase):
</domain>
"""
self.inspector.connection.getCapabilities()
self.inspector.connection.lookupByName(self.instance_name).AndReturn(
self.domain)
self.domain.XMLDesc(0).AndReturn(dom_xml)
with nested(mock.patch.object(self.inspector.connection,
'lookupByName',
return_value=self.domain),
mock.patch.object(self.domain, 'XMLDesc',
return_value=dom_xml),
mock.patch.object(self.domain, 'blockStats',
return_value=(1L, 2L, 3L, 4L, -1))):
disks = list(self.inspector.inspect_disks(self.instance_name))
self.domain.blockStats('vda').AndReturn((1L, 2L, 3L, 4L, -1))
self.mox.ReplayAll()
disks = list(self.inspector.inspect_disks(self.instance_name))
self.assertEqual(len(disks), 1)
disk0, info0 = disks[0]
self.assertEqual(disk0.device, 'vda')
self.assertEqual(info0.read_requests, 1L)
self.assertEqual(info0.read_bytes, 2L)
self.assertEqual(info0.write_requests, 3L)
self.assertEqual(info0.write_bytes, 4L)
self.assertEqual(len(disks), 1)
disk0, info0 = disks[0]
self.assertEqual(disk0.device, 'vda')
self.assertEqual(info0.read_requests, 1L)
self.assertEqual(info0.read_bytes, 2L)
self.assertEqual(info0.write_requests, 3L)
self.assertEqual(info0.write_bytes, 4L)
class TestLibvirtInspectionWithError(test.BaseTestCase):