Do not attempt to load osinfo if we do not have os_distro

We get a warning logged every time we try to load up osinfo
with an image metadata that does not have the 'os_distro'
property set. We should be smarter and just not try to load
osinfo at all if we know we cannot get results.

Change-Id: I79ebea3ed222fb0db01d0a33c62a677d0381f66c
Closes-Bug: #1675602
This commit is contained in:
Matt Riedemann 2017-03-23 22:07:03 -04:00
parent 2a41eb8786
commit 9910bade6f
2 changed files with 21 additions and 4 deletions

View File

@ -87,3 +87,18 @@ class LibvirtOsInfoTest(test.NoDBTestCase):
osinfo_obj = osinfo.HardwareProperties(self.img_meta)
self.assertEqual('rtl8139', osinfo_obj.network_model)
self.assertEqual('ide', osinfo_obj.disk_model)
@mock.patch('nova.virt.osinfo.LOG.warning')
def test_hardware_properties_from_meta_no_os_distro(self, mock_warn):
"""Verifies that HardwareProperties attributes are not being set
from image properties if there is no os_distro provided.
"""
img_meta = {'properties': {'hw_watchdog_action': 'disabled'}}
img_meta = objects.ImageMeta.from_dict(img_meta)
with mock.patch.object(osinfo._OsInfoDatabase,
'get_instance') as get_instance:
osinfo_obj = osinfo.HardwareProperties(img_meta)
self.assertIsNone(osinfo_obj.network_model)
self.assertIsNone(osinfo_obj.disk_model)
get_instance.assert_not_called()
mock_warn.assert_not_called()

View File

@ -90,10 +90,12 @@ class OsInfo(object):
self._os_obj = self._get_os_obj(os_name)
def _get_os_obj(self, os_name):
try:
return _OsInfoDatabase.get_instance().get_os(os_name)
except exception.NovaException as e:
LOG.warning(_LW("Cannot find OS information - Reason: (%s)"), e)
if os_name is not None:
try:
return _OsInfoDatabase.get_instance().get_os(os_name)
except exception.NovaException as e:
LOG.warning(_LW("Cannot find OS information "
"- Reason: (%s)"), e)
@property
def network_model(self):