diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 08fd0e2e..2f17acac 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -13,7 +13,8 @@ on: jobs: build: - runs-on: ubuntu-latest + runs-on: [ubuntu-latest, macos-latest, windows-latest] + strategy: matrix: python-version: [3.6, 3.7, 3.8] diff --git a/tests/conftest.py b/tests/conftest.py index 0bc47847..4d71b007 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -22,22 +22,9 @@ sys.original_platform = sys.platform from fastapi.testclient import TestClient from gns3server.app import app - from httpx import AsyncClient -# @pytest.fixture -# async def client(controller): -# -# async with AsyncClient(app=app, base_url="http://test") as ac: -# response = await ac.get("/") -# -# assert response.status_code == 200 -# assert response.json() == {"message": "Tomato"} -# -# return TestClient(app) - - if sys.platform.startswith("win"): @pytest.yield_fixture(scope="session") def loop(request): @@ -55,6 +42,12 @@ def http_client(): return AsyncClient(app=app, base_url="http://test-api") +@pytest.fixture(scope='function') +def ws_client(): + + return TestClient(app) + + @pytest.fixture def controller_config_path(tmpdir): @@ -96,21 +89,21 @@ def compute_project(tmpdir): @pytest.fixture -def compute_api(http_client): +def compute_api(http_client, ws_client): """ Return an helper allowing you to call the hypervisor API via HTTP """ - return Query(http_client, prefix="/compute", api_version=2) + return Query(http_client, ws_client, prefix="/compute", api_version=2) @pytest.fixture -def controller_api(http_client, controller): +def controller_api(http_client, ws_client, controller): """ Return an helper allowing you to call the server API without any prefix """ - return Query(http_client, api_version=2) + return Query(http_client, ws_client, api_version=2) @pytest.fixture diff --git a/tests/endpoints/base.py b/tests/endpoints/base.py index 9e724ac9..37dc1881 100644 --- a/tests/endpoints/base.py +++ b/tests/endpoints/base.py @@ -28,13 +28,14 @@ class Query: Helper to make queries against the test server """ - def __init__(self, http_client, prefix='', api_version=None): + def __init__(self, http_client, ws_client, prefix='', api_version=None): """ :param prefix: Prefix added before path (ex: /compute) :param api_version: Version of the API """ self._http_client = http_client + self._ws_client = ws_client self._prefix = prefix self._api_version = api_version @@ -53,6 +54,10 @@ class Query: def patch(self, path, **kwargs): return self._request("PATCH", path, **kwargs) + def ws(self, path): + + return self._ws_client.websocket_connect(self.get_url(path)) + def get_url(self, path): if self._api_version is None: diff --git a/tests/endpoints/compute/test_notifications.py b/tests/endpoints/compute/test_notifications.py index 603d8810..f5849612 100644 --- a/tests/endpoints/compute/test_notifications.py +++ b/tests/endpoints/compute/test_notifications.py @@ -21,20 +21,18 @@ import json from gns3server.compute.notification_manager import NotificationManager -# @pytest.mark.asyncio -# async def test_notification_ws(compute_api, http_client): -# -# ws = await http_client.ws_connect(compute_api.get_url("/notifications/ws")) -# answer = await ws.receive() -# answer = json.loads(answer.data) -# -# assert answer["action"] == "ping" -# -# NotificationManager.instance().emit("test", {}) -# -# answer = await ws.receive() -# answer = json.loads(answer.data) -# assert answer["action"] == "test" -# -# if not ws.closed: -# await ws.close() +@pytest.mark.asyncio +async def test_notification_ws(compute_api): + + with compute_api.ws("/notifications/ws") as ws: + + answer = ws.receive_text() + answer = json.loads(answer) + + assert answer["action"] == "ping" + + NotificationManager.instance().emit("test", {}) + + answer = ws.receive_text() + answer = json.loads(answer) + assert answer["action"] == "test"