From 08154e43aac4f018b21c1f70361d5d0b30e2fa23 Mon Sep 17 00:00:00 2001 From: grossmj Date: Fri, 5 Jul 2024 12:04:53 +0200 Subject: [PATCH] Fix error when snapshot exists with an underscore in the name --- gns3server/controller/project.py | 6 +++++- gns3server/controller/snapshot.py | 8 +++----- tests/controller/test_project.py | 2 +- tests/controller/test_snapshot.py | 10 ++++++++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index eed15198..3ca2274c 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -192,7 +192,11 @@ class Project: if os.path.exists(snapshot_dir): for snap in os.listdir(snapshot_dir): if snap.endswith(".gns3project"): - snapshot = Snapshot(self, filename=snap) + try: + snapshot = Snapshot(self, filename=snap) + except ValueError: + log.error("Invalid snapshot file: {}".format(snap)) + continue self._snapshots[snapshot.id] = snapshot # Create the project on demand on the compute node diff --git a/gns3server/controller/snapshot.py b/gns3server/controller/snapshot.py index 46041c3d..ef9c5348 100644 --- a/gns3server/controller/snapshot.py +++ b/gns3server/controller/snapshot.py @@ -55,12 +55,10 @@ class Snapshot: 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" else: - self._name = filename.split("_")[0] + self._name = filename.rsplit("_", 2)[0] datestring = filename.replace(self._name + "_", "").split(".")[0] - try: - self._created_at = datetime.strptime(datestring, FILENAME_TIME_FORMAT).replace(tzinfo=timezone.utc).timestamp() - except ValueError: - self._created_at = datetime.now(timezone.utc) + self._created_at = datetime.strptime(datestring, FILENAME_TIME_FORMAT).replace(tzinfo=timezone.utc).timestamp() + self._path = os.path.join(project.path, "snapshots", filename) @property diff --git a/tests/controller/test_project.py b/tests/controller/test_project.py index fb8bcfeb..f639c243 100644 --- a/tests/controller/test_project.py +++ b/tests/controller/test_project.py @@ -750,7 +750,7 @@ def test_snapshots(project): def test_get_snapshot(project): 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() snapshot = list(project.snapshots.values())[0] diff --git a/tests/controller/test_snapshot.py b/tests/controller/test_snapshot.py index 089c2701..9eaccb7b 100644 --- a/tests/controller/test_snapshot.py +++ b/tests/controller/test_snapshot.py @@ -61,15 +61,21 @@ def test_snapshot_filename(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__() == { "snapshot_id": snapshot._id, - "name": "test1", + "name": "snapshot_test", "project_id": project.id, "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): compute = AsyncioMagicMock()