feat(profiles): implement slice 3 profile system, flock concurrency, and sanitize archiver

This commit is contained in:
soda64 committed 2026-10-09 18:59:49 +02:00
1 parent 66ca179937
commit 646bd9d145
8 files changed
+536 -69

No files matched your search

+60
View File
@@ -121,5 +121,65 @@ class TestCliTracerBullet(unittest.TestCase):
exit_code = main(["verify", snap_id, "--target-dir", self.backup_dir])
self.assertEqual(exit_code, EXIT_CORRUPTED)
def test_snapshot_sanitize_redacts_secrets(self):
# Create sensitive files in mock_etc
shadow_path = os.path.join(self.mock_etc, "shadow")
with open(shadow_path, "w") as f:
f.write("root:$6$secretpasswordhash:19000:0:99999:7:::\n")
ssh_key_path = os.path.join(self.mock_etc, "ssh_host_ed25519_key")
with open(ssh_key_path, "w") as f:
f.write("-----BEGIN OPENSSH PRIVATE KEY-----\nsecret\n-----END OPENSSH PRIVATE KEY-----\n")
stdout = io.StringIO()
with patch("sys.stdout", stdout), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
exit_code = main([
"snapshot",
"--target-dir", self.backup_dir,
"--paths", self.mock_etc,
"--sanitize",
"--json"
])
self.assertEqual(exit_code, EXIT_SUCCESS)
snap_data = json.loads(stdout.getvalue().strip())
archive_path = snap_data["archive_path"]
with tarfile.open(archive_path, "r:gz") as tar:
names = tar.getnames()
# Sensitive files must NOT be in archive data/
self.assertFalse(any("data" in n and "shadow" in n for n in names))
self.assertFalse(any("data" in n and "ssh_host_ed25519_key" in n for n in names))
# meta/sanitized.txt must exist and list them
self.assertIn("meta/sanitized.txt", names)
sanitized_log = tar.extractfile("meta/sanitized.txt").read().decode("utf-8")
self.assertIn("shadow", sanitized_log)
self.assertIn("ssh_host_ed25519_key", sanitized_log)
def test_snapshot_no_compression(self):
stdout = io.StringIO()
with patch("sys.stdout", stdout), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
exit_code = main([
"snapshot",
"--target-dir", self.backup_dir,
"--paths", self.mock_etc,
"--no-compression",
"--json"
])
self.assertEqual(exit_code, EXIT_SUCCESS)
snap_data = json.loads(stdout.getvalue().strip())
archive_path = snap_data["archive_path"]
self.assertTrue(archive_path.endswith(".tar"))
# Verify it is openable as uncompressed tar
with tarfile.open(archive_path, "r:") as tar:
self.assertEqual(tar.getmembers()[0].name, "manifest.json")
# Verify command works on uncompressed tar
snap_id = snap_data["snapshot_id"]
with patch("sys.stdout", io.StringIO()):
exit_code = main(["verify", snap_id, "--target-dir", self.backup_dir])
self.assertEqual(exit_code, EXIT_SUCCESS)
if __name__ == "__main__":
unittest.main()
+51
View File
@@ -0,0 +1,51 @@
import os
import tempfile
import unittest
from linux_backuper.core.lock import SingleInstanceLock, LockAcquisitionError
class TestSingleInstanceLock(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.mkdtemp(prefix="test_lock_")
self.pid_path = os.path.join(self.temp_dir, "test.pid")
def tearDown(self):
import shutil
shutil.rmtree(self.temp_dir, ignore_errors=True)
def test_lock_acquire_and_release(self):
lock1 = SingleInstanceLock(pid_path=self.pid_path)
self.assertTrue(lock1.acquire())
self.assertTrue(os.path.isfile(self.pid_path))
with open(self.pid_path, "r") as f:
pid = int(f.read().strip())
self.assertEqual(pid, os.getpid())
# Second lock attempt must fail
lock2 = SingleInstanceLock(pid_path=self.pid_path)
with self.assertRaises(LockAcquisitionError):
lock2.acquire()
# Release first lock
lock1.release()
# Now lock2 should succeed
self.assertTrue(lock2.acquire())
lock2.release()
def test_lock_context_manager(self):
with SingleInstanceLock(pid_path=self.pid_path):
self.assertTrue(os.path.isfile(self.pid_path))
# Nested lock should fail
lock2 = SingleInstanceLock(pid_path=self.pid_path)
with self.assertRaises(LockAcquisitionError):
lock2.acquire()
# After context exit, lock file is unlinked or released
lock3 = SingleInstanceLock(pid_path=self.pid_path)
self.assertTrue(lock3.acquire())
lock3.release()
if __name__ == "__main__":
unittest.main()
+74
View File
@@ -0,0 +1,74 @@
import json
import os
import tempfile
import unittest
from linux_backuper.core.profiles import ProfileManager
from linux_backuper.models import ProfileConfig
class TestProfileManager(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.mkdtemp(prefix="test_profiles_")
def tearDown(self):
import shutil
shutil.rmtree(self.temp_dir, ignore_errors=True)
def test_builtin_profiles_exist(self):
mgr = ProfileManager()
profiles = mgr.list_profiles()
names = [p.name for p in profiles]
self.assertIn("quick", names)
self.assertIn("system", names)
self.assertIn("full", names)
def test_quick_profile_paths(self):
mgr = ProfileManager()
quick = mgr.get_profile("quick")
self.assertEqual(quick.name, "quick")
self.assertIn("/etc", quick.included_paths)
self.assertNotIn("/boot", quick.included_paths)
def test_system_profile_paths(self):
mgr = ProfileManager()
system = mgr.get_profile("system")
self.assertIn("/etc", system.included_paths)
self.assertIn("/boot", system.included_paths)
def test_full_profile_paths(self):
mgr = ProfileManager()
full = mgr.get_profile("full")
self.assertIn("/etc", full.included_paths)
self.assertIn("/boot", full.included_paths)
self.assertIn("/var/spool/cron", full.included_paths)
self.assertIn("/var/lib/systemd", full.included_paths)
def test_custom_profile_from_config(self):
config_path = os.path.join(self.temp_dir, "linux-backuper.json")
custom_data = {
"profiles": {
"custom_web": {
"description": "Web server custom profile",
"included_paths": ["/etc", "/var/www/html"],
"exclude_patterns": ["*.log", "*.tmp"],
"collect_packages": True
}
}
}
with open(config_path, "w") as f:
json.dump(custom_data, f)
mgr = ProfileManager(config_path=config_path)
p = mgr.get_profile("custom_web")
self.assertEqual(p.name, "custom_web")
self.assertIn("/var/www/html", p.included_paths)
self.assertIn("*.log", p.exclude_patterns)
def test_unknown_profile_raises_key_error(self):
mgr = ProfileManager()
with self.assertRaises(KeyError):
mgr.get_profile("non_existent_profile_xyz")
if __name__ == "__main__":
unittest.main()