mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Success to add a dynamips with apiv2
This commit is contained in:
8 files changed
+114
-13
No files matched your search
@@ -19,6 +19,7 @@
|
||||
import pytest
|
||||
import aiohttp
|
||||
from unittest.mock import MagicMock
|
||||
from tests.utils import AsyncioMagicMock
|
||||
|
||||
|
||||
from gns3server.controller.project import Project
|
||||
@@ -40,10 +41,15 @@ def test_json(tmpdir):
|
||||
def test_addVM(async_run):
|
||||
hypervisor = MagicMock()
|
||||
project = Project()
|
||||
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
hypervisor.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
vm = async_run(project.addVM(hypervisor, None, name="test", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
|
||||
hypervisor.post.assert_called_with('/projects/{}/vpcs/vms'.format(project.id),
|
||||
data={'console': None,
|
||||
'vm_id': vm.id,
|
||||
data={'vm_id': vm.id,
|
||||
'console_type': 'telnet',
|
||||
'startup_config': 'test.cfg',
|
||||
'name': 'test'})
|
||||
@@ -52,6 +58,11 @@ def test_addVM(async_run):
|
||||
def test_getVM(async_run):
|
||||
hypervisor = MagicMock()
|
||||
project = Project()
|
||||
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
hypervisor.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
vm = async_run(project.addVM(hypervisor, None, name="test", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
assert project.getVM(vm.id) == vm
|
||||
|
||||
@@ -62,6 +73,11 @@ def test_getVM(async_run):
|
||||
def test_addLink(async_run):
|
||||
hypervisor = MagicMock()
|
||||
project = Project()
|
||||
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
hypervisor.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
vm1 = async_run(project.addVM(hypervisor, None, name="test1", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
vm2 = async_run(project.addVM(hypervisor, None, name="test2", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
link = async_run(project.addLink())
|
||||
@@ -73,6 +89,11 @@ def test_addLink(async_run):
|
||||
def test_getLink(async_run):
|
||||
hypervisor = MagicMock()
|
||||
project = Project()
|
||||
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
hypervisor.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
link = async_run(project.addLink())
|
||||
assert project.getLink(link.id) == link
|
||||
|
||||
|
||||
@@ -17,16 +17,19 @@
|
||||
|
||||
import pytest
|
||||
import uuid
|
||||
import asyncio
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
from tests.utils import AsyncioMagicMock
|
||||
|
||||
from gns3server.controller.vm import VM
|
||||
from gns3server.controller.project import Project
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hypervisor():
|
||||
s = MagicMock()
|
||||
s = AsyncioMagicMock()
|
||||
s.id = "http://test.com:42"
|
||||
return s
|
||||
|
||||
@@ -64,15 +67,44 @@ def test_init_without_uuid(project, hypervisor):
|
||||
|
||||
|
||||
def test_create(vm, hypervisor, project, async_run):
|
||||
vm._console = 2048
|
||||
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
hypervisor.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
async_run(vm.create())
|
||||
data = {
|
||||
"console": None,
|
||||
"console": 2048,
|
||||
"console_type": "vnc",
|
||||
"vm_id": vm.id,
|
||||
"startup_script": "echo test",
|
||||
"name": "demo"
|
||||
}
|
||||
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms".format(vm.project.id), data=data)
|
||||
assert vm._console == 2048
|
||||
assert vm._properties == {"startup_script": "echo test"}
|
||||
|
||||
|
||||
def test_create_without_console(vm, hypervisor, project, async_run):
|
||||
"""
|
||||
None properties should be send. Because it can mean the emulator doesn"t support it
|
||||
"""
|
||||
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048, "test_value": "success"}
|
||||
hypervisor.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
async_run(vm.create())
|
||||
data = {
|
||||
"console_type": "vnc",
|
||||
"vm_id": vm.id,
|
||||
"startup_script": "echo test",
|
||||
"name": "demo"
|
||||
}
|
||||
hypervisor.post.assert_called_with("/projects/{}/vpcs/vms".format(vm.project.id), data=data)
|
||||
assert vm._console == 2048
|
||||
assert vm._properties == {"test_value": "success", "startup_script": "echo test"}
|
||||
|
||||
|
||||
def test_post(vm, hypervisor, async_run):
|
||||
|
||||
@@ -27,7 +27,7 @@ import pytest
|
||||
|
||||
|
||||
from unittest.mock import patch, MagicMock, PropertyMock
|
||||
from tests.utils import asyncio_patch
|
||||
from tests.utils import asyncio_patch, AsyncioMagicMock
|
||||
|
||||
from gns3server.handlers.api.controller.project_handler import ProjectHandler
|
||||
from gns3server.controller import Controller
|
||||
@@ -49,6 +49,10 @@ def project(http_controller, async_run):
|
||||
|
||||
|
||||
def test_create_link(http_controller, tmpdir, project, hypervisor, async_run):
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
hypervisor.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
vm1 = async_run(project.addVM(hypervisor, None))
|
||||
vm2 = async_run(project.addVM(hypervisor, None))
|
||||
|
||||
|
||||
@@ -26,8 +26,8 @@ import aiohttp
|
||||
import pytest
|
||||
|
||||
|
||||
from unittest.mock import patch, MagicMock, PropertyMock
|
||||
from tests.utils import asyncio_patch
|
||||
from unittest.mock import patch, MagicMock
|
||||
from tests.utils import asyncio_patch, AsyncioMagicMock
|
||||
|
||||
from gns3server.handlers.api.controller.project_handler import ProjectHandler
|
||||
from gns3server.controller import Controller
|
||||
@@ -47,12 +47,16 @@ def project(http_controller, async_run):
|
||||
|
||||
|
||||
def test_create_vm(http_controller, tmpdir, project, hypervisor):
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
hypervisor.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
response = http_controller.post("/projects/{}/vms".format(project.id), {
|
||||
"name": "test",
|
||||
"vm_type": "vpcs",
|
||||
"hypervisor_id": "example.com",
|
||||
"properties": {
|
||||
"startup_script": "echo test"
|
||||
"startup_script": "echo test"
|
||||
}
|
||||
}, example=True)
|
||||
assert response.status == 201
|
||||
|
||||
+16
-3
@@ -16,7 +16,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import asyncio
|
||||
from unittest.mock import patch, MagicMock
|
||||
import unittest.mock
|
||||
|
||||
|
||||
class _asyncio_patch:
|
||||
@@ -38,7 +38,7 @@ class _asyncio_patch:
|
||||
|
||||
def __enter__(self):
|
||||
"""Used when enter in the with block"""
|
||||
self._patcher = patch(self.function, return_value=self._fake_anwser())
|
||||
self._patcher = unittest.mock.patch(self.function, return_value=self._fake_anwser())
|
||||
mock_class = self._patcher.start()
|
||||
return mock_class
|
||||
|
||||
@@ -64,13 +64,26 @@ def asyncio_patch(function, *args, **kwargs):
|
||||
return _asyncio_patch(function, *args, **kwargs)
|
||||
|
||||
|
||||
class AsyncioMagicMock(MagicMock):
|
||||
class AsyncioMagicMock(unittest.mock.MagicMock):
|
||||
"""
|
||||
Magic mock returning coroutine
|
||||
"""
|
||||
|
||||
def __init__(self, return_value=None, **kwargs):
|
||||
if return_value:
|
||||
future = asyncio.Future()
|
||||
future.set_result(return_value)
|
||||
kwargs["return_value"] = future
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def _get_child_mock(self, **kw):
|
||||
"""Create the child mocks for attributes and return value.
|
||||
By default child mocks will be the same type as the parent.
|
||||
Subclasses of Mock may want to override this to customize the way
|
||||
child mocks are made.
|
||||
For non-callable mocks the callable variant will be used (rather than
|
||||
any custom subclass).
|
||||
|
||||
Original code: https://github.com/python/cpython/blob/121f86338111e49c547a55eb7f26db919bfcbde9/Lib/unittest/mock.py
|
||||
"""
|
||||
return AsyncioMagicMock(**kw)
|
||||
Reference in new issue
Block a user