Skip to content

Commit

Permalink
Prevent docker:save failing when no images are defined. (#1196)
Browse files Browse the repository at this point in the history
* Issue #1185 Prevent docker:save failing when no images are defined.

Signed-off-by: William Rose <william.rose@nuance.com>

* chore(docker:save): Some refactoring
  • Loading branch information
rhuss authored Apr 9, 2019
1 parent c381544 commit 65109ef
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* **0.29-SNAPSHOT**

* **0.29.0** (2019-04-08)
- Avoid failing docker:save when no images with build configuration are present ([#1185](https://github.com/fabric8io/docker-maven-plugin/issues/1185))
- Reintroduce minimal API-VERSION parameter in order to support docker versions below apiVersion 1.25
- docs: Correct default image naming
- Proxy settings are being ignored ([#1148](https://github.com/fabric8io/docker-maven-plugin/issues/1148))
Expand Down
33 changes: 25 additions & 8 deletions src/main/java/io/fabric8/maven/docker/SaveMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class SaveMojo extends AbstractDockerMojo {

// Used when not automatically determined
private final static ArchiveCompression STANDARD_ARCHIVE_COMPRESSION = ArchiveCompression.gzip;
private static final ArchiveCompression STANDARD_ARCHIVE_COMPRESSION = ArchiveCompression.gzip;

@Component
private MavenProjectHelper projectHelper;
Expand All @@ -40,10 +40,13 @@ public class SaveMojo extends AbstractDockerMojo {

@Override
protected void executeInternal(ServiceHub serviceHub) throws DockerAccessException, MojoExecutionException {
if (skipSave) {

List<ImageConfiguration> images = getResolvedImages();
if (skipSaveFor(images)) {
return;
}
String imageName = getImageName();

String imageName = getImageName(images);
String fileName = getFileName(imageName);
ensureSaveDir(fileName);
log.info("Saving image %s to %s", imageName, fileName);
Expand All @@ -52,7 +55,22 @@ protected void executeInternal(ServiceHub serviceHub) throws DockerAccessExcepti
}

serviceHub.getDockerAccess().saveImage(imageName, fileName, ArchiveCompression.fromFileName(fileName));
}

private boolean skipSaveFor(List<ImageConfiguration> images) {
if (skipSave) {
log.info("docker:save skipped because `skipSave` config is set to true");
return true;
}

if (saveName == null &&
saveAlias == null &&
images.stream().allMatch(i -> i.getBuildConfiguration() == null)) {
log.info("docker:save skipped because no image has a build configuration defined");
return true;
}

return false;
}

private String getFileName(String iName) throws MojoExecutionException {
Expand Down Expand Up @@ -96,13 +114,12 @@ private void ensureSaveDir(String fileName) throws MojoExecutionException {
}
}

private String getImageName() throws MojoExecutionException {
List<ImageConfiguration> images = getResolvedImages();
private String getImageName(List<ImageConfiguration> images) throws MojoExecutionException {
// specify image by name or alias
if (saveName == null && saveAlias == null) {
List<ImageConfiguration> buildImages = getImagesWithBuildConfig(images);
if (buildImages.size() == 1) {
return buildImages.get(0).getName();
return buildImages.get(0).getName();
}
throw new MojoExecutionException("More than one image with build configuration is defined. Please specify the image with 'docker.name' or 'docker.alias'.");
}
Expand All @@ -115,8 +132,8 @@ private String getImageName() throws MojoExecutionException {
}
}
throw new MojoExecutionException(saveName != null ?
"Can not find image with name '" + saveName + "'" :
"Can not find image with alias '"+ saveAlias + "'");
"Can not find image with name '" + saveName + "'" :
"Can not find image with alias '"+ saveAlias + "'");
}

private List<ImageConfiguration> getImagesWithBuildConfig(List<ImageConfiguration> images) {
Expand Down
81 changes: 81 additions & 0 deletions src/test/java/io/fabric8/maven/docker/SaveMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.fabric8.maven.docker;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.Test;

import io.fabric8.maven.docker.access.DockerAccessException;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.service.ServiceHub;
import io.fabric8.maven.docker.util.Logger;
import mockit.Deencapsulation;
import mockit.Injectable;
import mockit.Mocked;
import mockit.Tested;

public class SaveMojoTest {

@Injectable
Logger log;

@Tested(fullyInitialized = false)
private SaveMojo saveMojo;

@Mocked
ServiceHub serviceHub;

@Test
public void noFailureWithEmptyImageList() throws DockerAccessException, MojoExecutionException {
Deencapsulation.setField(saveMojo, "images", Collections.<ImageConfiguration>emptyList());
Deencapsulation.setField(saveMojo, "resolvedImages", Collections.<ImageConfiguration>emptyList());

saveMojo.executeInternal(serviceHub);
}

@Test
public void noFailureWithEmptyBuildImageList() throws DockerAccessException, MojoExecutionException {
ImageConfiguration image = new ImageConfiguration.Builder()
.name("example:latest")
.build();
Deencapsulation.setField(saveMojo, "images", Collections.singletonList(image));
Deencapsulation.setField(saveMojo, "resolvedImages", Collections.singletonList(image));

saveMojo.executeInternal(serviceHub);
}

@Test(expected = MojoExecutionException.class)
public void failureWithMultipleBuildImageList() throws DockerAccessException, MojoExecutionException {
ImageConfiguration image1 = new ImageConfiguration.Builder()
.name("example1:latest")
.buildConfig(new BuildImageConfiguration.Builder()
.from("scratch")
.build())
.build();
ImageConfiguration image2 = new ImageConfiguration.Builder()
.name("example2:latest")
.buildConfig(new BuildImageConfiguration.Builder()
.from("scratch")
.build())
.build();

List<ImageConfiguration> images = Arrays.asList(image1, image2);
Deencapsulation.setField(saveMojo, "images", images);
Deencapsulation.setField(saveMojo, "resolvedImages", images);

saveMojo.executeInternal(serviceHub);
}

@Test(expected = MojoExecutionException.class)
public void failureWithSaveAliasAndName() throws DockerAccessException, MojoExecutionException {
Deencapsulation.setField(saveMojo, "saveAlias", "not-null");
Deencapsulation.setField(saveMojo, "saveName", "not-null");
Deencapsulation.setField(saveMojo, "images", Collections.singletonList(new ImageConfiguration()));
Deencapsulation.setField(saveMojo, "resolvedImages", Collections.singletonList(new ImageConfiguration()));

saveMojo.executeInternal(serviceHub);
}
}

0 comments on commit 65109ef

Please sign in to comment.