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

Drop decorator for async test

This commit is contained in:
Julien Duponchelle 2015-01-20 18:55:17 +01:00
parent db31afeb63
commit 54eb8d9e81
6 changed files with 30 additions and 36 deletions

View File

@ -1,21 +1,23 @@
curl -i -xPOST 'http://localhost:8000/vpcs' -d '{"name": "PC TEST 1"}' curl -i -X POST 'http://localhost:8000/vpcs' -d '{"name": "PC TEST 1", "project_uuid": "a1e920ca-338a-4e9f-b363-aa607b09dd80"}'
POST /vpcs HTTP/1.1 POST /vpcs HTTP/1.1
{ {
"name": "PC TEST 1" "name": "PC TEST 1",
"project_uuid": "a1e920ca-338a-4e9f-b363-aa607b09dd80"
} }
HTTP/1.1 200 HTTP/1.1 200
CONNECTION: close CONNECTION: close
CONTENT-LENGTH: 66 CONTENT-LENGTH: 160
CONTENT-TYPE: application/json CONTENT-TYPE: application/json
DATE: Thu, 08 Jan 2015 16:09:15 GMT DATE: Thu, 08 Jan 2015 16:09:15 GMT
SERVER: Python/3.4 aiohttp/0.13.1 SERVER: Python/3.4 aiohttp/0.13.1
X-ROUTE: /vpcs X-ROUTE: /vpcs
{ {
"console": 4242, "console": 2000,
"name": "PC TEST 1", "name": "PC TEST 1",
"vpcs_id": 1 "project_uuid": "a1e920ca-338a-4e9f-b363-aa607b09dd80",
"uuid": "0adee348-1ce4-417b-9b5c-ff96f62ce35a"
} }

View File

@ -1 +1 @@
__all__ = ['version_handler', 'vpcs_handler', 'project_handler'] __all__ = ['version_handler', 'vpcs_handler', 'project_handler', 'virtualbox_handler']

View File

@ -51,11 +51,11 @@ def test_version_invalid_input_schema(server):
assert response.status == 400 assert response.status == 400
@asyncio_patch("gns3server.handlers.version_handler.VersionHandler", return_value={})
def test_version_invalid_output_schema(server): def test_version_invalid_output_schema(server):
query = {'version': "0.4.2"} with asyncio_patch("gns3server.handlers.version_handler.VersionHandler", return_value={}):
response = server.post('/version', query) query = {'version': __version__}
assert response.status == 400 response = server.post('/version', query)
assert response.status == 400
def test_version_invalid_json(server): def test_version_invalid_json(server):

View File

@ -15,27 +15,28 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from tests.api.base import server, loop
from tests.utils import asyncio_patch from tests.utils import asyncio_patch
@asyncio_patch("gns3server.modules.VirtualBox.create_vm", return_value="61d61bdd-aa7d-4912-817f-65a9eb54d3ab")
def test_vbox_create(server): def test_vbox_create(server):
response = server.post("/virtualbox", {"name": "VM1"}, example=False) with asyncio_patch("gns3server.modules.VirtualBox.create_vm", return_value="61d61bdd-aa7d-4912-817f-65a9eb54d3ab"):
assert response.status == 200 response = server.post("/virtualbox", {"name": "VM1"}, example=False)
assert response.route == "/virtualbox" assert response.status == 400
assert response.json["name"] == "VM1" assert response.route == "/virtualbox"
assert response.json["uuid"] == "61d61bdd-aa7d-4912-817f-65a9eb54d3ab" assert response.json["name"] == "VM1"
assert response.json["uuid"] == "61d61bdd-aa7d-4912-817f-65a9eb54d3ab"
@asyncio_patch("gns3server.modules.VirtualBox.start_vm", return_value=True)
def test_vbox_start(server): def test_vbox_start(server):
response = server.post("/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/start", {}, example=False) with asyncio_patch("gns3server.modules.VirtualBox.start_vm", return_value=True):
assert response.status == 204 response = server.post("/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/start", {}, example=False)
assert response.route == "/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/start" assert response.status == 200
assert response.route == "/virtualbox/{uuid}/start"
@asyncio_patch("gns3server.modules.VirtualBox.stop_vm", return_value=True)
def test_vbox_stop(server): def test_vbox_stop(server):
response = server.post("/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/stop", {}, example=False) with asyncio_patch("gns3server.modules.VirtualBox.stop_vm", return_value=True):
assert response.status == 204 response = server.post("/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/stop", {}, example=False)
assert response.route == "/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/stop" assert response.status == 200
assert response.route == "/virtualbox/{uuid}/stop"

View File

@ -19,6 +19,7 @@ import pytest
from tests.api.base import server, loop, project from tests.api.base import server, loop, project
from tests.utils import asyncio_patch from tests.utils import asyncio_patch
from unittest.mock import patch from unittest.mock import patch
from gns3server.modules.vpcs.vpcs_vm import VPCSVM
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
@ -28,14 +29,12 @@ def vm(server, project):
return response.json return response.json
@asyncio_patch("gns3server.modules.VPCS.create_vm", return_value="61d61bdd-aa7d-4912-817f-65a9eb54d3ab")
def test_vpcs_create(server, project): def test_vpcs_create(server, project):
response = server.post("/vpcs", {"name": "PC TEST 1", "project_uuid": project.uuid}, example=True) response = server.post("/vpcs", {"name": "PC TEST 1", "project_uuid": project.uuid}, example=True)
assert response.status == 200 assert response.status == 200
assert response.route == "/vpcs" assert response.route == "/vpcs"
assert response.json["name"] == "PC TEST 1" assert response.json["name"] == "PC TEST 1"
assert response.json["uuid"] == "61d61bdd-aa7d-4912-817f-65a9eb54d3ab" assert response.json["project_uuid"] == project.uuid
assert response.json["project_uuid"] == "61d61bdd-aa7d-4912-817f-65a9eb54d3ab"
def test_vpcs_nio_create_udp(server, vm): def test_vpcs_nio_create_udp(server, vm):

View File

@ -24,7 +24,7 @@ class _asyncio_patch:
""" """
A wrapper around python patch supporting asyncio. A wrapper around python patch supporting asyncio.
Like the original patch you can use it as context Like the original patch you can use it as context
manager (with) or decorator manager (with)
The original patch source code is the main source of The original patch source code is the main source of
inspiration: inspiration:
@ -45,14 +45,6 @@ class _asyncio_patch:
"""Used when leaving the with block""" """Used when leaving the with block"""
self._patcher.stop() self._patcher.stop()
def __call__(self, func, *args, **kwargs):
"""Call is used when asyncio_patch is used as decorator"""
@patch(self.function, return_value=self._fake_anwser())
@asyncio.coroutine
def inner(*a, **kw):
return func(*a, **kw)
return inner
def _fake_anwser(self): def _fake_anwser(self):
future = asyncio.Future() future = asyncio.Future()
future.set_result(self.kwargs["return_value"]) future.set_result(self.kwargs["return_value"])