Skip to content

Commit

Permalink
Merge pull request fabric8io#161 from officialpatterson/master
Browse files Browse the repository at this point in the history
Added new Feature so that volumes can be specified in POM
  • Loading branch information
mattnworb committed Jan 11, 2016
2 parents 1daa32e + 6903643 commit f0f580e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/spotify/docker/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public class BuildMojo extends AbstractDockerMojo {
@Parameter(property = "dockerEntryPoint")
private String entryPoint;

/** The volumes for the image */
@Parameter(property = "dockerVolumes")
private String[] volumes;

/** The cmd command for the image. Ignored if dockerDirectory is set. */
@Parameter(property = "dockerCmd")
private String cmd;
Expand Down Expand Up @@ -609,6 +613,13 @@ private void createDockerFile(final String directory, final List<String> filesTo
}
}

//Add VOLUMES to dockerfile
if (volumes != null) {
for (String volume : volumes) {
commands.add("VOLUME " + volume);
}
}

getLog().debug("Writing Dockerfile:" + System.lineSeparator() +
Joiner.on(System.lineSeparator()).join(commands));

Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/spotify/docker/BuildMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ public class BuildMojoTest extends AbstractMojoTestCase {
"ENTRYPOINT date",
"CMD [\"-u\"]"
);
private static final List<String> GENERATED_DOCKERFILEVOLUME = Arrays.asList(
"FROM busybox",
"MAINTAINER user",
"ENV FOO BAR",
"WORKDIR /opt/app",
"ADD resources/parent/child/child.xml resources/parent/child/",
"ADD resources/parent/parent.xml resources/parent/",
"ADD copy2.json .",
"RUN ln -s /a /b",
"RUN wget 127.0.0.1:8080",
"EXPOSE 8080 8081",
"USER app",
"ENTRYPOINT date",
"CMD [\"-u\"]",
"VOLUME /example0",
"VOLUME /example1",
"VOLUME /example2"
);

private static final List<String> PROFILE_GENERATED_DOCKERFILE = Arrays.asList(
"FROM busybox",
Expand All @@ -103,6 +121,23 @@ protected void setUp() throws Exception {
super.setUp();
deleteDirectory("target/docker");
}
//tests the docker volumes feature
public void testBuildWithDockerVolumes() throws Exception {
final File pom = getTestFile("src/test/resources/pom-build-docker-volumes.xml");
assertNotNull("Null pom.xml", pom);
assertTrue("pom.xml does not exist", pom.exists());

final BuildMojo mojo = setupMojo(pom);
final DockerClient docker = mock(DockerClient.class);

mojo.execute(docker);
verify(docker).build(eq(Paths.get("target/docker")), eq("busybox"),
any(AnsiProgressHandler.class));
assertFilesCopied();

assertEquals("wrong dockerfile contents", GENERATED_DOCKERFILEVOLUME,
Files.readAllLines(Paths.get("target/docker/Dockerfile"), UTF_8));
}

public void testBuildWithDockerDirectory() throws Exception {
final File pom = getTestFile("src/test/resources/pom-build-docker-directory.xml");
Expand Down
64 changes: 64 additions & 0 deletions src/test/resources/pom-build-docker-volumes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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>

<name>Docker Maven Plugin Test Pom</name>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<configuration>
<!-- a DockerFile should be generated since dockerDirectory is not specified -->
<baseImage>busybox</baseImage>
<maintainer>user</maintainer>
<dockerHost>http://host:2375</dockerHost>
<imageName>busybox</imageName>
<volumes>
<volume>/example0</volume>
<volume>/example1</volume>
<volume>/example2</volume>
</volumes>
<entryPoint>date</entryPoint>
<env>
<FOO>BAR</FOO>
</env>
<exposes>
<expose>8081</expose>
<expose>8080</expose>
</exposes>
<cmd>-u</cmd>
<!-- same copying tests as pom-build-docker-directory.xml, but make sure it works with auto-generated -->
<!-- docker file. pom-build-docker-directory.xml specified its own docker directory-->
<resources>
<resource>
<!-- test we handle all elements correctly -->
<targetPath>resources</targetPath>
<directory>src/test/resources/copy1</directory>
<include>**/*.xml</include>
<exclude>**/*exclude*</exclude>
</resource>
<resource>
<!-- test we handle missing elements correctly -->
<directory>src/test/resources/copy2</directory>
</resource>
</resources>
<runs>
<run>ln -s /a /b</run>
<run>wget 127.0.0.1:8080</run>
</runs>
<workdir>/opt/app</workdir>
<user>app</user>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit f0f580e

Please sign in to comment.