Skip to content

Commit

Permalink
Provide exception messages that caused stop exceptions, rather than g…
Browse files Browse the repository at this point in the history
…eneric message - related to #1251

Signed-off-by: Doyle Young <dyoung@gmail.com>
  • Loading branch information
doyleyoung authored and rohanKanojia committed Mar 8, 2021
1 parent ad7ef1a commit 2a43f2d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/java/io/fabric8/maven/docker/service/RunService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringJoiner;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

Expand Down Expand Up @@ -222,10 +223,16 @@ public void stopStartedContainers(boolean keepContainer,
thrownExceptions.add(exc);
}
if (!thrownExceptions.isEmpty()) {
DockerAccessException exception = new DockerAccessException("At least one exception thrown during container removal.");
StringJoiner description = new StringJoiner(",", "(", ")");
for (DockerAccessException dae : thrownExceptions) {
exception.addSuppressed(dae);
description.add(dae.getLocalizedMessage());
}

DockerAccessException exception = new DockerAccessException(description.toString());
for (DockerAccessException dae : thrownExceptions) {
exception.addSuppressed(dae);
}

throw exception;
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/io/fabric8/maven/docker/service/RunServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.fabric8.maven.docker.access.ExecException;
import io.fabric8.maven.docker.config.StopMode;
import io.fabric8.maven.docker.config.VolumeConfiguration;
import io.fabric8.maven.docker.util.GavLabel;
import org.apache.commons.io.IOUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -44,6 +45,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* This test need to be refactored. In fact, testing Mojos must be setup correctly at all. Blame on me that there are so
Expand Down Expand Up @@ -277,6 +279,38 @@ public void testWithException() throws Exception {
runService.stopContainer(container, createImageConfig(SHUTDOWN_WAIT, 0), false, false);
}

@Test
public void testWithMultipleStopExceptions() throws Exception {
GavLabel testLabel = new GavLabel("Im:A:Test");

String firstName = "first-container:latest";
ImageConfiguration first = new ImageConfiguration();
first.setName(firstName);

String secondName = "second-container:latest";
ImageConfiguration second = new ImageConfiguration();
second.setName(secondName);

tracker.registerContainer(firstName, first, testLabel);
tracker.registerContainer(secondName, second, testLabel);

LogOutputSpecFactory logOutputSpecFactory = new LogOutputSpecFactory(true, true, null);

new Expectations(){{
docker.stopContainer(firstName, 0); result = new DockerAccessException("TEST one");
docker.stopContainer(secondName, 0); result = new DockerAccessException("TEST two");
}};

runService = new RunService(docker, queryService, tracker, logOutputSpecFactory, log);

try {
runService.stopStartedContainers(false, true, true, testLabel);
fail("Should have thrown exception");
} catch (DockerAccessException | ExecException e) {
assertEquals(e.getLocalizedMessage(), "(TEST two,TEST one)");
}
}

@Test
public void testVolumesDuringStart() throws DockerAccessException {
ServiceHub hub = new ServiceHubFactory().createServiceHub(project, session, docker, log, new LogOutputSpecFactory(true, true, null));
Expand Down

0 comments on commit 2a43f2d

Please sign in to comment.