16
16
17
17
package integration .host ;
18
18
19
- import static org .junit .jupiter .api .Assertions .assertEquals ;
20
-
21
19
import com .fasterxml .jackson .core .JsonProcessingException ;
22
20
import com .fasterxml .jackson .databind .ObjectMapper ;
23
21
import eu .rekawek .toxiproxy .ToxiproxyClient ;
44
42
import java .util .concurrent .ExecutorService ;
45
43
import java .util .concurrent .Executors ;
46
44
import java .util .concurrent .TimeUnit ;
45
+ import java .util .concurrent .atomic .AtomicInteger ;
47
46
import java .util .logging .Logger ;
48
47
import org .testcontainers .containers .GenericContainer ;
49
48
import org .testcontainers .containers .Network ;
@@ -70,6 +69,7 @@ public class TestEnvironment implements AutoCloseable {
70
69
private static final TestEnvironmentConfiguration config = new TestEnvironmentConfiguration ();
71
70
private static final boolean USE_OTLP_CONTAINER_FOR_TRACES = false ;
72
71
72
+ private static final AtomicInteger ipAddressUsageRefCount = new AtomicInteger (0 );
73
73
private final TestEnvironmentInfo info =
74
74
new TestEnvironmentInfo (); // only this info is passed to test container
75
75
@@ -102,12 +102,22 @@ private TestEnvironment(TestEnvironmentRequest request) {
102
102
}
103
103
104
104
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
+
105
115
LOGGER .finest ("Building test env: " + request .getEnvPreCreateIndex ());
106
116
preCreateEnvironment (request .getEnvPreCreateIndex ());
107
117
108
118
TestEnvironment env ;
109
119
110
- switch (request . getDatabaseEngineDeployment () ) {
120
+ switch (deployment ) {
111
121
case DOCKER :
112
122
env = new TestEnvironment (request );
113
123
initDatabaseParams (env );
@@ -185,8 +195,7 @@ private static TestEnvironment createAuroraOrMultiAzEnvironment(TestEnvironmentR
185
195
if (result instanceof Exception ) {
186
196
throw new RuntimeException ((Exception ) result );
187
197
}
188
- if (result instanceof TestEnvironment ) {
189
- TestEnvironment resultTestEnvironment = (TestEnvironment ) result ;
198
+ if (result instanceof TestEnvironment resultTestEnvironment ) {
190
199
LOGGER .finer (() -> String .format ("Use pre-created DB cluster: %s.cluster-%s" ,
191
200
resultTestEnvironment .auroraClusterName , resultTestEnvironment .auroraClusterDomain ));
192
201
@@ -894,7 +903,11 @@ public void close() throws Exception {
894
903
895
904
private void deleteDbCluster () {
896
905
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
+ }
898
911
}
899
912
900
913
if (!this .reuseAuroraDbCluster ) {
0 commit comments