Port test_inspector to Python 3

* Replace contextlib.nested() with contextlib.ExitStack
* Add contextlib2 dependency for Python 2.6 and 2.7 to get ExitStack
* TestLibvirtInspection: set libvirtError because it must be a subclass
  of Exception on Python 3

Enable ceilometer.tests.compute.virt.libvirt.test_inspector on Python 3.

Change-Id: I82f4b911c1b3ede90805150630c222b1dd5f3474
This commit is contained in:
Victor Stinner 2015-05-28 00:55:21 +02:00
parent 7d70942132
commit ce08e620fa
3 changed files with 78 additions and 63 deletions

View File

@ -16,7 +16,10 @@
"""Tests for libvirt inspector. """Tests for libvirt inspector.
""" """
import contextlib try:
import contextlib2 as contextlib # for Python < 3.3
except ImportError:
import contextlib
import fixtures import fixtures
import mock import mock
@ -29,6 +32,9 @@ from ceilometer.compute.virt.libvirt import inspector as libvirt_inspector
class TestLibvirtInspection(base.BaseTestCase): class TestLibvirtInspection(base.BaseTestCase):
class fakeLibvirtError(Exception):
pass
def setUp(self): def setUp(self):
super(TestLibvirtInspection, self).setUp() super(TestLibvirtInspection, self).setUp()
@ -40,19 +46,21 @@ class TestLibvirtInspection(base.BaseTestCase):
self.inspector.connection = mock.Mock() self.inspector.connection = mock.Mock()
libvirt_inspector.libvirt = mock.Mock() libvirt_inspector.libvirt = mock.Mock()
libvirt_inspector.libvirt.VIR_DOMAIN_SHUTOFF = 5 libvirt_inspector.libvirt.VIR_DOMAIN_SHUTOFF = 5
libvirt_inspector.libvirt.libvirtError = self.fakeLibvirtError
self.domain = mock.Mock() self.domain = mock.Mock()
self.addCleanup(mock.patch.stopall) self.addCleanup(mock.patch.stopall)
def test_inspect_cpus(self): def test_inspect_cpus(self):
with contextlib.nested(mock.patch.object(self.inspector.connection, with contextlib.ExitStack() as stack:
'lookupByUUIDString', stack.enter_context(mock.patch.object(self.inspector.connection,
return_value=self.domain), 'lookupByUUIDString',
mock.patch.object(self.domain, 'info', return_value=self.domain))
return_value=(0, 0, 0, stack.enter_context(mock.patch.object(self.domain, 'info',
2, 999999))): return_value=(0, 0, 0,
cpu_info = self.inspector.inspect_cpus(self.instance) 2, 999999)))
self.assertEqual(2, cpu_info.number) cpu_info = self.inspector.inspect_cpus(self.instance)
self.assertEqual(999999, cpu_info.time) self.assertEqual(2, cpu_info.number)
self.assertEqual(999999, cpu_info.time)
def test_inspect_vnics(self): def test_inspect_vnics(self):
dom_xml = """ dom_xml = """
@ -120,17 +128,18 @@ class TestLibvirtInspection(base.BaseTestCase):
interfaceStats = interface_stats.__getitem__ interfaceStats = interface_stats.__getitem__
connection = self.inspector.connection connection = self.inspector.connection
with contextlib.nested(mock.patch.object(connection, with contextlib.ExitStack() as stack:
'lookupByUUIDString', stack.enter_context(mock.patch.object(connection,
return_value=self.domain), 'lookupByUUIDString',
mock.patch.object(self.domain, 'XMLDesc', return_value=self.domain))
return_value=dom_xml), stack.enter_context(mock.patch.object(self.domain, 'XMLDesc',
mock.patch.object(self.domain, return_value=dom_xml))
'interfaceStats', stack.enter_context(mock.patch.object(self.domain,
side_effect=interfaceStats), 'interfaceStats',
mock.patch.object(self.domain, 'info', side_effect=interfaceStats))
return_value=(0, 0, 0, stack.enter_context(mock.patch.object(self.domain, 'info',
2, 999999))): return_value=(0, 0, 0,
2, 999999)))
interfaces = list(self.inspector.inspect_vnics(self.instance)) interfaces = list(self.inspector.inspect_vnics(self.instance))
self.assertEqual(3, len(interfaces)) self.assertEqual(3, len(interfaces))
@ -172,12 +181,13 @@ class TestLibvirtInspection(base.BaseTestCase):
def test_inspect_vnics_with_domain_shutoff(self): def test_inspect_vnics_with_domain_shutoff(self):
connection = self.inspector.connection connection = self.inspector.connection
with contextlib.nested(mock.patch.object(connection, with contextlib.ExitStack() as stack:
'lookupByUUIDString', stack.enter_context(mock.patch.object(connection,
return_value=self.domain), 'lookupByUUIDString',
mock.patch.object(self.domain, 'info', return_value=self.domain))
return_value=(5, 0, 0, stack.enter_context(mock.patch.object(self.domain, 'info',
2, 999999))): return_value=(5, 0, 0,
2, 999999)))
inspect = self.inspector.inspect_vnics inspect = self.inspector.inspect_vnics
self.assertRaises(virt_inspector.InstanceShutOffException, self.assertRaises(virt_inspector.InstanceShutOffException,
list, inspect(self.instance)) list, inspect(self.instance))
@ -198,35 +208,37 @@ class TestLibvirtInspection(base.BaseTestCase):
</domain> </domain>
""" """
with contextlib.nested(mock.patch.object(self.inspector.connection, with contextlib.ExitStack() as stack:
'lookupByUUIDString', stack.enter_context(mock.patch.object(self.inspector.connection,
return_value=self.domain), 'lookupByUUIDString',
mock.patch.object(self.domain, 'XMLDesc', return_value=self.domain))
return_value=dom_xml), stack.enter_context(mock.patch.object(self.domain, 'XMLDesc',
mock.patch.object(self.domain, 'blockStats', return_value=dom_xml))
return_value=(1, 2, 3, stack.enter_context(mock.patch.object(self.domain, 'blockStats',
4, -1)), return_value=(1, 2, 3,
mock.patch.object(self.domain, 'info', 4, -1)))
return_value=(0, 0, 0, stack.enter_context(mock.patch.object(self.domain, 'info',
2, 999999))): return_value=(0, 0, 0,
disks = list(self.inspector.inspect_disks(self.instance)) 2, 999999)))
disks = list(self.inspector.inspect_disks(self.instance))
self.assertEqual(1, len(disks)) self.assertEqual(1, len(disks))
disk0, info0 = disks[0] disk0, info0 = disks[0]
self.assertEqual('vda', disk0.device) self.assertEqual('vda', disk0.device)
self.assertEqual(1, info0.read_requests) self.assertEqual(1, info0.read_requests)
self.assertEqual(2, info0.read_bytes) self.assertEqual(2, info0.read_bytes)
self.assertEqual(3, info0.write_requests) self.assertEqual(3, info0.write_requests)
self.assertEqual(4, info0.write_bytes) self.assertEqual(4, info0.write_bytes)
def test_inspect_disks_with_domain_shutoff(self): def test_inspect_disks_with_domain_shutoff(self):
connection = self.inspector.connection connection = self.inspector.connection
with contextlib.nested(mock.patch.object(connection, with contextlib.ExitStack() as stack:
'lookupByUUIDString', stack.enter_context(mock.patch.object(connection,
return_value=self.domain), 'lookupByUUIDString',
mock.patch.object(self.domain, 'info', return_value=self.domain))
return_value=(5, 0, 0, stack.enter_context(mock.patch.object(self.domain, 'info',
2, 999999))): return_value=(5, 0, 0,
2, 999999)))
inspect = self.inspector.inspect_disks inspect = self.inspector.inspect_disks
self.assertRaises(virt_inspector.InstanceShutOffException, self.assertRaises(virt_inspector.InstanceShutOffException,
list, inspect(self.instance)) list, inspect(self.instance))
@ -261,17 +273,18 @@ class TestLibvirtInspection(base.BaseTestCase):
</domain> </domain>
""" """
with contextlib.nested(mock.patch.object(self.inspector.connection, with contextlib.ExitStack() as stack:
'lookupByUUIDString', stack.enter_context(mock.patch.object(self.inspector.connection,
return_value=self.domain), 'lookupByUUIDString',
mock.patch.object(self.domain, 'XMLDesc', return_value=self.domain))
return_value=dom_xml), stack.enter_context(mock.patch.object(self.domain, 'XMLDesc',
mock.patch.object(self.domain, 'blockInfo', return_value=dom_xml))
return_value=(1, 2, 3, stack.enter_context(mock.patch.object(self.domain, 'blockInfo',
-1)), return_value=(1, 2, 3,
mock.patch.object(self.domain, 'info', -1)))
return_value=(0, 0, 0, stack.enter_context(mock.patch.object(self.domain, 'info',
2, 999999))): return_value=(0, 0, 0,
2, 999999)))
disks = list(self.inspector.inspect_disk_info(self.instance)) disks = list(self.inspector.inspect_disk_info(self.instance))
self.assertEqual(1, len(disks)) self.assertEqual(1, len(disks))

View File

@ -5,6 +5,7 @@
# Hacking already pins down pep8, pyflakes and flake8 # Hacking already pins down pep8, pyflakes and flake8
hacking>=0.10.0,<0.11 hacking>=0.10.0,<0.11
Babel>=1.3 Babel>=1.3
contextlib2>=0.4.0 # PSF License
coverage>=3.6 coverage>=3.6
discover discover
elasticsearch>=1.3.0 elasticsearch>=1.3.0

View File

@ -49,6 +49,7 @@ commands =
deps = -r{toxinidir}/requirements.txt deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements-py3.txt -r{toxinidir}/test-requirements-py3.txt
commands = python -m testtools.run \ commands = python -m testtools.run \
ceilometer.tests.compute.virt.libvirt.test_inspector \
ceilometer.tests.compute.virt.vmware.test_vsphere_operations \ ceilometer.tests.compute.virt.vmware.test_vsphere_operations \
ceilometer.tests.data_processing.test_notifications \ ceilometer.tests.data_processing.test_notifications \
ceilometer.tests.energy.test_kwapi \ ceilometer.tests.energy.test_kwapi \