[Libosinfo] [osinfo-db PATCH 1/2] tests: Add machinery for dealing with os relationships

Fabiano Fidêncio fidencio at redhat.com
Wed Mar 20 13:39:58 UTC 2019


This is going to be needed in order to test whether we're adding
duplicated devices in OSes which derives-from or clones some other OS.

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 tests/osinfo.py | 18 ++++++++++++++++++
 tests/util.py   | 29 +++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/tests/osinfo.py b/tests/osinfo.py
index eb63724..a203369 100644
--- a/tests/osinfo.py
+++ b/tests/osinfo.py
@@ -27,6 +27,24 @@ class Os():
         self._root = root
         self._cache = {}
 
+    def _get_id(self):
+        return self._root.get('id')
+    internal_id = _cache_property(_get_id)
+
+    def _get_derives_from(self):
+        derives_from = self._root.find('derives-from')
+        if derives_from is not None:
+            return derives_from.get('id')
+        return None
+    derives_from = _cache_property(_get_derives_from)
+
+    def _get_clones(self):
+        clones = self._root.find('clones')
+        if clones is not None:
+            return clones.get('id')
+        return None
+    clones = _cache_property(_get_clones)
+
     def _get_images(self):
         images = []
         for image in self._root.findall('image'):
diff --git a/tests/util.py b/tests/util.py
index 418b9d2..b957b08 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -1,6 +1,8 @@
 # This work is licensed under the GNU GPLv2 or later.
 # See the COPYING file in the top-level directory.
 
+from collections import defaultdict
+
 import os
 import xml.etree.ElementTree as ET
 
@@ -17,6 +19,7 @@ class _DataFiles():
         self.schema = os.path.join(self.datadir, 'schema', 'osinfo.rng')
         self._all_xml_cache = []
         self._oses_cache = []
+        self._os_related_cache = defaultdict(list)
 
         if not os.path.exists(self.datadir):
             raise RuntimeError("INTERNAL_OSINFO_DB_DATA_DIR=%s "
@@ -48,6 +51,32 @@ class _DataFiles():
                 self._oses_cache.append(osinfo.Os(osroot))
         return self._oses_cache
 
+    def get_os_related(self, _os):
+        if _os.internal_id not in self._os_related_cache:
+            directly_related = []
+            if _os.derives_from is not None:
+                for __os in self.oses():
+                    if _os.derives_from == __os.internal_id:
+                        directly_related.append(__os)
+                        break
+
+            if _os.clones is not None:
+                for __os in self.oses():
+                    if _os.clones == __os.internal_id:
+                        directly_related.append(__os)
+                        break
+
+            self._os_related_cache[_os.internal_id].extend(directly_related)
+
+            related = []
+            for __os in directly_related:
+                related.extend(self.get_os_related(__os))
+
+            for __os in related:
+                if __os not in self._os_related_cache[_os.internal_id]:
+                    self._os_related_cache[_os.internal_id].append(__os)
+        return self._os_related_cache[_os.internal_id]
+
     def xmls(self):
         return self._get_all_xml()
 
-- 
2.20.1




More information about the Libosinfo mailing list