Skip to content

Commit

Permalink
Put all docker compose files into COMPOSE_FILE environment variable (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zbstof authored and rnorth committed Jun 16, 2018
1 parent 89b60a3 commit 6a83e85
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.testcontainers.containers.BindMode.READ_ONLY;
import static org.testcontainers.containers.BindMode.READ_WRITE;
import static org.testcontainers.containers.ContainerisedDockerCompose.UNIX_PATH_SEPERATOR;

/**
* Container which launches Docker Compose, for the purposes of launching a defined set of containers.
Expand Down Expand Up @@ -623,9 +625,16 @@ public void invoke() {
final Map<String, String> environment = Maps.newHashMap(env);
environment.put(ENV_PROJECT_NAME, identifier);

final File dockerComposeBaseFile = composeFiles.get(0);
final File pwd = dockerComposeBaseFile.getAbsoluteFile().getParentFile().getAbsoluteFile();
environment.put(ENV_COMPOSE_FILE, new File(pwd, dockerComposeBaseFile.getAbsoluteFile().getName()).getAbsolutePath());

final List<String> absoluteDockerComposeFiles = composeFiles.stream()
.map(File::getAbsolutePath)
.map(MountableFile::forHostPath)
.map(MountableFile::getFilesystemPath)
.collect(toList());
final String composeFileEnvVariableValue = absoluteDockerComposeFiles.stream().collect(joining(UNIX_PATH_SEPERATOR + "")); // we always need the UNIX path separator
logger().debug("Set env COMPOSE_FILE={}", composeFileEnvVariableValue);
final File pwd = composeFiles.get(0).getAbsoluteFile().getParentFile().getAbsoluteFile();
environment.put(ENV_COMPOSE_FILE, composeFileEnvVariableValue);

logger().info("Local Docker Compose is running command: {}", cmd);

Expand Down

This file was deleted.

Empty file.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.testcontainers.junit;

import com.google.common.util.concurrent.Uninterruptibles;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.testcontainers.containers.DockerComposeContainer;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import static org.rnorth.visibleassertions.VisibleAssertions.info;
import static org.rnorth.visibleassertions.VisibleAssertions.pass;

@RunWith(Parameterized.class)
public class DockerComposeOverridesTest {

private static final File BASE_COMPOSE_FILE = new File("src/test/resources/docker-compose-base.yml");
private static final String BASE_ENV_VAR = "bar=base";
private static final File OVERRIDE_COMPOSE_FILE = new File("src/test/resources/docker-compose-non-default-override.yml");
private static final String OVERRIDE_ENV_VAR = "bar=overwritten";
private static final int SERVICE_PORT = 3000;
private static final String SERVICE_NAME = "alpine_1";

private final boolean localMode;
private final String expectedEnvVar;
private final File[] composeFiles;

public DockerComposeOverridesTest(boolean localMode, String expectedEnvVar, File... composeFiles) {
this.localMode = localMode;
this.expectedEnvVar = expectedEnvVar;
this.composeFiles = composeFiles;
}

@Parameters(name = "{index}: local[{0}], composeFiles[{2}], expectedEnvVar[{1}]")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][]{
{true, BASE_ENV_VAR, new File[]{BASE_COMPOSE_FILE}},
{true, OVERRIDE_ENV_VAR, new File[]{BASE_COMPOSE_FILE, OVERRIDE_COMPOSE_FILE}},
{false, BASE_ENV_VAR, new File[]{BASE_COMPOSE_FILE}},
{false, OVERRIDE_ENV_VAR, new File[]{BASE_COMPOSE_FILE, OVERRIDE_COMPOSE_FILE}},
});
}

@Test
public void test() {
try (DockerComposeContainer compose =
new DockerComposeContainer(composeFiles)
.withLocalCompose(localMode)
.withExposedService(SERVICE_NAME, SERVICE_PORT)) {

compose.start();

BufferedReader br = Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> {
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);

Socket socket = new Socket(compose.getServiceHost(SERVICE_NAME, SERVICE_PORT), compose.getServicePort(SERVICE_NAME, SERVICE_PORT));
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
});

Unreliables.retryUntilTrue(10, TimeUnit.SECONDS, () -> {
while (br.ready()) {
String line = br.readLine();
if (line.contains(expectedEnvVar)) {
pass("Mapped environment variable was found");
return true;
}
}
info("Mapped environment variable was not found yet - process probably not ready");
Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
return false;
});
}
}

}

0 comments on commit 6a83e85

Please sign in to comment.