Skip to content

Commit

Permalink
Add functionality to retrieve container by its name (#2314)
Browse files Browse the repository at this point in the history
Since `DockerComposeContainer#project` is private, there is currently no way of accessing containers started by Docker Compose.

This change adds a new API to the DockerComposeContainer to obtain a `ContainerState` reference by the service's name.

Fixes #2302.
  • Loading branch information
artjomka authored Feb 9, 2020
1 parent b111788 commit d4cf9d7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,30 @@
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.lifecycle.Startable;
import org.testcontainers.utility.*;
import org.testcontainers.utility.AuditLogger;
import org.testcontainers.utility.Base58;
import org.testcontainers.utility.CommandLine;
import org.testcontainers.utility.DockerLoggerFactory;
import org.testcontainers.utility.LogUtils;
import org.testcontainers.utility.MountableFile;
import org.testcontainers.utility.ResourceReaper;
import org.testcontainers.utility.TestcontainersConfiguration;
import org.zeroturnaround.exec.InvalidExitValueException;
import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.stream.slf4j.Slf4jStream;

import java.io.File;
import java.time.Duration;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -479,6 +494,10 @@ public SELF withRemoveImages(RemoveImages removeImages) {
return self();
}

public Optional<ContainerState> getContainerByServiceName(String serviceName) {
return Optional.ofNullable(serviceInstanceMap.get(serviceName));
}

private void followLogs(String containerId, Consumer<OutputFrame> consumer) {
LogUtils.followOutput(DockerClientFactory.instance().client(), containerId, consumer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.containers.DockerComposeContainer;

import java.io.File;
import java.util.Optional;

import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertFalse;
import static org.rnorth.visibleassertions.VisibleAssertions.assertNotNull;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;

/**
* Created by rnorth on 08/08/2015.
Expand All @@ -16,9 +22,8 @@ public class DockerComposeContainerTest extends BaseDockerComposeTest {

@Rule
public DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/compose-test.yml"))
.withExposedService("redis_1", REDIS_PORT)
.withExposedService("db_1", 3306)
;
.withExposedService("redis_1", REDIS_PORT)
.withExposedService("db_1", 3306);

@Override
protected DockerComposeContainer getEnvironment() {
Expand All @@ -33,4 +38,19 @@ public void testGetServicePort() {
assertNotNull("Port is set for service with instance number", serviceWithoutInstancePort);
assertEquals("Service ports are the same", serviceWithInstancePort, serviceWithoutInstancePort);
}

@Test
public void shouldRetrieveContainerByServiceName() {
String existingServiceName = "db_1";
Optional<ContainerState> result = environment.getContainerByServiceName(existingServiceName);
assertTrue(format("Container should be found by service name %s", existingServiceName), result.isPresent());
assertEquals("Mapped port for result container was wrong, probably wrong container found", result.get().getExposedPorts(), singletonList(3306));
}

@Test
public void shouldReturnEmptyResultOnNoneExistingService() {
String notExistingServiceName = "db_256";
Optional<ContainerState> result = environment.getContainerByServiceName(notExistingServiceName);
assertFalse(format("No container should be found under service name %s", notExistingServiceName), result.isPresent());
}
}

0 comments on commit d4cf9d7

Please sign in to comment.