Skip to content

Commit af668fa

Browse files
authored
add attribute to @LocalstackDockerProperties annotation to configure port binding (localstack#33)
1 parent 2534df6 commit af668fa

File tree

9 files changed

+75
-8
lines changed

9 files changed

+75
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ You can configure the Docker behaviour using the `@LocalstackDockerProperties` a
7171
| `pullNewImage` | Determines if a new image is pulled from the docker repo before the tests are run. | boolean | `false` |
7272
| `services` | Determines which services should be run when the localstack starts. | String[] | All |
7373
| `imageTag` | Use a specific image tag for docker container | String | `latest` |
74+
| `portEdge` | Port number for the edge service, the main entry point for all API invocations | String | `4566` |
75+
| `portElasticSearch` | Port number for the elasticsearch service | String | `4571` |
7476
| `hostNameResolver` | Used for determining the host name of the machine running the docker containers so that the containers can be addressed. | IHostNameResolver | `localhost` |
7577
| `environmentVariableProvider` | Used for injecting environment variables into the container. | IEnvironmentVariableProvider | Empty Map |
7678
| `useSingleDockerContainer` | Whether a singleton container should be used by all test classes. | boolean | `false` |

src/main/java/cloud/localstack/Localstack.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public void startup(LocalstackDockerConfiguration dockerConfiguration) {
6767
dockerConfiguration.isPullNewImage(),
6868
dockerConfiguration.isRandomizePorts(),
6969
dockerConfiguration.getImageTag(),
70+
dockerConfiguration.getPortEdge(),
71+
dockerConfiguration.getPortElasticSearch(),
7072
dockerConfiguration.getEnvironmentVariables(),
7173
dockerConfiguration.getPortMappings()
7274
);

src/main/java/cloud/localstack/docker/Container.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,27 @@ public class Container {
5050
* @param environmentVariables map of environment variables to be passed to the docker container
5151
*/
5252
public static Container createLocalstackContainer(
53-
String externalHostName, boolean pullNewImage, boolean randomizePorts, String imageTag,
54-
Map<String, String> environmentVariables, Map<Integer, Integer> portMappings) {
53+
String externalHostName, boolean pullNewImage, boolean randomizePorts, String imageTag, String portEdge,
54+
String portElasticSearch, Map<String, String> environmentVariables, Map<Integer, Integer> portMappings) {
5555

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

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

62+
String fullPortEdge = (portEdge == null ? LOCALSTACK_PORT_EDGE : portEdge) + ":" + LOCALSTACK_PORT_EDGE;
63+
String fullPortElasticSearch = (portElasticSearch == null ? LOCALSTACK_PORT_ELASTICSEARCH : portElasticSearch)
64+
+ ":" + LOCALSTACK_PORT_ELASTICSEARCH;
65+
6266
if(pullNewImage || !imageExists) {
6367
LOG.info("Pulling latest image...");
6468
new PullCommand(LOCALSTACK_NAME, imageTag).execute();
6569
}
6670

6771
RunCommand runCommand = new RunCommand(LOCALSTACK_NAME, imageTag)
68-
.withExposedPorts(LOCALSTACK_PORT_EDGE, randomizePorts)
69-
.withExposedPorts(LOCALSTACK_PORT_ELASTICSEARCH, randomizePorts)
72+
.withExposedPorts(fullPortEdge, randomizePorts)
73+
.withExposedPorts(fullPortElasticSearch, randomizePorts)
7074
.withEnvironmentVariable(LOCALSTACK_EXTERNAL_HOSTNAME, externalHostName)
7175
.withEnvironmentVariable(ENV_DEBUG, ENV_DEBUG_DEFAULT)
7276
.withEnvironmentVariable(ENV_USE_SSL, Localstack.INSTANCE.useSSL() ? "1" : "0")

src/main/java/cloud/localstack/docker/annotation/LocalstackDockerAnnotationProcessor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ private LocalstackDockerConfiguration processDockerPropertiesAnnotation(Localsta
3636
.ignoreDockerRunErrors(properties.ignoreDockerRunErrors())
3737
.randomizePorts(properties.randomizePorts())
3838
.imageTag(StringUtils.isEmpty(properties.imageTag()) ? null : properties.imageTag())
39+
.portEdge(properties.portEdge())
40+
.portElasticSearch(properties.portElasticSearch())
3941
.useSingleDockerContainer(properties.useSingleDockerContainer())
4042
.build();
4143
}

src/main/java/cloud/localstack/docker/annotation/LocalstackDockerConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public class LocalstackDockerConfiguration {
2525

2626
private final String imageTag;
2727

28+
@Builder.Default
29+
private final String portEdge = "4566";
30+
31+
@Builder.Default
32+
private final String portElasticSearch = "4571";
33+
2834
@Builder.Default
2935
private final String externalHostName = "localhost";
3036

src/main/java/cloud/localstack/docker/annotation/LocalstackDockerProperties.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151
*/
5252
String imageTag() default "";
5353

54+
/**
55+
* Port number for the edge service, the main entry point for all API invocations
56+
*/
57+
String portEdge() default "4566";
58+
59+
/**
60+
* Port number for the elasticsearch service
61+
*/
62+
String portElasticSearch() default "4571";
63+
5464
/**
5565
* Determines if the singleton container should be used by all test classes
5666
*/

src/main/java/cloud/localstack/docker/command/RunCommand.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public String execute() {
3636
}
3737

3838
public RunCommand withExposedPorts(String portsToExpose, boolean randomize) {
39-
String portsOption = String.format("%s:%s", randomize ? "" : portsToExpose, portsToExpose );
39+
String[] parts = portsToExpose.split(":");
40+
String hostPort = randomize ? "" : parts.length > 1 ? parts[0] : portsToExpose;
41+
String containerPort = parts.length > 1 ? parts[1] : portsToExpose;
42+
String portsOption = String.format("%s:%s", hostPort, containerPort);
4043
addOptions("-p", portsOption);
4144
return this;
4245
}

src/test/java/cloud/localstack/deprecated/PortBindingTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void testAccessPredefinedPort() {
3535
@Test
3636
public void createLocalstackContainerWithRandomPorts() throws Exception {
3737
Container container = Container.createLocalstackContainer(
38-
EXTERNAL_HOST_NAME, pullNewImage, true, null, null, null);
38+
EXTERNAL_HOST_NAME, pullNewImage, true, null, null, null, null, null);
3939

4040
try {
4141
container.waitForAllPorts(EXTERNAL_HOST_NAME);
@@ -53,7 +53,7 @@ public void createLocalstackContainerWithRandomPorts() throws Exception {
5353
@Test
5454
public void createLocalstackContainerWithStaticPorts() throws Exception {
5555
Container container = Container.createLocalstackContainer(
56-
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null);
56+
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null, null, null);
5757

5858
try {
5959
container.waitForAllPorts(EXTERNAL_HOST_NAME);

src/test/java/cloud/localstack/docker/ContainerTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.HashMap;
77

88
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertNotEquals;
910

1011
public class ContainerTest {
1112

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

2627
try {
2728
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
@@ -32,6 +33,43 @@ public void createLocalstackContainer() throws Exception {
3233
ArrayList<String> echoExternalEnv = buildEchoStatement(MY_PROPERTY);
3334
assertEquals(EXTERNAL_HOST_NAME, localStackContainer.executeCommand(echoDefaultEnv));
3435
assertEquals(MY_VALUE, localStackContainer.executeCommand(echoExternalEnv));
36+
37+
// Test Edge and ElasticSearch ports
38+
39+
assertEquals(4566, localStackContainer.getExternalPortFor(4566));
40+
assertEquals(4571, localStackContainer.getExternalPortFor(4571));
41+
}
42+
finally {
43+
localStackContainer.stop();
44+
}
45+
}
46+
47+
@Test
48+
public void createLocalstackContainerWithCustomPorts() throws Exception {
49+
Container localStackContainer = Container.createLocalstackContainer(
50+
EXTERNAL_HOST_NAME, pullNewImage, false, null, "45660", "45710", null, null);
51+
52+
try {
53+
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
54+
55+
assertEquals(45660, localStackContainer.getExternalPortFor(4566));
56+
assertEquals(45710, localStackContainer.getExternalPortFor(4571));
57+
}
58+
finally {
59+
localStackContainer.stop();
60+
}
61+
}
62+
63+
@Test
64+
public void createLocalstackContainerWithRandomPorts() throws Exception {
65+
Container localStackContainer = Container.createLocalstackContainer(
66+
EXTERNAL_HOST_NAME, pullNewImage, false, null, ":4566", ":4571", null, null);
67+
68+
try {
69+
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
70+
71+
assertNotEquals(4566, localStackContainer.getExternalPortFor(4566));
72+
assertNotEquals(4571, localStackContainer.getExternalPortFor(4571));
3573
}
3674
finally {
3775
localStackContainer.stop();

0 commit comments

Comments
 (0)