Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide exception messages that occurred during stop #1435

Merged
merged 5 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
31 changes: 31 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 @@ -43,7 +44,9 @@
import mockit.Mocked;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
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 +280,34 @@ 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);

Exception thrownException = assertThrows(DockerAccessException.class, () -> runService.stopStartedContainers(false, true, true, testLabel));
assertEquals("(TEST two,TEST one)", thrownException.getLocalizedMessage());
}

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