@@ -26,6 +26,7 @@ use redis::Commands;
26
26
use serde:: Deserialize ;
27
27
use serde:: Serialize ;
28
28
use serde_json:: Value ;
29
+ use testcontainers:: core:: client:: docker_client_instance;
29
30
use testcontainers:: core:: error:: WaitContainerError ;
30
31
use testcontainers:: core:: IntoContainerPort ;
31
32
use testcontainers:: core:: WaitFor ;
@@ -242,12 +243,12 @@ fn run_script(name: &str, args: &[&str]) -> Result<()> {
242
243
}
243
244
244
245
pub async fn run_ttc_container (
245
- docker : & Docker ,
246
246
image : & str ,
247
247
port : u16 ,
248
248
http_server_port : u16 ,
249
249
cs : & mut Vec < ContainerAsync < GenericImage > > ,
250
250
) -> Result < ( ) > {
251
+ let docker = & docker_client_instance ( ) . await ?;
251
252
let mut images = image. split ( ":" ) ;
252
253
let image = images. next ( ) . unwrap ( ) ;
253
254
let tag = images. next ( ) . unwrap_or ( "latest" ) ;
@@ -264,8 +265,6 @@ pub async fn run_ttc_container(
264
265
let start = Instant :: now ( ) ;
265
266
println ! ( "Start container {container_name}" ) ;
266
267
267
- stop_container ( docker, & container_name) . await ;
268
-
269
268
let mut i = 1 ;
270
269
loop {
271
270
let container_res = GenericImage :: new ( image, tag)
@@ -287,28 +286,24 @@ pub async fn run_ttc_container(
287
286
match container_res {
288
287
Ok ( container) => {
289
288
println ! (
290
- "Start container {} using {} secs success" ,
289
+ "Start container {container_name} {} using {duration } secs success" ,
291
290
container. id( ) ,
292
- duration
293
291
) ;
294
292
cs. push ( container) ;
295
293
return Ok ( ( ) ) ;
296
294
}
297
295
Err ( err) => {
298
- println ! (
299
- "Start container {} using {} secs failed: {}" ,
300
- container_name, duration, err
301
- ) ;
296
+ println ! ( "Start container {container_name} using {duration} secs failed: {err}" ) ;
302
297
if err. to_string ( ) . to_ascii_lowercase ( ) . contains ( "timeout" )
303
298
|| err. to_string ( ) . to_ascii_lowercase ( ) . contains ( "conflict" )
304
299
{
305
- println ! ( "start to stop container {container_name}" ) ;
300
+ println ! ( "Start to stop container {container_name}" ) ;
306
301
stop_container ( docker, & container_name) . await ;
307
302
}
308
303
if i == CONTAINER_RETRY_TIMES || duration >= CONTAINER_TIMEOUT_SECONDS {
309
304
break ;
310
305
} else {
311
- println ! ( "retry start container {container_name} after {duration}" ) ;
306
+ println ! ( "retry start container {container_name} after {duration} secs " ) ;
312
307
i += 1 ;
313
308
}
314
309
}
@@ -330,7 +325,7 @@ pub async fn lazy_run_dictionary_containers(
330
325
return Ok ( None ) ;
331
326
}
332
327
println ! ( "Start run dictionary source server container" ) ;
333
- let docker = Docker :: connect_with_local_defaults ( ) . unwrap ( ) ;
328
+ let docker = docker_client_instance ( ) . await ? ;
334
329
let redis = run_redis_server ( & docker) . await ?;
335
330
let mysql = run_mysql_server ( & docker) . await ?;
336
331
let dict_container = DictionaryContainer { redis, mysql } ;
@@ -469,11 +464,15 @@ async fn run_mysql_server(docker: &Docker) -> Result<ContainerAsync<Mysql>> {
469
464
470
465
// Stop the running container to avoid conflict
471
466
async fn stop_container ( docker : & Docker , container_name : & str ) {
472
- let _ = docker. stop_container ( container_name, None ) . await ;
467
+ if let Err ( err) = docker. stop_container ( container_name, None ) . await {
468
+ println ! ( "stop container {container_name} err: {err}" ) ;
469
+ }
473
470
let options = Some ( RemoveContainerOptions {
474
471
force : true ,
475
472
..Default :: default ( )
476
473
} ) ;
477
- let _ = docker. remove_container ( container_name, options) . await ;
478
- println ! ( "Stop container {container_name}" ) ;
474
+ if let Err ( err) = docker. remove_container ( container_name, options) . await {
475
+ println ! ( "remove container {container_name} err: {err}" ) ;
476
+ }
477
+ println ! ( "Stopped container {container_name}" ) ;
479
478
}
0 commit comments