44use super :: {
55 docker:: {
66 create_network, create_volume, delete_container, delete_volume, get_docker,
7- pull_docker_image, setup_docker_logging, StopContainerShutdownStep , CONTAINER_NETWORK_NAME ,
7+ pull_docker_image, setup_docker_logging, StopContainerShutdownStep ,
88 } ,
99 health_checker:: HealthChecker ,
1010 traits:: { ServiceManager , ShutdownStep } ,
@@ -121,6 +121,8 @@ pub struct PostgresManager {
121121 args : PostgresArgs ,
122122 test_dir : PathBuf ,
123123 force_restart : bool ,
124+ docker_network : String ,
125+ docker_network_already_exists : bool ,
124126}
125127
126128impl PostgresManager {
@@ -130,10 +132,13 @@ impl PostgresManager {
130132 {
131133 bail ! ( "The postgres database cannot be named postgres if --use-host-postgres is set" ) ;
132134 }
135+ let ( docker_network, docker_network_already_exists) = args. get_docker_network ( ) ;
133136 Ok ( Self {
134137 args : args. postgres_args . clone ( ) ,
135138 test_dir,
136139 force_restart : args. force_restart ,
140+ docker_network : docker_network. to_string ( ) ,
141+ docker_network_already_exists,
137142 } )
138143 }
139144
@@ -192,8 +197,11 @@ impl ServiceManager for PostgresManager {
192197 // `run_service`.
193198 pull_docker_image ( POSTGRES_IMAGE ) . await ?;
194199
195- // Create a network for the containers to talk to each other.
196- create_network ( CONTAINER_NETWORK_NAME ) . await ?;
200+ // Create a network for the containers to talk to each other if a
201+ // pre-existing network was not specified.
202+ if !self . docker_network_already_exists {
203+ create_network ( & self . docker_network ) . await ?;
204+ }
197205 }
198206
199207 Ok ( ( ) )
@@ -243,7 +251,7 @@ impl ServiceManager for PostgresManager {
243251 // Bind the container to the network we created in the pre_run. This does
244252 // not prevent the binary in the container from exposing itself to the host
245253 // on 127.0.0.1. See more here: https://stackoverflow.com/a/77432636/3846032.
246- network_mode : Some ( CONTAINER_NETWORK_NAME . to_string ( ) ) ,
254+ network_mode : Some ( self . docker_network . clone ( ) ) ,
247255 port_bindings : Some ( hashmap ! {
248256 POSTGRES_DEFAULT_PORT . to_string( ) => Some ( vec![ PortBinding {
249257 host_ip: Some ( "127.0.0.1" . to_string( ) ) ,
0 commit comments