[Libosinfo] [osinfo-db PATCH v2] tests: Add Resources tests

Fabiano Fidêncio fidencio at redhat.com
Wed Mar 20 10:27:30 UTC 2019


Those tests are going to ensure our database doesn't have:
- more than one resources element for the very same arch;
- minimum resources bigger than recommended resources;
- recommended resources bigger than maximum resources;
- recommended resources bigger than network install resources;
- network resources bigger than maximum resources;

All those tests were previously part of test-os, from libosinfo

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 tests/osinfo.py         |  58 +++++++++++++++++++++++
 tests/test_resources.py | 101 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 159 insertions(+)
 create mode 100644 tests/test_resources.py

diff --git a/tests/osinfo.py b/tests/osinfo.py
index 2950306..eb63724 100644
--- a/tests/osinfo.py
+++ b/tests/osinfo.py
@@ -58,6 +58,64 @@ class Os():
         return distro.text
     distro = _cache_property(_get_distro)
 
+    def _get_resources_list(self):
+        return self._root.findall('resources')
+    resources_list = _cache_property(_get_resources_list)
+
+    def _get_resources(self, node, resource_type):
+        valid_resources = ['minimum',
+                           'recommended',
+                           'maximum',
+                           'network-install']
+        if resource_type not in valid_resources:
+            return None
+        if node not in self.resources_list:
+            return None
+        resource = node.find(resource_type)
+        if resource is not None:
+            return Resources(resource)
+        return None
+
+    def get_minimum_resources(self, node):
+        return self._get_resources(node, 'minimum')
+
+    def get_recommended_resources(self, node):
+        return self._get_resources(node, 'recommended')
+
+    def get_maximum_resources(self, node):
+        return self._get_resources(node, 'maximum')
+
+    def get_network_install_resources(self, node):
+        return self._get_resources(node, 'network-install')
+
+
+class Resources():
+    def __init__(self, root):
+        self._root = root
+        self._cache = {}
+
+    def _get_value(self, string):
+        value = self._root.find(string)
+        if value is not None:
+            return int(value.text)
+        return None
+
+    def _get_cpu(self):
+        return self._get_value('cpu')
+    cpu = _cache_property(_get_cpu)
+
+    def _get_n_cpus(self):
+        return self._get_value('n-cpus')
+    n_cpus = _cache_property(_get_n_cpus)
+
+    def _get_ram(self):
+        return self._get_value('ram')
+    ram = _cache_property(_get_ram)
+
+    def _get_storage(self):
+        return self._get_value('storage')
+    storage = _cache_property(_get_storage)
+
 
 class Image():
     def __init__(self, root):
diff --git a/tests/test_resources.py b/tests/test_resources.py
new file mode 100644
index 0000000..d8cc588
--- /dev/null
+++ b/tests/test_resources.py
@@ -0,0 +1,101 @@
+# This work is licensed under the GNU GPLv2 or later.
+# See the COPYING file in the top-level directory.
+
+from collections import defaultdict
+
+import logging
+import pytest
+
+from . import util
+
+
+ at pytest.mark.parametrize('_os', util.DataFiles.oses(), ids=lambda o: o.shortid)
+def test_resources_uniqueness_by_arch(_os):
+    result = defaultdict(list)
+    for resources in _os.resources_list:
+        result[resources.get('arch')].append(resources)
+
+    for value in result.values():
+        assert len(value) == 1
+
+
+ at pytest.mark.parametrize('_os', util.DataFiles.oses(), ids=lambda o: o.shortid)
+def test_minimum_recommended_resources(_os):
+    _resources_helper(_os,
+                      _os.get_minimum_resources,
+                      'minimum',
+                      _os.get_recommended_resources,
+                      'recommended')
+
+
+ at pytest.mark.parametrize('_os', util.DataFiles.oses(), ids=lambda o: o.shortid)
+def test_recommended_maximum_resources(_os):
+    _resources_helper(_os,
+                      _os.get_recommended_resources,
+                      'recommended',
+                      _os.get_maximum_resources,
+                      'maximum')
+
+
+ at pytest.mark.parametrize('_os', util.DataFiles.oses(), ids=lambda o: o.shortid)
+def test_recommended_network_install_resources(_os):
+    _resources_helper(_os,
+                      _os.get_recommended_resources,
+                      'recommended',
+                      _os.get_network_install_resources,
+                      'network-install')
+
+
+ at pytest.mark.parametrize('_os', util.DataFiles.oses(), ids=lambda o: o.shortid)
+def test_network_install_maximum_resources(_os):
+    _resources_helper(_os,
+                      _os.get_network_install_resources,
+                      'network-install',
+                      _os.get_maximum_resources,
+                      'maximum')
+
+
+def _resources_helper(_os, smaller_func, smaller_str, bigger_func, bigger_str):
+    broken = []
+    for resource in _os.resources_list:
+        logging.info('resources | arch: %s', resource.get('arch'))
+        smaller = smaller_func(resource)
+        bigger = bigger_func(resource)
+
+        if smaller is None or bigger is None:
+            continue
+
+        if not _resources_check(smaller, smaller_str, bigger, bigger_str):
+            broken.append([smaller, bigger])
+    assert broken == []
+
+
+def _resources_check(smaller, smaller_str, bigger, bigger_str):
+    ret = True
+    if smaller.cpu is not None and bigger.cpu is not None:
+        if smaller.cpu > bigger.cpu:
+            logging.warning('cpu value of %s should not be bigger than %s '
+                            '(\'%d\' > \'%d\')',
+                            smaller_str, bigger_str, smaller.cpu, bigger.cpu)
+            ret = False
+    if smaller.n_cpus is not None and bigger.n_cpus is not None:
+        if smaller.n_cpus > bigger.n_cpus:
+            logging.warning('n-cpus value of %s should not be bigger than %s '
+                            '(\'%d\' > \'%d\')',
+                            smaller_str, bigger_str,
+                            smaller.n_cpus, bigger.n_cpus)
+            ret = False
+    if smaller.ram is not None and bigger.ram is not None:
+        if smaller.ram > bigger.ram:
+            logging.warning('ram value of %s should not be bigger than %s '
+                            '(\'%d\' > \'%d\')',
+                            smaller_str, bigger_str, smaller.ram, bigger.ram)
+            ret = False
+    if smaller.storage is not None and bigger.storage is not None:
+        if smaller.storage > bigger.storage:
+            logging.warning('storage value of %s should not be bigger than %s '
+                            '(\'%d\' > \'%d\')',
+                            smaller_str, bigger_str,
+                            smaller.storage, bigger.storage)
+            ret = False
+    return ret
-- 
2.20.1




More information about the Libosinfo mailing list