Skip to content

Commit

Permalink
[maven-release-plugin] prepare for next development iteration
Browse files Browse the repository at this point in the history
fix spotify#90 remove image mojo

fixing spotify#94 Remove image mojo now supports multiple tags

log the ids when removing images

ignore all items starting with dot (except .gitignore)
  • Loading branch information
spotify-helios-ci-agent authored and aharon hacmon committed Jul 29, 2015
1 parent a32f9bf commit ba74aa6
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Build products and artifacts
.*
!/.gitignore
*.html
.m2-repository/
target/
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.2.12</version>
<version>0.2.13-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>docker-maven-plugin</name>
<description>A maven plugin for docker</description>
Expand Down Expand Up @@ -34,7 +34,7 @@
<connection>scm:git:https://github.com/spotify/docker-maven-plugin</connection>
<developerConnection>scm:git:git@github.com:spotify/docker-maven-plugin</developerConnection>
<url>https://github.com/spotify/docker-maven-plugin</url>
<tag>v0.2.12</tag>
<tag>HEAD</tag>
</scm>

<distributionManagement>
Expand Down
41 changes: 32 additions & 9 deletions src/main/java/com/spotify/docker/RemoveImageMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@

package com.spotify.docker;

import static com.google.common.base.Strings.isNullOrEmpty;
import static com.spotify.docker.Utils.parseImageName;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.DockerException;
import com.spotify.docker.client.messages.RemovedImage;
import com.spotify.docker.client.shaded.javax.ws.rs.NotFoundException;
import com.spotify.docker.client.ImageNotFoundException;

/**
Expand All @@ -41,17 +48,33 @@ public class RemoveImageMojo extends AbstractDockerMojo {
@Parameter(property = "imageName", required = true)
private String imageName;

/** Additional tags to tag the image with. */
@Parameter(property = "dockerImageTags")
private List<String> imageTags;

protected void execute(DockerClient docker)
throws MojoExecutionException, DockerException, IOException, InterruptedException {

getLog().info("Removing -f " + imageName);

try {
// force the image to be removed but don't remove untagged parents
docker.removeImage(imageName, true, false);
} catch (ImageNotFoundException e) {
getLog().warn("Image " + imageName +
" does not exist and cannot be deleted - ignoring");
final String imageNameWithoutTag = parseImageName(imageName)[0];
if (imageTags == null){
imageTags = new ArrayList<String>();
imageTags.add("");
}
for (final String imageTag : imageTags) {
String currImagName = imageNameWithoutTag +
((isNullOrEmpty(imageTag)) ? "" : ( ":" + imageTag));
getLog().info("Removing -f " + currImagName);
try {
// force the image to be removed but don't remove untagged parents
for (RemovedImage removedImage: docker.removeImage(currImagName, true, false)){
getLog().info("Removed: " + removedImage.imageId());
}
} catch (ImageNotFoundException | NotFoundException e){
// ignoring 404 errors only
getLog().warn("Image " + imageName +
" does not exist and cannot be deleted - ignoring");
continue;
}
}

}
}
43 changes: 43 additions & 0 deletions src/test/java/com/spotify/docker/RemoveImageMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
package com.spotify.docker;

import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.DockerException;
import com.spotify.docker.client.ImageNotFoundException;
import com.spotify.docker.client.messages.RemovedImage;

import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.mockito.Mockito;

import java.io.File;
import java.util.ArrayList;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand All @@ -44,4 +49,42 @@ public void testRemoveImage() throws Exception {
verify(docker).removeImage("imageToRemove", true, false);
}

public void testRemoveMissingImage() throws Exception {
final File pom = getTestFile("src/test/resources/pom-removeImage.xml");
assertNotNull("Null pom.xml", pom);
assertTrue("pom.xml does not exist", pom.exists());

final RemoveImageMojo mojo = (RemoveImageMojo) lookupMojo("removeImage", pom);
assertNotNull(mojo);
final DockerClient docker = mock(DockerClient.class);
Mockito.when(docker.removeImage("imageToRemove", true, false)).thenThrow(new ImageNotFoundException("imageToRemove"));
try {
mojo.execute(docker);
verify(docker).removeImage("imageToRemove", true, false);
}
catch (DockerException e){
assertFalse("image to remove was missing", e instanceof ImageNotFoundException);
}
}

public void testRemoveImageWithTags() throws Exception {
final File pom = getTestFile("src/test/resources/pom-removeMultipleImages.xml");
assertNotNull("Null pom.xml", pom);
assertTrue("pom.xml does not exist", pom.exists());

final RemoveImageMojo mojo = (RemoveImageMojo) lookupMojo("removeImage", pom);
assertNotNull(mojo);
final DockerClient docker = mock(DockerClient.class);
Mockito.when(docker.removeImage("imageToRemove", true, false)).thenThrow(new ImageNotFoundException("imageToRemove"));
Mockito.when(docker.removeImage("imageToRemove:123456", true, false)).thenThrow(new ImageNotFoundException("imageToRemove:123456"));
Mockito.when(docker.removeImage("imageToRemove:bbbbbbb", true, false)).thenReturn(new ArrayList<RemovedImage>());
try {
mojo.execute(docker);
}
catch (DockerException e){
assertFalse("image to remove was missing", e instanceof ImageNotFoundException);
}
verify(docker).removeImage("imageToRemove:123456", true, false);
verify(docker).removeImage("imageToRemove:bbbbbbb", true, false);
}
}
51 changes: 51 additions & 0 deletions src/test/resources/pom-removeMultipleImages.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<configuration>
<dockerHost>http://host:2375</dockerHost>
<imageName>imageToRemove</imageName>
<imageTags>
<tag>123456</tag>
<tag>bbbbbbb</tag>
</imageTags>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit ba74aa6

Please sign in to comment.