mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-06 07:11:13 +02:00
API for duplication a Node
Support: * VPCS * Dynamips router * Ethernet switch Ref #1065
This commit is contained in:
18 files changed
+382
-32
No files matched your search
@@ -26,6 +26,7 @@ import asyncio
|
||||
from gns3server.compute.dynamips import Dynamips
|
||||
from gns3server.compute.dynamips.dynamips_error import DynamipsError
|
||||
from unittest.mock import patch
|
||||
from tests.utils import asyncio_patch, AsyncioMagicMock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -82,7 +83,7 @@ def test_release_dynamips_id(manager):
|
||||
manager.release_dynamips_id(project_2, 0)
|
||||
|
||||
|
||||
def test_project_closed(manager, project, loop):
|
||||
def test_project_closed(manager, project, async_run):
|
||||
|
||||
manager._dynamips_ids[project.id] = set([1, 2, 3])
|
||||
|
||||
@@ -90,7 +91,38 @@ def test_project_closed(manager, project, loop):
|
||||
os.makedirs(project_dir)
|
||||
open(os.path.join(project_dir, "test.ghost"), "w+").close()
|
||||
|
||||
loop.run_until_complete(asyncio.async(manager.project_closed(project)))
|
||||
async_run(manager.project_closed(project))
|
||||
|
||||
assert not os.path.exists(os.path.join(project_dir, "test.ghost"))
|
||||
assert project.id not in manager._dynamips_ids
|
||||
|
||||
|
||||
def test_duplicate_node(manager, project, async_run):
|
||||
"""
|
||||
Duplicate dynamips do nothing it's manage outside the
|
||||
filesystem
|
||||
"""
|
||||
with asyncio_patch('gns3server.compute.dynamips.nodes.c7200.C7200.create'):
|
||||
source_node = async_run(manager.create_node(
|
||||
'R1',
|
||||
project.id,
|
||||
str(uuid.uuid4()),
|
||||
platform="c7200"
|
||||
))
|
||||
destination_node = async_run(manager.create_node(
|
||||
'R2',
|
||||
project.id,
|
||||
str(uuid.uuid4()),
|
||||
platform="c7200"
|
||||
))
|
||||
destination_node._hypervisor = AsyncioMagicMock()
|
||||
|
||||
with open(os.path.join(source_node.working_dir, 'c3600_i1_nvram'), 'w+') as f:
|
||||
f.write("1")
|
||||
with open(source_node.startup_config_path, 'w+') as f:
|
||||
f.write('hostname R1\necho TEST')
|
||||
async_run(manager.duplicate_node(source_node.id, destination_node.id))
|
||||
assert not os.path.exists(os.path.join(destination_node.working_dir, 'c3600_i1_nvram'))
|
||||
with open(destination_node.startup_config_path) as f:
|
||||
content = f.read()
|
||||
assert content == '!\nhostname R2\necho TEST'
|
||||
@@ -19,9 +19,11 @@ import uuid
|
||||
import os
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
from tests.utils import AsyncioMagicMock, asyncio_patch
|
||||
|
||||
|
||||
from gns3server.compute.vpcs import VPCS
|
||||
from gns3server.compute.dynamips import Dynamips
|
||||
from gns3server.compute.qemu import Qemu
|
||||
from gns3server.compute.error import NodeError, ImageMissingError
|
||||
from gns3server.utils import force_unix_path
|
||||
@@ -273,3 +275,25 @@ def test_delete_node(async_run, vpcs, project):
|
||||
async_run(vpcs.delete_node(node_id))
|
||||
mock_emit.assert_called_with("node.deleted", node)
|
||||
assert node not in project.nodes
|
||||
|
||||
|
||||
def test_duplicate_vpcs(async_run, vpcs, project):
|
||||
source_node_id = str(uuid.uuid4())
|
||||
source_node = async_run(vpcs.create_node("PC-1", project.id, source_node_id, console=2222))
|
||||
with open(os.path.join(source_node.working_dir, "startup.vpc"), "w+") as f:
|
||||
f.write("set pcname PC-1\nip dhcp\n")
|
||||
destination_node_id = str(uuid.uuid4())
|
||||
destination_node = async_run(vpcs.create_node("PC-2", project.id, destination_node_id, console=2223))
|
||||
async_run(vpcs.duplicate_node(source_node_id, destination_node_id))
|
||||
with open(os.path.join(destination_node.working_dir, "startup.vpc")) as f:
|
||||
assert f.read() == "set pcname PC-2\nip dhcp\n"
|
||||
|
||||
|
||||
def test_duplicate_ethernet_switch(async_run, project):
|
||||
with asyncio_patch('gns3server.compute.dynamips.nodes.ethernet_switch.EthernetSwitch.create'):
|
||||
dynamips_manager = Dynamips.instance()
|
||||
source_node_id = str(uuid.uuid4())
|
||||
source_node = async_run(dynamips_manager.create_node("SW-1", project.id, source_node_id, node_type='ethernet_switch'))
|
||||
destination_node_id = str(uuid.uuid4())
|
||||
destination_node = async_run(dynamips_manager.create_node("SW-2", project.id, destination_node_id, node_type='ethernet_switch'))
|
||||
async_run(dynamips_manager.duplicate_node(source_node_id, destination_node_id))
|
||||
@@ -255,7 +255,7 @@ def test_update_startup_script_h(vm):
|
||||
def test_update_startup_script_with_escaping_characters_in_name(vm):
|
||||
vm.startup_script = "set pcname initial-name\n"
|
||||
vm.name = "test\\"
|
||||
assert vm.startup_script == "set pcname test\\{}".format(os.linesep)
|
||||
assert vm.startup_script == "set pcname test{}".format(os.linesep)
|
||||
|
||||
|
||||
def test_get_startup_script(vm):
|
||||
|
||||
@@ -597,6 +597,10 @@ def test_node_name(project, async_run):
|
||||
assert node.name == "helloworld-1"
|
||||
node = async_run(project.add_node(compute, "hello world-{0}", None, node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
assert node.name == "helloworld-2"
|
||||
node = async_run(project.add_node(compute, "VPCS-1", None, node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
assert node.name == "VPCS-1"
|
||||
node = async_run(project.add_node(compute, "VPCS-1", None, node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
assert node.name == "VPCS-2"
|
||||
|
||||
|
||||
def test_add_iou_node_and_check_if_gets_application_id(project, async_run):
|
||||
@@ -618,3 +622,22 @@ def test_add_iou_node_and_check_if_gets_application_id(project, async_run):
|
||||
compute, "test", None, node_type="iou", application_id=333, properties={"startup_config": "test.cfg"}))
|
||||
assert mocked_get_app_id.called
|
||||
assert node.properties['application_id'] == 333
|
||||
|
||||
|
||||
def test_duplicate_node(project, async_run):
|
||||
compute = MagicMock()
|
||||
compute.id = "local"
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
original = async_run(project.add_node(
|
||||
compute,
|
||||
"test",
|
||||
None,
|
||||
node_type="vpcs",
|
||||
properties={
|
||||
"startup_config": "test.cfg"
|
||||
}))
|
||||
new_node = async_run(project.duplicate_node(original, 42, 10, 11))
|
||||
assert new_node.x == 42
|
||||
@@ -16,6 +16,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import pytest
|
||||
import uuid
|
||||
import sys
|
||||
import os
|
||||
from tests.utils import asyncio_patch
|
||||
@@ -66,10 +67,10 @@ def test_vpcs_create_port(http_compute, project, free_console_port):
|
||||
def test_vpcs_nio_create_udp(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.add_ubridge_udp_connection"):
|
||||
response = http_compute.post("/projects/{project_id}/vpcs/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), {"type": "nio_udp",
|
||||
"lport": 4242,
|
||||
"rport": 4343,
|
||||
"rhost": "127.0.0.1"},
|
||||
example=True)
|
||||
"lport": 4242,
|
||||
"rport": 4343,
|
||||
"rhost": "127.0.0.1"},
|
||||
example=True)
|
||||
assert response.status == 201
|
||||
assert response.route == "/projects/{project_id}/vpcs/nodes/{node_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
|
||||
assert response.json["type"] == "nio_udp"
|
||||
@@ -92,9 +93,9 @@ def test_vpcs_nio_update_udp(http_compute, vm):
|
||||
def test_vpcs_delete_nio(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM._ubridge_send"):
|
||||
http_compute.post("/projects/{project_id}/vpcs/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), {"type": "nio_udp",
|
||||
"lport": 4242,
|
||||
"rport": 4343,
|
||||
"rhost": "127.0.0.1"})
|
||||
"lport": 4242,
|
||||
"rport": 4343,
|
||||
"rhost": "127.0.0.1"})
|
||||
response = http_compute.delete("/projects/{project_id}/vpcs/nodes/{node_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], node_id=vm["node_id"]), example=True)
|
||||
assert response.status == 204, response.body.decode()
|
||||
assert response.route == "/projects/{project_id}/vpcs/nodes/{node_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
|
||||
@@ -130,6 +131,20 @@ def test_vpcs_delete(http_compute, vm):
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vpcs_duplicate(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.vpcs.VPCS.duplicate_node", return_value=True) as mock:
|
||||
response = http_compute.post(
|
||||
"/projects/{project_id}/vpcs/nodes/{node_id}/duplicate".format(
|
||||
project_id=vm["project_id"],
|
||||
node_id=vm["node_id"]),
|
||||
body={
|
||||
"destination_node_id": str(uuid.uuid4())
|
||||
},
|
||||
example=True)
|
||||
assert mock.called
|
||||
assert response.status == 201
|
||||
|
||||
|
||||
def test_vpcs_update(http_compute, vm, tmpdir, free_console_port):
|
||||
response = http_compute.put("/projects/{project_id}/vpcs/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), {"name": "test",
|
||||
"console": free_console_port,
|
||||
|
||||
@@ -195,6 +195,18 @@ def test_reload_node(http_controller, tmpdir, project, compute, node):
|
||||
assert response.json == node.__json__()
|
||||
|
||||
|
||||
def test_duplicate_node(http_controller, tmpdir, project, compute, node):
|
||||
response = MagicMock()
|
||||
response.json({"console": 2035})
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
response = http_controller.post("/projects/{}/nodes/{}/duplicate".format(
|
||||
project.id, node.id),
|
||||
{"x": 10, "y": 5, "z": 0},
|
||||
example=True)
|
||||
assert response.status == 201, response.body.decode()
|
||||
|
||||
|
||||
def test_delete_node(http_controller, tmpdir, project, compute, node):
|
||||
response = MagicMock()
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
Reference in new issue
Block a user