|
12 | 12 | # language governing permissions and limitations under the License.
|
13 | 13 | from __future__ import absolute_import
|
14 | 14 |
|
| 15 | +import os |
15 | 16 | import pytest
|
16 | 17 | from mock import patch, Mock
|
17 | 18 |
|
18 | 19 | import sagemaker.local.utils
|
19 | 20 |
|
20 | 21 |
|
| 22 | +@patch("sagemaker.local.utils.os.path") |
| 23 | +@patch("sagemaker.local.utils.os") |
| 24 | +def test_copy_directory_structure(m_os, m_os_path): |
| 25 | + m_os_path.exists.return_value = False |
| 26 | + sagemaker.local.utils.copy_directory_structure("/tmp/", "code/") |
| 27 | + m_os.makedirs.assert_called_with("/tmp/", "code/") |
| 28 | + |
| 29 | + |
21 | 30 | @patch("shutil.rmtree", Mock())
|
22 | 31 | @patch("sagemaker.local.utils.recursive_copy")
|
23 | 32 | def test_move_to_destination_local(recursive_copy):
|
24 | 33 | # local files will just be recursively copied
|
25 |
| - sagemaker.local.utils.move_to_destination("/tmp/data", "file:///target/dir/", "job", None) |
26 |
| - recursive_copy.assert_called_with("/tmp/data", "/target/dir/") |
| 34 | + # given absolute path |
| 35 | + sagemaker.local.utils.move_to_destination("/tmp/data", "file:///target/dir", "job", None) |
| 36 | + recursive_copy.assert_called_with("/tmp/data", "/target/dir") |
| 37 | + # given relative path |
| 38 | + sagemaker.local.utils.move_to_destination("/tmp/data", "file://root/target/dir", "job", None) |
| 39 | + recursive_copy.assert_called_with("/tmp/data", os.path.abspath("./root/target/dir")) |
27 | 40 |
|
28 | 41 |
|
29 | 42 | @patch("shutil.rmtree", Mock())
|
@@ -52,3 +65,30 @@ def test_move_to_destination_s3(recursive_copy):
|
52 | 65 | def test_move_to_destination_illegal_destination():
|
53 | 66 | with pytest.raises(ValueError):
|
54 | 67 | sagemaker.local.utils.move_to_destination("/tmp/data", "ftp://ftp/in/2018", "job", None)
|
| 68 | + |
| 69 | + |
| 70 | +@patch("sagemaker.local.utils.os.path") |
| 71 | +@patch("sagemaker.local.utils.copy_tree") |
| 72 | +def test_recursive_copy(copy_tree, m_os_path): |
| 73 | + m_os_path.isdir.return_value = True |
| 74 | + sagemaker.local.utils.recursive_copy("source", "destination") |
| 75 | + copy_tree.assert_called_with("source", "destination") |
| 76 | + |
| 77 | + |
| 78 | +@patch("sagemaker.local.utils.os") |
| 79 | +@patch("sagemaker.local.utils.get_child_process_ids") |
| 80 | +def test_kill_child_processes(m_get_child_process_ids, m_os): |
| 81 | + m_get_child_process_ids.return_value = ["child_pids"] |
| 82 | + sagemaker.local.utils.kill_child_processes("pid") |
| 83 | + m_os.kill.assert_called_with("child_pids", 15) |
| 84 | + |
| 85 | + |
| 86 | +@patch("sagemaker.local.utils.subprocess") |
| 87 | +def test_get_child_process_ids(m_subprocess): |
| 88 | + cmd = "pgrep -P pid".split() |
| 89 | + process_mock = Mock() |
| 90 | + attrs = {"communicate.return_value": (b"\n", False), "returncode": 0} |
| 91 | + process_mock.configure_mock(**attrs) |
| 92 | + m_subprocess.Popen.return_value = process_mock |
| 93 | + sagemaker.local.utils.get_child_process_ids("pid") |
| 94 | + m_subprocess.Popen.assert_called_with(cmd, stdout=m_subprocess.PIPE, stderr=m_subprocess.PIPE) |
0 commit comments