1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-24 09:18:08 +00:00

Merge pull request #2391 from GNS3/bugfix/2388

Fix issues with invalid snapshot filenames
This commit is contained in:
Jeremy Grossmann 2024-07-05 12:49:59 +02:00 committed by GitHub
commit 29f848d833
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 9 deletions

View File

@ -192,7 +192,11 @@ class Project:
if os.path.exists(snapshot_dir): if os.path.exists(snapshot_dir):
for snap in os.listdir(snapshot_dir): for snap in os.listdir(snapshot_dir):
if snap.endswith(".gns3project"): if snap.endswith(".gns3project"):
try:
snapshot = Snapshot(self, filename=snap) snapshot = Snapshot(self, filename=snap)
except ValueError:
log.error("Invalid snapshot file: {}".format(snap))
continue
self._snapshots[snapshot.id] = snapshot self._snapshots[snapshot.id] = snapshot
# Create the project on demand on the compute node # Create the project on demand on the compute node

View File

@ -55,12 +55,10 @@ class Snapshot:
self._created_at = datetime.now(timezone.utc).timestamp() self._created_at = datetime.now(timezone.utc).timestamp()
filename = self._name + "_" + datetime.fromtimestamp(self._created_at, tz=timezone.utc).replace(tzinfo=None).strftime(FILENAME_TIME_FORMAT) + ".gns3project" filename = self._name + "_" + datetime.fromtimestamp(self._created_at, tz=timezone.utc).replace(tzinfo=None).strftime(FILENAME_TIME_FORMAT) + ".gns3project"
else: else:
self._name = filename.split("_")[0] self._name = filename.rsplit("_", 2)[0]
datestring = filename.replace(self._name + "_", "").split(".")[0] datestring = filename.replace(self._name + "_", "").split(".")[0]
try:
self._created_at = datetime.strptime(datestring, FILENAME_TIME_FORMAT).replace(tzinfo=timezone.utc).timestamp() self._created_at = datetime.strptime(datestring, FILENAME_TIME_FORMAT).replace(tzinfo=timezone.utc).timestamp()
except ValueError:
self._created_at = datetime.now(timezone.utc)
self._path = os.path.join(project.path, "snapshots", filename) self._path = os.path.join(project.path, "snapshots", filename)
@property @property

View File

@ -750,7 +750,7 @@ def test_snapshots(project):
def test_get_snapshot(project): def test_get_snapshot(project):
os.makedirs(os.path.join(project.path, "snapshots")) os.makedirs(os.path.join(project.path, "snapshots"))
open(os.path.join(project.path, "snapshots", "test1.gns3project"), "w+").close() open(os.path.join(project.path, "snapshots", "test1_260716_103713.gns3project"), "w+").close()
project.reset() project.reset()
snapshot = list(project.snapshots.values())[0] snapshot = list(project.snapshots.values())[0]

View File

@ -61,15 +61,21 @@ def test_snapshot_filename(project):
def test_json(project): def test_json(project):
snapshot = Snapshot(project, filename="test1_260716_100439.gns3project") snapshot = Snapshot(project, filename="snapshot_test_260716_100439.gns3project")
assert snapshot.__json__() == { assert snapshot.__json__() == {
"snapshot_id": snapshot._id, "snapshot_id": snapshot._id,
"name": "test1", "name": "snapshot_test",
"project_id": project.id, "project_id": project.id,
"created_at": 1469527479 "created_at": 1469527479
} }
def test_invalid_snapshot_filename(project):
with pytest.raises(ValueError):
Snapshot(project, filename="snapshot_test_invalid_file.gns3project")
async def test_restore(project, controller): async def test_restore(project, controller):
compute = AsyncioMagicMock() compute = AsyncioMagicMock()