Skip to content

Commit

Permalink
Add tests for the remote debug via SSH command
Browse files Browse the repository at this point in the history
  • Loading branch information
memona008 committed Sep 10, 2024
1 parent c436715 commit 6f0c75f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
68 changes: 68 additions & 0 deletions tests/commands/execution/test_ssh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pathlib
import subprocess
import time

from tests.commands.execution.test_watch import no_sleep
from tests.commands.execution.utils import get_execution_data_mock
from tests.fixture_data import EXECUTION_DATA
from valohai_cli.commands.execution.ssh import ssh


def test_ssh_in_completed_execution(runner, logged_in_and_linked, monkeypatch):
with get_execution_data_mock():
counter = EXECUTION_DATA["counter"]
output = runner.invoke(ssh, [str(counter)], catch_exceptions=False).output
assert f"Error: Execution #{counter} is complete. Cannot SSH into it.\n" in output

EXECUTION_DATA["status"] = "queued"
monkeypatch.setattr(time, "sleep", no_sleep)


def test_ssh_in_queued_execution(runner, logged_in_and_linked, monkeypatch):
counter = EXECUTION_DATA["counter"]
EXECUTION_DATA["status"] = "queued"
monkeypatch.setattr(time, "sleep", no_sleep)
with get_execution_data_mock():
output = runner.invoke(ssh, [str(counter)], catch_exceptions=False).output
assert f"Execution #{counter} is queued. Waiting for it to start...\n" in output


def test_ssh_with_no_ssh_details_present(runner, logged_in_and_linked, monkeypatch):
counter = EXECUTION_DATA["counter"]
EXECUTION_DATA["status"] = "started"
monkeypatch.setattr(time, "sleep", lambda x: None)
monkeypatch.setattr("valohai_cli.utils.ssh.find_ssh_details", lambda x: None)
with get_execution_data_mock():
output = runner.invoke(ssh, [str(counter)], catch_exceptions=False).output
assert "1/5 Retrying: No SSH details found...\n" in output
assert "2/5 Retrying: No SSH details found...\n" in output
assert "3/5 Retrying: No SSH details found...\n" in output
assert "4/5 Retrying: No SSH details found...\n" in output
assert "5/5 Retrying: No SSH details found...\n" in output


def test_ssh(runner, logged_in_and_linked, monkeypatch):
counter = EXECUTION_DATA["counter"]
EXECUTION_DATA["status"] = "started"
monkeypatch.setattr(time, "sleep", lambda x: None)

def mock_prompt():
return pathlib.Path("/path/to/private")

monkeypatch.setattr(
"valohai_cli.commands.execution.ssh.select_private_key_from_possible_directories",
mock_prompt,
)

with get_execution_data_mock():
output = runner.invoke(ssh, [str(counter)], catch_exceptions=False).output
assert "SSH address is 127.0.0.1:2222" in output

def mock_ssh_connection(*args, **kwargs):
print("Mock SSH interactive session executed")
return subprocess.CompletedProcess(args=args, returncode=0, stderr=b"")

monkeypatch.setattr("valohai_cli.commands.execution.ssh.make_ssh_connection", mock_ssh_connection)

output = runner.invoke(ssh, [str(counter)], input="1", catch_exceptions=False).output
assert "Mock SSH interactive session executed" in output
2 changes: 2 additions & 0 deletions tests/commands/execution/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA,
OUTPUT_DATUM_RESPONSE_DATA,
PROJECT_DATA,
STATUS_EVENT_RESPONSE_DATA,
)

API_PREFIX = "https://app.valohai.com/api/v0/"
Expand All @@ -29,6 +30,7 @@ def get_execution_data_mock():
m.get(f"{API_PREFIX}executions/", json={"results": [EXECUTION_DATA]})
m.get(f"{API_PREFIX}executions/{exec_id}/", json=EXECUTION_DATA)
m.get(f"{API_PREFIX}executions/{exec_id}/events/", json=EVENT_RESPONSE_DATA)
m.get(f"{API_PREFIX}executions/{exec_id}/status-events/?limit=100", json=STATUS_EVENT_RESPONSE_DATA)
m.get(f"{API_PREFIX}data/?output_execution={exec_id}&limit=9000", json=OUTPUT_DATUM_RESPONSE_DATA)
m.get(f"{API_PREFIX}data/{datum_id}/download/", json=OUTPUT_DATUM_DOWNLOAD_RESPONSE_DATA)
execution_by_counter_url = f"{API_PREFIX}executions/{project_id}:{execution_counter}/"
Expand Down
17 changes: 16 additions & 1 deletion tests/fixture_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@
path: model.h5
"""


PIPELINE_YAML = (
CONFIG_YAML
+ """
Expand Down Expand Up @@ -665,3 +664,19 @@ def main(config) -> Pipeline:
description: Model output file from TensorFlow
path: model.h5
"""

STATUS_EVENT_RESPONSE_DATA = {
"total": 2,
"status_events": [
{
"stream": "status",
"message": '::ssh::{"port": 2222, "address": "127.0.0.1"}',
"time": "2024-09-04T12:16:20.722000",
},
{
"stream": "status",
"message": " $ ssh -i <path-to-private-key> 127.0.0.1 -p 2222 -t /bin/bash",
"time": "2024-09-04T12:16:20.723000",
},
],
}

0 comments on commit 6f0c75f

Please sign in to comment.