186 lines
7.7 KiB
Python
186 lines
7.7 KiB
Python
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)
|
|
|
|
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()
|