Skip to content

Commit

Permalink
enable copyFileToContainer feature during container startup
Browse files Browse the repository at this point in the history
  • Loading branch information
dharanpu committed Jun 13, 2018
1 parent 3b974e9 commit a12aeae
Show file tree
Hide file tree
Showing 42 changed files with 214 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/out/production/resources/META-INF/services/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The contents of this folder is a workaround for issues described here: https://github.com/KostyaSha/docker-java-shade/issues/1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy
org.testcontainers.dockerclient.UnixSocketClientProviderStrategy
org.testcontainers.dockerclient.ProxiedUnixSocketClientProviderStrategy
org.testcontainers.dockerclient.DockerMachineClientProviderStrategy
org.testcontainers.dockerclient.WindowsClientProviderStrategy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.fasterxml.jackson.core.JsonFactory
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.fasterxml.jackson.databind.ObjectMapper
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.github.dockerjava.jaxrs.DockerCmdExecFactoryImpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.glassfish.jersey.internal.RuntimeDelegateImpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jvnet.hk2.external.generator.ServiceLocatorGeneratorImpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.testcontainers.shaded.org.jvnet.hk2.external.generator.ServiceLocatorGeneratorImpl
5 changes: 5 additions & 0 deletions core/out/test/resources/compose-dockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM alpine:3.2

ADD passthrough.sh /passthrough.sh

CMD /passthrough.sh
6 changes: 6 additions & 0 deletions core/out/test/resources/compose-dockerfile/passthrough.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env sh

while true; do
echo "Exposing env on port 3000"
env | nc -l -p 3000
done
6 changes: 6 additions & 0 deletions core/out/test/resources/compose-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
redis:
image: redis
db:
image: orchardup/mysql
environment:
MYSQL_DATABASE: wordpress
3 changes: 3 additions & 0 deletions core/out/test/resources/container-license-acceptance.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a
b

