[Libosinfo] [PATCH osinfo-db 5/7] tests: utils: Provide helpers for filtering the OS list

Cole Robinson crobinso at redhat.com
Wed Mar 20 21:53:24 UTC 2019


And use it in the test cases to reduce the amount of objects we act
on. This makes the test output quicker and less '.' spammy

This also revealed that osinfo-db doesn't actually track any 'image'
data despite there being a test for verifying the URLs...

Signed-off-by: Cole Robinson <crobinso at redhat.com>
---
 tests/test_devices.py   |  2 +-
 tests/test_resources.py | 10 +++++-----
 tests/test_urls.py      |  6 +++---
 tests/util.py           | 37 +++++++++++++++++++++++++++++++------
 4 files changed, 40 insertions(+), 15 deletions(-)

diff --git a/tests/test_devices.py b/tests/test_devices.py
index 8696ccc..0bcf8fb 100644
--- a/tests/test_devices.py
+++ b/tests/test_devices.py
@@ -4,7 +4,7 @@
 from . import util
 
 
- at util.os_parametrize('_os')
+ at util.os_parametrize('_os', filter_devices=True)
 def test_devices_duplication(_os):
     broken = []
     related = util.DataFiles.get_os_related(_os)
diff --git a/tests/test_resources.py b/tests/test_resources.py
index 6e00dba..46bdebb 100644
--- a/tests/test_resources.py
+++ b/tests/test_resources.py
@@ -8,7 +8,7 @@ import logging
 from . import util
 
 
- at util.os_parametrize('_os')
+ at util.os_parametrize('_os', filter_resources=True)
 def test_resources_uniqueness_by_arch(_os):
     result = defaultdict(list)
     for resources in _os.resources_list:
@@ -18,7 +18,7 @@ def test_resources_uniqueness_by_arch(_os):
         assert len(value) == 1
 
 
- at util.os_parametrize('_os')
+ at util.os_parametrize('_os', filter_resources=True)
 def test_minimum_recommended_resources(_os):
     _resources_helper(_os,
                       _os.get_minimum_resources,
@@ -27,7 +27,7 @@ def test_minimum_recommended_resources(_os):
                       'recommended')
 
 
- at util.os_parametrize('_os')
+ at util.os_parametrize('_os', filter_resources=True)
 def test_recommended_maximum_resources(_os):
     _resources_helper(_os,
                       _os.get_recommended_resources,
@@ -36,7 +36,7 @@ def test_recommended_maximum_resources(_os):
                       'maximum')
 
 
- at util.os_parametrize('_os')
+ at util.os_parametrize('_os', filter_resources=True)
 def test_recommended_network_install_resources(_os):
     _resources_helper(_os,
                       _os.get_recommended_resources,
@@ -45,7 +45,7 @@ def test_recommended_network_install_resources(_os):
                       'network-install')
 
 
- at util.os_parametrize('_os')
+ at util.os_parametrize('_os', filter_resources=True)
 def test_network_install_maximum_resources(_os):
     _resources_helper(_os,
                       _os.get_network_install_resources,
diff --git a/tests/test_urls.py b/tests/test_urls.py
index b6dcd62..552e6eb 100644
--- a/tests/test_urls.py
+++ b/tests/test_urls.py
@@ -7,7 +7,7 @@ import pytest
 from . import util
 
 
- at util.os_parametrize('_os')
+ at util.os_parametrize('_os', filter_images=True)
 @pytest.mark.skipif(os.environ.get('OSINFO_DB_NETWORK_TESTS') is None,
                     reason='Network related tests are not enabled')
 def test_images_url(_os):
@@ -19,7 +19,7 @@ def test_images_url(_os):
     assert broken == []
 
 
- at util.os_parametrize('_os')
+ at util.os_parametrize('_os', filter_trees=True)
 @pytest.mark.skipif(os.environ.get('OSINFO_DB_NETWORK_TESTS') is None,
                     reason='Network related tests are not enabled')
 def test_medias_url(_os):
@@ -31,7 +31,7 @@ def test_medias_url(_os):
     assert broken == []
 
 
- at util.os_parametrize('_os')
+ at util.os_parametrize('_os', filter_media=True)
 @pytest.mark.skipif(os.environ.get('OSINFO_DB_NETWORK_TESTS') is None,
                     reason='Network related tests are not enabled')
 def test_trees_url(_os):
diff --git a/tests/util.py b/tests/util.py
index 76e08da..08fbd3f 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -46,12 +46,31 @@ class _DataFiles():
         return [p for p in self._get_all_xml() if
                 p.startswith(os.path.join(self.datadir, dirname))]
 
-    def oses(self):
+    def oses(self, filter_media=False, filter_trees=False, filter_images=False,
+            filter_devices=False, filter_resources=False):
+        """
+        Return a list of osinfo.Os objects
+
+        :param filter_FOO: Only return objects that have at least one
+            instance of a FOO object
+        """
         if not self._oses_cache:
             for path in self._filter_xml('os'):
                 osroot = ET.parse(path).getroot().find('os')
                 self._oses_cache.append(osinfo.Os(osroot))
-        return self._oses_cache
+
+        oses = self._oses_cache[:]
+        if filter_media:
+            oses = [o for o in oses if o.medias]
+        if filter_trees:
+            oses = [o for o in oses if o.trees]
+        if filter_images:
+            oses = [o for o in oses if o.images]
+        if filter_devices:
+            oses = [o for o in oses if o.devices]
+        if filter_resources:
+            oses = [o for o in oses if o.resources_list]
+        return oses
 
     def get_os_related(self, _os):
         if _os.internal_id not in self._os_related_cache:
@@ -86,9 +105,15 @@ class _DataFiles():
 DataFiles = _DataFiles()
 
 
-def os_parametrize(argname):
+def os_parametrize(argname, **kwargs):
     """
-    Helper for parametrizing a test with an OS list.
+    Helper for parametrizing a test with an OS list. Passthrough any
+    extra arguments to DataFiles.oses()
     """
-    oses = DataFiles.oses()
-    return pytest.mark.parametrize(argname, oses, ids=lambda o: o.shortid)
+    def ids_cb(osxml):
+        # pytest passes us a weird value when oses is empty, like for
+        # test_urls image testing at the time of this commit
+        return getattr(osxml, "shortid", str(osxml))
+
+    oses = DataFiles.oses(**kwargs)
+    return pytest.mark.parametrize(argname, oses, ids=ids_cb)
-- 
2.21.0




More information about the Libosinfo mailing list