Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ You can configure the Docker behaviour using the `@LocalstackDockerProperties` a
| `pullNewImage` | Determines if a new image is pulled from the docker repo before the tests are run. | boolean | `false` |
| `services` | Determines which services should be run when the localstack starts. | String[] | All |
| `imageTag` | Use a specific image tag for docker container | String | `latest` |
| `portEdge` | Port number for the edge service, the main entry point for all API invocations | String | `4566` |
| `portElasticSearch` | Port number for the elasticsearch service | String | `4571` |
| `hostNameResolver` | Used for determining the host name of the machine running the docker containers so that the containers can be addressed. | IHostNameResolver | `localhost` |
| `environmentVariableProvider` | Used for injecting environment variables into the container. | IEnvironmentVariableProvider | Empty Map |
| `useSingleDockerContainer` | Whether a singleton container should be used by all test classes. | boolean | `false` |
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cloud/localstack/Localstack.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public void startup(LocalstackDockerConfiguration dockerConfiguration) {
dockerConfiguration.isPullNewImage(),
dockerConfiguration.isRandomizePorts(),
dockerConfiguration.getImageTag(),
dockerConfiguration.getPortEdge(),
dockerConfiguration.getPortElasticSearch(),
dockerConfiguration.getEnvironmentVariables(),
dockerConfiguration.getPortMappings()
);
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/cloud/localstack/docker/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,27 @@ public class Container {
* @param environmentVariables map of environment variables to be passed to the docker container
*/
public static Container createLocalstackContainer(
String externalHostName, boolean pullNewImage, boolean randomizePorts, String imageTag,
Map<String, String> environmentVariables, Map<Integer, Integer> portMappings) {
String externalHostName, boolean pullNewImage, boolean randomizePorts, String imageTag, String portEdge,
String portElasticSearch, Map<String, String> environmentVariables, Map<Integer, Integer> portMappings) {

environmentVariables = environmentVariables == null ? Collections.emptyMap() : environmentVariables;
portMappings = portMappings == null ? Collections.emptyMap() : portMappings;

String fullImageName = LOCALSTACK_NAME + ":" + (imageTag == null ? "latest" : imageTag);
boolean imageExists = new ListImagesCommand().execute().contains(fullImageName);

String fullPortEdge = (portEdge == null ? LOCALSTACK_PORT_EDGE : portEdge) + ":" + LOCALSTACK_PORT_EDGE;
String fullPortElasticSearch = (portElasticSearch == null ? LOCALSTACK_PORT_ELASTICSEARCH : portElasticSearch)
+ ":" + LOCALSTACK_PORT_ELASTICSEARCH;

if(pullNewImage || !imageExists) {
LOG.info("Pulling latest image...");
new PullCommand(LOCALSTACK_NAME, imageTag).execute();
}

RunCommand runCommand = new RunCommand(LOCALSTACK_NAME, imageTag)
.withExposedPorts(LOCALSTACK_PORT_EDGE, randomizePorts)
.withExposedPorts(LOCALSTACK_PORT_ELASTICSEARCH, randomizePorts)
.withExposedPorts(fullPortEdge, randomizePorts)
.withExposedPorts(fullPortElasticSearch, randomizePorts)
.withEnvironmentVariable(LOCALSTACK_EXTERNAL_HOSTNAME, externalHostName)
.withEnvironmentVariable(ENV_DEBUG, ENV_DEBUG_DEFAULT)
.withEnvironmentVariable(ENV_USE_SSL, Localstack.INSTANCE.useSSL() ? "1" : "0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ private LocalstackDockerConfiguration processDockerPropertiesAnnotation(Localsta
.ignoreDockerRunErrors(properties.ignoreDockerRunErrors())
.randomizePorts(properties.randomizePorts())
.imageTag(StringUtils.isEmpty(properties.imageTag()) ? null : properties.imageTag())
.portEdge(properties.portEdge())
.portElasticSearch(properties.portElasticSearch())
.useSingleDockerContainer(properties.useSingleDockerContainer())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class LocalstackDockerConfiguration {

private final String imageTag;

@Builder.Default
private final String portEdge = "4566";

@Builder.Default
private final String portElasticSearch = "4571";

@Builder.Default
private final String externalHostName = "localhost";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@
*/
String imageTag() default "";

/**
* Port number for the edge service, the main entry point for all API invocations
*/
String portEdge() default "4566";

/**
* Port number for the elasticsearch service
*/
String portElasticSearch() default "4571";

/**
* Determines if the singleton container should be used by all test classes
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public String execute() {
}

public RunCommand withExposedPorts(String portsToExpose, boolean randomize) {
String portsOption = String.format("%s:%s", randomize ? "" : portsToExpose, portsToExpose );
String[] parts = portsToExpose.split(":");
String hostPort = randomize ? "" : parts.length > 1 ? parts[0] : portsToExpose;
String containerPort = parts.length > 1 ? parts[1] : portsToExpose;
String portsOption = String.format("%s:%s", hostPort, containerPort);
addOptions("-p", portsOption);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void testAccessPredefinedPort() {
@Test
public void createLocalstackContainerWithRandomPorts() throws Exception {
Container container = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, true, null, null, null);
EXTERNAL_HOST_NAME, pullNewImage, true, null, null, null, null, null);

try {
container.waitForAllPorts(EXTERNAL_HOST_NAME);
Expand All @@ -53,7 +53,7 @@ public void createLocalstackContainerWithRandomPorts() throws Exception {
@Test
public void createLocalstackContainerWithStaticPorts() throws Exception {
Container container = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null);
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null, null, null);

try {
container.waitForAllPorts(EXTERNAL_HOST_NAME);
Expand Down
40 changes: 39 additions & 1 deletion src/test/java/cloud/localstack/docker/ContainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashMap;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class ContainerTest {

Expand All @@ -21,7 +22,7 @@ public void createLocalstackContainer() throws Exception {
HashMap<String, String> environmentVariables = new HashMap<>();
environmentVariables.put(MY_PROPERTY, MY_VALUE);
Container localStackContainer = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, true, null, environmentVariables, null);
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null, environmentVariables, null);

try {
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
Expand All @@ -32,6 +33,43 @@ public void createLocalstackContainer() throws Exception {
ArrayList<String> echoExternalEnv = buildEchoStatement(MY_PROPERTY);
assertEquals(EXTERNAL_HOST_NAME, localStackContainer.executeCommand(echoDefaultEnv));
assertEquals(MY_VALUE, localStackContainer.executeCommand(echoExternalEnv));

// Test Edge and ElasticSearch ports

assertEquals(4566, localStackContainer.getExternalPortFor(4566));
assertEquals(4571, localStackContainer.getExternalPortFor(4571));
}
finally {
localStackContainer.stop();
}
}

@Test
public void createLocalstackContainerWithCustomPorts() throws Exception {
Container localStackContainer = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, false, null, "45660", "45710", null, null);

try {
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);

assertEquals(45660, localStackContainer.getExternalPortFor(4566));
assertEquals(45710, localStackContainer.getExternalPortFor(4571));
}
finally {
localStackContainer.stop();
}
}

@Test
public void createLocalstackContainerWithRandomPorts() throws Exception {
Container localStackContainer = Container.createLocalstackContainer(
EXTERNAL_HOST_NAME, pullNewImage, false, null, ":4566", ":4571", null, null);

try {
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);

assertNotEquals(4566, localStackContainer.getExternalPortFor(4566));
assertNotEquals(4571, localStackContainer.getExternalPortFor(4571));
}
finally {
localStackContainer.stop();
Expand Down