6 changes: 6 additions & 0 deletions core/out/test/resources/docker-compose-base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '2'
services:
alpine:
build: compose-dockerfile
environment:
bar: base
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: '2'
services:
alpine:
environment:
bar: overwritten
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"key\nwith\nnewlines"="1"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"key with spaces"="1"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"key\twith\ttab"="1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"line1"="1" \
"line2"="2" \
"line3"="3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"1"="value with spaces" \
"2"="value\nwith\nnewlines" \
"3"="value\twith\ttab"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["some\nmultiline\nargument"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["a","b","c"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
value
as \
is
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello\
world
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM alpine:3.7

HEALTHCHECK --interval=1s CMD test -e /testfile

COPY write_file_and_loop.sh write_file_and_loop.sh
RUN chmod +x write_file_and_loop.sh

CMD ["/write_file_and_loop.sh"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/ash

echo sleeping
sleep 2
echo writing file
touch /testfile

while true; do sleep 1; done
1 change: 1 addition & 0 deletions core/out/test/resources/invalid-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is not a valid docker-compose file
29 changes: 29 additions & 0 deletions core/out/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="debug">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.apache.http" level="WARN"/>
<logger name="com.github.dockerjava" level="WARN"/>
<logger name="org.zeroturnaround.exec" level="WARN"/>
<logger name="com.zaxxer.hikari" level="INFO"/>
<logger name="org.rnorth.tcpunixsocketproxy" level="INFO" />
<logger name="io.netty" level="WARN" />
<logger name="org.mongodb" level="INFO" />
<logger name="org.testcontainers.shaded" level="WARN"/>
<logger name="com.zaxxer.hikari" level="INFO"/>

<turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
<Marker>PROFILER</Marker>
<OnMatch>DENY</OnMatch>
</turboFilter>
</configuration>
6 changes: 6 additions & 0 deletions core/out/test/resources/mappable-dockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM alpine:3.2
ADD folder/someFile.txt /someFile.txt
RUN cat /someFile.txt
ADD test.txt /test.txt
RUN cat /test.txt
CMD ping -c 5 www.google.com
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FOOBAR
5 changes: 5 additions & 0 deletions core/out/test/resources/nginx_on_8080.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This configuration makes Nginx listen on port port 8080

server {
listen 8080;
}
Empty file.
2 changes: 2 additions & 0 deletions core/out/test/resources/scaled-compose-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
redis:
image: redis
1 change: 1 addition & 0 deletions core/out/test/resources/test-recursive-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Used for DirectoryTarResourceTest
2 changes: 2 additions & 0 deletions core/out/test/resources/test_copy_to_container.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Some Test
Message
6 changes: 6 additions & 0 deletions core/out/test/resources/v2-compose-test-passthrough.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '2'
services:
alpine:
build: compose-dockerfile
environment:
bar: ${foo}
9 changes: 9 additions & 0 deletions core/out/test/resources/v2-compose-test-with-network.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '2'
services:
redis:
image: redis
networks:
- redis-net

networks:
redis-net:
6 changes: 6 additions & 0 deletions core/out/test/resources/v2-compose-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '2'
services:
redis:
image: redis
ports:
- 6379
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ default SELF withFileSystemBind(String hostPath, String containerPath) {
*/
SELF withExposedPorts(Integer... ports);

/**
* Set the file to be copied before starting a created container
*
* @param mountableFile a Mountable file with path of source file / folder on host machine
* @param containerPath a destination path on conatiner to which the files / folders to be copied
* @return this
*/
SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath);

/**
* Add an environment variable to be passed to the container.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ default boolean isRunning() {
}
}

/**
* @return is the container created?
*/
default boolean isCreated() {
if (getContainerId() == null) {
return false;
}

try {
Boolean created = getCurrentContainerInfo().getState().getStatus().equalsIgnoreCase("Created");
return Boolean.TRUE.equals(created);
} catch (DockerException e) {
return false;
}
}

/**
* @return has the container health state 'healthy'?
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
@Nullable
private String workingDirectory = null;

private Map<MountableFile, String> copyToFileContainerPathMap = new HashMap<>();

/*
* Unique instance of DockerClient for use by this container object.
*/
Expand Down Expand Up @@ -231,6 +233,7 @@ private void tryStart(Profiler profiler) {
applyConfiguration(createCommand);

containerId = createCommand.exec().getId();
copyToFileContainerPathMap.forEach(this::copyFileToContainer);

logger().info("Starting container with ID: {}", containerId);
profiler.start("Start container");
Expand Down Expand Up @@ -835,6 +838,15 @@ public SELF withWorkingDirectory(String workDir) {
return self();
}

/**
* {@inheritDoc}
*/
@Override
public SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath) {
copyToFileContainerPathMap.put(mountableFile, containerPath);
return self();
}

/**
* Get the IP address that this container may be reached on (may not be the local machine).
*
Expand Down Expand Up @@ -940,8 +952,8 @@ public ExecResult execInContainer(String... command)
@Override
public void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) {

if (!isRunning()) {
throw new IllegalStateException("copyFileToContainer can only be used while the Container is running");
if (!isRunning() && !isCreated()) {
throw new IllegalStateException("copyFileToContainer can only be used with created / running container");
}

this.dockerClient
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.testcontainers.junit;

import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.MountableFile;
import java.io.File;

public class CopyFileToContainerTest {
private static String folderPath = new File("./src/test/resources/mappable-resource/").getAbsolutePath();
private static String containerPath = "/";
private static String fileName = "test-resource.txt";

@ClassRule
public static GenericContainer container = new GenericContainer("couchbase:latest")
.withCopyFileToContainer(MountableFile.forHostPath(folderPath), containerPath );

@Test
public void checkFileCopied() {
try {
String filesList = container.execInContainer("ls","/").getStdout();

Assert.assertTrue(filesList.contains(fileName));
} catch (Exception e) {
e.printStackTrace();
}

}

}

0 comments on commit a12aeae

Please sign in to comment.