Skip to content

Commit e023920

Browse files
authored
Add LocalStackContainer#getEndpoint() (#7037)
Most recent `localstack/localstack` image versions expose only one port `4566` and services start lazily, no more `SERVICES` env var is required. For that reason, `getEndpoint()` becomes handy.
1 parent 5b9c639 commit e023920

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

modules/localstack/src/main/java/org/testcontainers/containers/localstack/LocalStackContainer.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,35 @@ public URI getEndpointOverride(EnabledService service) {
254254
}
255255
}
256256

257+
/**
258+
* Provides an endpoint to communicate with LocalStack service.
259+
* The provided endpoint should be set in the AWS Java SDK v2 when building a client, e.g.:
260+
* <pre><code>S3Client s3 = S3Client
261+
.builder()
262+
.endpointOverride(localstack.getEndpoint())
263+
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
264+
localstack.getAccessKey(), localstack.getSecretKey()
265+
)))
266+
.region(Region.of(localstack.getRegion()))
267+
.build()
268+
</code></pre>
269+
* <p><strong>Please note that this method is only intended to be used for configuring AWS SDK clients
270+
* that are running on the test host. If other containers need to call this one, they should be configured
271+
* specifically to do so using a Docker network and appropriate addressing.</strong></p>
272+
*
273+
* @return an {@link URI} endpoint
274+
*/
275+
public URI getEndpoint() {
276+
try {
277+
final String address = getHost();
278+
// resolve IP address and use that as the endpoint so that path-style access is automatically used for S3
279+
String ipAddress = InetAddress.getByName(address).getHostAddress();
280+
return new URI("http://" + ipAddress + ":" + getMappedPort(PORT));
281+
} catch (UnknownHostException | URISyntaxException e) {
282+
throw new IllegalStateException("Cannot obtain endpoint URL", e);
283+
}
284+
}
285+
257286
private int getServicePort(EnabledService service) {
258287
return legacyMode ? service.getPort() : PORT;
259288
}

modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void s3TestOverBridgeNetwork() throws IOException {
7676
.standard()
7777
.withEndpointConfiguration(
7878
new AwsClientBuilder.EndpointConfiguration(
79-
localstack.getEndpointOverride(Service.S3).toString(),
79+
localstack.getEndpoint().toString(),
8080
localstack.getRegion()
8181
)
8282
)
@@ -115,7 +115,7 @@ public void s3TestUsingAwsSdkV2() {
115115
// with_aws_sdk_v2 {
116116
S3Client s3 = S3Client
117117
.builder()
118-
.endpointOverride(localstack.getEndpointOverride(Service.S3))
118+
.endpointOverride(localstack.getEndpoint())
119119
.credentialsProvider(
120120
StaticCredentialsProvider.create(
121121
AwsBasicCredentials.create(localstack.getAccessKey(), localstack.getSecretKey())
@@ -138,7 +138,7 @@ public void sqsTestOverBridgeNetwork() {
138138
.standard()
139139
.withEndpointConfiguration(
140140
new AwsClientBuilder.EndpointConfiguration(
141-
localstack.getEndpointOverride(Service.SQS).toString(),
141+
localstack.getEndpoint().toString(),
142142
localstack.getRegion()
143143
)
144144
)
@@ -171,7 +171,7 @@ public void cloudWatchLogsTestOverBridgeNetwork() {
171171
.standard()
172172
.withEndpointConfiguration(
173173
new AwsClientBuilder.EndpointConfiguration(
174-
localstack.getEndpointOverride(Service.CLOUDWATCHLOGS).toString(),
174+
localstack.getEndpoint().toString(),
175175
localstack.getRegion()
176176
)
177177
)
@@ -195,7 +195,7 @@ public void kmsKeyCreationTest() {
195195
.standard()
196196
.withEndpointConfiguration(
197197
new AwsClientBuilder.EndpointConfiguration(
198-
localstack.getEndpointOverride(Service.KMS).toString(),
198+
localstack.getEndpoint().toString(),
199199
localstack.getRegion()
200200
)
201201
)
@@ -345,7 +345,7 @@ public static class WithRegion {
345345
@Test
346346
public void s3EndpointHasProperRegion() {
347347
final AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
348-
localstack.getEndpointOverride(Service.S3).toString(),
348+
localstack.getEndpoint().toString(),
349349
localstack.getRegion()
350350
);
351351
assertThat(endpointConfiguration.getSigningRegion())
@@ -366,7 +366,7 @@ public void s3ServiceStartLazily() {
366366
try (
367367
S3Client s3 = S3Client
368368
.builder()
369-
.endpointOverride(localstack.getEndpointOverride(Service.S3))
369+
.endpointOverride(localstack.getEndpoint())
370370
.credentialsProvider(
371371
StaticCredentialsProvider.create(
372372
AwsBasicCredentials.create(localstack.getAccessKey(), localstack.getSecretKey())

0 commit comments

Comments
 (0)