mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 14:01:15 +02:00
API for duplicate a project
Ref https://github.com/GNS3/gns3-gui/issues/995
This commit is contained in:
9 files changed
+285
-57
No files matched your search
@@ -200,6 +200,7 @@ def test_export_disallow_some_type(tmpdir, project, async_run):
|
||||
|
||||
with pytest.raises(aiohttp.web.HTTPConflict):
|
||||
z = async_run(export_project(project, str(tmpdir)))
|
||||
z = async_run(export_project(project, str(tmpdir), allow_all_nodes=True))
|
||||
|
||||
|
||||
def test_export_fix_path(tmpdir, project, async_run):
|
||||
@@ -271,3 +272,44 @@ def test_export_with_images(tmpdir, project, async_run):
|
||||
|
||||
with zipfile.ZipFile(str(tmpdir / 'zipfile.zip')) as myzip:
|
||||
myzip.getinfo("images/IOS/test.image")
|
||||
|
||||
|
||||
def test_export_keep_compute_id(tmpdir, project, async_run):
|
||||
"""
|
||||
If we want to restore the same computes we could ask to keep them
|
||||
in the file
|
||||
"""
|
||||
|
||||
with open(os.path.join(project.path, "test.gns3"), 'w+') as f:
|
||||
data = {
|
||||
"topology": {
|
||||
"computes": [
|
||||
{
|
||||
"compute_id": "6b7149c8-7d6e-4ca0-ab6b-daa8ab567be0",
|
||||
"host": "127.0.0.1",
|
||||
"name": "Remote 1",
|
||||
"port": 8001,
|
||||
"protocol": "http"
|
||||
}
|
||||
],
|
||||
"nodes": [
|
||||
{
|
||||
"compute_id": "6b7149c8-7d6e-4ca0-ab6b-daa8ab567be0",
|
||||
"node_type": "vpcs"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
json.dump(data, f)
|
||||
|
||||
z = async_run(export_project(project, str(tmpdir), keep_compute_id=True))
|
||||
|
||||
with open(str(tmpdir / 'zipfile.zip'), 'wb') as f:
|
||||
for data in z:
|
||||
f.write(data)
|
||||
|
||||
with zipfile.ZipFile(str(tmpdir / 'zipfile.zip')) as myzip:
|
||||
with myzip.open("project.gns3") as myfile:
|
||||
topo = json.loads(myfile.read().decode())["topology"]
|
||||
assert topo["nodes"][0]["compute_id"] == "6b7149c8-7d6e-4ca0-ab6b-daa8ab567be0"
|
||||
assert len(topo["computes"]) == 1
|
||||
@@ -131,7 +131,7 @@ def test_import_with_images(tmpdir, async_run, controller):
|
||||
assert os.path.exists(path), path
|
||||
|
||||
|
||||
def test_import_iou_non_linux(linux_platform, async_run, tmpdir, controller):
|
||||
def test_import_iou_linux(linux_platform, async_run, tmpdir, controller):
|
||||
"""
|
||||
On non linux host IOU should be local
|
||||
"""
|
||||
@@ -224,6 +224,49 @@ def test_import_iou_non_linux(windows_platform, async_run, tmpdir, controller):
|
||||
assert topo["topology"]["nodes"][1]["compute_id"] == "local"
|
||||
|
||||
|
||||
def test_import_keep_compute_id(windows_platform, async_run, tmpdir, controller):
|
||||
"""
|
||||
On linux host IOU should be moved to the GNS3 VM
|
||||
"""
|
||||
project_id = str(uuid.uuid4())
|
||||
controller._computes["vm"] = AsyncioMagicMock()
|
||||
|
||||
topology = {
|
||||
"project_id": str(uuid.uuid4()),
|
||||
"name": "test",
|
||||
"type": "topology",
|
||||
"topology": {
|
||||
"nodes": [
|
||||
{
|
||||
"compute_id": "local",
|
||||
"node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b",
|
||||
"node_type": "iou",
|
||||
"properties": {}
|
||||
}
|
||||
],
|
||||
"links": [],
|
||||
"computes": [],
|
||||
"drawings": []
|
||||
},
|
||||
"revision": 5,
|
||||
"version": "2.0.0"
|
||||
}
|
||||
|
||||
with open(str(tmpdir / "project.gns3"), 'w+') as f:
|
||||
json.dump(topology, f)
|
||||
|
||||
zip_path = str(tmpdir / "project.zip")
|
||||
with zipfile.ZipFile(zip_path, 'w') as myzip:
|
||||
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
|
||||
|
||||
with open(zip_path, "rb") as f:
|
||||
project = async_run(import_project(controller, project_id, f, keep_compute_id=True))
|
||||
|
||||
with open(os.path.join(project.path, "test.gns3")) as f:
|
||||
topo = json.load(f)
|
||||
assert topo["topology"]["nodes"][0]["compute_id"] == "local"
|
||||
|
||||
|
||||
def test_move_files_to_compute(tmpdir, async_run):
|
||||
project_id = str(uuid.uuid4())
|
||||
|
||||
@@ -261,11 +304,11 @@ def test_import_project_name_and_location(async_run, tmpdir, controller):
|
||||
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
|
||||
|
||||
with open(zip_path, "rb") as f:
|
||||
project = async_run(import_project(controller, project_id, f, name="hello", location=str(tmpdir / "test")))
|
||||
project = async_run(import_project(controller, project_id, f, name="hello", location=str(tmpdir / "hello")))
|
||||
|
||||
assert project.name == "hello"
|
||||
|
||||
assert os.path.exists(str(tmpdir / "test" / "hello" / "hello.gns3"))
|
||||
assert os.path.exists(str(tmpdir / "hello" / "hello.gns3"))
|
||||
|
||||
# A new project name is generated when you import twice the same name
|
||||
with open(zip_path, "rb") as f:
|
||||
|
||||
@@ -203,7 +203,7 @@ def test_delete_node_delete_link(async_run, controller):
|
||||
controller.notification.emit.assert_any_call("link.deleted", link.__json__())
|
||||
|
||||
|
||||
def test_getVM(async_run, controller):
|
||||
def test_get_node(async_run, controller):
|
||||
compute = MagicMock()
|
||||
project = Project(controller=controller, name="Test")
|
||||
|
||||
@@ -217,6 +217,11 @@ def test_getVM(async_run, controller):
|
||||
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
|
||||
project.get_node("test")
|
||||
|
||||
# Raise an error if the project is not opened
|
||||
async_run(project.close())
|
||||
with pytest.raises(aiohttp.web.HTTPForbidden):
|
||||
project.get_node(vm.id)
|
||||
|
||||
|
||||
def test_addLink(async_run, project, controller):
|
||||
compute = MagicMock()
|
||||
@@ -339,3 +344,32 @@ def test_is_running(project, async_run, node):
|
||||
assert project.is_running() is False
|
||||
node._status = "started"
|
||||
assert project.is_running() is True
|
||||
|
||||
|
||||
def test_duplicate(project, async_run, controller):
|
||||
"""
|
||||
Duplicate a project, the node should remain on the remote server
|
||||
if they were on remote server
|
||||
"""
|
||||
compute = MagicMock()
|
||||
compute.id = "remote"
|
||||
compute.list_files = AsyncioMagicMock(return_value=[])
|
||||
controller._computes["remote"] = compute
|
||||
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
remote_vpcs = async_run(project.add_node(compute, "test", None, node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
|
||||
# We allow node not allowed for standard import / export
|
||||
remote_virtualbox = async_run(project.add_node(compute, "test", None, node_type="virtualbox", properties={"startup_config": "test.cfg"}))
|
||||
|
||||
new_project = async_run(project.duplicate(name="Hello"))
|
||||
assert new_project.id != project.id
|
||||
assert new_project.name == "Hello"
|
||||
|
||||
async_run(new_project.open())
|
||||
|
||||
assert new_project.get_node(remote_vpcs.id).compute.id == "remote"
|
||||
assert new_project.get_node(remote_virtualbox.id).compute.id == "remote"
|
||||
Reference in new issue
Block a user