[osinfo-db PATCH] tests: Add Resources tests

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 tests/osinfo.py         |  54 ++++++++++++++++++++++
 tests/test_resources.py | 100 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 154 insertions(+)
 create mode 100644 tests/test_resources.py

diff --git a/tests/osinfo.py b/tests/osinfo.py
index 4b1aa55..166c882 100644
--- a/tests/osinfo.py
+++ b/tests/osinfo.py
@@ -42,6 +42,60 @@ class Os():
         return distro.text
     distro = property(_get_distro)
 
+    def _get_resources_list(self):
+        return self._root.findall('resources')
+    resources_list = property(_get_resources_list)
+
+    def _get_resources(self, node, resource_type):
+        valid_resources = ['minimum', 'recommended', 'maximum', 'network-install']
+        if not resource_type in valid_resources:
+            return None
+        if not node 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
+
+    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 = property(_get_cpu)
+
+    def _get_n_cpus(self):
+        return self._get_value('n-cpus')
+    n_cpus = property(_get_n_cpus)
+
+    def _get_ram(self):
+        return self._get_value('ram')
+    ram = property(_get_ram)
+
+    def _get_storage(self):
+        return self._get_value('storage')
+    storage = 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..69bba31
--- /dev/null
+++ b/tests/test_resources.py
@@ -0,0 +1,100 @@
+#!/usr/bin/env python3
+
+
+from collections import defaultdict
+
+import logging
+import pytest
+
+from . import util
+
+
+OSES = util.oses()
+
+def _os_id(_os):
+    return _os.shortid
+
+@pytest.mark.parametrize('_os', [*OSES], ids=_os_id)
+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
+
+@pytest.mark.parametrize('_os', [*OSES], ids=_os_id)
+def test_minimum_recommended_resources(_os):
+    _resources_helper(_os,
+                      _os.get_minimum_resources,
+                      'minimum',
+                      _os.get_recommended_resources,
+                      'recommended')
+
+@pytest.mark.parametrize('_os', [*OSES], ids=_os_id)
+def test_recommended_maximum_resources(_os):
+    _resources_helper(_os,
+                      _os.get_recommended_resources,
+                      'recommended',
+                      _os.get_maximum_resources,
+                      'maximum')
+
+@pytest.mark.parametrize('_os', [*OSES], ids=_os_id)
+def test_recommended_network_install_resources(_os):
+    _resources_helper(_os,
+                      _os.get_recommended_resources,
+                      'recommended',
+                      _os.get_network_install_resources,
+                      'network-install')
+
+@pytest.mark.parametrize('_os', [*OSES], ids=_os_id)
+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

_______________________________________________
Libosinfo mailing list
Libosinfo@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libosinfo




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Fedora Users]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux