#!/usr/bin/env python3 # -*- coding: utf-8 -*- """作业 7 · 解析自测。""" from __future__ import annotations import sys import _bootstrap # noqa: F401 from 作业7_self_heal import ( detect_vllm_anomaly, parse_container_status, restart_vllm_container, scan_logs_for_oom, send_recovery_notification, set_simulate_oom, clear_simulate_oom, ) def test_scan_logs() -> None: hits = scan_logs_for_oom("normal line\nCUDA out of memory: tried to allocate") assert hits and "CUDA" in hits[0] def test_parse_status() -> None: st = parse_container_status("/vllm-qwen3.6-27b|true|running|0|") assert st["running"] is True def test_detect_simulate() -> None: set_simulate_oom(True, "CUDA out of memory") got = detect_vllm_anomaly(runner=lambda c: "") assert got["anomaly"] is True and got["kind"] == "OOM" clear_simulate_oom() def test_restart_and_notify_mocks() -> None: def runner(cmd: str) -> str: if "restart" in cmd: return "running" return "/vllm|true|running|0|" rec = restart_vllm_container(runner=runner) assert rec["ok"] is True assert rec["action"] in ("docker restart", "simulate restart") def post(body: str) -> str: return '{"errcode":0,"errmsg":"ok"}' ding = send_recovery_notification("恢复完成", "test", http_post=post) assert ding.get("errcode") == 0 def main() -> int: test_scan_logs() test_parse_status() test_detect_simulate() test_restart_and_notify_mocks() print("[PASS] 作业7 解析自测") return 0 if __name__ == "__main__": sys.exit(main())