Make stateless lister constructors compatible with credentials

In effect, it just allows to add credentials to cgit, cran and pypi listers.

This fixes instances of error [1]

[1] https://sentry.softwareheritage.org/share/issue/2c35a9f129cf4982a2dd003a232d507a/

Related to T2998
This commit is contained in:
Antoine R. Dumont (@ardumont) 2021-01-28 13:31:45 +01:00
parent 72be074a79
commit ae17b6b9a0
No known key found for this signature in database
GPG key ID: 52E2E9840D10C3B8
7 changed files with 123 additions and 15 deletions

View file

@ -13,7 +13,7 @@ import requests
from requests.exceptions import HTTPError
from swh.lister import USER_AGENT
from swh.lister.pattern import StatelessLister
from swh.lister.pattern import CredentialsType, StatelessLister
from swh.scheduler.interface import SchedulerInterface
from swh.scheduler.model import ListedOrigin
@ -39,7 +39,11 @@ class CGitLister(StatelessLister[Repositories]):
LISTER_NAME = "cgit"
def __init__(
self, scheduler: SchedulerInterface, url: str, instance: Optional[str] = None
self,
scheduler: SchedulerInterface,
url: str,
instance: Optional[str] = None,
credentials: Optional[CredentialsType] = None,
):
"""Lister class for CGit repositories.
@ -55,7 +59,7 @@ class CGitLister(StatelessLister[Repositories]):
assert instance is not None # Make mypy happy
super().__init__(
scheduler=scheduler, credentials=None, url=url, instance=instance,
scheduler=scheduler, url=url, instance=instance, credentials=credentials,
)
self.session = requests.Session()

View file

@ -160,3 +160,39 @@ def test_lister_cgit_get_origin_from_repo_failing(
expected_nb_origins = 15
assert stats == ListerStats(pages=3, origins=expected_nb_origins)
@pytest.mark.parametrize(
"credentials, expected_credentials",
[
(None, []),
({"key": "value"}, []),
(
{"cgit": {"tizen": [{"username": "user", "password": "pass"}]}},
[{"username": "user", "password": "pass"}],
),
],
)
def test_lister_cgit_instantiation_with_credentials(
credentials, expected_credentials, swh_scheduler
):
url = "https://git.tizen/cgit/"
lister = CGitLister(
swh_scheduler, url=url, instance="tizen", credentials=credentials
)
# Credentials are allowed in constructor
assert lister.credentials == expected_credentials
def test_lister_cgit_from_configfile(swh_scheduler_config, mocker):
load_from_envvar = mocker.patch("swh.lister.pattern.load_from_envvar")
load_from_envvar.return_value = {
"scheduler": {"cls": "local", **swh_scheduler_config},
"url": "https://git.tizen/cgit/",
"instance": "tizen",
"credentials": {},
}
lister = CGitLister.from_configfile()
assert lister.scheduler is not None
assert lister.credentials is not None

View file

@ -3,8 +3,6 @@
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
from unittest.mock import patch
from swh.lister.pattern import ListerStats
@ -16,11 +14,11 @@ def test_cgit_ping(swh_scheduler_celery_app, swh_scheduler_celery_worker):
assert res.result == "OK"
@patch("swh.lister.cgit.tasks.CGitLister")
def test_cgit_lister_task(
lister, swh_scheduler_celery_app, swh_scheduler_celery_worker
swh_scheduler_celery_app, swh_scheduler_celery_worker, mocker
):
# setup the mocked CGitLister
lister = mocker.patch("swh.lister.cgit.tasks.CGitLister")
lister.from_configfile.return_value = lister
lister.run.return_value = ListerStats(pages=10, origins=500)