It's was hard but i have finally a beginning of test for start VPCS

This commit is contained in:
Julien Duponchelle committed 2015-01-15 13:02:43 +01:00
1 parent 0afea48e63
commit 6bb2b88f1a
6 files changed
+62 -47

No files matched your search

+1 -1
View File
@@ -52,7 +52,7 @@ def test_version_invalid_input_schema(server):
@asyncio_patch("gns3server.handlers.version_handler.VersionHandler", return_value={})
def test_version_invalid_output_schema():
def test_version_invalid_output_schema(server):
query = {'version': "0.4.2"}
response = server.post('/version', query)
assert response.status == 400
+1 -1
View File
@@ -21,7 +21,7 @@ from gns3server import modules
@asyncio_patch('gns3server.modules.VPCS.create_vm', return_value=84)
def test_vpcs_create(server, mock):
def test_vpcs_create(server):
response = server.post('/vpcs', {'name': 'PC TEST 1'}, example=False)
assert response.status == 200
assert response.route == '/vpcs'
+13 -1
View File
@@ -16,7 +16,13 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
from unittest.mock import patch
import asyncio
from tests.utils import asyncio_patch
#Move loop to util
from tests.api.base import loop
from asyncio.subprocess import Process
from unittest.mock import patch, Mock
from gns3server.modules.vpcs.vpcs_device import VPCSDevice
from gns3server.modules.vpcs.vpcs_error import VPCSError
@@ -39,3 +45,9 @@ def test_vm_invalid_vpcs_path(tmpdir):
assert vm.name == "test"
assert vm.id == 42
def test_start(tmpdir, loop):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=Mock()):
vm = VPCSDevice("test", 42, working_dir=str(tmpdir), path="/bin/test_fake")
loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running() == True
+35 -7
View File
@@ -19,15 +19,43 @@ import asyncio
from unittest.mock import patch
def asyncio_patch(function, *args, **kwargs):
@asyncio.coroutine
def fake_anwser(*a, **kw):
return kwargs["return_value"]
class _asyncio_patch:
"""
A wrapper around python patch supporting asyncio.
Like the original patch you can use it as context
manager (with) or decorator
def register(func):
@patch(function, return_value=fake_anwser)
The original patch source code is the main source of
inspiration:
https://hg.python.org/cpython/file/3.4/Lib/unittest/mock.py
"""
def __init__(self, function, *args, **kwargs):
self.function = function
self.args = args
self.kwargs = kwargs
def __enter__(self):
"""Used when enter in the with block"""
self._patcher = patch(self.function, return_value=self._fake_anwser())
self._patcher.start()
def __exit__(self, *exc_info):
"""Used when leaving the with block"""
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
return register
def _fake_anwser(self):
future = asyncio.Future()
future.set_result(self.kwargs["return_value"])
return future
def asyncio_patch(function, *args, **kwargs):
return _asyncio_patch(function, *args, **kwargs)