Skip to content

Commit 5d9d8b4

Browse files
authored
add support for configuring the Docker image name via @LocalstackDockerProperties (#40)
1 parent 228d451 commit 5d9d8b4

File tree

9 files changed

+69
-11
lines changed

9 files changed

+69
-11
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ The container can be configured by using the `@LocalstackDockerProperties` annot
2222
```
2323
...
2424
import cloud.localstack.LocalstackTestRunner;
25+
import cloud.localstack.ServiceName;
2526
import cloud.localstack.TestUtils;
2627
import cloud.localstack.docker.annotation.LocalstackDockerProperties;
2728
2829
@RunWith(LocalstackTestRunner.class)
29-
@LocalstackDockerProperties(services = { "s3", "sqs", "kinesis" })
30+
@LocalstackDockerProperties(services = { ServiceName.S3, "sqs", "kinesis" })
3031
public class MyCloudAppTest {
3132
3233
@Test
@@ -70,6 +71,7 @@ You can configure the Docker behaviour using the `@LocalstackDockerProperties` a
7071
|-----------------------------|------------------------------------------------------------------------------------------------------------------------------|------------------------------|---------------|
7172
| `pullNewImage` | Determines if a new image is pulled from the docker repo before the tests are run. | boolean | `false` |
7273
| `services` | Determines which services should be run when the localstack starts. | String[] | All |
74+
| `imageName` | Use a specific image name (organisation/repo) for docker container | String | `localstack/localstack` |
7375
| `imageTag` | Use a specific image tag for docker container | String | `latest` |
7476
| `portEdge` | Port number for the edge service, the main entry point for all API invocations | String | `4566` |
7577
| `portElasticSearch` | Port number for the elasticsearch service | String | `4571` |

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>cloud.localstack</groupId>
66
<artifactId>localstack-utils</artifactId>
77
<packaging>jar</packaging>
8-
<version>0.2.7</version>
8+
<version>0.2.8-SNAPSHOT</version>
99
<name>localstack-utils</name>
1010

1111
<description>Java utilities for the LocalStack platform.</description>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public void startup(LocalstackDockerConfiguration dockerConfiguration) {
6666
dockerConfiguration.getExternalHostName(),
6767
dockerConfiguration.isPullNewImage(),
6868
dockerConfiguration.isRandomizePorts(),
69+
dockerConfiguration.getImageName(),
6970
dockerConfiguration.getImageTag(),
7071
dockerConfiguration.getPortEdge(),
7172
dockerConfiguration.getPortElasticSearch(),

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Container {
2121
private static final Logger LOG = Logger.getLogger(Container.class.getName());
2222

2323
private static final String LOCALSTACK_NAME = "localstack/localstack";
24+
private static final String LOCALSTACK_TAG = "latest";
2425
private static final String LOCALSTACK_PORT_EDGE = "4566";
2526
private static final String LOCALSTACK_PORT_ELASTICSEARCH = "4571";
2627

@@ -47,16 +48,19 @@ public class Container {
4748
* @param pullNewImage determines if docker pull should be run to update to the latest image of the container
4849
* @param randomizePorts determines if the container should expose the default local stack ports or if it should expose randomized ports
4950
* in order to prevent conflicts with other localstack containers running on the same machine
51+
* @param imageName the name of the image defaults to {@value LOCALSTACK_NAME} if null
52+
* @param imageTag the tag of the image to pull, defaults to {@value LOCALSTACK_TAG} if null
5053
* @param environmentVariables map of environment variables to be passed to the docker container
5154
*/
5255
public static Container createLocalstackContainer(
53-
String externalHostName, boolean pullNewImage, boolean randomizePorts, String imageTag, String portEdge,
56+
String externalHostName, boolean pullNewImage, boolean randomizePorts, String imageName, String imageTag, String portEdge,
5457
String portElasticSearch, Map<String, String> environmentVariables, Map<Integer, Integer> portMappings) {
5558

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

59-
String fullImageName = LOCALSTACK_NAME + ":" + (imageTag == null ? "latest" : imageTag);
62+
String imageNameOrDefault = (imageName == null ? LOCALSTACK_NAME : imageName);
63+
String fullImageName = imageNameOrDefault + ":" + (imageTag == null ? LOCALSTACK_TAG : imageTag);
6064
boolean imageExists = new ListImagesCommand().execute().contains(fullImageName);
6165

6266
String fullPortEdge = (portEdge == null ? LOCALSTACK_PORT_EDGE : portEdge) + ":" + LOCALSTACK_PORT_EDGE;
@@ -65,10 +69,10 @@ public static Container createLocalstackContainer(
6569

6670
if(pullNewImage || !imageExists) {
6771
LOG.info("Pulling latest image...");
68-
new PullCommand(LOCALSTACK_NAME, imageTag).execute();
72+
new PullCommand(imageNameOrDefault, imageTag).execute();
6973
}
7074

71-
RunCommand runCommand = new RunCommand(LOCALSTACK_NAME, imageTag)
75+
RunCommand runCommand = new RunCommand(imageNameOrDefault, imageTag)
7276
.withExposedPorts(fullPortEdge, randomizePorts)
7377
.withExposedPorts(fullPortElasticSearch, randomizePorts)
7478
.withEnvironmentVariable(LOCALSTACK_EXTERNAL_HOSTNAME, externalHostName)
@@ -200,4 +204,11 @@ public void stop() {
200204
public String executeCommand(List<String> command) {
201205
return new ExecCommand(containerId).execute(command);
202206
}
207+
208+
/**
209+
* Returns the container ID which can be used to execute Docker CLI / API level commands on the container.
210+
*/
211+
String getContainerId() {
212+
return containerId;
213+
}
203214
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private LocalstackDockerConfiguration processDockerPropertiesAnnotation(Localsta
3535
.pullNewImage(properties.pullNewImage())
3636
.ignoreDockerRunErrors(properties.ignoreDockerRunErrors())
3737
.randomizePorts(properties.randomizePorts())
38+
.imageName(StringUtils.isEmpty(properties.imageName()) ? null : properties.imageName())
3839
.imageTag(StringUtils.isEmpty(properties.imageTag()) ? null : properties.imageTag())
3940
.portEdge(getEnvOrDefault("LOCALSTACK_EDGE_PORT", properties.portEdge()))
4041
.portElasticSearch(getEnvOrDefault("LOCALSTACK_ELASTICSEARCH_PORT", properties.portElasticSearch()))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class LocalstackDockerConfiguration {
2323

2424
private final boolean randomizePorts;
2525

26+
private final String imageName;
2627
private final String imageTag;
2728

2829
@Builder.Default

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
*/
4747
String[] services() default {};
4848

49+
/**
50+
* Use a specific image name (consisting of organisation and repository, e.g. localstack/localstack-full for docker container
51+
*/
52+
String imageName() default "";
53+
4954
/**
5055
* Use a specific image tag for docker container
5156
*/

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, null, null);
38+
EXTERNAL_HOST_NAME, pullNewImage, true, null, 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, null, null);
56+
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null, null, null, null);
5757

5858
try {
5959
container.waitForAllPorts(EXTERNAL_HOST_NAME);

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package cloud.localstack.docker;
22

3+
import cloud.localstack.Localstack;
4+
import cloud.localstack.docker.annotation.LocalstackDockerProperties;
35
import org.junit.Test;
6+
import org.junit.jupiter.api.extension.ExtendWith;
47

58
import java.util.ArrayList;
9+
import java.util.Arrays;
610
import java.util.HashMap;
711

812
import static org.junit.Assert.assertEquals;
@@ -22,7 +26,7 @@ public void createLocalstackContainer() throws Exception {
2226
HashMap<String, String> environmentVariables = new HashMap<>();
2327
environmentVariables.put(MY_PROPERTY, MY_VALUE);
2428
Container localStackContainer = Container.createLocalstackContainer(
25-
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null, environmentVariables, null);
29+
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, null, null, environmentVariables, null);
2630

2731
try {
2832
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
@@ -44,10 +48,43 @@ public void createLocalstackContainer() throws Exception {
4448
}
4549
}
4650

51+
@Test
52+
public void createLocalstackContainerWithFullImage() {
53+
54+
String customImageName = "localstack/localstack-full";
55+
Container localStackContainer = Container.createLocalstackContainer(
56+
EXTERNAL_HOST_NAME, pullNewImage, false, customImageName, null, null, null, null, null);
57+
58+
try {
59+
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
60+
61+
String imageName = new DockerExe()
62+
.execute(Arrays.asList("container", "inspect",
63+
localStackContainer.getContainerId(), "--format", "{{.Config.Image}}"));
64+
assertEquals(customImageName, imageName);
65+
}
66+
finally {
67+
localStackContainer.stop();
68+
}
69+
}
70+
71+
@ExtendWith(LocalstackDockerExtension.class)
72+
@LocalstackDockerProperties(imageName = "localstack/localstack-full")
73+
public static class ContainerTest1 {
74+
@org.junit.jupiter.api.Test
75+
public void imageName() {
76+
String imageName = new DockerExe()
77+
.execute(Arrays.asList("container", "inspect",
78+
Localstack.INSTANCE.getLocalStackContainer().getContainerId(),
79+
"--format", "{{.Config.Image}}"));
80+
assertEquals("localstack/localstack-full", imageName);
81+
}
82+
}
83+
4784
@Test
4885
public void createLocalstackContainerWithCustomPorts() throws Exception {
4986
Container localStackContainer = Container.createLocalstackContainer(
50-
EXTERNAL_HOST_NAME, pullNewImage, false, null, "45660", "45710", null, null);
87+
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, "45660", "45710", null, null);
5188

5289
try {
5390
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);
@@ -63,7 +100,7 @@ public void createLocalstackContainerWithCustomPorts() throws Exception {
63100
@Test
64101
public void createLocalstackContainerWithRandomPorts() throws Exception {
65102
Container localStackContainer = Container.createLocalstackContainer(
66-
EXTERNAL_HOST_NAME, pullNewImage, false, null, ":4566", ":4571", null, null);
103+
EXTERNAL_HOST_NAME, pullNewImage, false, null, null, ":4566", ":4571", null, null);
67104

68105
try {
69106
localStackContainer.waitForAllPorts(EXTERNAL_HOST_NAME);

0 commit comments

Comments
 (0)