libvirt: Only ask tpool.Proxy to autowrap vir* classes

I668643c836d46a25df46d4c99a973af5e50a39db attempted to fix service wide
pauses by providing a more complete list of classes to tpool.Proxy.

While this excluded libvirtError it can include internal libvirt-python
classes pointed to by private globals that have been introduced with the
use of type checking within the module.

Any attempt to wrap these internal classes will result in the failure
seen in bug #1901383. As a result this change simply ignores any class
found during inspection that doesn't start with the `vir` string, used
by libvirt to denote public methods and classes.

Closes-Bug: #1901383
Co-Authored-By: Daniel Berrange <berrange@redhat.com>
Change-Id: I568b0c4fd6069b9118ff116532f14abb46cc42ab
(cherry picked from commit 0d2ca53bb8)
This commit is contained in:
Lee Yarwood 2020-10-27 08:44:59 +00:00
parent 61717ff9d2
commit 048a3337a8
3 changed files with 16 additions and 5 deletions

View File

@ -1803,6 +1803,16 @@ virConnect = Connection
virSecret = Secret
# A private libvirt-python class and global only provided here for testing to
# ensure it's not returned by libvirt.host.Host.get_libvirt_proxy_classes.
class FakeHandler(object):
def __init__(self):
pass
_EventAddHandleFunc = FakeHandler
class FakeLibvirtFixture(fixtures.Fixture):
"""Performs global setup/stubbing for all libvirt tests.
"""

View File

@ -1387,8 +1387,9 @@ class LibvirtTpoolProxyTestCase(test.NoDBTestCase):
self.assertIn(fakelibvirt.virNodeDevice, proxy_classes)
self.assertIn(fakelibvirt.virSecret, proxy_classes)
# Assert that we filtered out libvirtError
# Assert that we filtered out libvirtError and any private classes
self.assertNotIn(fakelibvirt.libvirtError, proxy_classes)
self.assertNotIn(fakelibvirt._EventAddHandleFunc, proxy_classes)
def test_tpool_get_connection(self):
# Test that Host.get_connection() returns a tpool.Proxy

View File

@ -127,15 +127,15 @@ class Host(object):
@staticmethod
def _get_libvirt_proxy_classes(libvirt_module):
"""Return a tuple for tpool.Proxy's autowrap argument containing all
classes defined by the libvirt module except libvirtError.
public vir* classes defined by the libvirt module.
"""
# Get a list of (name, class) tuples of libvirt classes
classes = inspect.getmembers(libvirt_module, inspect.isclass)
# Return a list of just the classes, filtering out libvirtError because
# we don't need to proxy that
return tuple([cls[1] for cls in classes if cls[0] != 'libvirtError'])
# Return a list of just the vir* classes, filtering out libvirtError
# and any private globals pointing at private internal classes.
return tuple([cls[1] for cls in classes if cls[0].startswith("vir")])
def _wrap_libvirt_proxy(self, obj):
"""Return an object wrapped in a tpool.Proxy using autowrap appropriate