feat(restore): implement slice 4 snapshot management, diff engine, and safe restore

This commit is contained in:
soda64 committed 2026-10-09 19:19:28 +02:00
1 parent 646bd9d145
commit 8d4127de30
9 files changed
+1386 -4

No files matched your search

+131
View File
@@ -0,0 +1,131 @@
"""Unit tests for SafeRestoreEngine."""
import os
import shutil
import tempfile
import unittest
from unittest.mock import patch
from linux_backuper.core.archiver import SnapshotArchiver
from linux_backuper.core.restore import SafeRestoreEngine, DryRunException
from linux_backuper.models import SnapshotManifest
class TestSafeRestoreEngine(unittest.TestCase):
def setUp(self):
self.test_dir = tempfile.mkdtemp(prefix="test_restore_")
self.backup_dir = os.path.join(self.test_dir, "backups")
os.makedirs(self.backup_dir, exist_ok=True)
self.mock_root = os.path.join(self.test_dir, "root")
self.mock_etc = os.path.join(self.mock_root, "etc")
os.makedirs(self.mock_etc, exist_ok=True)
# Create files to back up
self.hosts_path = os.path.join(self.mock_etc, "hosts")
with open(self.hosts_path, "w") as f:
f.write("127.0.0.1 localhost\n")
self.resolv_path = os.path.join(self.mock_etc, "resolv.conf")
with open(self.resolv_path, "w") as f:
f.write("nameserver 8.8.8.8\n")
self.fstab_path = os.path.join(self.mock_etc, "fstab")
with open(self.fstab_path, "w") as f:
f.write("/dev/sda1 / ext4 defaults 0 1\n")
# Create netplan file (sensitive network)
self.netplan_dir = os.path.join(self.mock_etc, "netplan")
os.makedirs(self.netplan_dir, exist_ok=True)
self.net_cfg = os.path.join(self.netplan_dir, "01-netcfg.yaml")
with open(self.net_cfg, "w") as f:
f.write("network: {version: 2}\n")
# Create base snapshot
archiver = SnapshotArchiver(target_dir=self.backup_dir)
entries, sha, uncompressed, _ = archiver.collect_file_list([self.mock_etc])
m = SnapshotManifest(
schema_version="1.0",
snapshot_id="test_snap",
created_at="2026-10-01T12:00:00Z",
hostname="host1",
profile="quick",
compression="gzip",
os={"distro_id": "debian", "distro_name": "Debian", "version_id": "12", "kernel": "6.1", "arch": "x86_64"},
package_manager="dpkg",
package_count=0,
included_paths=[self.mock_etc],
stats={"file_count": len(entries), "uncompressed_bytes": uncompressed, "compressed_bytes": 0},
files_sha256=sha
)
self.archive_path = archiver.create_snapshot_archive("test_snap", m, entries)
def tearDown(self):
shutil.rmtree(self.test_dir, ignore_errors=True)
def test_plan_restore_skips_dynamic_and_fstab(self):
engine = SafeRestoreEngine(target_dir=self.backup_dir)
plan = engine.plan_restore("test_snap", target_root=self.mock_root, include_fstab=False)
actions_by_path = {item.rel_path: item for item in plan.items}
# resolv.conf must be skipped (dynamic system file)
resolv_item = [item for item in plan.items if "resolv.conf" in item.rel_path][0]
self.assertEqual(resolv_item.action, "skip")
self.assertIn("volatile", resolv_item.reason.lower())
# fstab must be skipped unless include_fstab is set
fstab_item = [item for item in plan.items if "fstab" in item.rel_path][0]
self.assertEqual(fstab_item.action, "skip")
self.assertIn("fstab", fstab_item.reason.lower())
# netplan must be flagged as sensitive network
net_item = [item for item in plan.items if "01-netcfg.yaml" in item.rel_path][0]
self.assertTrue(net_item.is_sensitive_network)
def test_dry_run_raises_and_modifies_nothing(self):
# Modify hosts on mock_root
with open(self.hosts_path, "w") as f:
f.write("corrupted hosts content\n")
engine = SafeRestoreEngine(target_dir=self.backup_dir)
plan = engine.plan_restore("test_snap", target_root=self.mock_root)
with self.assertRaises(DryRunException):
engine.execute_restore(plan, force=False)
# Verify hosts content was NOT modified
with open(self.hosts_path, "r") as f:
self.assertEqual(f.read(), "corrupted hosts content\n")
def test_execute_restore_with_force_creates_safety_snapshot_and_restores(self):
# Corrupt hosts
with open(self.hosts_path, "w") as f:
f.write("corrupted content\n")
# Delete netplan config
os.remove(self.net_cfg)
engine = SafeRestoreEngine(target_dir=self.backup_dir)
plan = engine.plan_restore("test_snap", target_root=self.mock_root)
# Allow non-root flock fallback
pid_file = os.path.join(self.test_dir, "test.pid")
with patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
res = engine.execute_restore(plan, force=True, pid_path=pid_file)
self.assertEqual(res["status"], "success")
self.assertTrue(res["safety_snapshot_id"].startswith("pre_restore_"))
self.assertTrue(os.path.isfile(res["safety_archive_path"]))
# Verify hosts restored
with open(self.hosts_path, "r") as f:
self.assertEqual(f.read(), "127.0.0.1 localhost\n")
# Verify netplan recreated
self.assertTrue(os.path.isfile(self.net_cfg))
with open(self.net_cfg, "r") as f:
self.assertEqual(f.read(), "network: {version: 2}\n")
if __name__ == "__main__":
unittest.main()