Skip to content

Commit

Permalink
fix(logging): added a friendly message if snaptype is UNKNOWN
Browse files Browse the repository at this point in the history
Related to Issue #46
  • Loading branch information
hirak99 committed Jan 17, 2025
1 parent 5b7dc35 commit 0aa9496
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/code/snap_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ def snaptime(self) -> datetime.datetime:

@property
def _snap_type(self) -> snap_mechanisms.SnapType:
return snap_mechanisms.SnapType[self.metadata.snap_type]
snap_type = snap_mechanisms.SnapType[self.metadata.snap_type]
if snap_type == snap_mechanisms.SnapType.UNKNOWN:
logging.warning(
f"Cannot determine type for '{self.target}'.\n"
f"This may occur if the metadata '{self._metadata_fname}' was manually deleted.\n"
f"If so, also delete the snapshot '{self.target}' manually."
)
return snap_type

def as_json(self) -> dict[str, Any]:
result: dict[str, Any] = {}
Expand Down
24 changes: 24 additions & 0 deletions src/code/snap_holder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import datetime
import json
import logging
import os
import tempfile
import unittest
Expand Down Expand Up @@ -122,6 +123,29 @@ def test_as_json(self):
{"comment": "Comment", "trigger": "S", "expiry": expiry.timestamp()},
)

def test_warn_unknown_type(self):
with tempfile.TemporaryDirectory() as dir:
snap_destination = os.path.join(dir, "root-20231122193630")
snap = snap_holder.Snapshot(snap_destination)
with self.assertLogs(level=logging.WARNING) as log:
self.assertEqual(snap._snap_type, snap_mechanisms.SnapType.UNKNOWN)
self.assertRegex(log.output[0], "This may occur if .* manually deleted")

# Check that there is no warning if the snap type is known.
with mock.patch.object(
btrfs_mechanism.BtrfsSnapMechanism,
"verify_volume",
return_value=True,
) as mock_verify_volume, mock.patch.object(
btrfs_mechanism.BtrfsSnapMechanism, "create", return_value=None
) as mock_create:
snap.create_from(snap_mechanisms.SnapType.BTRFS, "parent")
mock_verify_volume.assert_called_once_with("parent")
mock_create.assert_called_once_with("parent", snap_destination)
snap2 = snap_holder.Snapshot(snap_destination)
with self.assertNoLogs():
self.assertEqual(snap2._snap_type, snap_mechanisms.SnapType.BTRFS)


if __name__ == "__main__":
unittest.main()

0 comments on commit 0aa9496

Please sign in to comment.