"examples/benchmarks/computation_communication_overlap.py" did not exist on "923ce2773f5eaa7436dff19a6671e2f1ed5aa657"
test_ssh.py 3.86 KB
Newer Older
one's avatar
one committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Tests for hytop.core.ssh.collect_from_host (subprocess mocked)."""

from __future__ import annotations

import subprocess
from unittest.mock import MagicMock, patch

from hytop.core.ssh import collect_from_host


def _make_proc(returncode=0, stdout="", stderr=""):
    m = MagicMock()
    m.returncode = returncode
    m.stdout = stdout
    m.stderr = stderr
    return m


class TestCollectFromHostLocal:
    @patch("hytop.core.ssh.subprocess.run")
    def test_success_returns_no_error(self, mock_run):
        mock_run.return_value = _make_proc(stdout='{"card0":{}}')
        result = collect_from_host(
            "localhost", ssh_timeout=5, cmd_timeout=10, hy_smi_args=["--json"]
        )
        assert result.error is None
        assert result.host == "localhost"

    @patch("hytop.core.ssh.subprocess.run")
    def test_local_invokes_hy_smi_directly(self, mock_run):
        mock_run.return_value = _make_proc()
        collect_from_host("localhost", ssh_timeout=5, cmd_timeout=10, hy_smi_args=["--json"])
        cmd = mock_run.call_args[0][0]
        assert cmd[0] == "hy-smi"
        assert "ssh" not in cmd

    @patch("hytop.core.ssh.subprocess.run")
    def test_127_0_0_1_treated_as_local(self, mock_run):
        mock_run.return_value = _make_proc()
        collect_from_host("127.0.0.1", ssh_timeout=5, cmd_timeout=10, hy_smi_args=["--json"])
        cmd = mock_run.call_args[0][0]
        assert cmd[0] == "hy-smi"

    @patch("hytop.core.ssh.subprocess.run")
    def test_nonzero_exit_returns_error(self, mock_run):
        mock_run.return_value = _make_proc(returncode=1, stderr="permission denied")
        result = collect_from_host(
            "localhost", ssh_timeout=5, cmd_timeout=10, hy_smi_args=["--json"]
        )
        assert result.error is not None
        assert "exit 1" in result.error

    @patch(
        "hytop.core.ssh.subprocess.run",
        side_effect=subprocess.TimeoutExpired("cmd", 10),
    )
    def test_timeout_returns_error(self, mock_run):
        result = collect_from_host(
            "localhost", ssh_timeout=5, cmd_timeout=10, hy_smi_args=["--json"]
        )
        assert result.error is not None
        assert "timeout" in result.error

    @patch("hytop.core.ssh.subprocess.run", side_effect=OSError("no such file"))
    def test_oserror_returns_error(self, mock_run):
        result = collect_from_host(
            "localhost", ssh_timeout=5, cmd_timeout=10, hy_smi_args=["--json"]
        )
        assert result.error is not None
        assert "no such file" in result.error


class TestCollectFromHostRemote:
    @patch("hytop.core.ssh.subprocess.run")
    def test_remote_uses_ssh(self, mock_run):
        mock_run.return_value = _make_proc(stdout="{}")
        collect_from_host("node01", ssh_timeout=5, cmd_timeout=10, hy_smi_args=["--json"])
        cmd = mock_run.call_args[0][0]
        assert cmd[0] == "ssh"

    @patch("hytop.core.ssh.subprocess.run")
    def test_remote_hostname_in_cmd(self, mock_run):
        mock_run.return_value = _make_proc(stdout="{}")
        collect_from_host("node01", ssh_timeout=5, cmd_timeout=10, hy_smi_args=["--json"])
        cmd = mock_run.call_args[0][0]
        assert "node01" in cmd

    @patch("hytop.core.ssh.subprocess.run")
    def test_remote_batch_mode_set(self, mock_run):
        mock_run.return_value = _make_proc(stdout="{}")
        collect_from_host("node01", ssh_timeout=5, cmd_timeout=10, hy_smi_args=["--json"])
        cmd = mock_run.call_args[0][0]
        assert "BatchMode=yes" in cmd

    @patch("hytop.core.ssh.subprocess.run")
    def test_hy_smi_args_forwarded(self, mock_run):
        mock_run.return_value = _make_proc(stdout="{}")
        collect_from_host(
            "node01",
            ssh_timeout=5,
            cmd_timeout=10,
            hy_smi_args=["--json", "--showtemp"],
        )
        cmd = mock_run.call_args[0][0]
        assert "--json" in cmd
        assert "--showtemp" in cmd