12
12
# language governing permissions and limitations under the License.
13
13
from __future__ import absolute_import
14
14
15
-
16
15
from mock import patch
16
+ from mock .mock import MagicMock
17
+
17
18
from sagemaker .remote_function .runtime_environment .runtime_environment_manager import (
18
19
RuntimeEnvironmentError ,
19
20
_DependencySettings ,
@@ -78,14 +79,22 @@ def args_for_step():
78
79
"sagemaker.remote_function.runtime_environment.bootstrap_runtime_environment."
79
80
"_bootstrap_runtime_env_for_remote_function"
80
81
)
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 ,
82
89
bootstrap_remote ,
83
90
run_pre_exec_script ,
84
91
bootstrap_runtime ,
85
92
validate_python ,
86
93
_exit_process ,
87
94
):
88
95
bootstrap .main (args_for_remote ())
96
+
97
+ change_dir_permission .assert_not_called ()
89
98
validate_python .assert_called_once_with (TEST_PYTHON_VERSION , TEST_JOB_CONDA_ENV )
90
99
bootstrap_remote .assert_called_once_with (
91
100
TEST_PYTHON_VERSION ,
@@ -114,14 +123,21 @@ def test_main_success_remote_job(
114
123
"sagemaker.remote_function.runtime_environment.bootstrap_runtime_environment."
115
124
"_bootstrap_runtime_env_for_pipeline_step"
116
125
)
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 ,
118
133
bootstrap_step ,
119
134
run_pre_exec_script ,
120
135
bootstrap_runtime ,
121
136
validate_python ,
122
137
_exit_process ,
123
138
):
124
139
bootstrap .main (args_for_step ())
140
+ change_dir_permission .assert_not_called ()
125
141
validate_python .assert_called_once_with (TEST_PYTHON_VERSION , TEST_JOB_CONDA_ENV )
126
142
bootstrap_step .assert_called_once_with (
127
143
TEST_PYTHON_VERSION ,
@@ -150,14 +166,25 @@ def test_main_success_pipeline_step(
150
166
"sagemaker.remote_function.runtime_environment.bootstrap_runtime_environment."
151
167
"_bootstrap_runtime_env_for_remote_function"
152
168
)
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 ,
155
181
):
156
182
runtime_err = RuntimeEnvironmentError ("some failure reason" )
157
183
bootstrap_runtime .side_effect = runtime_err
158
184
159
185
bootstrap .main (args_for_remote ())
160
186
187
+ change_dir_permission .assert_not_called ()
161
188
validate_python .assert_called_once_with (TEST_PYTHON_VERSION , TEST_JOB_CONDA_ENV )
162
189
run_pre_exec_script .assert_not_called ()
163
190
bootstrap_runtime .assert_called ()
@@ -181,21 +208,125 @@ def test_main_failure_remote_job(
181
208
"sagemaker.remote_function.runtime_environment.bootstrap_runtime_environment."
182
209
"_bootstrap_runtime_env_for_pipeline_step"
183
210
)
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 ,
186
223
):
187
224
runtime_err = RuntimeEnvironmentError ("some failure reason" )
188
225
bootstrap_runtime .side_effect = runtime_err
189
226
190
227
bootstrap .main (args_for_step ())
191
228
229
+ change_dir_permission .assert_not_called ()
192
230
validate_python .assert_called_once_with (TEST_PYTHON_VERSION , TEST_JOB_CONDA_ENV )
193
231
run_pre_exec_script .assert_not_called ()
194
232
bootstrap_runtime .assert_called ()
195
233
write_failure .assert_called_with (str (runtime_err ))
196
234
_exit_process .assert_called_with (1 )
197
235
198
236
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
+
199
330
@patch ("shutil.unpack_archive" )
200
331
@patch ("os.getcwd" , return_value = CURR_WORKING_DIR )
201
332
@patch ("os.path.exists" , return_value = True )
0 commit comments