Filter snapshots directory during the snapshot, Fixes: #1297

This commit is contained in:
ziajka 2018-02-28 16:33:20 +01:00
parent 835bc24b44
commit 09c7d73ee9
2 changed files with 46 additions and 1 deletions

View File

@ -29,7 +29,8 @@ log = logging.getLogger(__name__)
@asyncio.coroutine @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. Export the project as zip. It's a ZipStream object.
The file will be read chunk by chunk when you iterate on The file will be read chunk by chunk when you iterate on
@ -111,6 +112,10 @@ def _filter_files(path):
if path.endswith("snapshots"): if path.endswith("snapshots"):
return True return True
# filter directory of snapshots
if "{sep}snapshots{sep}".format(sep=os.path.sep) in path:
return True
try: try:
i = s.index("project-files") i = s.index("project-files")
if s[i + 1] in ("tmp", "captures", "snapshots"): if s[i + 1] in ("tmp", "captures", "snapshots"):

View File

@ -22,6 +22,7 @@ import pytest
import aiohttp import aiohttp
import zipfile import zipfile
from pathlib import Path
from unittest.mock import patch from unittest.mock import patch
from unittest.mock import MagicMock from unittest.mock import MagicMock
from tests.utils import AsyncioMagicMock, AsyncioBytesIO 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: with myzip.open("images/dynamips/test.image") as myfile:
content = myfile.read() content = myfile.read()
assert content == b"IMAGE" 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]