From 9be76d98a24e133e0b7c344522cd98e12c55d751 Mon Sep 17 00:00:00 2001 From: ziajka Date: Wed, 28 Feb 2018 16:33:20 +0100 Subject: [PATCH] Filter snapshots directory during the snapshot, Fixes: #1297 --- gns3server/controller/export_project.py | 7 ++++- tests/controller/test_export_project.py | 40 +++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/gns3server/controller/export_project.py b/gns3server/controller/export_project.py index 57510f69..75482f4b 100644 --- a/gns3server/controller/export_project.py +++ b/gns3server/controller/export_project.py @@ -29,7 +29,8 @@ log = logging.getLogger(__name__) @asyncio.coroutine -def export_project(project, temporary_dir, include_images=False, keep_compute_id=False, allow_all_nodes=False): +def export_project(project, temporary_dir, include_images=False, keep_compute_id=False, + allow_all_nodes=False, ignore_prefixes=None): """ Export the project as zip. It's a ZipStream object. The file will be read chunk by chunk when you iterate on @@ -111,6 +112,10 @@ def _filter_files(path): if path.endswith("snapshots"): return True + # filter directory of snapshots + if "{sep}snapshots{sep}".format(sep=os.path.sep) in path: + return True + try: i = s.index("project-files") if s[i + 1] in ("tmp", "captures", "snapshots"): diff --git a/tests/controller/test_export_project.py b/tests/controller/test_export_project.py index 82f3d422..c1582dc0 100644 --- a/tests/controller/test_export_project.py +++ b/tests/controller/test_export_project.py @@ -22,6 +22,7 @@ import pytest import aiohttp import zipfile +from pathlib import Path from unittest.mock import patch from unittest.mock import MagicMock from tests.utils import AsyncioMagicMock, AsyncioBytesIO @@ -417,3 +418,42 @@ def test_export_images_from_vm(tmpdir, project, async_run, controller): with myzip.open("images/dynamips/test.image") as myfile: content = myfile.read() assert content == b"IMAGE" + + +def test_export_with_ignoring_snapshots(tmpdir, project, async_run): + 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) + + # create snapshot directory + snapshots_dir = os.path.join(project.path, 'snapshots') + os.makedirs(snapshots_dir) + Path(os.path.join(snapshots_dir, 'snap.gns3project')).touch() + + 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: + assert not os.path.join('snapshots', 'snap.gns3project') in [f.filename for f in myzip.filelist] +