- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.2k
Fix ssh host policy #4966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Fix ssh host policy #4966
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            22 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      7dbddbf
              
                Fix ssh host policy
              
              
                sage-maker 19cb25d
              
                Merge branch 'master' into fix-ssh-policy
              
              
                sage-maker 003de0a
              
                Merge branch 'master' into fix-ssh-policy
              
              
                sage-maker dbf8759
              
                Merge branch 'master' into fix-ssh-policy
              
              
                sage-maker c44cae6
              
                Filter policy by algo-
              
              
                sage-maker dac79c7
              
                Add docstring
              
              
                sage-maker 77b39b3
              
                Merge branch 'master' into fix-ssh-policy
              
              
                sage-maker f32b529
              
                Fix pylint
              
              
                sage-maker c762139
              
                Fix docstyle summary
              
              
                sage-maker 0d33f36
              
                Unit test
              
              
                sage-maker ced988f
              
                Fix unit test
              
              
                sage-maker bc70321
              
                Change to unit test
              
              
                sage-maker fb706ee
              
                Fix unit tests
              
              
                sage-maker ec3dbb6
              
                Test comment out flaky tests
              
              
                sage-maker bca605e
              
                Merge branch 'master' into fix-ssh-policy
              
              
                sage-maker a3314eb
              
                Merge branch 'master' into fix-ssh-policy
              
              
                sage-maker fbf0b9b
              
                Merge branch 'master' into fix-ssh-policy
              
              
                sage-maker 5b19a68
              
                Readd the flaky tests
              
              
                sage-maker 1444c87
              
                Merge branch 'master' into fix-ssh-policy
              
              
                sage-maker 351be13
              
                Merge branch 'master' into fix-ssh-policy
              
              
                benieric 4fd9f8c
              
                Remove flaky asserts
              
              
                sage-maker ce046d4
              
                Remove flaky asserts
              
              
                sage-maker File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            113 changes: 113 additions & 0 deletions
          
          113 
        
  tests/unit/sagemaker/modules/train/container_drivers/test_mpi_utils.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"). You | ||
| # may not use this file except in compliance with the License. A copy of | ||
| # the License is located at | ||
| # | ||
| # http://aws.amazon.com/apache2.0/ | ||
| # | ||
| # or in the "license" file accompanying this file. This file is | ||
| # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
| # ANY KIND, either express or implied. See the License for the specific | ||
| # language governing permissions and limitations under the License. | ||
| """MPI Utils Unit Tests.""" | ||
| from __future__ import absolute_import | ||
|  | ||
| import subprocess | ||
| from unittest.mock import Mock, patch | ||
|  | ||
| import paramiko | ||
| import pytest | ||
|  | ||
| # Mock the utils module before importing mpi_utils | ||
| mock_utils = Mock() | ||
| mock_utils.logger = Mock() | ||
| mock_utils.SM_EFA_NCCL_INSTANCES = [] | ||
| mock_utils.SM_EFA_RDMA_INSTANCES = [] | ||
| mock_utils.get_python_executable = Mock(return_value="/usr/bin/python") | ||
|  | ||
| with patch.dict("sys.modules", {"utils": mock_utils}): | ||
| from sagemaker.modules.train.container_drivers.mpi_utils import ( | ||
| CustomHostKeyPolicy, | ||
| _can_connect, | ||
| write_status_file_to_workers, | ||
| ) | ||
|  | ||
| TEST_HOST = "algo-1" | ||
| TEST_WORKER = "algo-2" | ||
| TEST_STATUS_FILE = "/tmp/test-status" | ||
|  | ||
|  | ||
| def test_custom_host_key_policy_valid_hostname(): | ||
| """Test CustomHostKeyPolicy accepts algo- prefixed hostnames.""" | ||
| policy = CustomHostKeyPolicy() | ||
| mock_client = Mock() | ||
| mock_key = Mock() | ||
| mock_key.get_name.return_value = "ssh-rsa" | ||
|  | ||
| policy.missing_host_key(mock_client, "algo-1", mock_key) | ||
|  | ||
| mock_client.get_host_keys.assert_called_once() | ||
| mock_client.get_host_keys().add.assert_called_once_with("algo-1", "ssh-rsa", mock_key) | ||
|  | ||
|  | ||
| def test_custom_host_key_policy_invalid_hostname(): | ||
| """Test CustomHostKeyPolicy rejects non-algo prefixed hostnames.""" | ||
| policy = CustomHostKeyPolicy() | ||
| mock_client = Mock() | ||
| mock_key = Mock() | ||
|  | ||
| with pytest.raises(paramiko.SSHException) as exc_info: | ||
| policy.missing_host_key(mock_client, "invalid-1", mock_key) | ||
|  | ||
| assert "Unknown host key for invalid-1" in str(exc_info.value) | ||
| mock_client.get_host_keys.assert_not_called() | ||
|  | ||
|  | ||
| @patch("paramiko.SSHClient") | ||
| @patch("sagemaker.modules.train.container_drivers.mpi_utils.logger") | ||
| def test_can_connect_success(mock_logger, mock_ssh_client): | ||
| """Test successful SSH connection.""" | ||
| mock_client = Mock() | ||
| mock_ssh_client.return_value.__enter__.return_value = mock_client | ||
| mock_client.connect.return_value = None # Successful connection | ||
|  | ||
| result = _can_connect(TEST_HOST) | ||
|  | ||
| assert result is True | ||
| mock_client.load_system_host_keys.assert_called_once() | ||
| mock_client.set_missing_host_key_policy.assert_called_once() | ||
| mock_client.connect.assert_called_once_with(TEST_HOST, port=22) | ||
|  | ||
|  | ||
| @patch("paramiko.SSHClient") | ||
| @patch("sagemaker.modules.train.container_drivers.mpi_utils.logger") | ||
| def test_can_connect_failure(mock_logger, mock_ssh_client): | ||
| """Test SSH connection failure.""" | ||
| mock_client = Mock() | ||
| mock_ssh_client.return_value.__enter__.return_value = mock_client | ||
| mock_client.connect.side_effect = paramiko.SSHException("Connection failed") | ||
|  | ||
| result = _can_connect(TEST_HOST) | ||
|  | ||
| assert result is False | ||
| mock_client.load_system_host_keys.assert_called_once() | ||
| mock_client.set_missing_host_key_policy.assert_called_once() | ||
| mock_client.connect.assert_called_once_with(TEST_HOST, port=22) | ||
|  | ||
|  | ||
| @patch("subprocess.run") | ||
| @patch("sagemaker.modules.train.container_drivers.mpi_utils.logger") | ||
| def test_write_status_file_to_workers_failure(mock_logger, mock_run): | ||
| """Test failed status file writing to workers with retry timeout.""" | ||
| mock_run.side_effect = subprocess.CalledProcessError(1, "ssh") | ||
|  | ||
| with pytest.raises(TimeoutError) as exc_info: | ||
| write_status_file_to_workers([TEST_WORKER], TEST_STATUS_FILE) | ||
|  | ||
| assert f"Timed out waiting for {TEST_WORKER}" in str(exc_info.value) | ||
| assert mock_run.call_count > 1 # Verifies that retries occurred | ||
|  | ||
|  | ||
| if __name__ == "__main__": | ||
| pytest.main([__file__]) | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice