[osinfo-db PATCH 2/4] tests: Add media tests

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

 



Media tests, at least for now, consist only in the equivalent of
test-mediauris.c from libosinfo and behaves in the same way, being
executed only if OSINFO_DB_NETWORK_TESTS is set.

Signed-off-by: Fabiano Fidêncio <fidencio@xxxxxxxxxx>
---
 .gitignore               |  5 ++++-
 Makefile                 | 17 ++++++++++++++-
 tests/unit/__init__.py   |  0
 tests/unit/osinfo.py     | 45 ++++++++++++++++++++++++++++++++++++++++
 tests/unit/test_media.py | 20 ++++++++++++++++++
 tests/unit/util.py       | 37 +++++++++++++++++++++++++++++++++
 6 files changed, 122 insertions(+), 2 deletions(-)
 create mode 100644 tests/unit/__init__.py
 create mode 100644 tests/unit/osinfo.py
 create mode 100644 tests/unit/test_media.py
 create mode 100644 tests/unit/util.py

diff --git a/.gitignore b/.gitignore
index 27200ed..0f17e4a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,8 +6,11 @@ po/osinfo-db.pot
 data/*/*/*.xml
 data/*/*/*/*.xml
 data/schema/osinfo.rng
+tests/*/__pycache__
+tests/*/*.log
+.pytest_cache
 *~
 #*
 osinfo-db-*.tar.xz
 osinfo-db.spec
-mingw-osinfo-db.spec
\ No newline at end of file
+mingw-osinfo-db.spec
diff --git a/Makefile b/Makefile
index 1367de3..d8e0f87 100644
--- a/Makefile
+++ b/Makefile
@@ -20,6 +20,8 @@ GETTEXT_PACKAGE = osinfo-db
 
 SED = sed
 
+TEE = tee
+
 DATA_FILES_IN = $(wildcard $(VPATH)/data/*/*/*.xml.in) $(wildcard $(VPATH)/data/*/*/*/*.xml.in)
 DATA_FILES = $(DATA_FILES_IN:$(VPATH)/%.in=%)
 
@@ -32,6 +34,8 @@ ZANATA = zanata
 
 XMLLINT = xmllint
 
+PYTHON = python3
+
 V = 0
 
 V_I18N = $(V_I18N_$(V))
@@ -125,4 +129,15 @@ lint: $(DATA_FILES) $(SCHEMA_FILES)
 	  fi; \
 	done
 
-check: lint
+unit-tests: $(DATA_FILES)
+	@command -v $(PYTHON) > /dev/null; \
+	if [ $$? -eq 0 ] ; then \
+	  for file in tests/unit/test_*.py; do \
+	    log_file=`echo $$file | $(SED) -e 's/\.py/.log/'`; \
+	    INTERNAL_OSINFO_DB_DATA_DIR=data $(PYTHON) -m pytest -s $$file --log-level=info | $(TEE) $$log_file; \
+	  done; \
+	else \
+	  echo "unit-tests are not going to be executed as no $(PYTHON) has been found"; \
+	fi
+
+check: lint unit-tests
diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/unit/osinfo.py b/tests/unit/osinfo.py
new file mode 100644
index 0000000..e54dc3c
--- /dev/null
+++ b/tests/unit/osinfo.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+
+import logging
+import requests
+from http.client import responses
+
+
+class Os():
+    def __init__(self, root):
+        self._root = root
+
+    def _get_medias(self):
+        medias = []
+        for media in self._root.findall('media'):
+            medias.append(Media(media))
+        return medias
+    medias = property(_get_medias)
+
+    def _get_shortid(self):
+        shortid = self._root.find('short-id')
+        return shortid.text
+    shortid = property(_get_shortid)
+
+
+class Media():
+    def __init__(self, root):
+        self._root = root
+
+    def _get_url(self):
+        url = self._root.find('url')
+        if url is not None:
+            return URL(url.text)
+    url = property(_get_url)
+
+
+class URL():
+    def __init__(self, url):
+        self._url = url
+
+    def check(self):
+        logging.info("url: %s" % self._url)
+        response = requests.head(self._url, allow_redirects=True)
+        logging.info("response: %s; code: %d" %
+                (responses[response.status_code], response.status_code))
+        return response.ok
diff --git a/tests/unit/test_media.py b/tests/unit/test_media.py
new file mode 100644
index 0000000..f779aaa
--- /dev/null
+++ b/tests/unit/test_media.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python3
+
+import os
+import pytest
+from . import util
+
+
+class TestMedia():
+    oses = util.oses()
+
+    def os_id(os):
+        return os.shortid
+
+    @pytest.mark.parametrize('os', [*oses], ids=os_id)
+    @pytest.mark.skipif(os.environ.get('OSINFO_DB_NETWORK_TESTS') is None,
+                        reason='Network related tests are not enabled')
+    def test_media_url(self, os):
+        for media in os.medias:
+            if media.url:
+                assert(media.url.check())
diff --git a/tests/unit/util.py b/tests/unit/util.py
new file mode 100644
index 0000000..cdb657e
--- /dev/null
+++ b/tests/unit/util.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+import logging
+import os
+import sys
+import xml.etree.ElementTree as ET
+
+from . import osinfo
+
+def _get_files(directory):
+    files = []
+    datadir = os.environ.get('INTERNAL_OSINFO_DB_DATA_DIR')
+    if datadir is not None:
+        root = os.path.join(datadir, directory)
+        for (dirpath, _, filenames) in os.walk(root):
+            for filename in filenames:
+                if not filename.endswith('.xml'):
+                    continue
+                files.append(os.path.join(dirpath, filename))
+    else:
+        logging.error('INTERNAL_OSINFO_DB_DATA_DIR is not set')
+    return files
+
+def _get_os(path):
+    tree = ET.parse(path)
+    root = tree.getroot()
+
+    os = root.find('os')
+    return os
+
+def oses():
+    oses = []
+    files = _get_files('os')
+    if files:
+        for f in files:
+            oses.append(osinfo.Os(_get_os(f)))
+    return oses
-- 
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