Skip to content

Commit dabc99c

Browse files
authored
Make it possible to provide custom LocalStack services (#3995)
1 parent 55838a9 commit dabc99c

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class LocalStackContainer extends GenericContainer<LocalStackContainer> {
3737

3838
static final int PORT = 4566;
3939
private static final String HOSTNAME_EXTERNAL_ENV_VAR = "HOSTNAME_EXTERNAL";
40-
private final List<Service> services = new ArrayList<>();
40+
private final List<EnabledService> services = new ArrayList<>();
4141

4242
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("localstack/localstack");
4343
private static final String DEFAULT_TAG = "0.11.2";
@@ -119,7 +119,7 @@ protected void configure() {
119119

120120
Preconditions.check("services list must not be empty", !services.isEmpty());
121121

122-
withEnv("SERVICES", services.stream().map(Service::getLocalStackName).collect(Collectors.joining(",")));
122+
withEnv("SERVICES", services.stream().map(EnabledService::getName).collect(Collectors.joining(",")));
123123

124124
String hostnameExternalReason;
125125
if (getEnvMap().containsKey(HOSTNAME_EXTERNAL_ENV_VAR)) {
@@ -144,12 +144,17 @@ private void exposePorts() {
144144
.forEach(this::addExposedPort);
145145
}
146146

147+
public LocalStackContainer withServices(Service... services) {
148+
this.services.addAll(Arrays.asList(services));
149+
return self();
150+
}
151+
147152
/**
148153
* Declare a set of simulated AWS services that should be launched by this container.
149154
* @param services one or more service names
150155
* @return this container object
151156
*/
152-
public LocalStackContainer withServices(Service... services) {
157+
public LocalStackContainer withServices(EnabledService... services) {
153158
this.services.addAll(Arrays.asList(services));
154159
return self();
155160
}
@@ -184,6 +189,10 @@ public AwsClientBuilder.EndpointConfiguration getEndpointConfiguration(Service s
184189
return new AwsClientBuilder.EndpointConfiguration(getEndpointOverride(service).toString(), getRegion());
185190
}
186191

192+
public URI getEndpointOverride(Service service) {
193+
return getEndpointOverride((EnabledService) service);
194+
}
195+
187196
/**
188197
* Provides an endpoint override that is preconfigured to communicate with a given simulated service.
189198
* The provided endpoint override should be set in the AWS Java SDK v2 when building a client, e.g.:
@@ -203,7 +212,7 @@ public AwsClientBuilder.EndpointConfiguration getEndpointConfiguration(Service s
203212
* @param service the service that is to be accessed
204213
* @return an {@link URI} endpoint override
205214
*/
206-
public URI getEndpointOverride(Service service) {
215+
public URI getEndpointOverride(EnabledService service) {
207216
try {
208217
final String address = getHost();
209218
String ipAddress = address;
@@ -218,8 +227,8 @@ public URI getEndpointOverride(Service service) {
218227
}
219228
}
220229

221-
private int getServicePort(Service service) {
222-
return legacyMode ? service.port : PORT;
230+
private int getServicePort(EnabledService service) {
231+
return legacyMode ? service.getPort() : PORT;
223232
}
224233

225234
/**
@@ -301,10 +310,22 @@ public String getRegion() {
301310
return "us-east-1";
302311
}
303312

313+
public interface EnabledService {
314+
static EnabledService named(String name) {
315+
return () -> name;
316+
}
317+
318+
String getName();
319+
320+
default int getPort() {
321+
return PORT;
322+
}
323+
}
324+
304325
@RequiredArgsConstructor
305326
@Getter
306327
@FieldDefaults(makeFinal = true)
307-
public enum Service {
328+
public enum Service implements EnabledService {
308329
API_GATEWAY("apigateway", 4567),
309330
EC2("ec2", 4597),
310331
KINESIS("kinesis", 4568),
@@ -335,6 +356,11 @@ public enum Service {
335356

336357
int port;
337358

359+
@Override
360+
public String getName() {
361+
return localStackName;
362+
}
363+
338364
@Deprecated
339365
/*
340366
Since version 0.11, LocalStack exposes all services on a single (4566) port.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static class WithoutNetwork {
6666
// without_network {
6767
@ClassRule
6868
public static LocalStackContainer localstack = new LocalStackContainer(LOCALSTACK_IMAGE)
69-
.withServices(S3, SQS, CLOUDWATCHLOGS, KMS);
69+
.withServices(S3, SQS, CLOUDWATCHLOGS, KMS, LocalStackContainer.EnabledService.named("events"));
7070
// }
7171

7272
@Test

0 commit comments

Comments
 (0)