Assistant: codex Assistant-Model: gpt-6.1-sol Assistant-Session: 01a1b4e3-7aa4-75b2-b432-114153ff6879
57 lines
2.4 KiB
Python
57 lines
2.4 KiB
Python
"""Regression scenarios for host exhaustion hidden by a roomy WSL filesystem."""
|
|
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import shutil
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
SPEC = importlib.util.spec_from_file_location(
|
|
"workstation_disk_guard", Path(__file__).parents[1] / "scripts/workstation_disk_guard.py")
|
|
guard = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(guard)
|
|
|
|
|
|
class DiskGuardTests(unittest.TestCase):
|
|
def report(self, host_free, guest_free=783, reserve=0):
|
|
usage_type = type(shutil.disk_usage("/"))
|
|
def usage(path):
|
|
free = (host_free if path == "/mnt/c" else guest_free) * guard.GIB
|
|
return usage_type(1024 * guard.GIB, 1024 * guard.GIB - free, free)
|
|
with patch.object(guard.Path, "is_mount", return_value=True), \
|
|
patch.object(guard.shutil, "disk_usage", side_effect=usage):
|
|
return guard.collect(reserve * guard.GIB)
|
|
|
|
def test_crash_capacity_is_critical_despite_roomy_guest(self):
|
|
report = self.report(1)
|
|
self.assertEqual(report["status"], "critical")
|
|
self.assertEqual(report["volumes"][1]["status"], "ok")
|
|
|
|
def test_reclaimed_capacity_warns_and_projected_build_can_refuse(self):
|
|
self.assertEqual(self.report(53)["status"], "warning")
|
|
self.assertEqual(self.report(53, reserve=25)["status"], "critical")
|
|
|
|
def test_guest_exhaustion_is_also_critical(self):
|
|
self.assertEqual(self.report(100, guest_free=5)["status"], "critical")
|
|
|
|
def test_thresholds_and_healthy_capacity(self):
|
|
self.assertEqual(self.report(30)["status"], "warning")
|
|
self.assertEqual(self.report(60)["status"], "ok")
|
|
|
|
def test_unmounted_host_cannot_silently_use_guest_capacity(self):
|
|
with patch.object(guard.Path, "is_mount", return_value=False):
|
|
self.assertEqual(guard.collect()["status"], "unknown")
|
|
|
|
def test_unreadable_capacity_is_unknown(self):
|
|
with patch.object(guard.Path, "is_mount", return_value=True), \
|
|
patch.object(guard.shutil, "disk_usage", side_effect=OSError):
|
|
self.assertEqual(guard.collect()["status"], "unknown")
|
|
|
|
def test_bad_reserve_inputs_are_rejected(self):
|
|
for value in ("nan", "inf", "-1"):
|
|
with self.subTest(value=value), self.assertRaises(guard.argparse.ArgumentTypeError):
|
|
guard.nonnegative_gib(value)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|