GNS3 / gns3-server

GNS3 server
GNU General Public License v3.0
768 stars 258 forks source link

Error when snapshot exists with an underscore in the name #2388

Open coloursofnoise opened 1 week ago

coloursofnoise commented 1 week ago

Creating a snapshot with an underscore in the name (e.g. test_snapshot) results in an error when trying to load the snapshot after the server has restarted:

Traceback (most recent call last):
  File "/home/gns3/.venv/gns3server-venv/lib/python3.8/site-packages/gns3server/web/route.py", line 202, in control_schema
    func(request, response)
  File "/home/gns3/.venv/gns3server-venv/lib/python3.8/site-packages/gns3server/handlers/api/controller/snapshot_handler.py", line 67, in list
    response.json(sorted(snapshots, key=lambda s: (s.created_at, s.name)))
  File "/home/gns3/.venv/gns3server-venv/lib/python3.8/site-packages/gns3server/handlers/api/controller/snapshot_handler.py", line 67, in <lambda>
    response.json(sorted(snapshots, key=lambda s: (s.created_at, s.name)))
  File "/home/gns3/.venv/gns3server-venv/lib/python3.8/site-packages/gns3server/controller/snapshot.py", line 80, in created_at
    return int(self._created_at)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime'

This is because the snapshot name is parsed by assuming that everything following the first underscore is the date: https://github.com/GNS3/gns3-server/blob/35f94b0c736ff9b30b9cc37b93ffff7c28e9636b/gns3server/controller/snapshot.py#L58-L59

A more robust solution might be to simply work backwards from the end of the filename instead using negative indexing:

-self._name = filename.split("_")[0]
+self._name = filename.split("_")[:-2].join("_")
 datestring = filename.replace(self._name + "_", "").split(".")[0]