@@ -209,6 +209,29 @@ fn request_with_traceparent<T>(message: T) -> Request<T> {
209209 request
210210}
211211
212+ async fn fake_docker_with_no_containers ( ) -> ( String , JoinHandle < ( ) > ) {
213+ use tokio:: io:: { AsyncReadExt , AsyncWriteExt } ;
214+
215+ let listener = tokio:: net:: TcpListener :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
216+ let address = listener. local_addr ( ) . unwrap ( ) ;
217+ let server = tokio:: spawn ( async move {
218+ while let Ok ( ( mut stream, _) ) = listener. accept ( ) . await {
219+ openshell_core:: net:: set_tcp_nodelay_best_effort ( & stream) ;
220+ let mut scratch = [ 0_u8 ; 4096_usize ] ;
221+ let _ = stream. read ( & mut scratch) . await ;
222+ let _ = stream
223+ . write_all (
224+ b"HTTP/1.1 200 OK\r \n \
225+ Content-Type: application/json\r \n \
226+ Content-Length: 2\r \n \r \n []",
227+ )
228+ . await ;
229+ let _ = stream. flush ( ) . await ;
230+ }
231+ } ) ;
232+ ( format ! ( "http://{address}" ) , server)
233+ }
234+
212235async fn standalone_traced_client ( ) -> (
213236 TestDriverClient ,
214237 tokio:: sync:: oneshot:: Sender < ( ) > ,
@@ -3718,3 +3741,77 @@ fn managed_container_identity_requires_the_configured_namespace() {
37183741 "demo"
37193742 ) ) ;
37203743}
3744+
3745+ #[ tokio:: test]
3746+ async fn delete_sandbox_reclaims_token_file_when_container_and_pending_are_gone ( ) {
3747+ let state_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
3748+ let ( endpoint, server) = fake_docker_with_no_containers ( ) . await ;
3749+
3750+ temp_env:: async_with_vars ( [ ( "XDG_STATE_HOME" , Some ( state_dir. path ( ) ) ) ] , async {
3751+ let config = runtime_config ( ) ;
3752+ let mut driver = test_driver_with_config ( config. clone ( ) ) ;
3753+ driver. docker = Arc :: new (
3754+ Docker :: connect_with_http ( & endpoint, 5 , bollard:: API_DEFAULT_VERSION ) . unwrap ( ) ,
3755+ ) ;
3756+
3757+ // Arrange the leak: token on disk, container gone, `pending` empty.
3758+ let token = openshell_core:: driver_utils:: sandbox_token_path (
3759+ "docker-sandbox-tokens" ,
3760+ Some ( & config. sandbox_namespace ) ,
3761+ "sandbox-1" ,
3762+ )
3763+ . unwrap ( ) ;
3764+
3765+ fs:: create_dir_all ( token. parent ( ) . unwrap ( ) ) . unwrap ( ) ;
3766+ fs:: write ( & token, "jwt\n " ) . unwrap ( ) ;
3767+
3768+ let deleted = driver. delete_sandbox_inner ( "sandbox-1" , "" ) . await . unwrap ( ) ;
3769+ assert ! ( !deleted, "nothing was removed, must not claim a deletion" ) ;
3770+ assert ! ( !token. exists( ) , "token file must be reclaimed" ) ;
3771+ } )
3772+ . await ;
3773+
3774+ server. abort ( ) ;
3775+ }
3776+
3777+ #[ tokio:: test]
3778+ async fn delete_sandbox_by_name_only_leaves_the_namespace_directory_alone ( ) {
3779+ // `DeleteSandbox` accepts a name without an id. With no id there is no
3780+ // token path to derive, so the cleanup must be a no-op: deriving a path
3781+ // from an empty id yields `<namespace>/sandbox.jwt`, whose parent is the
3782+ // shared namespace directory.
3783+ let state_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
3784+ let ( endpoint, server) = fake_docker_with_no_containers ( ) . await ;
3785+
3786+ temp_env:: async_with_vars ( [ ( "XDG_STATE_HOME" , Some ( state_dir. path ( ) ) ) ] , async {
3787+ let config = runtime_config ( ) ;
3788+ let mut driver = test_driver_with_config ( config. clone ( ) ) ;
3789+ driver. docker = Arc :: new (
3790+ Docker :: connect_with_http ( & endpoint, 5 , bollard:: API_DEFAULT_VERSION ) . unwrap ( ) ,
3791+ ) ;
3792+
3793+ let namespace_dir = openshell_core:: driver_utils:: sandbox_token_path (
3794+ "docker-sandbox-tokens" ,
3795+ Some ( & config. sandbox_namespace ) ,
3796+ "sandbox-1" ,
3797+ )
3798+ . unwrap ( )
3799+ . parent ( )
3800+ . and_then ( Path :: parent)
3801+ . unwrap ( )
3802+ . to_path_buf ( ) ;
3803+ fs:: create_dir_all ( & namespace_dir) . unwrap ( ) ;
3804+
3805+ let deleted = driver. delete_sandbox_inner ( "" , "sandbox-1" ) . await . unwrap ( ) ;
3806+
3807+ assert ! ( !deleted, "nothing was removed, must not claim a deletion" ) ;
3808+ assert ! (
3809+ namespace_dir. is_dir( ) ,
3810+ "namespace directory must survive a name-only delete: {}" ,
3811+ namespace_dir. display( )
3812+ ) ;
3813+ } )
3814+ . await ;
3815+
3816+ server. abort ( ) ;
3817+ }
0 commit comments