@@ -2209,3 +2209,43 @@ def test_nested_set_dict_multiple_keys(self):
22092209 d = {}
22102210 nested_set_dict (d , ["a" , "b" , "c" ], "value" )
22112211 assert d ["a" ]["b" ]["c" ] == "value"
2212+
2213+
2214+ class TestCreateOrUpdateCodeDir :
2215+ """Test _create_or_update_code_dir function."""
2216+
2217+ def test_create_or_update_code_dir_rejects_root (self , tmp_path ):
2218+ """Test that root path is rejected."""
2219+ from sagemaker .core .common_utils import _create_or_update_code_dir
2220+
2221+ with patch ("sagemaker.core.common_utils._get_resolved_path" , return_value = "/" ):
2222+ with pytest .raises (ValueError , match = "sensitive system root" ):
2223+ _create_or_update_code_dir ("/" , "script.py" , None , [], Mock (), str (tmp_path ))
2224+
2225+ def test_create_or_update_code_dir_rejects_etc (self , tmp_path ):
2226+ """Test that /etc path is rejected."""
2227+ from sagemaker .core .common_utils import _create_or_update_code_dir
2228+
2229+ with patch ("sagemaker.core.common_utils._get_resolved_path" , return_value = "/etc" ):
2230+ with pytest .raises (ValueError , match = "sensitive system root" ):
2231+ _create_or_update_code_dir ("/etc" , "script.py" , None , [], Mock (), str (tmp_path ))
2232+
2233+ def test_create_or_update_code_dir_rejects_var (self , tmp_path ):
2234+ """Test that /var path is rejected."""
2235+ from sagemaker .core .common_utils import _create_or_update_code_dir
2236+
2237+ with patch ("sagemaker.core.common_utils._get_resolved_path" , return_value = "/var" ):
2238+ with pytest .raises (ValueError , match = "sensitive system root" ):
2239+ _create_or_update_code_dir ("/var" , "script.py" , None , [], Mock (), str (tmp_path ))
2240+
2241+ def test_create_or_update_code_dir_allows_safe_path (self , tmp_path ):
2242+ """Test that safe paths are allowed."""
2243+ from sagemaker .core .common_utils import _create_or_update_code_dir
2244+
2245+ model_dir = tmp_path / "model"
2246+ model_dir .mkdir ()
2247+ script = tmp_path / "script.py"
2248+ script .write_text ("# script" )
2249+
2250+ _create_or_update_code_dir (str (model_dir ), str (script ), None , [], Mock (), str (tmp_path ))
2251+ assert (model_dir / "code" ).exists ()
0 commit comments