@@ -184,6 +184,29 @@ fn request_with_traceparent<T>(message: T) -> Request<T> {
184184 request
185185}
186186
187+ async fn fake_docker_with_no_containers ( ) -> ( String , JoinHandle < ( ) > ) {
188+ use tokio:: io:: { AsyncReadExt , AsyncWriteExt } ;
189+
190+ let listener = tokio:: net:: TcpListener :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
191+ let address = listener. local_addr ( ) . unwrap ( ) ;
192+ let server = tokio:: spawn ( async move {
193+ while let Ok ( ( mut stream, _) ) = listener. accept ( ) . await {
194+ openshell_core:: net:: set_tcp_nodelay_best_effort ( & stream) ;
195+ let mut scratch = [ 0_u8 ; 4096_usize ] ;
196+ let _ = stream. read ( & mut scratch) . await ;
197+ let _ = stream
198+ . write_all (
199+ b"HTTP/1.1 200 OK\r \n \
200+ Content-Type: application/json\r \n \
201+ Content-Length: 2\r \n \r \n []",
202+ )
203+ . await ;
204+ let _ = stream. flush ( ) . await ;
205+ }
206+ } ) ;
207+ ( format ! ( "http://{address}" ) , server)
208+ }
209+
187210async fn standalone_traced_client ( ) -> (
188211 TestDriverClient ,
189212 tokio:: sync:: oneshot:: Sender < ( ) > ,
@@ -3327,3 +3350,77 @@ fn docker_oom_kill_stays_terminal_despite_137() {
33273350 apply_docker_exit_classification ( & mut sandbox, & state) ;
33283351 assert_eq ! ( ready_reason( & sandbox) , CONDITION_EXITED ) ;
33293352}
3353+
3354+ #[ tokio:: test]
3355+ async fn delete_sandbox_reclaims_token_file_when_container_and_pending_are_gone ( ) {
3356+ let state_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
3357+ let ( endpoint, server) = fake_docker_with_no_containers ( ) . await ;
3358+
3359+ temp_env:: async_with_vars ( [ ( "XDG_STATE_HOME" , Some ( state_dir. path ( ) ) ) ] , async {
3360+ let config = runtime_config ( ) ;
3361+ let mut driver = test_driver_with_config ( config. clone ( ) ) ;
3362+ driver. docker = Arc :: new (
3363+ Docker :: connect_with_http ( & endpoint, 5 , bollard:: API_DEFAULT_VERSION ) . unwrap ( ) ,
3364+ ) ;
3365+
3366+ // Arrange the leak: token on disk, container gone, `pending` empty.
3367+ let token = openshell_core:: driver_utils:: sandbox_token_path (
3368+ "docker-sandbox-tokens" ,
3369+ Some ( & config. sandbox_namespace ) ,
3370+ "sandbox-1" ,
3371+ )
3372+ . unwrap ( ) ;
3373+
3374+ fs:: create_dir_all ( token. parent ( ) . unwrap ( ) ) . unwrap ( ) ;
3375+ fs:: write ( & token, "jwt\n " ) . unwrap ( ) ;
3376+
3377+ let deleted = driver. delete_sandbox_inner ( "sandbox-1" , "" ) . await . unwrap ( ) ;
3378+ assert ! ( !deleted, "nothing was removed, must not claim a deletion" ) ;
3379+ assert ! ( !token. exists( ) , "token file must be reclaimed" ) ;
3380+ } )
3381+ . await ;
3382+
3383+ server. abort ( ) ;
3384+ }
3385+
3386+ #[ tokio:: test]
3387+ async fn delete_sandbox_by_name_only_leaves_the_namespace_directory_alone ( ) {
3388+ // `DeleteSandbox` accepts a name without an id. With no id there is no
3389+ // token path to derive, so the cleanup must be a no-op: deriving a path
3390+ // from an empty id yields `<namespace>/sandbox.jwt`, whose parent is the
3391+ // shared namespace directory.
3392+ let state_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
3393+ let ( endpoint, server) = fake_docker_with_no_containers ( ) . await ;
3394+
3395+ temp_env:: async_with_vars ( [ ( "XDG_STATE_HOME" , Some ( state_dir. path ( ) ) ) ] , async {
3396+ let config = runtime_config ( ) ;
3397+ let mut driver = test_driver_with_config ( config. clone ( ) ) ;
3398+ driver. docker = Arc :: new (
3399+ Docker :: connect_with_http ( & endpoint, 5 , bollard:: API_DEFAULT_VERSION ) . unwrap ( ) ,
3400+ ) ;
3401+
3402+ let namespace_dir = openshell_core:: driver_utils:: sandbox_token_path (
3403+ "docker-sandbox-tokens" ,
3404+ Some ( & config. sandbox_namespace ) ,
3405+ "sandbox-1" ,
3406+ )
3407+ . unwrap ( )
3408+ . parent ( )
3409+ . and_then ( Path :: parent)
3410+ . unwrap ( )
3411+ . to_path_buf ( ) ;
3412+ fs:: create_dir_all ( & namespace_dir) . unwrap ( ) ;
3413+
3414+ let deleted = driver. delete_sandbox_inner ( "" , "sandbox-1" ) . await . unwrap ( ) ;
3415+
3416+ assert ! ( !deleted, "nothing was removed, must not claim a deletion" ) ;
3417+ assert ! (
3418+ namespace_dir. is_dir( ) ,
3419+ "namespace directory must survive a name-only delete: {}" ,
3420+ namespace_dir. display( )
3421+ ) ;
3422+ } )
3423+ . await ;
3424+
3425+ server. abort ( ) ;
3426+ }
0 commit comments