@@ -988,7 +988,7 @@ def test_pvc_subpath_binds_resolved_to_mountpoint(self, mock_docker):
988988 assert binds [0 ] == "/var/lib/docker/volumes/my-vol/_data/datasets/train:/mnt/train:ro"
989989
990990 def test_host_path_not_found_rejected (self , mock_docker ):
991- """Host path that does not exist should be rejected ."""
991+ """Host path create failure should return 500 with HOST_PATH_CREATE_FAILED ."""
992992 mock_client = MagicMock ()
993993 mock_client .containers .list .return_value = []
994994 mock_docker .from_env .return_value = mock_client
@@ -1012,11 +1012,12 @@ def test_host_path_not_found_rejected(self, mock_docker):
10121012 ],
10131013 )
10141014
1015- with pytest .raises (HTTPException ) as exc_info :
1016- service .create_sandbox (request )
1015+ with patch ("src.services.docker.os.makedirs" , side_effect = PermissionError ("denied" )):
1016+ with pytest .raises (HTTPException ) as exc_info :
1017+ service .create_sandbox (request )
10171018
1018- assert exc_info .value .status_code == status .HTTP_400_BAD_REQUEST
1019- assert exc_info .value .detail ["code" ] == SandboxErrorCodes .HOST_PATH_NOT_FOUND
1019+ assert exc_info .value .status_code == status .HTTP_500_INTERNAL_SERVER_ERROR
1020+ assert exc_info .value .detail ["code" ] == SandboxErrorCodes .HOST_PATH_CREATE_FAILED
10201021
10211022 def test_host_path_not_in_allowlist_rejected (self , mock_docker ):
10221023 """Host path not in allowlist should be rejected."""
@@ -1167,17 +1168,21 @@ def test_host_volume_with_subpath_resolved_correctly(self, mock_docker):
11671168 assert len (binds ) == 1
11681169 assert binds [0 ] == f"{ sub_dir } :/mnt/work:ro"
11691170
1170- def test_host_subpath_not_found_rejected (self , mock_docker ):
1171- """Host volume with non-existent subPath should be rejected ."""
1171+ def test_host_subpath_auto_created (self , mock_docker ):
1172+ """Host volume with non-existent subPath should be auto-created ."""
11721173 mock_client = MagicMock ()
11731174 mock_client .containers .list .return_value = []
1175+ mock_client .api .create_host_config .return_value = {}
1176+ mock_client .api .create_container .return_value = {"Id" : "cid" }
1177+ mock_client .containers .get .return_value = MagicMock ()
11741178 mock_docker .from_env .return_value = mock_client
11751179
11761180 service = DockerSandboxService (config = _app_config ())
11771181
11781182 import tempfile
11791183
11801184 with tempfile .TemporaryDirectory () as tmpdir :
1185+ sub = "auto-created-sub"
11811186 request = CreateSandboxRequest (
11821187 image = ImageSpec (uri = "python:3.11" ),
11831188 timeout = 120 ,
@@ -1191,16 +1196,31 @@ def test_host_subpath_not_found_rejected(self, mock_docker):
11911196 host = Host (path = tmpdir ),
11921197 mount_path = "/mnt/work" ,
11931198 read_only = False ,
1194- sub_path = "nonexistent- sub" ,
1199+ sub_path = sub ,
11951200 )
11961201 ],
11971202 )
11981203
1199- with pytest .raises (HTTPException ) as exc_info :
1200- service .create_sandbox (request )
1204+ import os
1205+
1206+ resolved = os .path .join (tmpdir , sub )
1207+ assert not os .path .exists (resolved )
12011208
1202- assert exc_info .value .status_code == status .HTTP_400_BAD_REQUEST
1203- assert exc_info .value .detail ["code" ] == SandboxErrorCodes .HOST_PATH_NOT_FOUND
1209+ # create_sandbox will proceed past volume validation (subpath
1210+ # auto-created) but will fail later during container provisioning
1211+ # (mock doesn't cover the full flow). We only care that the
1212+ # directory was created — NOT that it raised HOST_PATH_CREATE_FAILED.
1213+ try :
1214+ service .create_sandbox (request )
1215+ except HTTPException as e :
1216+ # If it's our own create-failed error, the auto-create didn't
1217+ # work — let the test fail explicitly.
1218+ if e .detail .get ("code" ) == SandboxErrorCodes .HOST_PATH_CREATE_FAILED :
1219+ raise
1220+ except Exception :
1221+ pass # other provisioning errors are expected
1222+
1223+ assert os .path .isdir (resolved )
12041224
12051225 def test_empty_allowlist_permits_any_host_path (self , mock_docker ):
12061226 """Empty allowed_host_paths (default) should permit any valid host path."""
0 commit comments