From 6633020fba07ba367285747888af99d31032ceaa Mon Sep 17 00:00:00 2001 From: Guilhem Saurel Date: Tue, 13 Jul 2021 10:28:40 +0200 Subject: [PATCH] clean tests --- .github/workflows/test.yml | 8 +++----- test.yml | 1 + tests/Dockerfile | 2 ++ tests/start.py | 25 ++++++++++++++++++++++--- tests/tests.py | 32 +------------------------------- 5 files changed, 29 insertions(+), 39 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 33692e1..31504e0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,11 +4,9 @@ jobs: tests: runs-on: ubuntu-latest steps: - - name: Check out repository code + - name: Checkout uses: actions/checkout@v2 - - name: Start - run: docker-compose -f test.yml up -d - name: Tests - run: docker-compose -f test.yml run --entrypoint "" tests ./tests/start.py - - name: "Upload coverage to Codecov" + run: docker-compose -f test.yml up --exit-code-from tests + - name: Coverage uses: codecov/codecov-action@v1 diff --git a/test.yml b/test.yml index 33eee30..7ffda14 100644 --- a/test.yml +++ b/test.yml @@ -5,6 +5,7 @@ services: build: context: . dockerfile: tests/Dockerfile + entrypoint: "" env_file: - tests/.env volumes: diff --git a/tests/Dockerfile b/tests/Dockerfile index af2e1bd..09778e3 100644 --- a/tests/Dockerfile +++ b/tests/Dockerfile @@ -15,3 +15,5 @@ RUN chown -R 991:991 . RUN pip install --no-cache-dir markdown matrix-nio httpx coverage WORKDIR /app + +CMD ./tests/start.py diff --git a/tests/start.py b/tests/start.py index 79ac0a8..97cc0f0 100755 --- a/tests/start.py +++ b/tests/start.py @@ -11,7 +11,16 @@ import yaml from synapse._scripts.register_new_matrix_user import request_registration BOT_URL = 'http://localhost:4785' -MATRIX_URL, MATRIX_ID, MATRIX_PW = (environ[v] for v in ['MATRIX_URL', 'MATRIX_ID', 'MATRIX_PW']) +KEY, MATRIX_URL, MATRIX_ID, MATRIX_PW = (environ[v] for v in ['API_KEY', 'MATRIX_URL', 'MATRIX_ID', 'MATRIX_PW']) +FULL_ID = f'@{MATRIX_ID}:{MATRIX_URL.split("/")[2]}' + + +def bot_req(req=None, key=None, room_id=None): + """Bot requests boilerplate.""" + if key is not None: + req['key'] = key + url = BOT_URL if room_id is None else f'{BOT_URL}/{room_id}' + return httpx.post(url, json=req).json() def wait_available(url: str, key: str, timeout: int = 10) -> bool: @@ -34,21 +43,31 @@ def wait_available(url: str, key: str, timeout: int = 10) -> bool: def run_and_test(): """Launch the bot and its tests.""" + # Start the server, and wait for it + srv = Popen(['python', '-m', 'synapse.app.homeserver', '--config-path', '/srv/homeserver.yaml']) if not wait_available(f'{MATRIX_URL}/_matrix/client/r0/login', 'flows'): return False - # Try to register a user for the bot. + # Register a user for the bot. with open('/srv/homeserver.yaml') as f: secret = yaml.safe_load(f.read()).get("registration_shared_secret", None) request_registration(MATRIX_ID, MATRIX_PW, MATRIX_URL, secret, admin=True) + # Start the bot, and wait for it bot = Popen(['coverage', 'run', 'matrix_webhook.py']) - if not wait_available(BOT_URL, 'status'): return False + # Run the main unittest module ret = main(module=None, exit=False).result.wasSuccessful() + + srv.terminate() + + # TODO Check what the bot says when the server is offline + # print(bot_req({'text': 'bye'}, KEY), {'status': 200, 'ret': 'OK'}) + bot.terminate() + for cmd in ['report', 'html', 'xml']: run(['coverage', cmd]) return ret diff --git a/tests/tests.py b/tests/tests.py index 9a0e807..3bf710e 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1,23 +1,10 @@ """Main test module.""" -import os import unittest -import httpx import nio -from .start import BOT_URL, MATRIX_ID, MATRIX_PW, MATRIX_URL - -KEY = os.environ['API_KEY'] -FULL_ID = f'@{MATRIX_ID}:{MATRIX_URL.split("/")[2]}' - - -def bot_req(req=None, key=None, room_id=None): - """Bot requests boilerplate.""" - if key is not None: - req['key'] = key - url = BOT_URL if room_id is None else f'{BOT_URL}/{room_id}' - return httpx.post(url, json=req).json() +from .start import FULL_ID, KEY, MATRIX_ID, MATRIX_PW, MATRIX_URL, bot_req class BotTest(unittest.IsolatedAsyncioTestCase): @@ -50,20 +37,3 @@ class BotTest(unittest.IsolatedAsyncioTestCase): self.assertEqual(message.sender, FULL_ID) self.assertEqual(message.body, text) self.assertEqual(message.formatted_body, '

Hello

') - - async def test_z_disconnected(self): - """Send a message after disconnection, and check the error.""" - client = nio.AsyncClient(MATRIX_URL, MATRIX_ID) - await client.login(MATRIX_PW) - token = client.access_token - - resp = httpx.post(f'{MATRIX_URL}/_synapse/admin/v1/deactivate/{FULL_ID}', - json={'erase': True}, - params={'access_token': token}) - self.assertEqual(resp.json(), {'id_server_unbind_result': 'success'}) - - await client.logout(all_devices=True) - await client.close() - - # TODO: I was hopping that one wouldn't be happy - self.assertEqual(bot_req({'text': 'bye'}, KEY), {'status': 200, 'ret': 'OK'})