|
| 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 | +"""Unit tests for _run_clone_command_silent in sagemaker.modules.utils.""" |
| 14 | +from __future__ import absolute_import |
| 15 | + |
| 16 | +import logging |
| 17 | +import subprocess |
| 18 | +from unittest.mock import patch |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from sagemaker.modules.utils import _run_clone_command_silent |
| 23 | + |
| 24 | +MODULE = "sagemaker.modules.utils" |
| 25 | +DEST = "/tmp/dest" |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize( |
| 29 | + "repo_url,userinfo", |
| 30 | + [ |
| 31 | + ("https://oauth2:abc123@example.com/org/repo.git", "oauth2:abc123"), |
| 32 | + ("https://user:p%40ss@example.com/org/repo.git", "user:p%40ss"), |
| 33 | + ("https://user:p@ss@example.com/org/repo.git", "user:p@ss"), |
| 34 | + ], |
| 35 | +) |
| 36 | +def test_clone_failure_uses_sanitized_url(repo_url, userinfo, caplog): |
| 37 | + with patch( |
| 38 | + f"{MODULE}.subprocess.check_call", |
| 39 | + side_effect=subprocess.CalledProcessError(128, ["git", "clone", repo_url, DEST]), |
| 40 | + ): |
| 41 | + with caplog.at_level(logging.ERROR): |
| 42 | + with pytest.raises(subprocess.CalledProcessError) as exc_info: |
| 43 | + _run_clone_command_silent(repo_url, DEST) |
| 44 | + |
| 45 | + error = exc_info.value |
| 46 | + expected_url = "https://<credentials-redacted>@example.com/org/repo.git" |
| 47 | + assert error.returncode == 128 |
| 48 | + assert error.cmd == ["git", "clone", expected_url, DEST] |
| 49 | + assert error.__suppress_context__ |
| 50 | + assert userinfo not in str(error) |
| 51 | + assert userinfo not in caplog.text |
| 52 | + assert expected_url in caplog.text |
| 53 | + |
| 54 | + |
| 55 | +def test_clone_failure_without_userinfo_keeps_url(caplog): |
| 56 | + repo_url = "https://example.com/org/repo.git" |
| 57 | + with patch( |
| 58 | + f"{MODULE}.subprocess.check_call", |
| 59 | + side_effect=subprocess.CalledProcessError(128, ["git", "clone", repo_url, DEST]), |
| 60 | + ): |
| 61 | + with caplog.at_level(logging.ERROR): |
| 62 | + with pytest.raises(subprocess.CalledProcessError) as exc_info: |
| 63 | + _run_clone_command_silent(repo_url, DEST) |
| 64 | + |
| 65 | + assert exc_info.value.cmd == ["git", "clone", repo_url, DEST] |
| 66 | + assert repo_url in caplog.text |
| 67 | + |
| 68 | + |
| 69 | +def test_clone_success_uses_original_url(): |
| 70 | + repo_url = "https://oauth2:abc123@example.com/org/repo.git" |
| 71 | + with patch(f"{MODULE}.subprocess.check_call") as mock_check_call: |
| 72 | + _run_clone_command_silent(repo_url, DEST) |
| 73 | + |
| 74 | + args, kwargs = mock_check_call.call_args |
| 75 | + assert args[0] == ["git", "clone", repo_url, DEST] |
| 76 | + assert kwargs["env"]["GIT_TERMINAL_PROMPT"] == "0" |
0 commit comments