feat: add root check, singleton lock, RHEL/Fedora upgrade auto-installer & container tests for AlmaLinux 9 and Arch Linux
This commit is contained in:
1 parent
507a3fd310
commit
44c9423166
10 files changed
+711
-27
No files matched your search
@@ -104,18 +104,123 @@ run_distro_test() {
|
||||
test -f /var/log/linux-updater/latest.log
|
||||
"
|
||||
;;
|
||||
almalinux9)
|
||||
IMAGE="docker.io/library/almalinux:9"
|
||||
CMD="
|
||||
set -e
|
||||
dnf install -y -q python3 rpm-build procps-ng > /dev/null
|
||||
rm -rf dist/rpm
|
||||
echo '=== 1. Verifying Native Python on AlmaLinux 9 ==='
|
||||
python3 --version
|
||||
echo '=== 2. Building packages and RPM inside AlmaLinux 9 container ==='
|
||||
python3 packaging/build_packages.py
|
||||
echo '=== 3. Installing generated .rpm package ==='
|
||||
rpm -Uvh --replacepkgs --replacefiles dist/rpm/RPMS/noarch/linux-updater-*.noarch.rpm
|
||||
echo '=== 4. Testing installed command ==='
|
||||
linux-updater --version
|
||||
echo '=== 5. Testing pre-flight health checks ==='
|
||||
linux-updater health
|
||||
echo '=== 6. Testing root privilege guardrails ==='
|
||||
id -u testuser >/dev/null 2>&1 || useradd -m testuser
|
||||
if su - testuser -c 'linux-updater update'; then
|
||||
echo 'FAIL: Unprivileged update succeeded without root!' >&2
|
||||
exit 1
|
||||
else
|
||||
echo 'OK: Unprivileged update correctly blocked.'
|
||||
fi
|
||||
echo 'OK: Verifying non-root dry-run and health check allowed:'
|
||||
su - testuser -c 'linux-updater update --dry-run -y'
|
||||
su - testuser -c 'linux-updater health'
|
||||
echo '=== 7. Testing singleton concurrency lock ==='
|
||||
python3 -c 'from linux_updater.core.lock import InstanceLock; import time; lock = InstanceLock(); lock.acquire(); time.sleep(3)' &
|
||||
LOCK_PID=\$!
|
||||
sleep 0.5
|
||||
if linux-updater health; then
|
||||
echo 'FAIL: Second instance succeeded despite active lock!' >&2
|
||||
kill \$LOCK_PID 2>/dev/null || true
|
||||
exit 1
|
||||
else
|
||||
echo 'OK: Second instance correctly blocked by singleton lock.'
|
||||
fi
|
||||
wait \$LOCK_PID 2>/dev/null || true
|
||||
echo '=== 8. Testing package updates (check, dry-run, actual) ==='
|
||||
linux-updater check
|
||||
linux-updater update --dry-run -y
|
||||
linux-updater update -y
|
||||
echo '=== 9. Testing major release upgrade (dry-run with ELevate) ==='
|
||||
linux-updater release-upgrade --dry-run -y
|
||||
echo '=== 10. Verifying standard /var/log/linux-updater logging ==='
|
||||
ls -la /var/log/linux-updater
|
||||
test -f /var/log/linux-updater/latest.log
|
||||
echo '=== AlmaLinux 9 verification complete ==='
|
||||
"
|
||||
;;
|
||||
archlinux)
|
||||
IMAGE="docker.io/library/archlinux:latest"
|
||||
EXTRA_FLAGS="--platform linux/amd64"
|
||||
CMD="
|
||||
set -e
|
||||
sed -i 's/^DownloadUser/#DownloadUser/' /etc/pacman.conf
|
||||
sed -i 's/^#DisableSandboxSyscalls/DisableSandboxSyscalls/' /etc/pacman.conf
|
||||
pacman -Sy --noconfirm python tar gzip procps-ng which > /dev/null
|
||||
rm -rf dist/linux-updater-*.tar.gz linux-updater-1.0.0
|
||||
echo '=== 1. Verifying Python on Arch Linux ==='
|
||||
python3 --version
|
||||
echo '=== 2. Building packages (tarball distribution) ==='
|
||||
python3 packaging/build_packages.py
|
||||
echo '=== 3. Installing from generated tarball via install.sh ==='
|
||||
tar -xzf dist/linux-updater-1.0.0.tar.gz
|
||||
(cd linux-updater-1.0.0 && ./install.sh)
|
||||
echo '=== 4. Testing installed command ==='
|
||||
linux-updater --version
|
||||
echo '=== 5. Testing pre-flight health checks ==='
|
||||
linux-updater health
|
||||
echo '=== 6. Testing root privilege guardrails ==='
|
||||
id -u testuser >/dev/null 2>&1 || useradd -m testuser
|
||||
if su - testuser -c 'linux-updater update'; then
|
||||
echo 'FAIL: Unprivileged update succeeded without root!' >&2
|
||||
exit 1
|
||||
else
|
||||
echo 'OK: Unprivileged update correctly blocked.'
|
||||
fi
|
||||
echo 'OK: Verifying non-root dry-run and health check allowed:'
|
||||
su - testuser -c 'linux-updater update --dry-run -y'
|
||||
su - testuser -c 'linux-updater health'
|
||||
echo '=== 7. Testing singleton concurrency lock ==='
|
||||
python3 -c 'from linux_updater.core.lock import InstanceLock; import time; lock = InstanceLock(); lock.acquire(); time.sleep(3)' &
|
||||
LOCK_PID=\$!
|
||||
sleep 0.5
|
||||
if linux-updater health; then
|
||||
echo 'FAIL: Second instance succeeded despite active lock!' >&2
|
||||
kill \$LOCK_PID 2>/dev/null || true
|
||||
exit 1
|
||||
else
|
||||
echo 'OK: Second instance correctly blocked by singleton lock.'
|
||||
fi
|
||||
wait \$LOCK_PID 2>/dev/null || true
|
||||
echo '=== 8. Testing package updates (check & dry-run) ==='
|
||||
linux-updater check
|
||||
linux-updater update --dry-run -y
|
||||
echo '=== 9. Testing rolling release upgrade (dry-run) ==='
|
||||
linux-updater release-upgrade --dry-run -y
|
||||
echo '=== 10. Verifying standard /var/log/linux-updater logging ==='
|
||||
ls -la /var/log/linux-updater
|
||||
test -f /var/log/linux-updater/latest.log
|
||||
echo '=== Arch Linux verification complete ==='
|
||||
"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown distro: ${D}" >&2
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
podman run --rm -v "${WORKSPACE}:/workspace:Z" -w /workspace "${IMAGE}" bash -c "${CMD}"
|
||||
podman run --rm ${EXTRA_FLAGS} -v "${WORKSPACE}:/workspace:Z" -w /workspace "${IMAGE}" bash -c "${CMD}"
|
||||
echo ">>> [SUCCESS] Testing for ${D} PASSED!"
|
||||
}
|
||||
|
||||
if [ "${DISTRO}" = "all" ]; then
|
||||
for target in debian ubuntu fedora almalinux8; do
|
||||
for target in debian ubuntu fedora almalinux8 almalinux9 archlinux; do
|
||||
run_distro_test "${target}"
|
||||
done
|
||||
else
|
||||
|
||||
+57
-3
@@ -44,9 +44,10 @@ class TestCLIIntegration(unittest.TestCase):
|
||||
code = main(["--json", "health"])
|
||||
self.assertEqual(code, 0)
|
||||
|
||||
@patch("linux_updater.cli.check_root_privileges", return_value=True)
|
||||
@patch("linux_updater.cli.DistroDetector.detect")
|
||||
@patch("linux_updater.cli.get_manager")
|
||||
def test_cli_user_abort_returns_code_3(self, mock_get_manager, mock_detect):
|
||||
def test_cli_user_abort_returns_code_3(self, mock_get_manager, mock_detect, mock_root):
|
||||
mock_detect.return_value = DistroInfo(
|
||||
family=DistroFamily.DEBIAN,
|
||||
distro_id="ubuntu",
|
||||
@@ -72,9 +73,10 @@ class TestCLIIntegration(unittest.TestCase):
|
||||
code = main(["update"])
|
||||
self.assertEqual(code, 3)
|
||||
|
||||
@patch("linux_updater.cli.check_root_privileges", return_value=True)
|
||||
@patch("linux_updater.cli.DistroDetector.detect")
|
||||
@patch("linux_updater.cli.get_manager")
|
||||
def test_cli_update_reboot_required_returns_code_2(self, mock_get_manager, mock_detect):
|
||||
def test_cli_update_reboot_required_returns_code_2(self, mock_get_manager, mock_detect, mock_root):
|
||||
mock_detect.return_value = DistroInfo(
|
||||
family=DistroFamily.DEBIAN,
|
||||
distro_id="ubuntu",
|
||||
@@ -97,9 +99,10 @@ class TestCLIIntegration(unittest.TestCase):
|
||||
code = main(["-y", "update"])
|
||||
self.assertEqual(code, 2)
|
||||
|
||||
@patch("linux_updater.cli.check_root_privileges", return_value=True)
|
||||
@patch("linux_updater.cli.DistroDetector.detect")
|
||||
@patch("linux_updater.cli.get_manager")
|
||||
def test_cli_release_upgrade_calls_manager_with_target(self, mock_get_manager, mock_detect):
|
||||
def test_cli_release_upgrade_calls_manager_with_target(self, mock_get_manager, mock_detect, mock_root):
|
||||
mock_detect.return_value = DistroInfo(
|
||||
family=DistroFamily.RHEL,
|
||||
distro_id="fedora",
|
||||
@@ -126,6 +129,57 @@ class TestCLIIntegration(unittest.TestCase):
|
||||
non_interactive=True,
|
||||
)
|
||||
|
||||
@patch("linux_updater.cli.check_root_privileges", return_value=False)
|
||||
@patch("linux_updater.cli.DistroDetector.detect")
|
||||
def test_cli_root_required_for_mutating_commands(self, mock_detect, mock_root):
|
||||
mock_detect.return_value = DistroInfo(
|
||||
family=DistroFamily.DEBIAN,
|
||||
distro_id="ubuntu",
|
||||
version_id="24.04",
|
||||
pretty_name="Ubuntu 24.04",
|
||||
)
|
||||
# Non-root user running update -> exit code 1
|
||||
code = main(["update"])
|
||||
self.assertEqual(code, 1)
|
||||
|
||||
@patch("linux_updater.cli.check_root_privileges", return_value=False)
|
||||
@patch("linux_updater.cli.DistroDetector.detect")
|
||||
@patch("linux_updater.cli.get_manager")
|
||||
def test_cli_dry_run_allowed_without_root(self, mock_get_manager, mock_detect, mock_root):
|
||||
mock_detect.return_value = DistroInfo(
|
||||
family=DistroFamily.DEBIAN,
|
||||
distro_id="ubuntu",
|
||||
version_id="24.04",
|
||||
pretty_name="Ubuntu 24.04",
|
||||
)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.get_lock_files.return_value = []
|
||||
mock_manager.check_updates.return_value = []
|
||||
mock_manager.apply_updates.return_value = CommandResult(0, "simulated", "", True)
|
||||
mock_manager.is_reboot_required.return_value = RebootStatus(required=False)
|
||||
mock_get_manager.return_value = mock_manager
|
||||
|
||||
with patch("os.statvfs") as mock_statvfs:
|
||||
mock_stat = MagicMock()
|
||||
mock_stat.f_bavail = 5 * 1024 * 1024
|
||||
mock_stat.f_frsize = 1024
|
||||
mock_statvfs.return_value = mock_stat
|
||||
|
||||
code = main(["update", "--dry-run", "-y"])
|
||||
self.assertEqual(code, 0)
|
||||
|
||||
@patch("linux_updater.cli.InstanceLock.acquire", return_value=False)
|
||||
@patch("linux_updater.cli.DistroDetector.detect")
|
||||
def test_cli_instance_lock_blocks_concurrent_execution(self, mock_detect, mock_acquire):
|
||||
mock_detect.return_value = DistroInfo(
|
||||
family=DistroFamily.DEBIAN,
|
||||
distro_id="ubuntu",
|
||||
version_id="24.04",
|
||||
pretty_name="Ubuntu 24.04",
|
||||
)
|
||||
code = main(["health"])
|
||||
self.assertEqual(code, 1)
|
||||
|
||||
def test_config_file_and_cli_overrides(self):
|
||||
config_data = {
|
||||
"thresholds": {"min_boot_mb": 500},
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from linux_updater.core.lock import InstanceLock
|
||||
|
||||
|
||||
class TestInstanceLock(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.NamedTemporaryFile(delete=False)
|
||||
self.tmp.close()
|
||||
self.lock_path = Path(self.tmp.name)
|
||||
if self.lock_path.exists():
|
||||
self.lock_path.unlink()
|
||||
|
||||
def tearDown(self):
|
||||
if self.lock_path.exists():
|
||||
self.lock_path.unlink()
|
||||
|
||||
def test_acquire_and_release(self):
|
||||
lock = InstanceLock(lock_path=self.lock_path)
|
||||
self.assertTrue(lock.acquire())
|
||||
self.assertTrue(self.lock_path.exists())
|
||||
self.assertEqual(lock.get_holder_pid(), os.getpid())
|
||||
|
||||
lock.release()
|
||||
self.assertFalse(lock.acquired)
|
||||
self.assertFalse(self.lock_path.exists())
|
||||
|
||||
def test_second_instance_blocked(self):
|
||||
lock1 = InstanceLock(lock_path=self.lock_path)
|
||||
self.assertTrue(lock1.acquire())
|
||||
|
||||
lock2 = InstanceLock(lock_path=self.lock_path)
|
||||
self.assertFalse(lock2.acquire())
|
||||
self.assertEqual(lock2.get_holder_pid(), os.getpid())
|
||||
|
||||
lock1.release()
|
||||
|
||||
# Now lock2 can acquire
|
||||
self.assertTrue(lock2.acquire())
|
||||
lock2.release()
|
||||
|
||||
def test_context_manager(self):
|
||||
with InstanceLock(lock_path=self.lock_path) as lock:
|
||||
self.assertTrue(lock.acquired)
|
||||
self.assertEqual(lock.get_holder_pid(), os.getpid())
|
||||
|
||||
# Second lock fails inside context manager
|
||||
lock2 = InstanceLock(lock_path=self.lock_path)
|
||||
self.assertFalse(lock2.acquire())
|
||||
|
||||
# Released after exit
|
||||
self.assertFalse(self.lock_path.exists())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -131,6 +131,73 @@ systemd 255.5-1 -> 255.6-1 [extra]
|
||||
status = manager.is_reboot_required()
|
||||
self.assertTrue(status.required)
|
||||
|
||||
def test_dnf_fedora_auto_install_upgrade_plugin(self):
|
||||
from linux_updater.models import DistroInfo
|
||||
fedora_info = DistroInfo(
|
||||
family=DistroFamily.RHEL,
|
||||
distro_id="fedora",
|
||||
version_id="40",
|
||||
pretty_name="Fedora Linux 40",
|
||||
)
|
||||
manager = DnfManager(runner=self.mock_runner, distro_info=fedora_info)
|
||||
|
||||
# Simulation: 1st check fails, dnf install succeeds, 2nd check succeeds, then dnf system-upgrade download runs
|
||||
self.mock_runner.run.side_effect = [
|
||||
CommandResult(1, "", "unknown command", False), # system-upgrade --help fails
|
||||
CommandResult(0, "Installed python3-dnf-plugins-core", "", True), # install plugin
|
||||
CommandResult(0, "Usage: dnf system-upgrade", "", True), # system-upgrade --help succeeds
|
||||
CommandResult(0, "Download complete", "", True), # dnf system-upgrade download
|
||||
]
|
||||
|
||||
res = manager.release_upgrade(target_release="41", dry_run=False, non_interactive=True)
|
||||
self.assertTrue(res.success)
|
||||
# Verify python3-dnf-plugins-core install was triggered
|
||||
called_cmds = [call[0][0] for call in self.mock_runner.run.call_args_list]
|
||||
self.assertEqual(called_cmds[1], ["dnf", "install", "-y", "python3-dnf-plugins-core"])
|
||||
self.assertIn("--releasever=41", called_cmds[3])
|
||||
|
||||
def test_dnf_almalinux_auto_install_elevate_and_leapp(self):
|
||||
from linux_updater.models import DistroInfo
|
||||
alma_info = DistroInfo(
|
||||
family=DistroFamily.RHEL,
|
||||
distro_id="almalinux",
|
||||
version_id="9.4",
|
||||
pretty_name="AlmaLinux 9.4 (Seafoam Ocelot)",
|
||||
)
|
||||
manager = DnfManager(runner=self.mock_runner, distro_info=alma_info)
|
||||
|
||||
# Simulation: leapp not in PATH, install elevate-release succeeds, install leapp-upgrade succeeds, leapp preupgrade runs
|
||||
with patch("shutil.which") as mock_which:
|
||||
mock_which.side_effect = lambda binary: "dnf" if binary == "dnf" else ("/usr/bin/leapp" if binary == "leapp" and len(self.mock_runner.run.call_args_list) >= 2 else None)
|
||||
self.mock_runner.run.side_effect = [
|
||||
CommandResult(0, "Installed elevate-release", "", True), # elevate-release rpm
|
||||
CommandResult(0, "Installed leapp-upgrade leapp-data-almalinux", "", True), # leapp pkgs
|
||||
CommandResult(0, "REPORT: all checks passed", "", True), # leapp preupgrade
|
||||
]
|
||||
|
||||
res = manager.release_upgrade(dry_run=True)
|
||||
self.assertTrue(res.success)
|
||||
called_cmds = [call[0][0] for call in self.mock_runner.run.call_args_list]
|
||||
self.assertIn("http://repo.almalinux.org/elevate/elevate-release-latest-el9.noarch.rpm", called_cmds[0])
|
||||
self.assertEqual(called_cmds[1], ["dnf", "install", "-y", "leapp-upgrade", "leapp-data-almalinux"])
|
||||
|
||||
def test_dnf_almalinux_elevate_failure_provides_clear_english_troubleshooting(self):
|
||||
from linux_updater.models import DistroInfo
|
||||
alma_info = DistroInfo(
|
||||
family=DistroFamily.RHEL,
|
||||
distro_id="almalinux",
|
||||
version_id="9.4",
|
||||
pretty_name="AlmaLinux 9.4",
|
||||
)
|
||||
manager = DnfManager(runner=self.mock_runner, distro_info=alma_info)
|
||||
|
||||
with patch("shutil.which", return_value=None):
|
||||
self.mock_runner.run.return_value = CommandResult(1, "", "Network is unreachable", False)
|
||||
res = manager.release_upgrade(dry_run=False)
|
||||
self.assertFalse(res.success)
|
||||
self.assertIn("Failed to install ELevate repository package", res.stderr)
|
||||
self.assertIn("Troubleshooting:", res.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user