Add hypervisor inspector sanity check

Based on existed self-disabled pollster, this sanity check would
prevent pollsters on mis-configured inspector from being loaded.

Change-Id: Ic4a7ce010e17b2f0f356687cc61a59276d59283d
Closes-Bug: #1485560
This commit is contained in:
Edwin Zhai 2015-08-18 13:19:03 +08:00
parent 0d39daa881
commit d17e75d267
5 changed files with 49 additions and 0 deletions

View File

@ -24,6 +24,11 @@ from ceilometer.compute.virt import inspector as virt_inspector
@six.add_metaclass(abc.ABCMeta)
class BaseComputePollster(plugin_base.PollsterBase):
def setup_environment(self):
super(BaseComputePollster, self).setup_environment()
# propagate exception from check_sanity
self.inspector.check_sanity()
@property
def inspector(self):
try:

View File

@ -184,10 +184,22 @@ class NoDataException(InspectorException):
pass
class NoSanityException(InspectorException):
pass
# Main virt inspector abstraction layering over the hypervisor API.
#
class Inspector(object):
def check_sanity(self):
"""Check the sanity of hypervisor inspector.
Each subclass could overwrite it to throw any exception
when detecting mis-configured inspector
"""
pass
def inspect_cpus(self, instance):
"""Inspect the CPU statistics for an instance.

View File

@ -81,6 +81,10 @@ class LibvirtInspector(virt_inspector.Inspector):
return self.connection
def check_sanity(self):
if not self._get_connection():
raise virt_inspector.NoSanityException()
@retry_on_disconnect
def _lookup_by_uuid(self, instance):
instance_name = util.instance_name(instance)

View File

@ -39,6 +39,9 @@ class PollingException(Exception):
pass
@mock.patch('ceilometer.compute.pollsters.'
'BaseComputePollster.setup_environment',
mock.Mock(return_value=None))
class TestManager(base.BaseTestCase):
@mock.patch('ceilometer.pipeline.setup_polling', mock.MagicMock())
@ -181,6 +184,9 @@ class TestRunTasks(agentbase.BaseAgentManagerTestCase):
resource_metadata=agentbase.default_test_data.resource_metadata)
@staticmethod
@mock.patch('ceilometer.compute.pollsters.'
'BaseComputePollster.setup_environment',
mock.Mock(return_value=None))
def create_manager():
return manager.AgentManager()

View File

@ -341,3 +341,25 @@ class TestLibvirtInspectionWithError(base.BaseTestCase):
def test_inspect_unknown_error(self):
self.assertRaises(virt_inspector.InspectorException,
self.inspector.inspect_cpus, 'foo')
class TestLibvirtInitWithError(base.BaseTestCase):
def setUp(self):
super(TestLibvirtInitWithError, self).setUp()
self.inspector = libvirt_inspector.LibvirtInspector()
libvirt_inspector.libvirt = mock.Mock()
@mock.patch('ceilometer.compute.virt.libvirt.inspector.'
'LibvirtInspector._get_connection',
mock.Mock(return_value=None))
def test_init_error(self):
self.assertRaises(virt_inspector.NoSanityException,
self.inspector.check_sanity)
@mock.patch('ceilometer.compute.virt.libvirt.inspector.'
'LibvirtInspector._get_connection',
mock.Mock(side_effect=virt_inspector.NoDataException))
def test_init_exception(self):
self.assertRaises(virt_inspector.NoDataException,
self.inspector.check_sanity)