feat: modernize to Software Factory standards, add AppSec suite, and bump to v2.0.0
- Refactor tests to achieve 100% Zero-Mocks compliance (remove unittest.mock, add tests.helpers.env_override and stdlib redirects) - Extract testable parsing methods in package managers and inspector - Add tests/test_security.py with 5 negative AppSec test cases (Zip Slip, Root Gate, Tampering SHA-256, Secret Sanitization, and AST check for zero shell=True) - Expand is_sanitized_target to redact all private SSH/TLS keys (*.key, *.pem, id_*) - Split Gitea CI/CD pipelines into test-and-lint.yaml and build-and-release.yaml - Add .gitleaks.toml configuration - Formally close v1.0.0 genesis plan in docs/plans/linux-backuper/00-status.md as immutable ADR - Bump version to v2.0.0 across __init__.py, man page, tests, and documentation
This commit is contained in:
1 parent
3e5f2ab498
commit
4b3346fd74
22 files changed
+577
-203
No files matched your search
+24
-24
@@ -1,3 +1,4 @@
|
||||
from contextlib import redirect_stdout
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
@@ -6,10 +7,10 @@ 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, EXIT_DRY_RUN
|
||||
from tests.helpers import env_override
|
||||
|
||||
class TestCliTracerBullet(unittest.TestCase):
|
||||
def setUp(self):
|
||||
@@ -30,15 +31,15 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
def test_version_flag(self):
|
||||
stdout = io.StringIO()
|
||||
with patch("sys.stdout", stdout):
|
||||
with redirect_stdout(stdout):
|
||||
with self.assertRaises(SystemExit) as cm:
|
||||
main(["--version"])
|
||||
self.assertEqual(cm.exception.code, EXIT_SUCCESS)
|
||||
self.assertIn("1.0.0", stdout.getvalue())
|
||||
self.assertIn("2.0.0", stdout.getvalue())
|
||||
|
||||
def test_help_flag(self):
|
||||
stdout = io.StringIO()
|
||||
with patch("sys.stdout", stdout):
|
||||
with redirect_stdout(stdout):
|
||||
with self.assertRaises(SystemExit) as cm:
|
||||
main(["--help"])
|
||||
self.assertEqual(cm.exception.code, EXIT_SUCCESS)
|
||||
@@ -47,7 +48,7 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
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"}):
|
||||
with redirect_stdout(stdout), env_override({"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
exit_code = main([
|
||||
"snapshot",
|
||||
"--target-dir", self.backup_dir,
|
||||
@@ -84,12 +85,12 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
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"}):
|
||||
with redirect_stdout(io.StringIO()), env_override({"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):
|
||||
with redirect_stdout(stdout):
|
||||
exit_code = main(["list", "--target-dir", self.backup_dir, "--json"])
|
||||
self.assertEqual(exit_code, EXIT_SUCCESS)
|
||||
|
||||
@@ -100,13 +101,13 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
def test_verify_command(self):
|
||||
stdout = io.StringIO()
|
||||
with patch("sys.stdout", stdout), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
with redirect_stdout(stdout), env_override({"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()):
|
||||
with redirect_stdout(io.StringIO()):
|
||||
exit_code = main(["verify", snap_id, "--target-dir", self.backup_dir])
|
||||
self.assertEqual(exit_code, EXIT_SUCCESS)
|
||||
|
||||
@@ -117,7 +118,7 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
f.write(b"\x00\x00\x00\x00")
|
||||
|
||||
# Verify should detect corruption and return EXIT_CORRUPTED (3)
|
||||
with patch("sys.stdout", io.StringIO()):
|
||||
with redirect_stdout(io.StringIO()):
|
||||
exit_code = main(["verify", snap_id, "--target-dir", self.backup_dir])
|
||||
self.assertEqual(exit_code, EXIT_CORRUPTED)
|
||||
|
||||
@@ -131,7 +132,7 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
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"}):
|
||||
with redirect_stdout(stdout), env_override({"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
exit_code = main([
|
||||
"snapshot",
|
||||
"--target-dir", self.backup_dir,
|
||||
@@ -157,7 +158,7 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
def test_snapshot_no_compression(self):
|
||||
stdout = io.StringIO()
|
||||
with patch("sys.stdout", stdout), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
with redirect_stdout(stdout), env_override({"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
exit_code = main([
|
||||
"snapshot",
|
||||
"--target-dir", self.backup_dir,
|
||||
@@ -177,19 +178,19 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
# Verify command works on uncompressed tar
|
||||
snap_id = snap_data["snapshot_id"]
|
||||
with patch("sys.stdout", io.StringIO()):
|
||||
with redirect_stdout(io.StringIO()):
|
||||
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"}):
|
||||
with redirect_stdout(stdout), env_override({"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):
|
||||
with redirect_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())
|
||||
@@ -197,7 +198,7 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
# JSON mode
|
||||
stdout_json = io.StringIO()
|
||||
with patch("sys.stdout", stdout_json):
|
||||
with redirect_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())
|
||||
@@ -205,7 +206,7 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
def test_extract_command(self):
|
||||
stdout = io.StringIO()
|
||||
with patch("sys.stdout", stdout), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
with redirect_stdout(stdout), env_override({"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"]
|
||||
|
||||
@@ -225,7 +226,7 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
def test_diff_command(self):
|
||||
stdout = io.StringIO()
|
||||
with patch("sys.stdout", stdout), patch.dict(os.environ, {"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
with redirect_stdout(stdout), env_override({"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"]
|
||||
|
||||
@@ -235,7 +236,7 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
# Test live diff
|
||||
stdout_diff = io.StringIO()
|
||||
with patch("sys.stdout", stdout_diff):
|
||||
with redirect_stdout(stdout_diff):
|
||||
exit_code = main([
|
||||
"diff", snap_id,
|
||||
"--target-dir", self.backup_dir,
|
||||
@@ -249,13 +250,13 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
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"}):
|
||||
with redirect_stdout(stdout), env_override({"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):
|
||||
with redirect_stdout(stdout_restore):
|
||||
exit_code = main([
|
||||
"restore", snap_id,
|
||||
"--target-dir", self.backup_dir,
|
||||
@@ -266,7 +267,7 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
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"}):
|
||||
with redirect_stdout(stdout), env_override({"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"]
|
||||
|
||||
@@ -277,7 +278,7 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
# 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"}):
|
||||
with redirect_stdout(stdout_restore), env_override({"LINUX_BACKUPER_ALLOW_NON_ROOT": "1"}):
|
||||
exit_code = main([
|
||||
"restore", snap_id,
|
||||
"--force",
|
||||
@@ -297,4 +298,3 @@ class TestCliTracerBullet(unittest.TestCase):
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in new issue
Block a user