1212# language governing permissions and limitations under the License.
1313from __future__ import absolute_import
1414
15-
1615from mock import patch
16+ from mock .mock import MagicMock
17+
1718from sagemaker .remote_function .runtime_environment .runtime_environment_manager import (
1819 RuntimeEnvironmentError ,
1920 _DependencySettings ,
@@ -78,14 +79,22 @@ def args_for_step():
7879 "sagemaker.remote_function.runtime_environment.bootstrap_runtime_environment."
7980 "_bootstrap_runtime_env_for_remote_function"
8081)
81- def test_main_success_remote_job (
82+ @patch ("getpass.getuser" , MagicMock (return_value = "root" ))
83+ @patch (
84+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
85+ "RuntimeEnvironmentManager.change_dir_permission"
86+ )
87+ def test_main_success_remote_job_with_root_user (
88+ change_dir_permission ,
8289 bootstrap_remote ,
8390 run_pre_exec_script ,
8491 bootstrap_runtime ,
8592 validate_python ,
8693 _exit_process ,
8794):
8895 bootstrap .main (args_for_remote ())
96+
97+ change_dir_permission .assert_not_called ()
8998 validate_python .assert_called_once_with (TEST_PYTHON_VERSION , TEST_JOB_CONDA_ENV )
9099 bootstrap_remote .assert_called_once_with (
91100 TEST_PYTHON_VERSION ,
@@ -114,14 +123,21 @@ def test_main_success_remote_job(
114123 "sagemaker.remote_function.runtime_environment.bootstrap_runtime_environment."
115124 "_bootstrap_runtime_env_for_pipeline_step"
116125)
117- def test_main_success_pipeline_step (
126+ @patch ("getpass.getuser" , MagicMock (return_value = "root" ))
127+ @patch (
128+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
129+ "RuntimeEnvironmentManager.change_dir_permission"
130+ )
131+ def test_main_success_pipeline_step_with_root_user (
132+ change_dir_permission ,
118133 bootstrap_step ,
119134 run_pre_exec_script ,
120135 bootstrap_runtime ,
121136 validate_python ,
122137 _exit_process ,
123138):
124139 bootstrap .main (args_for_step ())
140+ change_dir_permission .assert_not_called ()
125141 validate_python .assert_called_once_with (TEST_PYTHON_VERSION , TEST_JOB_CONDA_ENV )
126142 bootstrap_step .assert_called_once_with (
127143 TEST_PYTHON_VERSION ,
@@ -150,14 +166,25 @@ def test_main_success_pipeline_step(
150166 "sagemaker.remote_function.runtime_environment.bootstrap_runtime_environment."
151167 "_bootstrap_runtime_env_for_remote_function"
152168)
153- def test_main_failure_remote_job (
154- bootstrap_runtime , run_pre_exec_script , write_failure , _exit_process , validate_python
169+ @patch ("getpass.getuser" , MagicMock (return_value = "root" ))
170+ @patch (
171+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
172+ "RuntimeEnvironmentManager.change_dir_permission"
173+ )
174+ def test_main_failure_remote_job_with_root_user (
175+ change_dir_permission ,
176+ bootstrap_runtime ,
177+ run_pre_exec_script ,
178+ write_failure ,
179+ _exit_process ,
180+ validate_python ,
155181):
156182 runtime_err = RuntimeEnvironmentError ("some failure reason" )
157183 bootstrap_runtime .side_effect = runtime_err
158184
159185 bootstrap .main (args_for_remote ())
160186
187+ change_dir_permission .assert_not_called ()
161188 validate_python .assert_called_once_with (TEST_PYTHON_VERSION , TEST_JOB_CONDA_ENV )
162189 run_pre_exec_script .assert_not_called ()
163190 bootstrap_runtime .assert_called ()
@@ -181,21 +208,125 @@ def test_main_failure_remote_job(
181208 "sagemaker.remote_function.runtime_environment.bootstrap_runtime_environment."
182209 "_bootstrap_runtime_env_for_pipeline_step"
183210)
184- def test_main_failure_pipeline_step (
185- bootstrap_runtime , run_pre_exec_script , write_failure , _exit_process , validate_python
211+ @patch ("getpass.getuser" , MagicMock (return_value = "root" ))
212+ @patch (
213+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
214+ "RuntimeEnvironmentManager.change_dir_permission"
215+ )
216+ def test_main_failure_pipeline_step_with_root_user (
217+ change_dir_permission ,
218+ bootstrap_runtime ,
219+ run_pre_exec_script ,
220+ write_failure ,
221+ _exit_process ,
222+ validate_python ,
186223):
187224 runtime_err = RuntimeEnvironmentError ("some failure reason" )
188225 bootstrap_runtime .side_effect = runtime_err
189226
190227 bootstrap .main (args_for_step ())
191228
229+ change_dir_permission .assert_not_called ()
192230 validate_python .assert_called_once_with (TEST_PYTHON_VERSION , TEST_JOB_CONDA_ENV )
193231 run_pre_exec_script .assert_not_called ()
194232 bootstrap_runtime .assert_called ()
195233 write_failure .assert_called_with (str (runtime_err ))
196234 _exit_process .assert_called_with (1 )
197235
198236
237+ @patch ("sys.exit" )
238+ @patch (
239+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
240+ "RuntimeEnvironmentManager._validate_python_version"
241+ )
242+ @patch (
243+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
244+ "RuntimeEnvironmentManager.bootstrap"
245+ )
246+ @patch (
247+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
248+ "RuntimeEnvironmentManager.run_pre_exec_script"
249+ )
250+ @patch (
251+ "sagemaker.remote_function.runtime_environment.bootstrap_runtime_environment."
252+ "_bootstrap_runtime_env_for_remote_function"
253+ )
254+ @patch ("getpass.getuser" , MagicMock (return_value = "non_root" ))
255+ @patch (
256+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
257+ "RuntimeEnvironmentManager.change_dir_permission"
258+ )
259+ def test_main_remote_job_with_non_root_user (
260+ change_dir_permission ,
261+ bootstrap_remote ,
262+ run_pre_exec_script ,
263+ bootstrap_runtime ,
264+ validate_python ,
265+ _exit_process ,
266+ ):
267+ bootstrap .main (args_for_remote ())
268+
269+ change_dir_permission .assert_called_once_with (
270+ dirs = bootstrap .JOB_OUTPUT_DIRS , new_permission = "777"
271+ )
272+ validate_python .assert_called_once_with (TEST_PYTHON_VERSION , TEST_JOB_CONDA_ENV )
273+ bootstrap_remote .assert_called_once_with (
274+ TEST_PYTHON_VERSION ,
275+ TEST_JOB_CONDA_ENV ,
276+ _DependencySettings (TEST_DEPENDENCY_FILE_NAME ),
277+ )
278+ run_pre_exec_script .assert_not_called ()
279+ bootstrap_runtime .assert_not_called ()
280+ _exit_process .assert_called_with (0 )
281+
282+
283+ @patch ("sys.exit" )
284+ @patch (
285+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
286+ "RuntimeEnvironmentManager._validate_python_version"
287+ )
288+ @patch (
289+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
290+ "RuntimeEnvironmentManager.bootstrap"
291+ )
292+ @patch (
293+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
294+ "RuntimeEnvironmentManager.run_pre_exec_script"
295+ )
296+ @patch (
297+ "sagemaker.remote_function.runtime_environment.bootstrap_runtime_environment."
298+ "_bootstrap_runtime_env_for_pipeline_step"
299+ )
300+ @patch ("getpass.getuser" , MagicMock (return_value = "non_root" ))
301+ @patch (
302+ "sagemaker.remote_function.runtime_environment.runtime_environment_manager."
303+ "RuntimeEnvironmentManager.change_dir_permission"
304+ )
305+ def test_main_pipeline_step_with_non_root_user (
306+ change_dir_permission ,
307+ bootstrap_step ,
308+ run_pre_exec_script ,
309+ bootstrap_runtime ,
310+ validate_python ,
311+ _exit_process ,
312+ ):
313+ bootstrap .main (args_for_step ())
314+
315+ change_dir_permission .assert_called_once_with (
316+ dirs = bootstrap .JOB_OUTPUT_DIRS , new_permission = "777"
317+ )
318+ validate_python .assert_called_once_with (TEST_PYTHON_VERSION , TEST_JOB_CONDA_ENV )
319+ bootstrap_step .assert_called_once_with (
320+ TEST_PYTHON_VERSION ,
321+ FUNC_STEP_WORKSPACE ,
322+ TEST_JOB_CONDA_ENV ,
323+ None ,
324+ )
325+ run_pre_exec_script .assert_not_called ()
326+ bootstrap_runtime .assert_not_called ()
327+ _exit_process .assert_called_with (0 )
328+
329+
199330@patch ("shutil.unpack_archive" )
200331@patch ("os.getcwd" , return_value = CURR_WORKING_DIR )
201332@patch ("os.path.exists" , return_value = True )
0 commit comments