diff --git a/appsettings/management/commands/backup.py b/appsettings/management/commands/backup.py index 924d21ff..a8a8d6d1 100644 --- a/appsettings/management/commands/backup.py +++ b/appsettings/management/commands/backup.py @@ -46,7 +46,11 @@ class Command(BaseCommand): raise # only after a good backup: a failing one never costs an older one; # the partial ones are what a killed run left behind - for old in [*sorted(backups.glob(PREFIX + "*"))[:-keep], *backups.glob(f".{PREFIX}*")]: + # the newest by when they were made, not by name: a name is the local time, which goes back + # an hour in autumn (and was the panel's zone or UTC as an installation's TZ changed); + # by name among those of the same time, which a coarse file system clock gives + made = sorted(backups.glob(PREFIX + "*"), key=lambda path: (path.stat().st_mtime_ns, path.name)) + for old in [*made[:-keep], *backups.glob(f".{PREFIX}*")]: shutil.rmtree(old) return backups / name diff --git a/appsettings/tests/test_backup.py b/appsettings/tests/test_backup.py index 6a1d1fe8..4a6790a3 100644 --- a/appsettings/tests/test_backup.py +++ b/appsettings/tests/test_backup.py @@ -2,6 +2,7 @@ readable only by the panel's user, the newest --keep of them kept.""" import fcntl +from datetime import datetime import os import shutil import sqlite3 @@ -85,12 +86,24 @@ class BackupTests(SimpleTestCase): self.assertEqual(oct(copy.stat().st_mode & 0o777), "0o600") def test_only_the_newest_backups_and_no_partial_one_are_kept(self): - for name in ("webvirtcloud-backup-20260101-000000", "webvirtcloud-backup-20260102-000000", - ".webvirtcloud-backup-20260103-000000"): + for made, name in ((1, "webvirtcloud-backup-20260101-000000"), (2, "webvirtcloud-backup-20260102-000000"), + (3, ".webvirtcloud-backup-20260103-000000")): (self.backups / name).mkdir(parents=True) + os.utime(self.backups / name, (made, made)) backup = self.backup("--keep", "2") self.assertEqual(self.kept(), ["webvirtcloud-backup-20260102-000000", backup.name]) + def test_the_newest_are_kept_by_when_they_were_made_not_by_name(self): + # 02:50 CEST, then 02:10 CET forty minutes later as the clock went back; or an + # older backup named in another time zone + older = self.backups / "webvirtcloud-backup-20261025-025000" + older.mkdir(parents=True) + os.utime(older, (1, 1)) + with patch("appsettings.management.commands.backup.datetime") as clock: + clock.now.return_value = datetime(2026, 10, 25, 2, 10) + newest = self.backup("--keep", "1") + self.assertEqual(self.kept(), [newest.name]) + def test_a_failed_backup_leaves_nothing_and_deletes_no_older_one(self): (self.backups / "webvirtcloud-backup-20260101-000000").mkdir(parents=True) with patch.object(Command, "copy", side_effect=OSError("No space left on device")), self.assertRaises(OSError): diff --git a/vrtManager/instance.py b/vrtManager/instance.py index e10d807b..9886f2c8 100644 --- a/vrtManager/instance.py +++ b/vrtManager/instance.py @@ -65,7 +65,7 @@ from libvirt import ( from libvirt_qemu import qemuAgentCommand from collections import OrderedDict -from datetime import datetime +from datetime import datetime, timezone from xml.etree import ElementTree from lxml import etree @@ -2136,7 +2136,8 @@ class wvmInstance(wvmConnect): detaches.setdefault(dev, path) snapshots.append( { - "date": datetime.fromtimestamp(int(tree.findtext("creationTime"))), + # aware: sorted in UTC, shown in the panel's time zone + "date": datetime.fromtimestamp(int(tree.findtext("creationTime")), timezone.utc), "name": snap.getName(), "description": tree.findtext("description") or "", "memory": tree.find("memory[@snapshot='external']") is not None, diff --git a/vrtManager/tests/test_external_snapshots.py b/vrtManager/tests/test_external_snapshots.py index a0e2abf2..80562fe8 100644 --- a/vrtManager/tests/test_external_snapshots.py +++ b/vrtManager/tests/test_external_snapshots.py @@ -4,7 +4,7 @@ cases are in vrtManager/tests/test_live_snapshots.py.""" import json import unittest -from datetime import datetime +from datetime import datetime, timezone from unittest.mock import MagicMock, patch import libvirt @@ -110,7 +110,8 @@ class SnapshotListTestCase(unittest.TestCase): [entry] = p.get_snapshot(EXTERNAL) self.assertEqual(entry["name"], "one") self.assertEqual(entry["description"], "d & e") - self.assertEqual(entry["date"], datetime.fromtimestamp(1700000000)) + # aware: two snapshots an hour apart around an autumn clock change stay in order + self.assertEqual(entry["date"], datetime(2023, 11, 14, 22, 13, 20, tzinfo=timezone.utc)) self.assertTrue(entry["memory"]) self.assertTrue(entry["current"])