Files
soda64 4b3346fd74
Test, Lint and Security Gate / Security Scan and Quality Audits (push) Failing after 8s
Test, Lint and Security Gate / Unit & Security Tests (Coverage >= 80%) (push) Has been skipped
Build and Publish Release / Build Packages and Publish (push) Successful in 1m31s
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
2026-10-23 22:08:15 +02:00

186 lines
6.9 KiB
Python

import json
import os
import tempfile
import unittest
from linux_backuper.models import PackageItem, StorageState, NetworkState
from linux_backuper.managers.base import BasePackageManager
from linux_backuper.managers.rpm import RpmPackageManager
from linux_backuper.managers.dpkg import DpkgPackageManager
from linux_backuper.managers.pacman import PacmanPackageManager
from linux_backuper.core.inspector import SystemInspector
class TestPackageManagerAdapters(unittest.TestCase):
def test_rpm_parser(self):
sample_output = """bash|5.1.8-9.el9|x86_64
coreutils|8.32-35.el9|x86_64
systemd|252-32.el9_4.6|x86_64
"""
mgr = RpmPackageManager()
packages = mgr.parse_package_output(sample_output)
self.assertEqual(len(packages), 3)
self.assertEqual(packages[0].name, "bash")
self.assertEqual(packages[0].version, "5.1.8-9.el9")
self.assertEqual(packages[0].arch, "x86_64")
def test_dpkg_parser(self):
sample_output = """adduser|3.134ubuntu1|all
apt|2.4.12|amd64
systemd|249.11-0ubuntu3.12|amd64
"""
mgr = DpkgPackageManager()
packages = mgr.parse_package_output(sample_output)
self.assertEqual(len(packages), 3)
self.assertEqual(packages[1].name, "apt")
self.assertEqual(packages[1].version, "2.4.12")
self.assertEqual(packages[1].arch, "amd64")
def test_pacman_parser(self):
sample_output = """bash 5.2.026-2
coreutils 9.5-1
systemd 255.7-1
"""
mgr = PacmanPackageManager()
packages = mgr.parse_package_output(sample_output)
self.assertEqual(len(packages), 3)
self.assertEqual(packages[0].name, "bash")
self.assertEqual(packages[0].version, "5.2.026-2")
class TestSystemInspector(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.mkdtemp(prefix="test_inspector_")
def tearDown(self):
import shutil
shutil.rmtree(self.temp_dir, ignore_errors=True)
def test_inspect_users_and_groups_parsing(self):
passwd_content = """root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
soda64:x:1000:1000:Soda User:/home/soda64:/bin/zsh
"""
group_content = """root:x:0:
wheel:x:10:soda64
soda64:x:1000:
"""
passwd_file = os.path.join(self.temp_dir, "passwd")
group_file = os.path.join(self.temp_dir, "group")
with open(passwd_file, "w") as f:
f.write(passwd_content)
with open(group_file, "w") as f:
f.write(group_content)
inspector = SystemInspector(distro_id="almalinux")
users_info = inspector.parse_users_and_groups(passwd_path=passwd_file, group_path=group_file)
self.assertEqual(len(users_info["users"]), 3)
self.assertEqual(users_info["users"][0]["username"], "root")
self.assertEqual(users_info["users"][0]["uid"], 0)
self.assertEqual(users_info["users"][2]["shell"], "/bin/zsh")
self.assertEqual(len(users_info["groups"]), 3)
self.assertEqual(users_info["groups"][1]["name"], "wheel")
def test_inspect_storage_json_parsing(self):
sample_lsblk_json = json.dumps({
"blockdevices": [
{"name": "sda", "size": "50G", "type": "disk", "children": [
{"name": "sda1", "size": "1G", "type": "part", "mountpoint": "/boot"},
{"name": "sda2", "size": "49G", "type": "part", "mountpoint": "/"}
]}
]
})
sample_df_output = """Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 48G 12G 34G 27% /
/dev/sda1 ext4 976M 142M 767M 16% /boot
"""
inspector = SystemInspector(distro_id="debian")
block_devices = inspector.parse_lsblk_json(sample_lsblk_json)
mount_points = inspector.parse_df_output(sample_df_output)
storage_state = StorageState(
block_devices=block_devices,
mount_points=mount_points
)
self.assertIsInstance(storage_state, StorageState)
self.assertEqual(len(storage_state.block_devices), 1)
self.assertEqual(storage_state.block_devices[0]["name"], "sda")
self.assertEqual(len(storage_state.mount_points), 2)
self.assertEqual(storage_state.mount_points[0]["mount"], "/")
def test_inspect_network_parsing(self):
sample_ip_a = json.dumps([
{
"ifname": "eth0",
"flags": ["UP", "LOWER_UP"],
"addr_info": [
{"family": "inet", "local": "192.168.10.55", "prefixlen": 24}
]
}
])
sample_ip_r = json.dumps([
{"dst": "default", "gateway": "192.168.10.1", "dev": "eth0"}
])
sample_resolv = "nameserver 192.168.10.1\nnameserver 1.1.1.1\n"
resolv_path = os.path.join(self.temp_dir, "resolv.conf")
with open(resolv_path, "w") as f:
f.write(sample_resolv)
inspector = SystemInspector(distro_id="almalinux")
raw_ifaces = inspector.parse_ip_a(sample_ip_a)
raw_routes = inspector.parse_ip_r(sample_ip_r)
interfaces = [{
"name": raw_ifaces[0]["ifname"],
"flags": raw_ifaces[0]["flags"],
"addresses": ["192.168.10.55/24"]
}]
routes = [{
"dst": raw_routes[0]["dst"],
"gateway": raw_routes[0]["gateway"],
"dev": raw_routes[0]["dev"]
}]
net_state = NetworkState(
interfaces=interfaces,
routes=routes,
dns_servers=["192.168.10.1", "1.1.1.1"]
)
self.assertIsInstance(net_state, NetworkState)
self.assertEqual(len(net_state.interfaces), 1)
self.assertEqual(net_state.interfaces[0]["name"], "eth0")
self.assertEqual(len(net_state.routes), 1)
self.assertEqual(net_state.routes[0]["gateway"], "192.168.10.1")
self.assertEqual(net_state.dns_servers, ["192.168.10.1", "1.1.1.1"])
def test_dump_meta_files(self):
inspector = SystemInspector(distro_id="almalinux")
meta_dir = os.path.join(self.temp_dir, "meta")
files_map = inspector.dump_meta_files(
meta_dir,
packages=[PackageItem(name="bash", version="5.1.8", arch="x86_64")]
)
self.assertIn("meta/packages.txt", files_map)
self.assertIn("meta/users.json", files_map)
self.assertIn("meta/storage.json", files_map)
self.assertIn("meta/network.json", files_map)
self.assertIn("meta/kernel.txt", files_map)
self.assertIn("meta/hardware.txt", files_map)
# Check that files actually exist on disk
for rel_path, abs_path in files_map.items():
self.assertTrue(os.path.isfile(abs_path), f"File {abs_path} was not created")
# Check content of packages.txt
with open(files_map["meta/packages.txt"], "r") as f:
content = f.read()
self.assertIn("bash|5.1.8|x86_64", content)
if __name__ == "__main__":
unittest.main()