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

Combine DB container tests into single module, and improve error handling/display #243

Merged
merged 6 commits into from
Nov 19, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ public void addEnv(String key, String value) {
*/
@Override
public void addFileSystemBind(String hostPath, String containerPath, BindMode mode) {

if (hostPath.contains(".jar!")) {
// the host file is inside a JAR resource - copy to a temporary location that Docker can read
hostPath = PathUtils.extractClassPathResourceToTempLocation(hostPath);
}

if (SystemUtils.IS_OS_WINDOWS) {
hostPath = PathUtils.createMinGWPath(hostPath);
}
Expand Down Expand Up @@ -815,6 +821,10 @@ public ExecResult execInContainer(Charset outputCharset, String... command)

}

if (!isRunning()) {
Copy link
Member

Choose a reason for hiding this comment

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

nice catch!

throw new IllegalStateException("Container is not running so exec cannot be run");
}

this.dockerClient
.execCreateCmd(this.containerId)
.withCmd(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public void onNext(Frame frame) {
}
}

@Override
public void onError(Throwable throwable) {
// Sink any errors
try {
close();
} catch (IOException ignored) { }
}

@Override
public void close() throws IOException {
// send an END frame to every consumer... but only once per consumer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.slf4j.Logger;

import java.util.function.Consumer;
import java.util.regex.Pattern;

/**
* A consumer for container output that logs output to an SLF4J logger.
Expand All @@ -11,6 +12,8 @@ public class Slf4jLogConsumer implements Consumer<OutputFrame> {
private final Logger logger;
private String prefix = "";

private static final Pattern ANSI_CODE_PATTERN = Pattern.compile("\\[\\d[ABCD]");

public Slf4jLogConsumer(Logger logger) {
this.logger = logger;
}
Expand All @@ -28,6 +31,11 @@ public void accept(OutputFrame outputFrame) {
if (utf8String != null) {
OutputFrame.OutputType outputType = outputFrame.getType();
String message = utf8String.trim();

if (ANSI_CODE_PATTERN.matcher(message).matches()) {
return;
}

switch (outputType) {
case END:
break;
Expand Down
81 changes: 77 additions & 4 deletions core/src/main/java/org/testcontainers/utility/PathUtils.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package org.testcontainers.utility;

import lombok.NonNull;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.io.InputStream;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import static org.slf4j.LoggerFactory.getLogger;

/**
* Filesystem operation utility methods.
*/
public class PathUtils {

private static final Logger log = getLogger(PathUtils.class);

/**
* Recursively delete a directory and all its subdirectories and files.
* @param directory path to the directory to delete.
Expand Down Expand Up @@ -67,4 +74,70 @@ public static String createMinGWPath(String path) {
mingwPath = mingwPath.replace(":","");
return mingwPath;
}

/**
* Extract a file or directory tree from a JAR file to a temporary location.
* This allows Docker to mount classpath resources as files.
* @param hostPath the path on the host, expected to be of the format 'file:/path/to/some.jar!/classpath/path/to/resource'
* @return the path of the temporary file/directory
*/
public static String extractClassPathResourceToTempLocation(final String hostPath) {
File tmpLocation = new File(".testcontainers-tmp-" + Base58.randomString(5));
//noinspection ResultOfMethodCallIgnored
tmpLocation.delete();

String jarPath = hostPath.replaceFirst("file:", "").replaceAll("!.*", "");
String internalPath = hostPath.replaceAll("[^!]*!/", "");

try (JarFile jarFile = new JarFile(jarPath)) {
Enumeration<JarEntry> entries = jarFile.entries();

while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
final String name = entry.getName();
if (name.startsWith(internalPath)) {
log.debug("Copying classpath resource(s) from {} to {} to permit Docker to bind",
hostPath,
tmpLocation);
copyFromJarToLocation(jarFile, entry, internalPath, tmpLocation);
}
}

} catch (IOException e) {
throw new IllegalStateException("Failed to process JAR file when extracting classpath resource: " + hostPath, e);
}

// Mark temporary files/dirs for deletion at JVM shutdown
deleteOnExit(tmpLocation.toPath());

return tmpLocation.getAbsolutePath();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private static void copyFromJarToLocation(final JarFile jarFile,
final JarEntry entry,
final String fromRoot,
final File toRoot) throws IOException {

String destinationName = entry.getName().replaceFirst(fromRoot, "");
File newFile = new File(toRoot, destinationName);

if (!entry.isDirectory()) {
// Create parent directories
newFile.mkdirs();
newFile.delete();
newFile.deleteOnExit();

try (InputStream is = jarFile.getInputStream(entry)) {
Files.copy(is, newFile.toPath());
} catch (IOException e) {
log.error("Failed to extract classpath resource " + entry.getName() + " from JAR file " + jarFile.getName(), e);
throw e;
}
}
}

public static void deleteOnExit(final Path path) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> recursiveDeleteDir(path)));
}
}
17 changes: 17 additions & 0 deletions core/src/main/java/org/testcontainers/utility/ResourceReaper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.InternalServerErrorException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Network;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -67,6 +68,8 @@ public void registerContainerForCleanup(String containerId, String imageName) {
*/
public void stopAndRemoveContainer(String containerId) {
stopContainer(containerId, registeredContainers.get(containerId));

registeredContainers.remove(containerId);
Copy link
Member

Choose a reason for hiding this comment

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

maybe we should do it in finally {} block?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually even though it's a little brittle I'd prefer to keep it that way. If stopContainer fails for some reason, my feeling is that it's slightly better to potentially try again by leaving the containerId in the registered set.

}

/**
Expand All @@ -83,6 +86,13 @@ public void stopAndRemoveContainer(String containerId, String imageName) {

private void stopContainer(String containerId, String imageName) {

List<Container> allContainers = dockerClient.listContainersCmd().withShowAll(true).exec();

if (allContainers.stream().map(Container::getId).noneMatch(containerId::equals)) {
LOGGER.trace("Was going to clean up container but it apparently no longer exists: {}");
return;
}

boolean running;
try {
InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(containerId).exec();
Expand All @@ -105,6 +115,13 @@ private void stopContainer(String containerId, String imageName) {
}
}

try {
dockerClient.inspectContainerCmd(containerId).exec();
} catch (NotFoundException e) {
LOGGER.trace("Was going to remove container but it apparently no longer exists: {}");
return;
}

try {
LOGGER.trace("Removing container: {}", containerId);
try {
Expand Down
117 changes: 117 additions & 0 deletions modules/jdbc-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-parent</artifactId>
<version>1.1.7-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>jdbc-test</artifactId>
<name>TestContainers :: JDBC :: Tests</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mysql</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>postgresql</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mariadb</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Database drivers for testing -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1101-jdbc41</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>1.4.6</version>
<scope>test</scope>
</dependency>

<!-- Database connection pools for testing -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java6</artifactId>
<version>2.3.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.5.4</version>
</dependency>
<dependency>
<groupId>org.vibur</groupId>
<artifactId>vibur-dbcp</artifactId>
<version>9.0</version>
</dependency>

<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>proprietary-deps</id>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>oracle-xe</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Oracle JDBC Driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>test-proprietary-deps</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<testSourceDirectory>src/testProprietary/java</testSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Loading