1616
1717package integration .host ;
1818
19- import static org .junit .jupiter .api .Assertions .assertEquals ;
20-
2119import com .fasterxml .jackson .core .JsonProcessingException ;
2220import com .fasterxml .jackson .databind .ObjectMapper ;
2321import eu .rekawek .toxiproxy .ToxiproxyClient ;
4442import java .util .concurrent .ExecutorService ;
4543import java .util .concurrent .Executors ;
4644import java .util .concurrent .TimeUnit ;
45+ import java .util .concurrent .atomic .AtomicInteger ;
4746import java .util .logging .Logger ;
4847import org .testcontainers .containers .GenericContainer ;
4948import org .testcontainers .containers .Network ;
@@ -70,6 +69,7 @@ public class TestEnvironment implements AutoCloseable {
7069 private static final TestEnvironmentConfiguration config = new TestEnvironmentConfiguration ();
7170 private static final boolean USE_OTLP_CONTAINER_FOR_TRACES = false ;
7271
72+ private static final AtomicInteger ipAddressUsageRefCount = new AtomicInteger (0 );
7373 private final TestEnvironmentInfo info =
7474 new TestEnvironmentInfo (); // only this info is passed to test container
7575
@@ -102,12 +102,22 @@ private TestEnvironment(TestEnvironmentRequest request) {
102102 }
103103
104104 public static TestEnvironment build (TestEnvironmentRequest request ) throws IOException {
105+ DatabaseEngineDeployment deployment = request .getDatabaseEngineDeployment ();
106+ if (deployment == DatabaseEngineDeployment .AURORA
107+ || deployment == DatabaseEngineDeployment .RDS
108+ || deployment == DatabaseEngineDeployment .RDS_MULTI_AZ ) {
109+ // These environment require creating external database cluster that should be publicly available.
110+ // Corresponding AWS Security Groups should be configured and the test task runner IP address
111+ // should be whitelisted.
112+ ipAddressUsageRefCount .incrementAndGet ();
113+ }
114+
105115 LOGGER .finest ("Building test env: " + request .getEnvPreCreateIndex ());
106116 preCreateEnvironment (request .getEnvPreCreateIndex ());
107117
108118 TestEnvironment env ;
109119
110- switch (request . getDatabaseEngineDeployment () ) {
120+ switch (deployment ) {
111121 case DOCKER :
112122 env = new TestEnvironment (request );
113123 initDatabaseParams (env );
@@ -185,8 +195,7 @@ private static TestEnvironment createAuroraOrMultiAzEnvironment(TestEnvironmentR
185195 if (result instanceof Exception ) {
186196 throw new RuntimeException ((Exception ) result );
187197 }
188- if (result instanceof TestEnvironment ) {
189- TestEnvironment resultTestEnvironment = (TestEnvironment ) result ;
198+ if (result instanceof TestEnvironment resultTestEnvironment ) {
190199 LOGGER .finer (() -> String .format ("Use pre-created DB cluster: %s.cluster-%s" ,
191200 resultTestEnvironment .auroraClusterName , resultTestEnvironment .auroraClusterDomain ));
192201
@@ -894,7 +903,11 @@ public void close() throws Exception {
894903
895904 private void deleteDbCluster () {
896905 if (!this .reuseAuroraDbCluster && !StringUtils .isNullOrEmpty (this .runnerIP )) {
897- auroraUtil .ec2DeauthorizesIP (runnerIP );
906+ if (ipAddressUsageRefCount .decrementAndGet () == 0 ) {
907+ // Another test environments are still in use of test task runner IP address.
908+ // The last execute tst environment will do the cleanup.
909+ auroraUtil .ec2DeauthorizesIP (runnerIP );
910+ }
898911 }
899912
900913 if (!this .reuseAuroraDbCluster ) {
0 commit comments