2021-03-06 16:21:28 +01:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
from threading import Thread
|
|
|
|
|
2023-05-20 15:26:58 +02:00
|
|
|
import pytest
|
2024-01-04 23:24:25 +01:00
|
|
|
import requests
|
2023-05-20 15:26:58 +02:00
|
|
|
|
2023-07-23 23:01:15 +02:00
|
|
|
from platypush import Application, Config
|
2021-03-06 16:21:28 +01:00
|
|
|
|
|
|
|
from .utils import config_file, set_base_url
|
|
|
|
|
2024-01-04 23:24:25 +01:00
|
|
|
app_start_timeout = 15
|
2021-03-06 16:21:28 +01:00
|
|
|
|
|
|
|
|
2021-03-06 20:02:25 +01:00
|
|
|
def clear_loggers():
|
|
|
|
"""
|
|
|
|
Remove handlers from all loggers at teardown.
|
|
|
|
This is to prevent pytest spitting out logging errors on teardown if the logging objects have been deinitialized
|
|
|
|
(see https://github.com/pytest-dev/pytest/issues/5502#issuecomment-647157873).
|
|
|
|
"""
|
|
|
|
loggers = [logging.getLogger()] + list(logging.Logger.manager.loggerDict.values())
|
|
|
|
for logger in loggers:
|
|
|
|
handlers = getattr(logger, 'handlers', [])
|
|
|
|
for handler in handlers:
|
|
|
|
logger.removeHandler(handler)
|
|
|
|
|
|
|
|
|
2024-01-04 23:24:25 +01:00
|
|
|
def _wait_for_app(app: Application, timeout: int = app_start_timeout):
|
|
|
|
logging.info('Waiting for the app to start')
|
|
|
|
start_time = time.time()
|
|
|
|
http = None
|
|
|
|
success = False
|
|
|
|
|
|
|
|
while not http and time.time() - start_time < timeout:
|
|
|
|
http = (app.backends or {}).get('http')
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
assert http, f'HTTP backend not started after {timeout} seconds'
|
|
|
|
|
|
|
|
while not success and time.time() - start_time < timeout:
|
|
|
|
try:
|
2024-07-24 21:34:30 +02:00
|
|
|
response = requests.get(
|
|
|
|
f'http://localhost:{http.port}/', timeout=1, allow_redirects=False
|
|
|
|
)
|
2024-01-04 23:24:25 +01:00
|
|
|
response.raise_for_status()
|
|
|
|
success = True
|
|
|
|
except Exception as e:
|
2024-07-24 21:34:30 +02:00
|
|
|
logging.info('App not ready yet: %s', e)
|
2024-01-04 23:24:25 +01:00
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
assert success, f'App not ready after {timeout} seconds'
|
|
|
|
|
|
|
|
|
2021-03-06 16:21:28 +01:00
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def app():
|
|
|
|
logging.info('Starting Platypush test service')
|
|
|
|
|
|
|
|
Config.init(config_file)
|
2023-07-24 10:37:07 +02:00
|
|
|
_app = Application(
|
|
|
|
config_file=config_file,
|
|
|
|
redis_queue='platypush-tests/bus',
|
|
|
|
start_redis=True,
|
|
|
|
redis_port=16379,
|
|
|
|
)
|
2023-05-20 15:26:58 +02:00
|
|
|
Thread(target=_app.run).start()
|
2024-01-04 23:24:25 +01:00
|
|
|
_wait_for_app(_app)
|
2023-05-20 15:26:58 +02:00
|
|
|
yield _app
|
2021-03-06 16:21:28 +01:00
|
|
|
|
|
|
|
logging.info('Stopping Platypush test service')
|
2023-08-13 22:23:15 +02:00
|
|
|
_app.stop()
|
2021-03-06 20:02:25 +01:00
|
|
|
clear_loggers()
|
2023-05-20 15:26:58 +02:00
|
|
|
db = (Config.get('main.db') or {}).get('engine', '')[len('sqlite:///') :]
|
2021-03-06 16:21:28 +01:00
|
|
|
|
2023-05-20 15:26:58 +02:00
|
|
|
if db and os.path.isfile(db):
|
|
|
|
logging.info('Removing temporary db file %s', db)
|
|
|
|
os.unlink(db)
|
2021-03-06 16:21:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def db_file():
|
2023-05-20 15:26:58 +02:00
|
|
|
yield Config.get('main.db')['engine'][len('sqlite:///') :]
|
2021-03-06 16:21:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def base_url():
|
|
|
|
backends = Config.get_backends()
|
|
|
|
assert 'http' in backends, 'Missing HTTP server configuration'
|
2023-05-20 15:26:58 +02:00
|
|
|
url = f'http://localhost:{backends["http"]["port"]}'
|
2021-03-06 16:21:28 +01:00
|
|
|
set_base_url(url)
|
|
|
|
yield url
|