Skip to content

Commit

Permalink
Issue fabric8io#1185 Prevent docker:save failing when no images are d…
Browse files Browse the repository at this point in the history
…efined.

Signed-off-by: William Rose <william.rose@nuance.com>
  • Loading branch information
wrosenuance committed Apr 9, 2019
1 parent c381544 commit fa0fb8d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 15 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
37 changes: 22 additions & 15 deletions src/main/java/io/fabric8/maven/docker/SaveMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,27 @@ protected void executeInternal(ServiceHub serviceHub) throws DockerAccessExcepti
if (skipSave) {
return;
}
String imageName = getImageName();

List<ImageConfiguration> images = getResolvedImages();
List<ImageConfiguration> buildImages = getImagesWithBuildConfig(images);
String imageName;

if (saveName == null && saveAlias == null) {
if (buildImages.isEmpty()) {
log.info("No images have a build configuration defined: save skipped");
return;
}
if (buildImages.size() == 1) {
imageName = buildImages.get(0).getName();
} else {
throw new MojoExecutionException("More than one image with build configuration is defined. Please specify the image with 'docker.name' or 'docker.alias'.");
}
} else if (saveName != null && saveAlias != null) {
throw new MojoExecutionException("Cannot specify both name and alias.");
} else {
imageName = getImageName(images);
}

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

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

}

private String getFileName(String iName) throws MojoExecutionException {
Expand Down Expand Up @@ -96,19 +115,7 @@ private void ensureSaveDir(String fileName) throws MojoExecutionException {
}
}

private String getImageName() throws MojoExecutionException {
List<ImageConfiguration> images = getResolvedImages();
// 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();
}
throw new MojoExecutionException("More than one image with build configuration is defined. Please specify the image with 'docker.name' or 'docker.alias'.");
}
if (saveName != null && saveAlias != null) {
throw new MojoExecutionException("Cannot specify both name and alias.");
}
private String getImageName(List<ImageConfiguration> images) throws MojoExecutionException {
for (ImageConfiguration ic : images) {
if (equalName(ic) || equalAlias(ic)) {
return ic.getName();
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 fa0fb8d

Please sign in to comment.