feat(restore): implement slice 4 snapshot management, diff engine, and safe restore
This commit is contained in:
1 parent
646bd9d145
commit
8d4127de30
9 files changed
+1386
-4
No files matched your search
+116
-1
@@ -9,7 +9,7 @@ import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from linux_backuper.cli import main
|
||||
from linux_backuper.core.constants import EXIT_SUCCESS, EXIT_CORRUPTED
|
||||
from linux_backuper.core.constants import EXIT_SUCCESS, EXIT_CORRUPTED, EXIT_DRY_RUN
|
||||
|
||||
class TestCliTracerBullet(unittest.TestCase):
|
||||
def setUp(self):
|
||||
@@ -181,5 +181,120 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
exit_code = main(["verify", snap_id, "--target-dir", self.backup_dir])
|
||||
self.assertEqual(exit_code, EXIT_SUCCESS)
|
||||
|
||||
def test_show_command(self):
|
||||
stdout = io.StringIO()
|
||||
with patch("sys.stdout", stdout), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
main(["snapshot", "--target-dir", self.backup_dir, "--paths", self.mock_etc, "--json"])
|
||||
snap_id = json.loads(stdout.getvalue().strip())["snapshot_id"]
|
||||
|
||||
# Text mode
|
||||
stdout_show = io.StringIO()
|
||||
with patch("sys.stdout", stdout_show):
|
||||
exit_code = main(["show", snap_id, "--target-dir", self.backup_dir])
|
||||
self.assertEqual(exit_code, EXIT_SUCCESS)
|
||||
self.assertIn("Snapshot ID:", stdout_show.getvalue())
|
||||
self.assertIn(snap_id, stdout_show.getvalue())
|
||||
|
||||
# JSON mode
|
||||
stdout_json = io.StringIO()
|
||||
with patch("sys.stdout", stdout_json):
|
||||
exit_code = main(["show", snap_id, "--target-dir", self.backup_dir, "--json"])
|
||||
self.assertEqual(exit_code, EXIT_SUCCESS)
|
||||
data = json.loads(stdout_json.getvalue().strip())
|
||||
self.assertEqual(data["snapshot_id"], snap_id)
|
||||
|
||||
def test_extract_command(self):
|
||||
stdout = io.StringIO()
|
||||
with patch("sys.stdout", stdout), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
main(["snapshot", "--target-dir", self.backup_dir, "--paths", self.mock_etc, "--json"])
|
||||
snap_id = json.loads(stdout.getvalue().strip())["snapshot_id"]
|
||||
|
||||
dest_file = os.path.join(self.temp_dir, "extracted_hostname.txt")
|
||||
# Extract filename
|
||||
sample_path = [f for f in os.listdir(self.mock_etc) if f == "hostname"][0]
|
||||
exit_code = main([
|
||||
"extract", snap_id,
|
||||
"--file", sample_path,
|
||||
"--out", dest_file,
|
||||
"--target-dir", self.backup_dir
|
||||
])
|
||||
self.assertEqual(exit_code, EXIT_SUCCESS)
|
||||
self.assertTrue(os.path.isfile(dest_file))
|
||||
with open(dest_file, "r") as f:
|
||||
self.assertEqual(f.read(), "test-host.local\n")
|
||||
|
||||
def test_diff_command(self):
|
||||
stdout = io.StringIO()
|
||||
with patch("sys.stdout", stdout), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
main(["snapshot", "--target-dir", self.backup_dir, "--paths", self.mock_etc, "--json"])
|
||||
snap_id = json.loads(stdout.getvalue().strip())["snapshot_id"]
|
||||
|
||||
# Modify sample.conf on mock etc
|
||||
with open(os.path.join(self.mock_etc, "sample.conf"), "w") as f:
|
||||
f.write("key=modified_value\n")
|
||||
|
||||
# Test live diff
|
||||
stdout_diff = io.StringIO()
|
||||
with patch("sys.stdout", stdout_diff):
|
||||
exit_code = main([
|
||||
"diff", snap_id,
|
||||
"--target-dir", self.backup_dir,
|
||||
"--target-root", self.temp_dir,
|
||||
"--json"
|
||||
])
|
||||
self.assertEqual(exit_code, EXIT_SUCCESS)
|
||||
diff_data = json.loads(stdout_diff.getvalue().strip())
|
||||
self.assertEqual(diff_data["type"], "live_vs_snapshot")
|
||||
self.assertTrue(any("sample.conf" in f for f in diff_data["files"]["modified"]))
|
||||
|
||||
def test_restore_dry_run_exits_with_code_5(self):
|
||||
stdout = io.StringIO()
|
||||
with patch("sys.stdout", stdout), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
main(["snapshot", "--target-dir", self.backup_dir, "--paths", self.mock_etc, "--json"])
|
||||
snap_id = json.loads(stdout.getvalue().strip())["snapshot_id"]
|
||||
|
||||
# Default restore (no --force) must return EXIT_DRY_RUN (5)
|
||||
stdout_restore = io.StringIO()
|
||||
with patch("sys.stdout", stdout_restore):
|
||||
exit_code = main([
|
||||
"restore", snap_id,
|
||||
"--target-dir", self.backup_dir,
|
||||
"--target-root", self.temp_dir
|
||||
])
|
||||
self.assertEqual(exit_code, EXIT_DRY_RUN)
|
||||
self.assertIn("RESTORE EXECUTION PLAN (Dry Run)", stdout_restore.getvalue())
|
||||
|
||||
def test_restore_force_executes_successfully(self):
|
||||
stdout = io.StringIO()
|
||||
with patch("sys.stdout", stdout), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
main(["snapshot", "--target-dir", self.backup_dir, "--paths", self.mock_etc, "--json"])
|
||||
snap_id = json.loads(stdout.getvalue().strip())["snapshot_id"]
|
||||
|
||||
# Alter sample.conf
|
||||
with open(os.path.join(self.mock_etc, "sample.conf"), "w") as f:
|
||||
f.write("key=tampered_value\n")
|
||||
|
||||
# Restore with --force and non-root test override
|
||||
pid_file = os.path.join(self.temp_dir, "restore_test.pid")
|
||||
stdout_restore = io.StringIO()
|
||||
with patch("sys.stdout", stdout_restore), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
exit_code = main([
|
||||
"restore", snap_id,
|
||||
"--force",
|
||||
"--target-dir", self.backup_dir,
|
||||
"--target-root", self.temp_dir,
|
||||
"--pid-path", pid_file,
|
||||
"--json"
|
||||
])
|
||||
self.assertEqual(exit_code, EXIT_SUCCESS)
|
||||
res = json.loads(stdout_restore.getvalue().strip())
|
||||
self.assertEqual(res["status"], "success")
|
||||
self.assertTrue(res["safety_snapshot_id"].startswith("pre_restore_"))
|
||||
|
||||
# Check sample.conf restored
|
||||
with open(os.path.join(self.mock_etc, "sample.conf"), "r") as f:
|
||||
self.assertEqual(f.read(), "key=value\n")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in new issue
Block a user