File tree Expand file tree Collapse file tree 5 files changed +64
-2
lines changed Expand file tree Collapse file tree 5 files changed +64
-2
lines changed Original file line number Diff line number Diff line change @@ -349,6 +349,23 @@ impl Client {
349349 . map_err ( ClientError :: UploadToContainerError )
350350 }
351351
352+ pub ( crate ) async fn container_is_running (
353+ & self ,
354+ container_id : & str ,
355+ ) -> Result < bool , ClientError > {
356+ let container_info = self
357+ . bollard
358+ . inspect_container ( container_id, Some ( InspectContainerOptions { size : false } ) )
359+ . await
360+ . map_err ( ClientError :: InspectContainer ) ?;
361+
362+ if let Some ( state) = container_info. state {
363+ Ok ( state. running . unwrap_or_default ( ) )
364+ } else {
365+ Ok ( false )
366+ }
367+ }
368+
352369 pub ( crate ) async fn container_exit_code (
353370 & self ,
354371 container_id : & str ,
@@ -358,6 +375,7 @@ impl Client {
358375 . inspect_container ( container_id, Some ( InspectContainerOptions { size : false } ) )
359376 . await
360377 . map_err ( ClientError :: InspectContainer ) ?;
378+
361379 let Some ( state) = container_info. state else {
362380 return Ok ( None ) ;
363381 } ;
Original file line number Diff line number Diff line change @@ -369,6 +369,12 @@ where
369369 Ok ( stderr)
370370 }
371371
372+ /// Returns whether the container is still running.
373+ pub async fn is_running ( & self ) -> Result < bool > {
374+ let status = self . docker_client . container_is_running ( & self . id ) . await ?;
375+ Ok ( status)
376+ }
377+
372378 /// Returns `Some(exit_code)` when the container is finished and `None` when the container is still running.
373379 pub async fn exit_code ( & self ) -> Result < Option < i64 > > {
374380 let exit_code = self . docker_client . container_exit_code ( & self . id ) . await ?;
Original file line number Diff line number Diff line change @@ -209,6 +209,11 @@ where
209209 Ok ( stderr)
210210 }
211211
212+ /// Returns whether the container is still running.
213+ pub fn is_running ( & self ) -> Result < bool > {
214+ self . rt ( ) . block_on ( self . async_impl ( ) . is_running ( ) )
215+ }
216+
212217 /// Returns `Some(exit_code)` when the container is finished and `None` when the container is still running.
213218 pub fn exit_code ( & self ) -> Result < Option < i64 > > {
214219 self . rt ( ) . block_on ( self . async_impl ( ) . exit_code ( ) )
Original file line number Diff line number Diff line change @@ -268,6 +268,24 @@ async fn async_copy_files_to_container() -> anyhow::Result<()> {
268268 Ok ( ( ) )
269269}
270270
271+ #[ tokio:: test]
272+ async fn async_container_is_running ( ) -> anyhow:: Result < ( ) > {
273+ let _ = pretty_env_logger:: try_init ( ) ;
274+
275+ // Container that should run until manually quit
276+ let container = GenericImage :: new ( "simple_web_server" , "latest" )
277+ . with_wait_for ( WaitFor :: message_on_stdout ( "server is ready" ) )
278+ . start ( )
279+ . await ?;
280+
281+ assert ! ( container. is_running( ) . await ?) ;
282+
283+ container. stop ( ) . await ?;
284+
285+ assert ! ( !container. is_running( ) . await ?) ;
286+ Ok ( ( ) )
287+ }
288+
271289#[ tokio:: test]
272290async fn async_container_exit_code ( ) -> anyhow:: Result < ( ) > {
273291 let _ = pretty_env_logger:: try_init ( ) ;
@@ -283,6 +301,5 @@ async fn async_container_exit_code() -> anyhow::Result<()> {
283301 container. stop ( ) . await ?;
284302
285303 assert_eq ! ( container. exit_code( ) . await ?, Some ( 0 ) ) ;
286-
287304 Ok ( ( ) )
288305}
Original file line number Diff line number Diff line change @@ -288,6 +288,23 @@ fn sync_copy_files_to_container() -> anyhow::Result<()> {
288288 Ok ( ( ) )
289289}
290290
291+ #[ test]
292+ fn sync_container_is_running ( ) -> anyhow:: Result < ( ) > {
293+ let _ = pretty_env_logger:: try_init ( ) ;
294+
295+ // Container that should run until manually quit
296+ let container = GenericImage :: new ( "simple_web_server" , "latest" )
297+ . with_wait_for ( WaitFor :: message_on_stdout ( "server is ready" ) )
298+ . start ( ) ?;
299+
300+ assert ! ( container. is_running( ) ?) ;
301+
302+ container. stop ( ) ?;
303+
304+ assert ! ( !container. is_running( ) ?) ;
305+ Ok ( ( ) )
306+ }
307+
291308#[ test]
292309fn sync_container_exit_code ( ) -> anyhow:: Result < ( ) > {
293310 let _ = pretty_env_logger:: try_init ( ) ;
@@ -302,6 +319,5 @@ fn sync_container_exit_code() -> anyhow::Result<()> {
302319 container. stop ( ) ?;
303320
304321 assert_eq ! ( container. exit_code( ) ?, Some ( 0 ) ) ;
305-
306322 Ok ( ( ) )
307323}
You can’t perform that action at this time.
0 commit comments