Skip to content

Commit

Permalink
Make it possible to provide custom LocalStack services (#3995)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Apr 12, 2021
1 parent 55838a9 commit dabc99c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class LocalStackContainer extends GenericContainer<LocalStackContainer> {

static final int PORT = 4566;
private static final String HOSTNAME_EXTERNAL_ENV_VAR = "HOSTNAME_EXTERNAL";
private final List<Service> services = new ArrayList<>();
private final List<EnabledService> services = new ArrayList<>();

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

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

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

String hostnameExternalReason;
if (getEnvMap().containsKey(HOSTNAME_EXTERNAL_ENV_VAR)) {
Expand All @@ -144,12 +144,17 @@ private void exposePorts() {
.forEach(this::addExposedPort);
}

public LocalStackContainer withServices(Service... services) {
this.services.addAll(Arrays.asList(services));
return self();
}

/**
* Declare a set of simulated AWS services that should be launched by this container.
* @param services one or more service names
* @return this container object
*/
public LocalStackContainer withServices(Service... services) {
public LocalStackContainer withServices(EnabledService... services) {
this.services.addAll(Arrays.asList(services));
return self();
}
Expand Down Expand Up @@ -184,6 +189,10 @@ public AwsClientBuilder.EndpointConfiguration getEndpointConfiguration(Service s
return new AwsClientBuilder.EndpointConfiguration(getEndpointOverride(service).toString(), getRegion());
}

public URI getEndpointOverride(Service service) {
return getEndpointOverride((EnabledService) service);
}

/**
* Provides an endpoint override that is preconfigured to communicate with a given simulated service.
* The provided endpoint override should be set in the AWS Java SDK v2 when building a client, e.g.:
Expand All @@ -203,7 +212,7 @@ public AwsClientBuilder.EndpointConfiguration getEndpointConfiguration(Service s
* @param service the service that is to be accessed
* @return an {@link URI} endpoint override
*/
public URI getEndpointOverride(Service service) {
public URI getEndpointOverride(EnabledService service) {
try {
final String address = getHost();
String ipAddress = address;
Expand All @@ -218,8 +227,8 @@ public URI getEndpointOverride(Service service) {
}
}

private int getServicePort(Service service) {
return legacyMode ? service.port : PORT;
private int getServicePort(EnabledService service) {
return legacyMode ? service.getPort() : PORT;
}

/**
Expand Down Expand Up @@ -301,10 +310,22 @@ public String getRegion() {
return "us-east-1";
}

public interface EnabledService {
static EnabledService named(String name) {
return () -> name;
}

String getName();

default int getPort() {
return PORT;
}
}

@RequiredArgsConstructor
@Getter
@FieldDefaults(makeFinal = true)
public enum Service {
public enum Service implements EnabledService {
API_GATEWAY("apigateway", 4567),
EC2("ec2", 4597),
KINESIS("kinesis", 4568),
Expand Down Expand Up @@ -335,6 +356,11 @@ public enum Service {

int port;

@Override
public String getName() {
return localStackName;
}

@Deprecated
/*
Since version 0.11, LocalStack exposes all services on a single (4566) port.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static class WithoutNetwork {
// without_network {
@ClassRule
public static LocalStackContainer localstack = new LocalStackContainer(LOCALSTACK_IMAGE)
.withServices(S3, SQS, CLOUDWATCHLOGS, KMS);
.withServices(S3, SQS, CLOUDWATCHLOGS, KMS, LocalStackContainer.EnabledService.named("events"));
// }

@Test
Expand Down

0 comments on commit dabc99c

Please sign in to comment.