matrix-webhook/tests/start.py

123 lines
3.5 KiB
Python
Raw Permalink Normal View History

2021-07-11 10:17:09 -04:00
#!/usr/bin/env python
"""Entry point to start an instrumentalized bot for coverage and run tests."""
2021-07-14 14:00:52 -04:00
import argparse
import logging
2021-07-12 19:16:24 -04:00
from os import environ
2021-07-11 10:17:09 -04:00
from subprocess import Popen, run
2021-07-12 19:16:24 -04:00
from time import time
2021-07-11 10:17:09 -04:00
from unittest import main
2021-07-12 19:16:24 -04:00
import httpx
import yaml
from synapse._scripts.register_new_matrix_user import request_registration
2021-07-13 05:28:13 -04:00
BOT_URL = "http://localhost:4785"
KEY, MATRIX_URL, MATRIX_ID, MATRIX_PW = (
environ[v] for v in ["API_KEY", "MATRIX_URL", "MATRIX_ID", "MATRIX_PW"]
)
2021-07-13 04:28:40 -04:00
FULL_ID = f'@{MATRIX_ID}:{MATRIX_URL.split("/")[2]}'
2021-07-14 14:00:52 -04:00
LOGGER = logging.getLogger("matrix-webhook.tests.start")
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-v", "--verbose", action="count", default=0, help="increment verbosity level"
)
2021-07-13 04:28:40 -04:00
def bot_req(
req=None,
key=None,
room_id=None,
params=None,
key_as_param=False,
room_as_parameter=False,
):
2021-07-13 04:28:40 -04:00
"""Bot requests boilerplate."""
2021-07-31 07:06:52 -04:00
if params is None:
params = {}
2021-07-13 04:28:40 -04:00
if key is not None:
if key_as_param:
params["key"] = key
else:
req["key"] = key
if room_as_parameter:
params["room_id"] = room_id
url = BOT_URL if room_id is None or room_as_parameter else f"{BOT_URL}/{room_id}"
return httpx.post(url, params=params, json=req).json()
2021-07-12 19:16:24 -04:00
def wait_available(url: str, key: str, timeout: int = 10) -> bool:
"""Wait until a service answer correctly or timeout."""
2021-07-13 05:28:13 -04:00
2021-07-13 02:53:10 -04:00
def check_json(url: str, key: str) -> bool:
2021-08-27 12:12:11 -04:00
"""
Ensure a service at a given url answers with valid json including a certain key.
"""
2021-07-13 02:53:10 -04:00
try:
data = httpx.get(url).json()
return key in data
except httpx.ConnectError:
return False
2021-07-12 19:16:24 -04:00
start = time()
while True:
if check_json(url, key):
return True
if time() > start + timeout:
return False
2021-07-11 10:17:09 -04:00
def run_and_test():
"""Launch the bot and its tests."""
2021-07-13 04:28:40 -04:00
# Start the server, and wait for it
2021-07-14 14:00:52 -04:00
LOGGER.info("Spawning synapse")
2021-07-13 05:28:13 -04:00
srv = Popen(
[
"python",
"-m",
"synapse.app.homeserver",
"--config-path",
"/srv/homeserver.yaml",
]
)
if not wait_available(f"{MATRIX_URL}/_matrix/client/r0/login", "flows"):
2021-07-12 19:16:24 -04:00
return False
2021-07-13 04:28:40 -04:00
# Register a user for the bot.
2021-07-14 14:00:52 -04:00
LOGGER.info("Registering the bot")
2021-07-13 05:28:13 -04:00
with open("/srv/homeserver.yaml") as f:
2021-07-12 19:16:24 -04:00
secret = yaml.safe_load(f.read()).get("registration_shared_secret", None)
request_registration(MATRIX_ID, MATRIX_PW, MATRIX_URL, secret, admin=True)
2021-07-13 04:28:40 -04:00
# Start the bot, and wait for it
2021-07-14 14:00:52 -04:00
LOGGER.info("Spawning the bot")
2021-07-18 10:52:39 -04:00
bot = Popen(["coverage", "run", "-m", "matrix_webhook", "-vvvvv"])
2021-07-13 05:28:13 -04:00
if not wait_available(BOT_URL, "status"):
2021-07-12 19:16:24 -04:00
return False
2021-07-13 04:28:40 -04:00
# Run the main unittest module
2021-07-14 14:00:52 -04:00
LOGGER.info("Runnig unittests")
2021-07-11 10:17:09 -04:00
ret = main(module=None, exit=False).result.wasSuccessful()
2021-07-13 04:28:40 -04:00
2021-07-14 14:00:52 -04:00
LOGGER.info("Stopping synapse")
2021-07-13 04:28:40 -04:00
srv.terminate()
# TODO Check what the bot says when the server is offline
2021-07-31 05:21:29 -04:00
# print(bot_req({'data': 'bye'}, KEY), {'status': 200, 'ret': 'OK'})
2021-07-13 04:28:40 -04:00
2021-07-14 14:00:52 -04:00
LOGGER.info("Stopping the bot")
2021-07-11 10:17:09 -04:00
bot.terminate()
2021-07-13 04:28:40 -04:00
2021-07-14 14:00:52 -04:00
LOGGER.info("Processing coverage")
2021-07-13 05:28:13 -04:00
for cmd in ["report", "html", "xml"]:
run(["coverage", cmd])
2021-07-11 10:17:09 -04:00
return ret
2021-07-13 05:28:13 -04:00
if __name__ == "__main__":
2021-07-14 14:00:52 -04:00
args = parser.parse_args()
log_format = "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(message)s"
logging.basicConfig(level=50 - 10 * args.verbose, format=log_format)
2021-07-11 10:17:09 -04:00
exit(not run_and_test())