2016-07-21 15:48:13 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2020-06-16 07:29:03 +03:00
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
2016-07-21 15:48:13 +03:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
import pytest
|
2016-07-21 15:48:13 +03:00
|
|
|
import os
|
|
|
|
import uuid
|
|
|
|
import json
|
|
|
|
import zipfile
|
|
|
|
|
2022-03-30 13:38:34 +03:00
|
|
|
from pathlib import Path
|
2016-07-22 17:05:22 +03:00
|
|
|
from tests.utils import asyncio_patch, AsyncioMagicMock
|
|
|
|
|
|
|
|
from gns3server.controller.import_project import import_project, _move_files_to_compute
|
2016-07-22 12:43:14 +03:00
|
|
|
from gns3server.version import __version__
|
2016-07-21 15:48:13 +03:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_import_project(tmpdir, controller):
|
2016-07-21 15:48:13 +03:00
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
project_id = str(uuid.uuid4())
|
2016-07-21 15:48:13 +03:00
|
|
|
topology = {
|
|
|
|
"project_id": str(uuid.uuid4()),
|
|
|
|
"name": "test",
|
2017-05-23 17:08:30 +03:00
|
|
|
"auto_open": True,
|
|
|
|
"auto_start": True,
|
2016-07-21 15:48:13 +03:00
|
|
|
"topology": {
|
|
|
|
},
|
|
|
|
"version": "2.0.0"
|
|
|
|
}
|
|
|
|
|
|
|
|
with open(str(tmpdir / "project.gns3"), 'w+') as f:
|
|
|
|
json.dump(topology, f)
|
|
|
|
with open(str(tmpdir / "b.png"), 'w+') as f:
|
|
|
|
f.write("B")
|
|
|
|
|
|
|
|
zip_path = str(tmpdir / "project.zip")
|
|
|
|
with zipfile.ZipFile(zip_path, 'w') as myzip:
|
|
|
|
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
|
|
|
|
myzip.write(str(tmpdir / "b.png"), "b.png")
|
|
|
|
myzip.write(str(tmpdir / "b.png"), "project-files/dynamips/test")
|
|
|
|
myzip.write(str(tmpdir / "b.png"), "project-files/qemu/test")
|
|
|
|
|
|
|
|
with open(zip_path, "rb") as f:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f)
|
2016-07-21 15:48:13 +03:00
|
|
|
|
|
|
|
assert project.name == "test"
|
2016-07-26 11:32:43 +03:00
|
|
|
assert project.id == project_id
|
2016-07-21 15:48:13 +03:00
|
|
|
|
|
|
|
assert os.path.exists(os.path.join(project.path, "b.png"))
|
|
|
|
assert not os.path.exists(os.path.join(project.path, "project.gns3"))
|
|
|
|
assert os.path.exists(os.path.join(project.path, "test.gns3"))
|
|
|
|
assert os.path.exists(os.path.join(project.path, "project-files/dynamips/test"))
|
|
|
|
assert os.path.exists(os.path.join(project.path, "project-files/qemu/test"))
|
|
|
|
|
|
|
|
# A new project name is generated when you import twice the same name
|
|
|
|
with open(zip_path, "rb") as f:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, str(uuid.uuid4()), f)
|
2017-05-23 17:08:30 +03:00
|
|
|
assert project.auto_open is False
|
|
|
|
assert project.auto_start is False
|
2016-07-21 15:48:13 +03:00
|
|
|
assert project.name != "test"
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2022-03-30 13:38:34 +03:00
|
|
|
async def test_import_project_override(projects_dir, controller):
|
2016-07-26 11:32:43 +03:00
|
|
|
"""
|
|
|
|
In the case of snapshot we will import a project for
|
|
|
|
override the previous keeping the same project id & location
|
|
|
|
"""
|
|
|
|
|
2022-03-30 13:38:34 +03:00
|
|
|
tmpdir = Path(projects_dir)
|
2020-06-16 07:29:03 +03:00
|
|
|
project_id = str(uuid.uuid4())
|
2016-07-26 11:32:43 +03:00
|
|
|
topology = {
|
|
|
|
"project_id": project_id,
|
|
|
|
"name": "test",
|
|
|
|
"topology": {
|
|
|
|
},
|
|
|
|
"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:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f, location=str(tmpdir))
|
2016-07-26 11:32:43 +03:00
|
|
|
|
|
|
|
assert project.name == "test"
|
|
|
|
assert project.id == project_id
|
|
|
|
|
|
|
|
# Overide the project with same project
|
|
|
|
with open(zip_path, "rb") as f:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f, location=str(tmpdir))
|
2016-07-26 11:32:43 +03:00
|
|
|
assert project.id == project_id
|
|
|
|
assert project.name == "test"
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_import_upgrade(tmpdir, controller):
|
2016-07-22 14:24:44 +03:00
|
|
|
"""
|
|
|
|
Topology made for previous GNS3 version are upgraded during the process
|
|
|
|
"""
|
2016-07-22 12:43:14 +03:00
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
project_id = str(uuid.uuid4())
|
2016-07-22 12:43:14 +03:00
|
|
|
topology = {
|
|
|
|
"project_id": str(uuid.uuid4()),
|
|
|
|
"name": "test",
|
|
|
|
"topology": {
|
|
|
|
},
|
|
|
|
"version": "1.4.2"
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f)
|
2016-07-22 12:43:14 +03:00
|
|
|
|
|
|
|
with open(os.path.join(project.path, "test.gns3")) as f:
|
|
|
|
topo = json.load(f)
|
|
|
|
assert topo["version"] == __version__
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2021-04-12 10:32:23 +03:00
|
|
|
async def test_import_with_images(config, tmpdir, controller):
|
2016-07-21 15:48:13 +03:00
|
|
|
|
2016-07-21 18:55:15 +03:00
|
|
|
project_id = str(uuid.uuid4())
|
|
|
|
topology = {
|
|
|
|
"project_id": str(uuid.uuid4()),
|
|
|
|
"name": "test",
|
|
|
|
"topology": {
|
|
|
|
},
|
|
|
|
"version": "2.0.0"
|
|
|
|
}
|
|
|
|
|
|
|
|
with open(str(tmpdir / "project.gns3"), 'w+') as f:
|
|
|
|
json.dump(topology, f)
|
|
|
|
|
|
|
|
with open(str(tmpdir / "test.image"), 'w+') as f:
|
|
|
|
f.write("B")
|
|
|
|
|
|
|
|
zip_path = str(tmpdir / "project.zip")
|
|
|
|
with zipfile.ZipFile(zip_path, 'w') as myzip:
|
|
|
|
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
|
|
|
|
myzip.write(str(tmpdir / "test.image"), "images/IOS/test.image")
|
|
|
|
|
|
|
|
with open(zip_path, "rb") as f:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f)
|
2016-07-21 18:55:15 +03:00
|
|
|
|
|
|
|
assert not os.path.exists(os.path.join(project.path, "images/IOS/test.image"))
|
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
path = os.path.join(config.settings.Server.images_path, "IOS", "test.image")
|
2016-07-21 18:55:15 +03:00
|
|
|
assert os.path.exists(path), path
|
2016-07-22 14:50:15 +03:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_import_iou_linux_no_vm(linux_platform, tmpdir, controller):
|
2016-07-22 14:50:15 +03:00
|
|
|
"""
|
2016-07-25 19:02:22 +03:00
|
|
|
On non linux host IOU should be local if we don't have a GNS3 VM
|
2016-07-22 14:50:15 +03:00
|
|
|
"""
|
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
project_id = str(uuid.uuid4())
|
2017-09-05 11:38:17 +03:00
|
|
|
controller._computes["local"] = AsyncioMagicMock()
|
|
|
|
|
2016-07-22 14:50:15 +03:00
|
|
|
topology = {
|
|
|
|
"project_id": str(uuid.uuid4()),
|
|
|
|
"name": "test",
|
|
|
|
"type": "topology",
|
|
|
|
"topology": {
|
2016-07-22 14:54:57 +03:00
|
|
|
"nodes": [
|
|
|
|
{
|
|
|
|
"compute_id": "local",
|
|
|
|
"node_type": "iou",
|
2017-04-11 11:57:01 +03:00
|
|
|
"name": "test",
|
2016-07-22 14:54:57 +03:00
|
|
|
"properties": {}
|
|
|
|
}
|
2016-07-22 14:50:15 +03:00
|
|
|
],
|
|
|
|
"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:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f)
|
2016-07-22 14:50:15 +03:00
|
|
|
|
|
|
|
with open(os.path.join(project.path, "test.gns3")) as f:
|
|
|
|
topo = json.load(f)
|
|
|
|
assert topo["topology"]["nodes"][0]["compute_id"] == "local"
|
|
|
|
|
2016-07-25 19:02:22 +03:00
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_import_iou_linux_with_vm(linux_platform, tmpdir, controller):
|
2016-07-25 19:02:22 +03:00
|
|
|
"""
|
|
|
|
On non linux host IOU should be vm if we have a GNS3 VM configured
|
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-07-25 19:02:22 +03:00
|
|
|
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",
|
2017-04-11 11:57:01 +03:00
|
|
|
"name": "test",
|
2016-07-25 19:02:22 +03:00
|
|
|
"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")
|
2017-05-15 11:20:57 +03:00
|
|
|
|
|
|
|
with open(zip_path, "rb") as f:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f)
|
2017-05-15 11:20:57 +03:00
|
|
|
|
|
|
|
with open(os.path.join(project.path, "test.gns3")) as f:
|
|
|
|
topo = json.load(f)
|
|
|
|
assert topo["topology"]["nodes"][0]["compute_id"] == "vm"
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_import_nat_non_linux(windows_platform, tmpdir, controller):
|
2017-05-15 11:20:57 +03:00
|
|
|
"""
|
|
|
|
On non linux host NAT should be moved to the GNS3 VM
|
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2017-05-15 11:20:57 +03:00
|
|
|
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": "nat",
|
|
|
|
"name": "test",
|
|
|
|
"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")
|
2016-07-25 19:02:22 +03:00
|
|
|
|
|
|
|
with open(zip_path, "rb") as f:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f)
|
2016-07-25 19:02:22 +03:00
|
|
|
|
|
|
|
with open(os.path.join(project.path, "test.gns3")) as f:
|
|
|
|
topo = json.load(f)
|
|
|
|
assert topo["topology"]["nodes"][0]["compute_id"] == "vm"
|
|
|
|
|
2016-07-22 14:50:15 +03:00
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_import_iou_non_linux(windows_platform, tmpdir, controller):
|
2016-07-22 14:50:15 +03:00
|
|
|
"""
|
|
|
|
On non linux host IOU should be moved to the GNS3 VM
|
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-07-22 14:50:15 +03:00
|
|
|
project_id = str(uuid.uuid4())
|
2016-07-22 17:05:22 +03:00
|
|
|
controller._computes["vm"] = AsyncioMagicMock()
|
2016-07-22 14:50:15 +03:00
|
|
|
|
|
|
|
topology = {
|
|
|
|
"project_id": str(uuid.uuid4()),
|
|
|
|
"name": "test",
|
|
|
|
"type": "topology",
|
|
|
|
"topology": {
|
2016-07-22 14:54:57 +03:00
|
|
|
"nodes": [
|
|
|
|
{
|
|
|
|
"compute_id": "local",
|
2016-07-22 17:05:22 +03:00
|
|
|
"node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b",
|
2016-07-22 14:54:57 +03:00
|
|
|
"node_type": "iou",
|
2017-04-11 11:57:01 +03:00
|
|
|
"name": "test",
|
2016-07-22 14:54:57 +03:00
|
|
|
"properties": {}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"compute_id": "local",
|
|
|
|
"node_type": "vpcs",
|
2017-04-11 11:57:01 +03:00
|
|
|
"name": "test2",
|
2016-07-22 14:54:57 +03:00
|
|
|
"properties": {}
|
|
|
|
}
|
|
|
|
],
|
2016-07-22 14:50:15 +03:00
|
|
|
"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:
|
2016-07-22 17:05:22 +03:00
|
|
|
with asyncio_patch("gns3server.controller.import_project._move_files_to_compute") as mock:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f)
|
2016-07-22 17:05:22 +03:00
|
|
|
controller._computes["vm"].post.assert_called_with('/projects', data={'name': 'test', 'project_id': project_id})
|
2016-07-22 14:50:15 +03:00
|
|
|
|
|
|
|
with open(os.path.join(project.path, "test.gns3")) as f:
|
|
|
|
topo = json.load(f)
|
|
|
|
assert topo["topology"]["nodes"][0]["compute_id"] == "vm"
|
|
|
|
assert topo["topology"]["nodes"][1]["compute_id"] == "local"
|
2016-07-22 17:05:22 +03:00
|
|
|
|
2016-12-08 17:55:16 +02:00
|
|
|
mock.assert_called_with(controller._computes["vm"], project_id, project.path, os.path.join('project-files', 'iou', topo["topology"]["nodes"][0]['node_id']))
|
|
|
|
|
2016-07-22 17:05:22 +03:00
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_import_node_id(linux_platform, tmpdir, controller):
|
2016-09-19 17:36:19 +03:00
|
|
|
"""
|
|
|
|
When importing a node, node_id should change
|
|
|
|
"""
|
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
project_id = str(uuid.uuid4())
|
2017-09-05 11:38:17 +03:00
|
|
|
controller._computes["local"] = AsyncioMagicMock()
|
|
|
|
|
2016-09-19 17:36:19 +03:00
|
|
|
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",
|
2017-04-11 11:57:01 +03:00
|
|
|
"name": "test",
|
2016-09-19 17:36:19 +03:00
|
|
|
"properties": {}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"compute_id": "local",
|
|
|
|
"node_id": "c3ae286c-c81f-40d9-a2d0-5874b2f2478d",
|
|
|
|
"node_type": "iou",
|
2017-04-11 11:57:01 +03:00
|
|
|
"name": "test2",
|
2016-09-19 17:36:19 +03:00
|
|
|
"properties": {}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"links": [
|
|
|
|
{
|
|
|
|
"link_id": "b570a150-c09f-47d9-8d32-9ca5b03234d6",
|
|
|
|
"nodes": [
|
|
|
|
{
|
|
|
|
"adapter_number": 0,
|
|
|
|
"node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b",
|
|
|
|
"port_number": 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"adapter_number": 0,
|
|
|
|
"node_id": "c3ae286c-c81f-40d9-a2d0-5874b2f2478d",
|
|
|
|
"port_number": 0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"computes": [],
|
|
|
|
"drawings": [
|
|
|
|
{
|
|
|
|
"drawing_id": "08d665ba-e982-4d54-82b4-aa0c4d5ba6a3",
|
|
|
|
"rotation": 0,
|
|
|
|
"x": -210,
|
|
|
|
"y": -108,
|
|
|
|
"z": 0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"revision": 5,
|
|
|
|
"version": "2.0.0"
|
|
|
|
}
|
|
|
|
|
|
|
|
with open(str(tmpdir / "project.gns3"), 'w+') as f:
|
|
|
|
json.dump(topology, f)
|
|
|
|
|
|
|
|
# Fake .gns3project
|
|
|
|
zip_path = str(tmpdir / "project.zip")
|
|
|
|
with zipfile.ZipFile(zip_path, 'w') as myzip:
|
|
|
|
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
|
|
|
|
myzip.writestr("project-files/iou/0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b/startup.cfg", "test")
|
|
|
|
myzip.writestr("project-files/iou/c3ae286c-c81f-40d9-a2d0-5874b2f2478d/startup.cfg", "test")
|
|
|
|
|
|
|
|
with open(zip_path, "rb") as f:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f)
|
2016-09-19 17:36:19 +03:00
|
|
|
|
|
|
|
with open(os.path.join(project.path, "test.gns3")) as f:
|
|
|
|
topo = json.load(f)
|
|
|
|
# Node id should have change
|
|
|
|
assert topo["topology"]["nodes"][0]["node_id"] not in ["0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "c3ae286c-c81f-40d9-a2d0-5874b2f2478d"]
|
|
|
|
|
|
|
|
# Link should have change
|
|
|
|
link = topo["topology"]["links"][0]
|
|
|
|
assert link["link_id"] != "b570a150-c09f-47d9-8d32-9ca5b03234d6"
|
|
|
|
assert link["nodes"][0]["node_id"] not in ["0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "c3ae286c-c81f-40d9-a2d0-5874b2f2478d"]
|
|
|
|
|
|
|
|
# Drawing id should change
|
|
|
|
assert topo["topology"]["drawings"][0]["drawing_id"] != "08d665ba-e982-4d54-82b4-aa0c4d5ba6a3"
|
|
|
|
|
|
|
|
# Node files should have moved to the new node id
|
|
|
|
assert not os.path.exists(os.path.join(project.path, "project-files", "iou", "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "startup.cfg"))
|
|
|
|
assert not os.path.exists(os.path.join(project.path, "project-files", "iou", "c3ae286c-c81f-40d9-a2d0-5874b2f2478d", "startup.cfg"))
|
|
|
|
assert os.path.exists(os.path.join(project.path, "project-files", "iou", topo["topology"]["nodes"][0]["node_id"], "startup.cfg"))
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_import_keep_compute_id(windows_platform, tmpdir, controller):
|
2016-07-25 15:47:37 +03:00
|
|
|
"""
|
|
|
|
On linux host IOU should be moved to the GNS3 VM
|
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-07-25 15:47:37 +03:00
|
|
|
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",
|
2017-04-11 11:57:01 +03:00
|
|
|
"name": "test",
|
2016-07-25 15:47:37 +03:00
|
|
|
"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:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f, keep_compute_id=True)
|
2016-07-25 15:47:37 +03:00
|
|
|
|
|
|
|
with open(os.path.join(project.path, "test.gns3")) as f:
|
|
|
|
topo = json.load(f)
|
|
|
|
assert topo["topology"]["nodes"][0]["compute_id"] == "local"
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_move_files_to_compute(tmpdir):
|
|
|
|
|
2016-07-22 17:05:22 +03:00
|
|
|
project_id = str(uuid.uuid4())
|
|
|
|
|
|
|
|
os.makedirs(str(tmpdir / "project-files" / "docker"))
|
|
|
|
(tmpdir / "project-files" / "docker" / "test").open("w").close()
|
|
|
|
(tmpdir / "project-files" / "docker" / "test2").open("w").close()
|
|
|
|
|
|
|
|
with asyncio_patch("gns3server.controller.import_project._upload_file") as mock:
|
2020-06-16 07:29:03 +03:00
|
|
|
await _move_files_to_compute(None, project_id, str(tmpdir), os.path.join("project-files", "docker"))
|
2016-07-22 17:05:22 +03:00
|
|
|
|
2016-09-27 19:44:32 +03:00
|
|
|
mock.assert_any_call(None, project_id, str(tmpdir / "project-files" / "docker" / "test"), os.path.join("project-files", "docker", "test"))
|
2016-12-08 17:55:16 +02:00
|
|
|
mock.assert_any_call(None, project_id, str(tmpdir / "project-files" / "docker" / "test2"), os.path.join("project-files", "docker", "test2"))
|
2016-07-22 17:05:22 +03:00
|
|
|
assert not os.path.exists(str(tmpdir / "project-files" / "docker"))
|
2016-07-25 10:41:41 +03:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2022-03-30 13:38:34 +03:00
|
|
|
async def test_import_project_name_and_location(projects_dir, controller):
|
2016-07-25 10:41:41 +03:00
|
|
|
"""
|
|
|
|
Import a project with a different location and name
|
|
|
|
"""
|
|
|
|
|
2022-03-30 13:38:34 +03:00
|
|
|
tmpdir = Path(projects_dir)
|
2020-06-16 07:29:03 +03:00
|
|
|
project_id = str(uuid.uuid4())
|
2016-07-25 10:41:41 +03:00
|
|
|
topology = {
|
|
|
|
"project_id": str(uuid.uuid4()),
|
|
|
|
"name": "test",
|
|
|
|
"topology": {
|
|
|
|
},
|
|
|
|
"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:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, project_id, f, name="hello", location=str(tmpdir / "hello"))
|
2016-07-25 10:41:41 +03:00
|
|
|
|
|
|
|
assert project.name == "hello"
|
|
|
|
|
2016-07-25 15:47:37 +03:00
|
|
|
assert os.path.exists(str(tmpdir / "hello" / "hello.gns3"))
|
2016-07-25 10:41:41 +03:00
|
|
|
|
|
|
|
# A new project name is generated when you import twice the same name
|
|
|
|
with open(zip_path, "rb") as f:
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await import_project(controller, str(uuid.uuid4()), f, name="hello", location=str(tmpdir / "test"))
|
2016-07-25 10:41:41 +03:00
|
|
|
assert project.name == "hello-1"
|