Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localstack: Make it possible to provide custom LocalStack services #3995

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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