mirror of
https://github.com/GNS3/gns3-server.git
synced 2025-06-23 09:15:34 +00:00
Refactor tests
* Use pytest-aiohttp * Use the async def / await syntax. * Fix tests to run with Python 3.8
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 GNS3 Technologies Inc.
|
||||
# Copyright (C) 2020 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
@ -17,61 +17,60 @@
|
||||
|
||||
import os
|
||||
import pytest
|
||||
import asyncio
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import MagicMock
|
||||
from gns3server.web.web_server import WebServer
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
@pytest.fixture
|
||||
def web_server():
|
||||
|
||||
WebServer._instance = MagicMock()
|
||||
yield WebServer._instance
|
||||
WebServer._instance = None
|
||||
|
||||
|
||||
def test_shutdown_local(http_controller, web_server, config):
|
||||
async def test_shutdown_local(controller_api, web_server, config):
|
||||
|
||||
async def hello():
|
||||
return 0
|
||||
|
||||
web_server.shutdown_server.return_value = hello()
|
||||
config.set("Server", "local", True)
|
||||
response = http_controller.post('/shutdown', example=True)
|
||||
response = await controller_api.post('/shutdown')
|
||||
assert response.status == 201
|
||||
assert web_server.shutdown_server.called
|
||||
|
||||
|
||||
def test_shutdown_non_local(http_controller, web_server, config):
|
||||
"""
|
||||
Disallow shutdown of a non local GNS3 server
|
||||
"""
|
||||
async def test_shutdown_non_local(controller_api, web_server, config):
|
||||
|
||||
WebServer._instance = MagicMock()
|
||||
config.set("Server", "local", False)
|
||||
response = http_controller.post('/shutdown')
|
||||
response = await controller_api.post('/shutdown')
|
||||
assert response.status == 403
|
||||
assert not web_server.shutdown_server.called
|
||||
|
||||
|
||||
def test_debug(http_controller, config, tmpdir):
|
||||
config._main_config_file = str(tmpdir / "test.conf")
|
||||
async def test_debug(controller_api, config, tmpdir):
|
||||
|
||||
config._main_config_file = str(tmpdir / "test.conf")
|
||||
config.set("Server", "local", True)
|
||||
response = http_controller.post('/debug')
|
||||
response = await controller_api.post('/debug')
|
||||
assert response.status == 201
|
||||
debug_dir = os.path.join(config.config_dir, "debug")
|
||||
assert os.path.exists(debug_dir)
|
||||
assert os.path.exists(os.path.join(debug_dir, "controller.txt"))
|
||||
|
||||
|
||||
def test_debug_non_local(http_controller, config, tmpdir):
|
||||
config._main_config_file = str(tmpdir / "test.conf")
|
||||
async def test_debug_non_local(controller_api, config, tmpdir):
|
||||
|
||||
config._main_config_file = str(tmpdir / "test.conf")
|
||||
config.set("Server", "local", False)
|
||||
response = http_controller.post('/debug')
|
||||
response = await controller_api.post('/debug')
|
||||
assert response.status == 403
|
||||
|
||||
|
||||
def test_statistics_output(http_controller):
|
||||
response = http_controller.get('/statistics')
|
||||
async def test_statistics_output(controller_api):
|
||||
|
||||
response = await controller_api.get('/statistics')
|
||||
assert response.status == 200
|
||||
|
Reference in New Issue
Block a user