Skip to content
Closed
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
22 changes: 14 additions & 8 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.core.command.ExecStartResultCallback;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.Synchronized;
Expand All @@ -31,6 +30,8 @@
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -53,20 +54,17 @@ public class DockerClientFactory {

public static final String SESSION_ID = UUID.randomUUID().toString();

public static final Map<String, String> DEFAULT_LABELS = ImmutableMap.of(
TESTCONTAINERS_LABEL, "true",
TESTCONTAINERS_SESSION_ID_LABEL, SESSION_ID
);
public static final Map<String, String> DEFAULT_LABELS = defaultLabels();

private static final String TINY_IMAGE = TestcontainersConfiguration.getInstance().getTinyImage();
private static DockerClientFactory instance;

private static DockerClientFactory instance;
// Cached client configuration

private DockerClientProviderStrategy strategy;
private boolean initialized = false;
private String activeApiVersion;
private String activeExecutionDriver;

@Getter(lazy = true)
private final boolean fileMountingSupported = checkMountableFile();

Expand Down Expand Up @@ -265,10 +263,10 @@ private <T> T runInsideDocker(DockerClient client, Consumer<CreateContainerCmd>

@VisibleForTesting
static class DiskSpaceUsage {

Optional<Long> availableMB = Optional.empty();
Optional<Integer> usedPercent = Optional.empty();
}

@VisibleForTesting
DiskSpaceUsage parseAvailableDiskSpace(String dfOutput) {
DiskSpaceUsage df = new DiskSpaceUsage();
Expand Down Expand Up @@ -314,8 +312,16 @@ public boolean isUsing(Class<? extends DockerClientProviderStrategy> providerStr
}

private static class NotEnoughDiskSpaceException extends RuntimeException {

NotEnoughDiskSpaceException(String message) {
super(message);
}
}

private static Map<String, String> defaultLabels() {
Map<String, String> map = new HashMap<>();
map.put(TESTCONTAINERS_LABEL, "true");
map.put(TESTCONTAINERS_SESSION_ID_LABEL, SESSION_ID);
return Collections.unmodifiableMap(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.Uninterruptibles;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -19,9 +18,18 @@
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.startupcheck.IndefiniteWaitOneShotStartupCheckStrategy;
import org.testcontainers.containers.wait.strategy.*;
import org.testcontainers.containers.wait.strategy.Wait;
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.yaml.snakeyaml.Yaml;
import org.zeroturnaround.exec.InvalidExitValueException;
import org.zeroturnaround.exec.ProcessExecutor;
Expand All @@ -30,10 +38,20 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
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.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -674,7 +692,7 @@ public void invoke() {
throw new ContainerLaunchException("Local Docker Compose not found. Is " + COMPOSE_EXECUTABLE + " on the PATH?");
}

final Map<String, String> environment = Maps.newHashMap(env);
final Map<String, String> environment = new HashMap<>(env);
environment.put(ENV_PROJECT_NAME, identifier);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.github.dockerjava.api.model.VolumesFrom;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NonNull;
Expand All @@ -28,7 +27,6 @@
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.runner.Description;
Expand Down Expand Up @@ -71,6 +69,7 @@
import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
Expand All @@ -93,7 +92,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.collect.Lists.newArrayList;
import static org.testcontainers.utility.CommandLine.runShellCommand;

/**
Expand All @@ -104,7 +102,7 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
extends FailureDetectingExternalResource
implements Container<SELF>, AutoCloseable, WaitStrategyTarget, Startable {

private static final Charset UTF8 = Charset.forName("UTF-8");
private static final Charset UTF8 = StandardCharsets.UTF_8;

public static final int CONTAINER_RUNNING_TIMEOUT_SEC = 30;

Expand Down Expand Up @@ -435,7 +433,7 @@ final String hash(CreateContainerCmd createCommand) {
Optional<String> findContainerForReuse(String hash) {
// TODO locking
return dockerClient.listContainersCmd()
.withLabelFilter(ImmutableMap.of(HASH_LABEL, hash))
.withLabelFilter(labelFilterFor(hash))
.withLimit(1)
.withStatusFilter(Arrays.asList("running"))
.exec()
Expand All @@ -444,6 +442,12 @@ Optional<String> findContainerForReuse(String hash) {
.map(it -> it.getId());
}

private Map<String, String> labelFilterFor(String hash) {
Map<String, String> labels = new HashMap<>();
labels.put(HASH_LABEL, hash);
return Collections.unmodifiableMap(labels);
}

/**
* Set any custom settings for the create command such as shared memory size.
*/
Expand Down Expand Up @@ -926,7 +930,7 @@ protected void finished(Description description) {
*/
@Override
public SELF withExposedPorts(Integer... ports) {
this.setExposedPorts(newArrayList(ports));
this.setExposedPorts(new ArrayList<>(Arrays.asList(ports)));
return self();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
import com.google.common.base.Charsets;

import java.nio.charset.StandardCharsets;

/**
* Holds a frame of container output (usually one line, possibly more)
Expand Down Expand Up @@ -33,7 +34,7 @@ public String getUtf8String() {
return "";
}

return new String(bytes, Charsets.UTF_8);
return new String(bytes, StandardCharsets.UTF_8);
}

public enum OutputType {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.testcontainers.containers.output;

import com.google.common.base.Charsets;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Created by rnorth on 26/03/2016.
Expand Down Expand Up @@ -33,7 +32,7 @@ public void accept(OutputFrame outputFrame) {

public String toUtf8String() {
byte[] bytes = stringBuffer.toByteArray();
return new String(bytes, Charsets.UTF_8);
return new String(bytes, StandardCharsets.UTF_8);
}

public String toString(Charset charset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.github.dockerjava.core.SSLConfig;
import com.github.dockerjava.core.WebTarget;
import com.github.dockerjava.core.exec.PingCmdExec;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.MultimapBuilder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -22,9 +21,12 @@
import java.net.InetAddress;
import java.net.URI;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

import static java.util.Collections.unmodifiableList;

@Slf4j
public class OkHttpDockerCmdExecFactory extends AbstractDockerCmdExecFactory {

Expand Down Expand Up @@ -107,7 +109,7 @@ protected WebTarget getBaseResource() {
return new OkHttpWebTarget(
okHttpClient,
baseUrl,
ImmutableList.of(),
unmodifiableList(new ArrayList<>()),
MultimapBuilder.hashKeys().hashSetValues().build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.github.dockerjava.core.InvocationBuilder;
import com.github.dockerjava.core.WebTarget;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.SetMultimap;
import lombok.SneakyThrows;
import lombok.Value;
Expand All @@ -15,7 +14,10 @@
import okhttp3.OkHttpClient;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand All @@ -32,7 +34,7 @@ class OkHttpWebTarget implements WebTarget {

HttpUrl baseUrl;

ImmutableList<String> path;
List<String> path;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class is package-private so no backward compatibility issues


SetMultimap<String, String> queryParams;

Expand Down Expand Up @@ -64,22 +66,20 @@ public InvocationBuilder request() {

@Override
public OkHttpWebTarget path(String... components) {
return this.withPath(
ImmutableList.<String>builder()
.addAll(path)
.add(components)
.build()
);
List<String> newPath = new ArrayList<>(path);
Collections.addAll(newPath, components);

return this.withPath(Collections.unmodifiableList(newPath));
}

@Override
public OkHttpWebTarget resolveTemplate(String name, Object value) {
ImmutableList.Builder<String> newPath = ImmutableList.builder();
List<String> newPath = new ArrayList<>();
for (String component : path) {
component = component.replaceAll("\\{" + name + "\\}", value.toString());
newPath.add(component);
}
return this.withPath(newPath.build());
return this.withPath(newPath);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.BuildResponseItem;
import com.github.dockerjava.core.command.BuildImageResultCallback;
import com.google.common.collect.Sets;
import lombok.Cleanup;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -31,7 +29,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.zip.GZIPOutputStream;

@Slf4j
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.testcontainers.utility;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import lombok.experimental.UtilityClass;

import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
Expand All @@ -18,7 +18,7 @@ public class LicenseAcceptance {
public static void assertLicenseAccepted(final String imageName) {
try {
final URL url = Resources.getResource(ACCEPTANCE_FILE_NAME);
final List<String> acceptedLicences = Resources.readLines(url, Charsets.UTF_8);
final List<String> acceptedLicences = Resources.readLines(url, StandardCharsets.UTF_8);

if (acceptedLicences.stream().map(String::trim).anyMatch(imageName::equals)) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.testcontainers.utility;

import com.google.common.base.Charsets;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,6 +17,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -148,7 +148,7 @@ private static URL getClasspathResource(@NotNull final String resourcePath, @Not
private static String unencodeResourceURIToFilePath(@NotNull final String resource) {
try {
// Convert any url-encoded characters (e.g. spaces) back into unencoded form
return URLDecoder.decode(resource.replaceAll("\\+", "%2B"), Charsets.UTF_8.name())
return URLDecoder.decode(resource.replaceAll("\\+", "%2B"), StandardCharsets.UTF_8.toString())
.replaceFirst("jar:", "")
.replaceFirst("file:", "")
.replaceAll("!.*", "");
Expand Down
Loading