"""GPU 一覧の表示のテスト.
各 GPU の状態・使い方の型と使用率の平均/最大・計算プロセスごとの所有ユーザー(自分は「自分」)・
経過時間・SM 使用率の最大が表示されること.
設定にアドレス・ユーザー名を書くキーは無いため,見出しはサーバ名(ssh の Host 名)だけを出し,
「自分」は probe の `@@labremote-user` で決まる.
"""
from __future__ import annotations
from collections.abc import Sequence
import pytest
from probe_helpers import PMON_HEADER, gpu_line, probe_text, sample_lines
from labremote.config import GpuFreeThreshold, ServerConfig
from labremote.gpu import ServerProbe, parse_probe_output, select_gpu
from labremote.report import format_project_line, format_server_report, format_shared_warning
THRESHOLD = GpuFreeThreshold(
max_memory_used_mib=1024, max_utilization_percent=80, observe_sec=10, sample_interval_ms=500
)
UUID0 = "GPU-0000"
SERVER = ServerConfig(
name="Newton",
priority=1,
remote_dir="~/work/DemoProject",
data_ready=True,
)
def _probe(remote_user: str | None = "carol", server: ServerConfig = SERVER) -> ServerProbe:
text = probe_text(
[gpu_line(0, UUID0, used=500, util=0)],
# 平均 21%・最大 74%(断続的に使用)
samples=sample_lines(0, [0, 74, 10, 0], memory=500),
pmon=[
*PMON_HEADER,
" 0 100 C 74 1 - - - - python",
" 0 200 C 5 1 - - - - python",
],
apps=[f"{UUID0}, 100, 250", f"{UUID0}, 200, 250"],
owners=["100 carol 99720", "200 dave 45"],
remote_user=remote_user,
)
return ServerProbe(server, parse_probe_output(text, THRESHOLD))
def test_report_shows_state_pattern_users_elapsed_and_sm() -> None:
report = format_server_report([_probe()], show_jobs=False)
assert "Newton" in report
assert "空き(利用者あり)" in report
assert "断続的に使用" in report
assert "平均 21%" in report
assert "最大 74%" in report
assert "自分" in report
assert "dave" in report
assert "27.7 時間" in report
assert "SM 最大 74%" in report
def test_report_marks_unreachable_server() -> None:
probe = ServerProbe(SERVER, None, "Connection timed out")
report = format_server_report([probe], show_jobs=True)
assert "Newton" in report
assert "接続不可" in report
def test_shared_selection_warns_with_user_names() -> None:
selection = select_gpu([_probe()])
assert selection is not None
warning = format_shared_warning(selection)
assert warning is not None
assert "dave" in warning
def test_report_without_own_user_does_not_mark_self() -> None:
# 自分の名前が分からない(@@labremote-user が無い)ときは誰も「自分」にしない
report = format_server_report([_probe(None)], show_jobs=False)
assert "自分" not in report
assert "carol" in report
def test_shared_warning_hides_own_user_name() -> None:
selection = select_gpu([_probe()])
assert selection is not None
warning = format_shared_warning(selection)
assert warning is not None
assert "carol" not in warning
def test_report_header_shows_server_name_only() -> None:
# 接続先はサーバ名と同じ.[user@]host の形は出さない
report = format_server_report([_probe()], show_jobs=False)
first = report.splitlines()[0]
assert first.startswith("[Newton] priority=1 ")
assert "@" not in first
assert "None" not in report
# ---------------------------------------------------------------------------
# 表示の言葉(SPEC_03「表示の言葉」「表示する項目」)
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("name", ["DemoProject", "demo-project_2", "日本語の名前"])
def test_project_line_shows_name_as_is_in_one_line(name: str) -> None:
"""SPEC_03「表示の言葉」: gpus・status の先頭に project.name を 1 行で出す."""
line = format_project_line(name)
assert name in line
assert "\n" not in line
assert "\r" not in line
def _probe_with_others(others: list[str], jobs: Sequence[str] = ()) -> ServerProbe:
text = probe_text(
[gpu_line(0, UUID0, used=5, util=0)],
apps=[],
jobs=list(jobs),
others=others,
remote_user="carol",
)
return ServerProbe(SERVER, parse_probe_output(text, THRESHOLD))
@pytest.mark.parametrize("others", [["hidden-session-a"], ["hidden-a", "hidden-b", "hidden-c"]])
def test_status_counts_other_sessions_without_names(others: list[str]) -> None:
"""SPEC_03「表示する項目」: ジョブでない tmux セッションは件数だけを出し,名前を出さない."""
report = format_server_report([_probe_with_others(others, jobs=["train1"])], show_jobs=True)
count_lines = [
line for line in report.splitlines() if "このプロジェクトのジョブではない" in line
]
assert len(count_lines) == 1
assert str(len(others)) in count_lines[0]
assert "件" in count_lines[0]
for name in others:
assert name not in report
# 実行中のジョブの名前は出す
assert "train1" in report
# ツールを指す古い書き方は使わない(SPEC_03「表示の言葉」)
assert "このツール" not in report
def test_status_without_other_sessions_has_no_count_line() -> None:
"""SPEC_03「表示する項目」: ジョブでないセッションが無ければ件数の行を出さない."""
report = format_server_report([_probe_with_others([], jobs=["train1"])], show_jobs=True)
assert "このプロジェクトのジョブではない" not in report
assert "tmux セッション" not in report
def test_gpus_does_not_show_session_count_line() -> None:
"""SPEC_03「表示する項目」: 件数の行は status のもの(gpus はジョブを出さない)."""
report = format_server_report([_probe_with_others(["hidden-a"])], show_jobs=False)
assert "hidden-a" not in report
assert "このプロジェクトのジョブではない" not in report