[Libosinfo] [osinfo-db v2 2/4] tests: Add URL tests
Fabiano Fidêncio
fidencio at redhat.com
Mon Mar 18 13:38:48 UTC 2019
URL tests consist only in the equivalent to test-*uris.c from libosinfo
and behaves in the very same way, being executed only if
OSINFO_DB_NETWORK_TESTS is set.
Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
.gitignore | 5 ++-
Makefile | 17 ++++++++-
tests/__init__.py | 0
tests/osinfo.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++
tests/test_urls.py | 47 +++++++++++++++++++++++++
tests/util.py | 37 ++++++++++++++++++++
6 files changed, 190 insertions(+), 2 deletions(-)
create mode 100644 tests/__init__.py
create mode 100644 tests/osinfo.py
create mode 100644 tests/test_urls.py
create mode 100644 tests/util.py
diff --git a/.gitignore b/.gitignore
index 27200ed..938a7b9 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..ca07fe3 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/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/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/osinfo.py b/tests/osinfo.py
new file mode 100644
index 0000000..4391cd0
--- /dev/null
+++ b/tests/osinfo.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+
+
+from http.client import responses
+
+import logging
+import requests
+
+
+class Os():
+ def __init__(self, root):
+ self._root = root
+
+ def _get_images(self):
+ images = []
+ for image in self._root.findall('image'):
+ images.append(Image(image))
+ return images
+ images = property(_get_images)
+
+ def _get_medias(self):
+ medias = []
+ for media in self._root.findall('media'):
+ medias.append(Media(media))
+ return medias
+ medias = property(_get_medias)
+
+ def _get_trees(self):
+ trees = []
+ for tree in self._root.findall('tree'):
+ trees.append(Tree(tree))
+ return trees
+ trees = property(_get_trees)
+
+ def _get_shortid(self):
+ shortid = self._root.find('short-id')
+ return shortid.text
+ shortid = property(_get_shortid)
+
+
+class Image():
+ 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)
+ return None
+ url = property(_get_url)
+
+
+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)
+ return None
+ url = property(_get_url)
+
+
+class Tree():
+ 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)
+ return None
+ 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/test_urls.py b/tests/test_urls.py
new file mode 100644
index 0000000..f0856bc
--- /dev/null
+++ b/tests/test_urls.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+
+import os
+import pytest
+
+from . import util
+
+
+OSES = util.oses()
+
+def _os_id(_os):
+ return _os.shortid
+
+ at pytest.mark.parametrize('_os', [*OSES], ids=_os_id)
+ at pytest.mark.skipif(os.environ.get('OSINFO_DB_NETWORK_TESTS') is None,
+ reason='Network related tests are not enabled')
+def test_images_url(_os):
+ broken = []
+ for image in _os.images:
+ if image.url:
+ if not image.url.check():
+ broken.append(image.url)
+ assert len(broken) == 0
+
+ at pytest.mark.parametrize('_os', [*OSES], ids=_os_id)
+ at pytest.mark.skipif(os.environ.get('OSINFO_DB_NETWORK_TESTS') is None,
+ reason='Network related tests are not enabled')
+def test_medias_url(_os):
+ broken = []
+ for media in _os.medias:
+ if media.url:
+ if not media.url.check():
+ broken.append(media.url)
+ assert len(broken) == 0
+
+ at pytest.mark.parametrize('_os', [*OSES], ids=_os_id)
+ at pytest.mark.skipif(os.environ.get('OSINFO_DB_NETWORK_TESTS') is None,
+ reason='Network related tests are not enabled')
+def test_trees_url(_os):
+ broken = []
+ for tree in _os.trees:
+ if tree.url:
+ if not tree.url.check():
+ broken.append(tree.url)
+ assert len(broken) == 0
+
diff --git a/tests/util.py b/tests/util.py
new file mode 100644
index 0000000..48e7f7d
--- /dev/null
+++ b/tests/util.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+import logging
+import os
+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 _file in files:
+ _oses.append(osinfo.Os(_get_os(_file)))
+ return _oses
--
2.20.1
More information about the Libosinfo
mailing list