diff --git a/conftest.py b/conftest.py index e709cbe..2daa11d 100644 --- a/conftest.py +++ b/conftest.py @@ -1,28 +1,10 @@ # Copyright (C) 2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import os -import pytest - pytest_plugins = ["swh.scheduler.pytest_plugin", "swh.lister.pytest_plugin"] os.environ["LC_ALL"] = "C.UTF-8" - - -@pytest.fixture -def mock_get_scheduler(monkeypatch, swh_scheduler): - """Override the get_scheduler function in swh.lister.core.lister_base, to - return the swh_scheduler fixture. - """ - from swh.lister.core import lister_base - - # Match the signature from swh.scheduler.get_scheduler - def get_scheduler(cls, args={}): - return swh_scheduler - - monkeypatch.setattr(lister_base, "get_scheduler", get_scheduler) - - yield monkeypatch diff --git a/swh/lister/pytest_plugin.py b/swh/lister/pytest_plugin.py index 31dc013..29c194b 100644 --- a/swh/lister/pytest_plugin.py +++ b/swh/lister/pytest_plugin.py @@ -1,64 +1,62 @@ # Copyright (C) 2019-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import logging import os import pytest from sqlalchemy import create_engine import yaml from swh.lister import SUPPORTED_LISTERS, get_lister from swh.lister.core.models import initialize logger = logging.getLogger(__name__) @pytest.fixture def lister_db_url(postgresql_proc, postgresql): db_url = "postgresql://{user}@{host}:{port}/{dbname}".format( host=postgresql_proc.host, port=postgresql_proc.port, user="postgres", dbname="tests", ) logger.debug("lister db_url: %s", db_url) return db_url @pytest.fixture def lister_under_test(): """Fixture to determine which lister to test""" return "core" @pytest.fixture def swh_lister_config(lister_db_url, swh_scheduler_config): return { "scheduler": {"cls": "local", "args": {"db": swh_scheduler_config}["db"]}, "lister": {"cls": "local", "args": {"db": lister_db_url},}, "credentials": {}, "cache_responses": False, } @pytest.fixture(autouse=True) def swh_config(swh_lister_config, monkeypatch, tmp_path): conf_path = os.path.join(str(tmp_path), "lister.yml") with open(conf_path, "w") as f: f.write(yaml.dump(swh_lister_config)) monkeypatch.setenv("SWH_CONFIG_FILENAME", conf_path) return conf_path @pytest.fixture -def swh_lister( - mock_get_scheduler, lister_db_url, swh_scheduler, lister_under_test, swh_config -): +def swh_lister(lister_db_url, swh_scheduler, lister_under_test, swh_config): assert lister_under_test in SUPPORTED_LISTERS lister = get_lister(lister_under_test, db_url=lister_db_url) initialize(create_engine(lister_db_url), drop_tables=True) return lister diff --git a/swh/lister/tests/test_cli.py b/swh/lister/tests/test_cli.py index e71a2fa..82c3f49 100644 --- a/swh/lister/tests/test_cli.py +++ b/swh/lister/tests/test_cli.py @@ -1,68 +1,68 @@ # Copyright (C) 2019-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import pytest from swh.lister.cli import SUPPORTED_LISTERS, get_lister from swh.lister.core.lister_base import ListerBase from .test_utils import init_db def test_get_lister_wrong_input(): """Unsupported lister should raise""" with pytest.raises(ValueError) as e: get_lister("unknown", "db-url") assert "Invalid lister" in str(e.value) -def test_get_lister(mock_get_scheduler): +def test_get_lister(): """Instantiating a supported lister should be ok """ db_url = init_db().url() # exclude listers because they need special instantiation treatment unrelated to # this test (launchpad: network mock, gnu: scheduler load task) listers_to_instantiate = set(SUPPORTED_LISTERS) - {"launchpad", "gnu"} for lister_name in listers_to_instantiate: lst = get_lister(lister_name, db_url) assert isinstance(lst, ListerBase) def test_get_lister_override(): """Overriding the lister configuration should populate its config """ db_url = init_db().url() listers = { "gitlab": "https://other.gitlab.uni/api/v4/", "phabricator": "https://somewhere.org/api/diffusion.repository.search", "cgit": "https://some.where/cgit", } # check the override ends up defined in the lister for lister_name, url in listers.items(): lst = get_lister( lister_name, db_url, **{"url": url, "priority": "high", "policy": "oneshot",}, ) assert lst.url == url assert lst.config["priority"] == "high" assert lst.config["policy"] == "oneshot" # check the default urls are used and not the override (since it's not # passed) for lister_name, url in listers.items(): lst = get_lister(lister_name, db_url) # no override so this does not end up in lister's configuration assert "url" not in lst.config assert "priority" not in lst.config assert "oneshot" not in lst.config assert lst.url == lst.DEFAULT_URL