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

Improve the usability of GenericContainer #550

Merged
merged 3 commits into from
Jan 27, 2018
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ All notable changes to this project will be documented in this file.

### Changed
- Added Kafka module ([\#546](https://github.com/testcontainers/testcontainers-java/pull/546))
- Environment variables are now stored as Map instead of List ([\#550](https://github.com/testcontainers/testcontainers-java/pull/550))
- Added `withEnv(String name, Function<Optional<String>, String> mapper)` with optional previous value ([\#550](https://github.com/testcontainers/testcontainers-java/pull/550))
- Added `withFileSystemBind` overloaded method with `READ_WRITE` file mode by default ([\#550](https://github.com/testcontainers/testcontainers-java/pull/550))

## [1.5.1] - 2017-12-19

Expand Down
47 changes: 47 additions & 0 deletions core/src/main/java/org/testcontainers/containers/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.function.Function;

public interface Container<SELF extends Container<SELF>> extends LinkableContainer {

Expand Down Expand Up @@ -135,6 +137,17 @@ default void addFileSystemBind(final String hostPath, final String containerPath
*/
SELF waitingFor(@NonNull WaitStrategy waitStrategy);

/**
* Adds a file system binding.
*
* @param hostPath the file system path on the host
* @param containerPath the file system path inside the container
* @return this
*/
default SELF withFileSystemBind(String hostPath, String containerPath) {
return withFileSystemBind(hostPath, containerPath, BindMode.READ_WRITE);
}

/**
* Adds a file system binding.
*
Expand Down Expand Up @@ -171,6 +184,18 @@ default void addFileSystemBind(final String hostPath, final String containerPath
*/
SELF withEnv(String key, String value);

/**
* Add an environment variable to be passed to the container.
*
* @param key environment variable key
* @param mapper environment variable value mapper, accepts old value as an argument
* @return this
*/
default SELF withEnv(String key, Function<Optional<String>, String> mapper) {
Optional<String> oldValue = Optional.ofNullable(getEnvMap().get(key));
return withEnv(key, mapper.apply(oldValue));
}

/**
* Add environment variables to be passed to the container.
*
Expand Down Expand Up @@ -383,6 +408,11 @@ default Integer getFirstMappedPort() {
*/
SELF withLogConsumer(Consumer<OutputFrame> consumer);

/**
*
* @deprecated please use {@code org.testcontainers.DockerClientFactory.instance().client().infoCmd().exec()}
*/
@Deprecated
Info fetchDockerDaemonInfo() throws IOException;

/**
Expand Down Expand Up @@ -438,8 +468,15 @@ ExecResult execInContainer(Charset outputCharset, String... command)

Future<String> getImage();

/**
*
* @deprecated use getEnvMap
*/
@Deprecated
List<String> getEnv();

Map<String, String> getEnvMap();

String[] getCommandParts();

List<Bind> getBinds();
Expand All @@ -452,12 +489,22 @@ ExecResult execInContainer(Charset outputCharset, String... command)

DockerClient getDockerClient();

/**
*
* @deprecated please use {@code org.testcontainers.DockerClientFactory.instance().client().infoCmd().exec()}
*/
@Deprecated
Info getDockerDaemonInfo();

String getContainerId();

String getContainerName();

/**
*
* @deprecated please use {@code org.testcontainers.DockerClientFactory.instance().client().inspectContainerCmd(container.getContainerId()).exec()}
*/
@Deprecated
InspectContainerResponse getContainerInfo();

void setExposedPorts(List<Integer> exposedPorts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
private Future<String> image;

@NonNull
private List<String> env = new ArrayList<>();
private Map<String, String> env = new HashMap<>();

@NonNull
private String[] commandParts = new String[0];
Expand Down Expand Up @@ -396,7 +396,8 @@ private void applyConfiguration(CreateContainerCmd createCommand) {
createCommand.withCmd(commandParts);
}

String[] envArray = env.stream()
String[] envArray = env.entrySet().stream()
.map(it -> it.getKey() + "=" + it.getValue())
.toArray(String[]::new);
createCommand.withEnv(envArray);

Expand Down Expand Up @@ -533,12 +534,37 @@ public void setCommand(@NonNull String... commandParts) {
this.commandParts = commandParts;
}

@Override
public Map<String, String> getEnvMap() {
return env;
}

/**
* {@inheritDoc}
*/
@Override
public List<String> getEnv() {
return env.entrySet().stream()
.map(it -> it.getKey() + "=" + it.getValue())
.collect(Collectors.toList());
}

@Override
public void setEnv(List<String> env) {
this.env = env.stream()
.map(it -> it.split("="))
.collect(Collectors.toMap(
it -> it[0],
it -> it[1]
));
}

/**
* {@inheritDoc}
*/
@Override
public void addEnv(String key, String value) {
env.add(key + "=" + value);
env.put(key, value);
}

/**
Expand Down Expand Up @@ -886,6 +912,9 @@ public SELF withLogConsumer(Consumer<OutputFrame> consumer) {
return self();
}

/**
* {@inheritDoc}
*/
@Override
public synchronized Info fetchDockerDaemonInfo() throws IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ public static void setupContent() throws FileNotFoundException {
* dirty way for testing.
*/
@ClassRule
public static GenericContainer alpineEnvVar = new GenericContainer("alpine:3.2")
public static GenericContainer alpineEnvVar = new GenericContainer<>("alpine:3.2")
.withExposedPorts(80)
.withEnv("MAGIC_NUMBER", "42")
.withEnv("MAGIC_NUMBER", "4")
.withEnv("MAGIC_NUMBER", oldValue -> oldValue.orElse("") + "2")
.withCommand("/bin/sh", "-c", "while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done");

/**
Expand Down