207 lines
7.1 KiB
Python
207 lines
7.1 KiB
Python
import os
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
from linux_updater.models import CheckStatus, DistroFamily, DistroInfo, RebootStatus
|
|
from linux_updater.config import UpdaterConfig
|
|
from linux_updater.managers.base import PackageManager
|
|
from linux_updater.core.health import HealthChecker
|
|
|
|
|
|
class DummyManager(PackageManager):
|
|
@property
|
|
def name(self) -> str:
|
|
return "dummy"
|
|
|
|
def refresh_repositories(self):
|
|
pass
|
|
|
|
def check_updates(self):
|
|
return []
|
|
|
|
def apply_updates(self, packages=None, dry_run=False, non_interactive=False):
|
|
pass
|
|
|
|
def release_upgrade(self, target_release=None, dry_run=False, non_interactive=False):
|
|
pass
|
|
|
|
def clean_cache(self):
|
|
pass
|
|
|
|
def autoremove(self):
|
|
pass
|
|
|
|
def is_reboot_required(self) -> RebootStatus:
|
|
return RebootStatus(required=False)
|
|
|
|
def get_lock_files(self):
|
|
return [Path("/tmp/test_dummy.lock")]
|
|
|
|
|
|
class TestHealthChecker(unittest.TestCase):
|
|
def setUp(self):
|
|
self.config = UpdaterConfig(
|
|
min_boot_mb=250,
|
|
min_var_mb=1024,
|
|
min_root_mb=1024,
|
|
min_ram_mb=300,
|
|
)
|
|
self.manager = DummyManager()
|
|
self.checker = HealthChecker(config=self.config, manager=self.manager)
|
|
|
|
@patch("os.statvfs")
|
|
def test_disk_space_sufficient(self, mock_statvfs):
|
|
mock_stat = MagicMock()
|
|
mock_stat.f_bavail = 10 * 1024 * 1024
|
|
mock_stat.f_frsize = 1024
|
|
mock_statvfs.return_value = mock_stat
|
|
|
|
results = self.checker.check_disk_space()
|
|
for r in results:
|
|
self.assertEqual(r.status, CheckStatus.PASS)
|
|
|
|
@patch("os.statvfs")
|
|
def test_disk_space_low_boot(self, mock_statvfs):
|
|
def stat_side_effect(path):
|
|
mock_stat = MagicMock()
|
|
mock_stat.f_frsize = 1024
|
|
if str(path).rstrip("/") == "/boot":
|
|
mock_stat.f_bavail = 100 * 1024 # 100 MB (< 250 MB)
|
|
else:
|
|
mock_stat.f_bavail = 5 * 1024 * 1024 # 5 GB
|
|
return mock_stat
|
|
|
|
mock_statvfs.side_effect = stat_side_effect
|
|
results = self.checker.check_disk_space()
|
|
boot_res = next(r for r in results if r.name == "disk_boot")
|
|
self.assertEqual(boot_res.status, CheckStatus.FAIL)
|
|
self.assertFalse(boot_res.can_remediate)
|
|
|
|
@patch("os.statvfs")
|
|
def test_disk_space_low_var_can_remediate(self, mock_statvfs):
|
|
def stat_side_effect(path):
|
|
mock_stat = MagicMock()
|
|
mock_stat.f_frsize = 1024
|
|
if str(path).rstrip("/") == "/var":
|
|
mock_stat.f_bavail = 500 * 1024 # 500 MB (< 1024 MB)
|
|
else:
|
|
mock_stat.f_bavail = 5 * 1024 * 1024 # 5 GB
|
|
return mock_stat
|
|
|
|
mock_statvfs.side_effect = stat_side_effect
|
|
results = self.checker.check_disk_space()
|
|
var_res = next(r for r in results if r.name == "disk_var")
|
|
self.assertEqual(var_res.status, CheckStatus.FAIL)
|
|
self.assertTrue(var_res.can_remediate)
|
|
self.assertEqual(var_res.remediation_id, "clean_cache")
|
|
|
|
def test_locks_no_lockfiles(self):
|
|
if Path("/tmp/test_dummy.lock").exists():
|
|
Path("/tmp/test_dummy.lock").unlink()
|
|
results = self.checker.check_locks()
|
|
self.assertEqual(len(results), 1)
|
|
self.assertEqual(results[0].status, CheckStatus.PASS)
|
|
|
|
@patch("linux_updater.core.health.HealthChecker._is_pid_alive")
|
|
def test_locks_active_process(self, mock_pid_alive):
|
|
lock_p = Path("/tmp/test_dummy.lock")
|
|
lock_p.write_text("12345\n")
|
|
mock_pid_alive.return_value = True
|
|
try:
|
|
results = self.checker.check_locks()
|
|
self.assertEqual(len(results), 1)
|
|
self.assertEqual(results[0].status, CheckStatus.FAIL)
|
|
self.assertFalse(results[0].can_remediate)
|
|
finally:
|
|
if lock_p.exists():
|
|
lock_p.unlink()
|
|
|
|
@patch("linux_updater.core.health.HealthChecker._is_pid_alive")
|
|
def test_locks_dead_stale_process(self, mock_pid_alive):
|
|
lock_p = Path("/tmp/test_dummy.lock")
|
|
lock_p.write_text("99999\n")
|
|
mock_pid_alive.return_value = False
|
|
try:
|
|
results = self.checker.check_locks()
|
|
self.assertEqual(len(results), 1)
|
|
self.assertEqual(results[0].status, CheckStatus.FAIL)
|
|
self.assertTrue(results[0].can_remediate)
|
|
self.assertEqual(results[0].remediation_id, "remove_stale_lock")
|
|
finally:
|
|
if lock_p.exists():
|
|
lock_p.unlink()
|
|
|
|
def test_locks_advisory_lock_free(self):
|
|
lock_p = Path("/tmp/test_dummy.lock")
|
|
lock_p.touch()
|
|
try:
|
|
results = self.checker.check_locks()
|
|
self.assertEqual(len(results), 1)
|
|
self.assertEqual(results[0].status, CheckStatus.PASS)
|
|
self.assertIn("No lock active", results[0].message)
|
|
finally:
|
|
if lock_p.exists():
|
|
lock_p.unlink()
|
|
|
|
def test_locks_advisory_lock_held(self):
|
|
import fcntl
|
|
lock_p = Path("/tmp/test_dummy.lock")
|
|
lock_p.touch()
|
|
f = open(str(lock_p), "r+")
|
|
fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
try:
|
|
results = self.checker.check_locks()
|
|
self.assertEqual(len(results), 1)
|
|
self.assertEqual(results[0].status, CheckStatus.FAIL)
|
|
self.assertIn("Advisory lock actively held", results[0].message)
|
|
self.assertFalse(results[0].can_remediate)
|
|
finally:
|
|
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
|
|
f.close()
|
|
if lock_p.exists():
|
|
lock_p.unlink()
|
|
|
|
def test_memory_sufficient(self):
|
|
meminfo = """MemTotal: 8147284 kB
|
|
MemFree: 2145124 kB
|
|
MemAvailable: 5123456 kB
|
|
Buffers: 345124 kB
|
|
Cached: 2567890 kB
|
|
"""
|
|
with tempfile.NamedTemporaryFile("w", delete=False) as t:
|
|
t.write(meminfo)
|
|
t_path = Path(t.name)
|
|
|
|
try:
|
|
checker = HealthChecker(config=self.config, manager=self.manager, meminfo_path=t_path)
|
|
results = checker.check_memory_and_oom()
|
|
mem_res = next(r for r in results if r.name == "memory_available")
|
|
self.assertEqual(mem_res.status, CheckStatus.PASS)
|
|
finally:
|
|
if t_path.exists():
|
|
t_path.unlink()
|
|
|
|
def test_memory_low_ram(self):
|
|
meminfo = """MemTotal: 8147284 kB
|
|
MemFree: 100124 kB
|
|
MemAvailable: 153600 kB
|
|
"""
|
|
with tempfile.NamedTemporaryFile("w", delete=False) as t:
|
|
t.write(meminfo)
|
|
t_path = Path(t.name)
|
|
|
|
try:
|
|
checker = HealthChecker(config=self.config, manager=self.manager, meminfo_path=t_path)
|
|
results = checker.check_memory_and_oom()
|
|
mem_res = next(r for r in results if r.name == "memory_available")
|
|
self.assertEqual(mem_res.status, CheckStatus.FAIL)
|
|
finally:
|
|
if t_path.exists():
|
|
t_path.unlink()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|