2020-08-11 09:43:03 +00:00
|
|
|
# MIT licensed
|
|
|
|
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al.
|
|
|
|
|
2017-07-04 09:04:29 +00:00
|
|
|
import asyncio
|
2018-09-21 04:17:21 +00:00
|
|
|
import structlog
|
2020-08-20 06:56:40 +00:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
2017-07-04 09:04:29 +00:00
|
|
|
|
2021-12-29 08:36:23 +00:00
|
|
|
import tomli
|
2020-08-17 07:11:14 +00:00
|
|
|
import pytest
|
2022-02-22 22:28:23 +00:00
|
|
|
import pytest_asyncio
|
2018-05-08 10:18:10 +00:00
|
|
|
|
2020-08-17 07:11:14 +00:00
|
|
|
from nvchecker import core
|
|
|
|
from nvchecker import __main__ as main
|
|
|
|
from nvchecker.util import Entries, VersData, RawResult
|
|
|
|
|
2020-08-20 07:18:05 +00:00
|
|
|
use_keyfile = False
|
|
|
|
|
2020-08-17 07:11:14 +00:00
|
|
|
async def run(
|
|
|
|
entries: Entries, max_concurrency: int = 20,
|
|
|
|
) -> VersData:
|
2020-09-20 14:15:54 +00:00
|
|
|
task_sem = asyncio.Semaphore(max_concurrency)
|
2020-08-17 07:11:14 +00:00
|
|
|
result_q: asyncio.Queue[RawResult] = asyncio.Queue()
|
2020-08-20 06:56:40 +00:00
|
|
|
keyfile = os.environ.get('KEYFILE')
|
2020-08-20 07:18:05 +00:00
|
|
|
if use_keyfile and keyfile:
|
2020-08-20 06:56:40 +00:00
|
|
|
filepath = Path(keyfile)
|
|
|
|
keymanager = core.KeyManager(filepath)
|
2020-08-17 07:11:14 +00:00
|
|
|
else:
|
|
|
|
keymanager = core.KeyManager(None)
|
|
|
|
|
2020-09-28 07:09:51 +00:00
|
|
|
dispatcher = core.setup_httpclient()
|
2021-06-08 06:55:57 +00:00
|
|
|
entry_waiter = core.EntryWaiter()
|
2020-09-28 07:09:51 +00:00
|
|
|
futures = dispatcher.dispatch(
|
2020-09-20 14:15:54 +00:00
|
|
|
entries, task_sem, result_q,
|
2021-06-08 06:55:57 +00:00
|
|
|
keymanager, entry_waiter, 1, {},
|
2020-08-17 07:11:14 +00:00
|
|
|
)
|
2018-05-08 10:18:10 +00:00
|
|
|
|
2020-08-17 07:11:14 +00:00
|
|
|
oldvers: VersData = {}
|
2021-06-08 06:55:57 +00:00
|
|
|
result_coro = core.process_result(oldvers, result_q, entry_waiter)
|
2020-08-17 07:11:14 +00:00
|
|
|
runner_coro = core.run_tasks(futures)
|
2018-05-08 11:15:36 +00:00
|
|
|
|
2022-01-20 08:38:10 +00:00
|
|
|
vers, _has_failures = await main.run(result_coro, runner_coro)
|
|
|
|
return vers
|
2018-05-08 10:18:10 +00:00
|
|
|
|
2022-02-22 22:28:23 +00:00
|
|
|
@pytest_asyncio.fixture(scope="module")
|
2020-08-17 07:11:14 +00:00
|
|
|
async def get_version():
|
|
|
|
async def __call__(name, config):
|
|
|
|
entries = {name: config}
|
|
|
|
newvers = await run(entries)
|
2020-08-17 08:21:02 +00:00
|
|
|
return newvers.get(name)
|
2018-05-08 10:18:10 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
return __call__
|
2017-07-04 09:04:29 +00:00
|
|
|
|
2022-02-22 22:28:23 +00:00
|
|
|
@pytest_asyncio.fixture(scope="module")
|
2020-08-17 07:11:14 +00:00
|
|
|
async def run_str():
|
|
|
|
async def __call__(str):
|
2021-12-29 08:36:23 +00:00
|
|
|
entries = tomli.loads(str)
|
2020-08-17 07:11:14 +00:00
|
|
|
newvers = await run(entries)
|
|
|
|
return newvers.popitem()[1]
|
2017-07-04 09:04:29 +00:00
|
|
|
|
2020-08-17 07:11:14 +00:00
|
|
|
return __call__
|
2017-07-04 09:04:29 +00:00
|
|
|
|
2022-02-22 22:28:23 +00:00
|
|
|
@pytest_asyncio.fixture(scope="module")
|
2020-08-17 07:11:14 +00:00
|
|
|
async def run_str_multi():
|
|
|
|
async def __call__(str):
|
2021-12-29 08:36:23 +00:00
|
|
|
entries = tomli.loads(str)
|
2020-08-17 07:11:14 +00:00
|
|
|
newvers = await run(entries)
|
|
|
|
return newvers
|
2017-07-04 09:04:29 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
return __call__
|
2017-07-04 09:04:29 +00:00
|
|
|
|
2021-12-29 08:36:23 +00:00
|
|
|
loop = asyncio.new_event_loop()
|
2020-07-02 08:10:15 +00:00
|
|
|
@pytest.fixture(scope="session")
|
2017-07-04 09:04:29 +00:00
|
|
|
def event_loop(request):
|
2018-10-10 09:08:28 +00:00
|
|
|
"""Override pytest-asyncio's event_loop fixture,
|
|
|
|
Don't create an instance of the default event loop for each test case.
|
2020-07-02 08:10:15 +00:00
|
|
|
We need the same ioloop across tests for the aiohttp support.
|
2018-10-10 09:08:28 +00:00
|
|
|
"""
|
|
|
|
yield loop
|
2018-09-21 04:17:21 +00:00
|
|
|
|
2019-03-07 08:25:54 +00:00
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
2018-09-21 04:17:21 +00:00
|
|
|
def raise_on_logger_msg():
|
2018-10-10 09:08:28 +00:00
|
|
|
def proc(logger, method_name, event_dict):
|
2018-12-15 11:44:26 +00:00
|
|
|
if method_name in ('warning', 'error'):
|
2019-03-07 08:45:14 +00:00
|
|
|
if 'exc_info' in event_dict:
|
2022-06-12 06:22:14 +00:00
|
|
|
exc = event_dict['exc_info']
|
|
|
|
if isinstance(exc, Exception):
|
|
|
|
raise exc
|
|
|
|
else: # exc_info=True
|
|
|
|
raise
|
2019-05-01 09:26:10 +00:00
|
|
|
if not event_dict['event'].startswith(('rate limited', 'no-result')):
|
2019-04-08 04:15:49 +00:00
|
|
|
raise RuntimeError(event_dict['event'])
|
2018-10-10 09:08:28 +00:00
|
|
|
return event_dict['event']
|
2018-09-21 04:17:21 +00:00
|
|
|
|
2018-10-10 09:08:28 +00:00
|
|
|
structlog.configure([proc])
|
2019-10-15 09:27:34 +00:00
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
# register an additional marker
|
|
|
|
config.addinivalue_line(
|
|
|
|
'markers', 'needs_net: mark test to require Internet access',
|
|
|
|
)
|
2020-08-20 07:18:05 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def keyfile():
|
|
|
|
global use_keyfile
|
|
|
|
if 'KEYFILE' not in os.environ:
|
|
|
|
pytest.skip('KEYFILE not set')
|
|
|
|
return
|
|
|
|
|
|
|
|
use_keyfile = True
|
|
|
|
yield
|
|
|
|
use_keyfile = False
|