1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-26 00:38:10 +00:00

Test compute notifications and run tests on macOS and Windows.

This commit is contained in:
grossmj 2020-10-19 19:25:32 +10:30
parent 6af8c8933e
commit 596ee9b66d
4 changed files with 33 additions and 36 deletions

View File

@ -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]

View File

@ -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

View File

@ -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:

View File

@ -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"