guest: Lookup osinfo object from libosinfo metadata XML

This commit is contained in:
Cole Robinson
2018-09-13 15:03:36 -04:00
parent bad5eabd93
commit 86ef998023
3 changed files with 30 additions and 0 deletions
+14
View File
@@ -225,6 +225,20 @@ class TestXMLMisc(unittest.TestCase):
xml2 = g.get_xml()
self.assertEqual(xml1, xml2)
def test_guest_osinfo_metadata(self):
g = _make_guest()
self.assertEqual(g.osinfo.name, "generic")
g.set_os_name("fedora17")
self.assertEqual(g.osinfo.name, "fedora17")
g = _make_guest()
g._metadata.libosinfo.os_id = "http://fedoraproject.org/fedora/20" # pylint: disable=protected-access
self.assertEqual(g.osinfo.name, "fedora20")
g = _make_guest()
g._metadata.libosinfo.os_id = "http://example.com/idontexit" # pylint: disable=protected-access
self.assertEqual(g.osinfo.name, "generic")
def test_dir_searchable(self):
# Normally the dir searchable test is skipped in the unittest,
# but let's contrive an example that should trigger all the code
+10
View File
@@ -241,6 +241,16 @@ class Guest(XMLBuilder):
##############################
def _get_osinfo(self):
if self.__osinfo:
return self.__osinfo
os_id = self._metadata.libosinfo.os_id
if os_id:
self.__osinfo = OSDB.lookup_os_by_full_id(os_id)
if not self.__osinfo:
logging.debug("XML had libosinfo os id=%s but we didn't "
"find any libosinfo object matching that", os_id)
if not self.__osinfo:
self.set_os_name("generic")
return self.__osinfo
+6
View File
@@ -159,6 +159,11 @@ class _OSDB(object):
# Public APIs #
###############
def lookup_os_by_full_id(self, full_id):
for osobj in self._all_variants.values():
if osobj.full_id == full_id:
return osobj
def lookup_os(self, key):
key = self._aliases.get(key) or key
return self._all_variants.get(key)
@@ -211,6 +216,7 @@ class _OsVariant(object):
self._os = o
self._family = self._os and self._os.get_family() or None
self.full_id = self._os and self._os.get_id() or None
self.name = self._os and self._os.get_short_id() or "generic"
self.label = self._os and self._os.get_name() or "Generic"
self.codename = self._os and self._os.get_codename() or ""