Symbolic links support for project export/import

This commit is contained in:
grossmj committed 2024-10-19 15:49:23 +10:00
1 parent cb46c0fbcc
commit 24bfc205db
6 files changed
+105 -25

No files matched your search

+7 -1
View File
@@ -21,6 +21,7 @@ import json
import pytest
import aiohttp
import zipfile
import stat
from pathlib import Path
from unittest.mock import patch
@@ -116,6 +117,8 @@ async def test_export(tmpdir, project):
with open(os.path.join(path, "project-files", "snapshots", "test"), 'w+') as f:
f.write("WORLD")
os.symlink("/tmp/anywhere", os.path.join(path, "vm-1", "dynamips", "symlink"))
with aiozipstream.ZipFile() as z:
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir / "IOS"),):
await export_project(z, project, str(tmpdir), include_images=False)
@@ -131,9 +134,12 @@ async def test_export(tmpdir, project):
assert 'vm-1/dynamips/empty-dir/' in myzip.namelist()
assert 'project-files/snapshots/test' not in myzip.namelist()
assert 'vm-1/dynamips/test_log.txt' not in myzip.namelist()
assert 'images/IOS/test.image' not in myzip.namelist()
assert 'vm-1/dynamips/symlink' in myzip.namelist()
zip_info = myzip.getinfo('vm-1/dynamips/symlink')
assert stat.S_ISLNK(zip_info.external_attr >> 16)
with myzip.open("project.gns3") as myfile:
topo = json.loads(myfile.read().decode())["topology"]
assert topo["nodes"][0]["compute_id"] == "local" # All node should have compute_id local after export
+52
View File
@@ -21,7 +21,11 @@ import json
import zipfile
from tests.utils import asyncio_patch, AsyncioMagicMock
from unittest.mock import patch, MagicMock
from gns3server.utils.asyncio import aiozipstream
from gns3server.controller.project import Project
from gns3server.controller.export_project import export_project
from gns3server.controller.import_project import import_project, _move_files_to_compute
from gns3server.version import __version__
@@ -106,6 +110,54 @@ async def test_import_project_override(tmpdir, controller):
assert project.name == "test"
async def write_file(path, z):
with open(path, 'wb') as f:
async for chunk in z:
f.write(chunk)
async def test_import_project_containing_symlink(tmpdir, controller):
project = Project(controller=controller, name="test")
project.dump = MagicMock()
path = project.path
project_id = str(uuid.uuid4())
topology = {
"project_id": str(uuid.uuid4()),
"name": "test",
"auto_open": True,
"auto_start": True,
"topology": {
},
"version": "2.0.0"
}
with open(os.path.join(path, "project.gns3"), 'w+') as f:
json.dump(topology, f)
os.makedirs(os.path.join(path, "vm1", "dynamips"))
symlink_path = os.path.join(project.path, "vm1", "dynamips", "symlink")
symlink_target = "/tmp/anywhere"
os.symlink(symlink_target, symlink_path)
zip_path = str(tmpdir / "project.zip")
with aiozipstream.ZipFile() as z:
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir / "IOS"),):
await export_project(z, project, str(tmpdir), include_images=False)
await write_file(zip_path, z)
with open(zip_path, "rb") as f:
project = await import_project(controller, project_id, f)
assert project.name == "test"
assert project.id == project_id
symlink_path = os.path.join(project.path, "vm1", "dynamips", "symlink")
assert os.path.islink(symlink_path)
assert os.readlink(symlink_path) == symlink_target
async def test_import_upgrade(tmpdir, controller):
"""
Topology made for previous GNS3 version are upgraded during the process