|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""MPI Utils Unit Tests.""" |
| 14 | +from __future__ import absolute_import |
| 15 | + |
| 16 | +import subprocess |
| 17 | +from unittest.mock import Mock, patch |
| 18 | + |
| 19 | +import paramiko |
| 20 | +import pytest |
| 21 | + |
| 22 | +# Mock the utils module before importing mpi_utils |
| 23 | +mock_utils = Mock() |
| 24 | +mock_utils.logger = Mock() |
| 25 | +mock_utils.SM_EFA_NCCL_INSTANCES = [] |
| 26 | +mock_utils.SM_EFA_RDMA_INSTANCES = [] |
| 27 | +mock_utils.get_python_executable = Mock(return_value="/usr/bin/python") |
| 28 | + |
| 29 | +with patch.dict("sys.modules", {"utils": mock_utils}): |
| 30 | + from sagemaker.modules.train.container_drivers.mpi_utils import ( |
| 31 | + CustomHostKeyPolicy, |
| 32 | + _can_connect, |
| 33 | + write_status_file_to_workers, |
| 34 | + ) |
| 35 | + |
| 36 | +TEST_HOST = "algo-1" |
| 37 | +TEST_WORKER = "algo-2" |
| 38 | +TEST_STATUS_FILE = "/tmp/test-status" |
| 39 | + |
| 40 | + |
| 41 | +def test_custom_host_key_policy_valid_hostname(): |
| 42 | + """Test CustomHostKeyPolicy accepts algo- prefixed hostnames.""" |
| 43 | + policy = CustomHostKeyPolicy() |
| 44 | + mock_client = Mock() |
| 45 | + mock_key = Mock() |
| 46 | + mock_key.get_name.return_value = "ssh-rsa" |
| 47 | + |
| 48 | + policy.missing_host_key(mock_client, "algo-1", mock_key) |
| 49 | + |
| 50 | + mock_client.get_host_keys.assert_called_once() |
| 51 | + mock_client.get_host_keys().add.assert_called_once_with("algo-1", "ssh-rsa", mock_key) |
| 52 | + |
| 53 | + |
| 54 | +def test_custom_host_key_policy_invalid_hostname(): |
| 55 | + """Test CustomHostKeyPolicy rejects non-algo prefixed hostnames.""" |
| 56 | + policy = CustomHostKeyPolicy() |
| 57 | + mock_client = Mock() |
| 58 | + mock_key = Mock() |
| 59 | + |
| 60 | + with pytest.raises(paramiko.SSHException) as exc_info: |
| 61 | + policy.missing_host_key(mock_client, "invalid-1", mock_key) |
| 62 | + |
| 63 | + assert "Unknown host key for invalid-1" in str(exc_info.value) |
| 64 | + mock_client.get_host_keys.assert_not_called() |
| 65 | + |
| 66 | + |
| 67 | +@patch("paramiko.SSHClient") |
| 68 | +@patch("sagemaker.modules.train.container_drivers.mpi_utils.logger") |
| 69 | +def test_can_connect_success(mock_logger, mock_ssh_client): |
| 70 | + """Test successful SSH connection.""" |
| 71 | + mock_client = Mock() |
| 72 | + mock_ssh_client.return_value.__enter__.return_value = mock_client |
| 73 | + mock_client.connect.return_value = None # Successful connection |
| 74 | + |
| 75 | + result = _can_connect(TEST_HOST) |
| 76 | + |
| 77 | + assert result is True |
| 78 | + mock_client.load_system_host_keys.assert_called_once() |
| 79 | + mock_client.set_missing_host_key_policy.assert_called_once() |
| 80 | + mock_client.connect.assert_called_once_with(TEST_HOST, port=22) |
| 81 | + |
| 82 | + |
| 83 | +@patch("paramiko.SSHClient") |
| 84 | +@patch("sagemaker.modules.train.container_drivers.mpi_utils.logger") |
| 85 | +def test_can_connect_failure(mock_logger, mock_ssh_client): |
| 86 | + """Test SSH connection failure.""" |
| 87 | + mock_client = Mock() |
| 88 | + mock_ssh_client.return_value.__enter__.return_value = mock_client |
| 89 | + mock_client.connect.side_effect = paramiko.SSHException("Connection failed") |
| 90 | + |
| 91 | + result = _can_connect(TEST_HOST) |
| 92 | + |
| 93 | + assert result is False |
| 94 | + mock_client.load_system_host_keys.assert_called_once() |
| 95 | + mock_client.set_missing_host_key_policy.assert_called_once() |
| 96 | + mock_client.connect.assert_called_once_with(TEST_HOST, port=22) |
| 97 | + |
| 98 | + |
| 99 | +@patch("subprocess.run") |
| 100 | +@patch("sagemaker.modules.train.container_drivers.mpi_utils.logger") |
| 101 | +def test_write_status_file_to_workers_failure(mock_logger, mock_run): |
| 102 | + """Test failed status file writing to workers with retry timeout.""" |
| 103 | + mock_run.side_effect = subprocess.CalledProcessError(1, "ssh") |
| 104 | + |
| 105 | + with pytest.raises(TimeoutError) as exc_info: |
| 106 | + write_status_file_to_workers([TEST_WORKER], TEST_STATUS_FILE) |
| 107 | + |
| 108 | + assert f"Timed out waiting for {TEST_WORKER}" in str(exc_info.value) |
| 109 | + assert mock_run.call_count > 1 # Verifies that retries occurred |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + pytest.main([__file__]) |
0 commit comments