Add a set of functional tests to ensure osinfo-db-export and osinfo-db-import work as expected. The covered scenearios are: - osinfo-db-export DIR - It's a negative test - osinfo-db-export --system and osinfo-db-import --system - osinfo-db-export --local FILENAME - osinfo-db-import --local FILENAME - cat FILENAME | osinfo-db-import - - cat FILENAME | osinfo-db-import - osinfo-db-export --user --license LICENSE --version VERSION VERSIONED_FILENAME - osinfo-db-import --root / --user VERSIONED_FILENAME - osinfo-db-import --user URL - osinfo-db-import --latest The last two tests are only enabled when OSINFO_DB_TOOLS_NETWORK_TESTS is set. Last but not least, differently than the tests for osinfo-db-path and osinfo-db-validate, those tests pretty much only make sense when taken as pairs. So, we export a db, import it back and ensure the content is the same. https://gitlab.com/libosinfo/osinfo-db-tools/issues/2 Signed-off-by: Fabiano Fidêncio <fidencio@xxxxxxxxxx> --- tests/Makefile.am | 1 + tests/test_osinfo_db_export_import.py | 263 ++++++++++++++++++++++++++ 2 files changed, 264 insertions(+) create mode 100755 tests/test_osinfo_db_export_import.py diff --git a/tests/Makefile.am b/tests/Makefile.am index fb7dfab..f9755a0 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -5,6 +5,7 @@ test_helpers = \ $(NULL) test_programs = \ + test_osinfo_db_export_import.py \ test_osinfo_db_path.py \ test_osinfo_db_validate.py \ $(NULL) diff --git a/tests/test_osinfo_db_export_import.py b/tests/test_osinfo_db_export_import.py new file mode 100755 index 0000000..b2a83f6 --- /dev/null +++ b/tests/test_osinfo_db_export_import.py @@ -0,0 +1,263 @@ +#!/usr/bin/env python3 +# +# This work is licensed under the GNU GPLv2 or later. +# See the COPYING file in the top-level directory. + +import filecmp +import json +import os +import pytest +import requests +import shutil +import sys +import time +import util + + +def test_negative_osinfo_db_export_path(): + """ + Test osinfo-db-export dir fails + """ + cmd = [util.Tools.db_export, util.Data.positive] + returncode = util.get_returncode(cmd) + assert returncode == 1 + + +def test_osinfo_db_export_import_system(): + """ + Test osinfo-db-export --system and osinfo-db-import --system + """ + default_filename = "osinfo-db-%s.tar.xz" % time.strftime("%Y%m%d") + + os.environ["OSINFO_SYSTEM_DIR"] = util.Data.positive + cmd = [util.Tools.db_export, util.ToolsArgs.SYSTEM] + returncode = util.get_returncode(cmd) + assert returncode == 0 + assert os.path.isfile(default_filename) + + tempdir = util.tempdir() + os.environ["OSINFO_SYSTEM_DIR"] = tempdir + cmd = [util.Tools.db_import, util.ToolsArgs.SYSTEM, default_filename] + returncode = util.get_returncode(cmd) + assert returncode == 0 + dcmp = filecmp.dircmp(util.Data.positive, tempdir) + assert len(dcmp.right_only) == 1 + assert "VERSION" in dcmp.right_only + assert dcmp.left_only == [] + assert dcmp.diff_files == [] + shutil.rmtree(tempdir) + os.unlink(default_filename) + + +@pytest.fixture +def osinfo_db_export_local(): + filename = "foobar.tar.xz" + + os.environ["OSINFO_LOCAL_DIR"] = util.Data.positive + cmd = [util.Tools.db_export, util.ToolsArgs.LOCAL, filename] + return filename, util.get_returncode(cmd) + + + +@pytest.mark.usefixtures("osinfo_db_export_local") +def test_osinfo_db_export_local(osinfo_db_export_local): + """ + Test osinfo-db-export --local FILENAME + """ + filename, returncode = osinfo_db_export_local + + assert returncode == 0 + assert os.path.isfile(filename) + + + +@pytest.mark.usefixtures("osinfo_db_export_local") +def test_osinfo_db_import_local_file(osinfo_db_export_local): + """ + Test osinfo-db-import --local FILENAME + """ + filename, _ = osinfo_db_export_local + + tempdir = util.tempdir() + os.environ["OSINFO_LOCAL_DIR"] = tempdir + cmd = [util.Tools.db_import, util.ToolsArgs.LOCAL, filename] + returncode = util.get_returncode(cmd) + assert returncode == 0 + dcmp = filecmp.dircmp(util.Data.positive, tempdir) + assert len(dcmp.right_only) == 1 + assert "VERSION" in dcmp.right_only + assert dcmp.left_only == [] + assert dcmp.diff_files == [] + shutil.rmtree(tempdir) + os.unlink(filename) + + +@pytest.mark.usefixtures("osinfo_db_export_local") +def test_osinfo_db_import_local_dash(osinfo_db_export_local): + """ + Test cat FILENAME | osinfo-db-import - + """ + filename, _ = osinfo_db_export_local + + tempdir = util.tempdir() + os.environ["OSINFO_LOCAL_DIR"] = tempdir + cmd = ["cat", filename] + cmd2 = [util.Tools.db_import, util.ToolsArgs.LOCAL, "-"] + returncode = util.get_returncode(cmd, cmd2) + assert returncode == 0 + dcmp = filecmp.dircmp(util.Data.positive, tempdir) + assert len(dcmp.right_only) == 1 + assert "VERSION" in dcmp.right_only + assert dcmp.left_only == [] + assert dcmp.diff_files == [] + shutil.rmtree(tempdir) + os.unlink(filename) + + +@pytest.mark.usefixtures("osinfo_db_export_local") +def test_osinfo_db_import_local_no_file(osinfo_db_export_local): + """ + Test cat FILENAME | osinfo-db-import + """ + filename, _ = osinfo_db_export_local + + tempdir = util.tempdir() + os.environ["OSINFO_LOCAL_DIR"] = tempdir + cmd1 = ["cat", filename] + cmd2 = [util.Tools.db_import, util.ToolsArgs.LOCAL] + returncode = util.get_returncode(cmd1, cmd2) + assert returncode == 0 + dcmp = filecmp.dircmp(util.Data.positive, tempdir) + assert len(dcmp.right_only) == 1 + assert "VERSION" in dcmp.right_only + assert dcmp.left_only == [] + assert dcmp.diff_files == [] + shutil.rmtree(tempdir) + os.unlink(filename) + + +@pytest.fixture +def osinfo_db_export_user_license_version(): + version = "foobar" + versioned_filename = "osinfo-db-%s.tar.xz" % version + + os.environ["OSINFO_USER_DIR"] = util.Data.positive + cmd = [util.Tools.db_export, util.ToolsArgs.USER, + util.ToolsArgs.LICENSE, util.Data.license, + util.ToolsArgs.VERSION, version] + return versioned_filename, version, util.get_returncode(cmd) + + + +@pytest.mark.usefixtures("osinfo_db_export_user_license_version") +def test_osinfo_db_export_user_license_version( + osinfo_db_export_user_license_version): + """ + Test osinfo-db-export --user --license LICENSE --version VERSION + """ + filename, _, returncode = osinfo_db_export_user_license_version + + assert returncode == 0 + assert os.path.isfile(filename) + + +@pytest.mark.usefixtures("osinfo_db_export_user_license_version") +def test_osinfo_db_import_root_user_versioned_file( + osinfo_db_export_user_license_version): + """ + Test osinfo-db-import --root / --user VERSIONED_FILE + """ + filename, version, returncode = osinfo_db_export_user_license_version + + tempdir = util.tempdir() + os.environ["OSINFO_USER_DIR"] = tempdir + cmd = [util.Tools.db_import, util.ToolsArgs.ROOT, "/", + util.ToolsArgs.USER, filename] + returncode = util.get_returncode(cmd) + assert returncode == 0 + dcmp = filecmp.dircmp(util.Data.positive, tempdir) + assert len(dcmp.right_only) == 2 + assert "VERSION" in dcmp.right_only + with open(os.path.join(tempdir, "VERSION")) as out: + content = out.read() + assert content == version + assert "LICENSE" in dcmp.right_only + fcmp = filecmp.cmp(os.path.join(tempdir, "LICENSE"), util.Data.license) + assert fcmp is True + assert dcmp.left_only == [] + assert dcmp.diff_files == [] + shutil.rmtree(tempdir) + os.unlink(filename) + + +@pytest.mark.skipif(os.environ.get("OSINFO_DB_TOOLS_NETWORK_TESTS") is None, + reason="Network related tests are not enabled") +def test_osinfo_db_import_url(): + """ + Test osinfo-db-import URL + """ + url = "https://releases.pagure.org/libosinfo/osinfo-db-20190304.tar.xz" + + tempdir = util.tempdir() + os.environ["OSINFO_USER_DIR"] = tempdir + cmd = [util.Tools.db_import, util.ToolsArgs.USER, url] + returncode = util.get_returncode(cmd) + assert returncode == 0 + + test_file = "downloaded.tar.xz" + tempdir2 = util.tempdir() + req = requests.get(url) + open(test_file, "wb").write(req.content) + assert os.path.isfile(test_file) + + cmd = [util.Tools.db_import, util.ToolsArgs.DIR, tempdir2, test_file] + returncode = util.get_returncode(cmd) + assert returncode == 0 + + dcmp = filecmp.dircmp(tempdir, tempdir2) + assert dcmp.right_only == [] + assert dcmp.left_only == [] + assert dcmp.diff_files == [] + shutil.rmtree(tempdir) + shutil.rmtree(tempdir2) + os.unlink(test_file) + + +@pytest.mark.skipif(os.environ.get("OSINFO_DB_TOOLS_NETWORK_TESTS") is None, + reason="Network related tests are not enabled") +def test_osinfo_db_import_latest(): + """ + Test osinfo-dbimport --latest + """ + tempdir = util.tempdir() + os.environ["OSINFO_USER_DIR"] = tempdir + cmd = [util.Tools.db_import, util.ToolsArgs.LATEST] + returncode = util.get_returncode(cmd) + assert returncode == 0 + + latest_url = "https://db.libosinfo.org/latest.json" + req = requests.get(latest_url) + data = json.loads(req.content) + url = data["release"]["archive"] + + test_file = "downloaded.tar.xz" + tempdir2 = util.tempdir() + req = requests.get(url) + open(test_file, "wb").write(req.content) + assert os.path.isfile(test_file) + + cmd = [util.Tools.db_import, util.ToolsArgs.DIR, tempdir2, test_file] + returncode = util.get_returncode(cmd) + assert returncode == 0 + + dcmp = filecmp.dircmp(tempdir, tempdir2) + assert dcmp.right_only == [] + assert dcmp.left_only == [] + assert dcmp.diff_files == [] + shutil.rmtree(tempdir) + shutil.rmtree(tempdir2) + os.unlink(test_file) + + +if __name__ == "__main__": + exit(pytest.main(sys.argv)) -- 2.21.0 _______________________________________________ Libosinfo mailing list Libosinfo@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libosinfo