feat(core): implement slice 1 tracer bullet end-to-end skeleton

This commit is contained in:
soda64 committed 2026-10-09 18:37:27 +02:00
1 parent 0aed5bb003
commit 2602b66a90
13 files changed
+882 -1

No files matched your search

+1
View File
@@ -0,0 +1 @@
# tests/__init__.py
+125
View File
@@ -0,0 +1,125 @@
import io
import json
import os
import stat
import sys
import tarfile
import tempfile
import unittest
from unittest.mock import patch
from linux_backuper.cli import main
from linux_backuper.core.constants import EXIT_SUCCESS, EXIT_CORRUPTED
class TestCliTracerBullet(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.mkdtemp(prefix="test_linux_backuper_")
self.backup_dir = os.path.join(self.temp_dir, "backups")
os.makedirs(self.backup_dir, exist_ok=True)
# Mock etc directory with dummy file
self.mock_etc = os.path.join(self.temp_dir, "etc")
os.makedirs(self.mock_etc, exist_ok=True)
with open(os.path.join(self.mock_etc, "hostname"), "w") as f:
f.write("test-host.local\n")
with open(os.path.join(self.mock_etc, "sample.conf"), "w") as f:
f.write("key=value\n")
def tearDown(self):
import shutil
shutil.rmtree(self.temp_dir, ignore_errors=True)
def test_version_flag(self):
stdout = io.StringIO()
with patch("sys.stdout", stdout):
with self.assertRaises(SystemExit) as cm:
main(["--version"])
self.assertEqual(cm.exception.code, EXIT_SUCCESS)
self.assertIn("1.0.0", stdout.getvalue())
def test_help_flag(self):
stdout = io.StringIO()
with patch("sys.stdout", stdout):
with self.assertRaises(SystemExit) as cm:
main(["--help"])
self.assertEqual(cm.exception.code, EXIT_SUCCESS)
self.assertIn("linux-backuper", stdout.getvalue())
self.assertIn("snapshot", stdout.getvalue())
def test_snapshot_tracer_bullet_creates_archive(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,
"--profile", "quick",
"--json"
])
self.assertEqual(exit_code, EXIT_SUCCESS)
output_data = json.loads(stdout.getvalue().strip())
self.assertIn("snapshot_id", output_data)
self.assertIn("archive_path", output_data)
archive_path = output_data["archive_path"]
self.assertTrue(os.path.exists(archive_path))
# Check file permissions (0600 on POSIX)
file_mode = stat.S_IMODE(os.stat(archive_path).st_mode)
self.assertEqual(file_mode, 0o600)
# Verify tar members: manifest.json MUST be the very first member
with tarfile.open(archive_path, "r:gz") as tar:
members = tar.getmembers()
self.assertGreater(len(members), 0)
self.assertEqual(members[0].name, "manifest.json")
# Extract manifest and verify structure
manifest_file = tar.extractfile(members[0])
manifest = json.loads(manifest_file.read().decode("utf-8"))
self.assertEqual(manifest["snapshot_id"], output_data["snapshot_id"])
self.assertEqual(manifest["profile"], "quick")
self.assertIn("os", manifest)
self.assertIn("files_sha256", manifest)
def test_list_command(self):
# First create a snapshot
with patch("sys.stdout", io.StringIO()), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
main(["snapshot", "--target-dir", self.backup_dir, "--paths", self.mock_etc])
# Now test listing
stdout = io.StringIO()
with patch("sys.stdout", stdout):
exit_code = main(["list", "--target-dir", self.backup_dir, "--json"])
self.assertEqual(exit_code, EXIT_SUCCESS)
listed = json.loads(stdout.getvalue().strip())
self.assertEqual(len(listed), 1)
self.assertIn("snapshot_id", listed[0])
self.assertEqual(listed[0]["profile"], "quick")
def test_verify_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_data = json.loads(stdout.getvalue().strip())
snap_id = snap_data["snapshot_id"]
# Verify should succeed on untouched archive
with patch("sys.stdout", io.StringIO()):
exit_code = main(["verify", snap_id, "--target-dir", self.backup_dir])
self.assertEqual(exit_code, EXIT_SUCCESS)
# Corrupt archive CRC
archive_path = snap_data["archive_path"]
with open(archive_path, "r+b") as f:
f.seek(-4, os.SEEK_END)
f.write(b"\x00\x00\x00\x00")
# Verify should detect corruption and return EXIT_CORRUPTED (3)
with patch("sys.stdout", io.StringIO()):
exit_code = main(["verify", snap_id, "--target-dir", self.backup_dir])
self.assertEqual(exit_code, EXIT_CORRUPTED)
if __name__ == "__main__":
unittest.main()
+80
View File
@@ -0,0 +1,80 @@
import os
import tempfile
import unittest
from linux_backuper.core.detector import detect_distro, parse_os_release
from linux_backuper.models import OsInfo
class TestOsDetector(unittest.TestCase):
def test_parse_almalinux_9(self):
content = """NAME="AlmaLinux"
VERSION="9.4 (Seafoam)"
ID="almalinux"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.4"
PRETTY_NAME="AlmaLinux 9.4 (Seafoam)"
"""
info = parse_os_release(content)
self.assertEqual(info["id"], "almalinux")
self.assertEqual(info["name"], "AlmaLinux")
self.assertEqual(info["version_id"], "9.4")
def test_parse_debian_12(self):
content = """PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
"""
info = parse_os_release(content)
self.assertEqual(info["id"], "debian")
self.assertEqual(info["name"], "Debian GNU/Linux")
self.assertEqual(info["version_id"], "12")
def test_parse_ubuntu_2204(self):
content = """NAME="Ubuntu"
VERSION="22.04.4 LTS (Jammy Jellyfish)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 22.04.4 LTS"
VERSION_ID="22.04"
"""
info = parse_os_release(content)
self.assertEqual(info["id"], "ubuntu")
self.assertEqual(info["name"], "Ubuntu")
self.assertEqual(info["version_id"], "22.04")
def test_parse_arch(self):
content = """NAME="Arch Linux"
PRETTY_NAME="Arch Linux"
ID=arch
BUILD_ID=rolling
"""
info = parse_os_release(content)
self.assertEqual(info["id"], "arch")
self.assertEqual(info["name"], "Arch Linux")
self.assertEqual(info["version_id"], "rolling")
def test_detect_distro_with_custom_file(self):
with tempfile.NamedTemporaryFile("w+", delete=False) as f:
f.write("""NAME="Fedora Linux"
VERSION="40 (Cloud Edition)"
ID=fedora
VERSION_ID="40"
""")
temp_path = f.name
try:
os_info = detect_distro(os_release_path=temp_path)
self.assertIsInstance(os_info, OsInfo)
self.assertEqual(os_info.distro_id, "fedora")
self.assertEqual(os_info.distro_name, "Fedora Linux")
self.assertEqual(os_info.version_id, "40")
self.assertTrue(len(os_info.arch) > 0)
self.assertTrue(len(os_info.kernel) > 0)
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
if __name__ == "__main__":
unittest.main()