Skip to content

Commit

Permalink
Merge branch 'fix_1168_more_robust_stop_procedure' of https://github.…
Browse files Browse the repository at this point in the history
…com/j3t/docker-maven-plugin into fix_1168_more_robust_stop_procedure
  • Loading branch information
Jens Thielscher committed Apr 19, 2021
2 parents ac614a2 + cfa300e commit 71e3cc0
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 23 deletions.
2 changes: 0 additions & 2 deletions src/main/java/io/fabric8/maven/docker/StopMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ private Collection<Container> getContainersForImage(QueryService queryService, I

return ContainerNamingUtil.getContainersToStop(image,
containerNamePattern,
getBuildTimestamp(),
queryService.getContainersForImage(image.getName(), !keepContainer));
}

Expand Down Expand Up @@ -213,7 +212,6 @@ private Set<Network> getNetworksToRemove(QueryService queryService, GavLabel gav
Collection<Container> existingContainers =
ContainerNamingUtil.getContainersToStop(image,
containerNamePattern,
getBuildTimestamp(),
queryService.getContainersForImage(image.getName(), !keepContainer));

for (Container container : existingContainers) {
Expand Down
73 changes: 60 additions & 13 deletions src/test/java/io/fabric8/maven/docker/StopMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.util.Collections;
import java.util.List;

import javax.print.Doc;

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

Expand Down Expand Up @@ -224,7 +222,7 @@ public void stopWithSingleImageNotLabelledButAllContainersTrue() throws IOExcept
* Mock project with one image, query service indicates three running images, none labelled,
* but allContainers is true.
*
* The containers are named with ascending indices - only the last should be stopped.
* The containers are named with ascending indices - all three containers should be stopped.
*
* @throws IOException
* @throws MojoExecutionException
Expand All @@ -245,17 +243,16 @@ public void stopWithMultipleImagesCheckLast(@Mocked Container running1, @Mocked

thenContainerLookupByImageOccurs("example:latest");
thenListContainersIsNotCalled();
thenContainerIsNotStopped("container-id-1");
thenContainerIsNotStopped("container-id-2");
thenContainerIsStopped("container-id-1", false, false);
thenContainerIsStopped("container-id-2", false, false);
thenContainerIsStopped("container-id-3", false, false);
}

/**
* Mock project with one image, query service indicates three running images, none labelled,
* but allContainers is true.
*
* The containers are named with ascending indices, but there's a gap, so the last before
* the gap and all the ones after are stopped.
* The containers are named with ascending indices, but there's a gap (all three containers schould be stopped).
*
* @throws IOException
* @throws MojoExecutionException
Expand All @@ -277,7 +274,7 @@ public void stopWithMultipleImagesIndexPatternMissingIndices(@Mocked Container r

thenContainerLookupByImageOccurs("example:latest");
thenListContainersIsNotCalled();
thenContainerIsNotStopped("container-id-1");
thenContainerIsStopped("container-id-1", false, false);
thenContainerIsStopped("container-id-2", false, false);
thenContainerIsStopped("container-id-4", false, false);
}
Expand All @@ -286,8 +283,7 @@ public void stopWithMultipleImagesIndexPatternMissingIndices(@Mocked Container r
* Mock project with one image, query service indicates three running images, none labelled,
* but allContainers is true.
*
* The containers are named with ascending indices - but this doesn't match the container
* name pattern, so all are stopped.
* The containers are named with ascending indices - but this doesn't match the containerNamePattern so that all containers are kept.
*
* @throws IOException
* @throws MojoExecutionException
Expand All @@ -305,16 +301,67 @@ public void stopWithMultipleImagesNoIndexNamePattern(@Mocked Container running1,
givenRunningContainer(running3, "container-id-3", "example-3");
givenContainersAreRunningForImage("example:latest", running1, running2, running3);

// If name pattern doesn't contain index placeholder, all containers are stopped
Deencapsulation.setField(stopMojo, "containerNamePattern", "%n");

whenMojoExecutes();

thenContainerLookupByImageOccurs("example:latest");
thenListContainersIsNotCalled();
thenContainerIsNotStopped("container-id-1");
thenContainerIsNotStopped("container-id-2");
thenContainerIsNotStopped("container-id-3");
}

/**
* Mock project with one image, query service indicates two running containers, both labelled with GAV.
*
* The containers are named with ascending indices but the containerNamePattern is set to %a (alias naming strategy) and the
* alias is set to the name of the first container so that only the first container is stopped.
*/
@Test
public void stopWithAliasContainerNamePattern(@Mocked Container running1, @Mocked Container running2)
throws IOException, MojoExecutionException, ExecException {

givenProjectWithResolvedImage(singleImageWithRunAndAlias("example-1"));
Deencapsulation.setField(stopMojo, "containerNamePattern", "%a");
givenRunningContainer(running1, "container-id-1", "example-1");
givenContainerHasGavLabels(running1);
givenRunningContainer(running2, "container-id-2", "example-2");
givenContainerHasGavLabels(running2);
givenContainersAreRunningForImage("example:latest", running1, running2);

whenMojoExecutes();

thenContainerLookupByImageOccurs("example:latest");
thenListContainersIsNotCalled();
thenContainerIsStopped("container-id-1", false, false);
thenContainerIsStopped("container-id-2", false, false);
thenContainerIsStopped("container-id-3", false, false);
thenContainerIsNotStopped("container-id-2");
}

/**
* Mock project with one image, query service indicates two running containers, both labelled with GAV.
*
* The containers are named with ascending indices but with different prefixes and the containerNamePattern is set to %a (alias naming
* strategy) and the alias is set to the name of the first container so that only the first container is stopped.
*/
@Test
public void stopWithCustomIndexContainerNamePattern(@Mocked Container running1, @Mocked Container running2)
throws IOException, MojoExecutionException, ExecException {

givenProjectWithResolvedImage(singleImageWithRun());
Deencapsulation.setField(stopMojo, "containerNamePattern", "a-example-%i");
givenRunningContainer(running1, "container-id-1", "a-example-1");
givenContainerHasGavLabels(running1);
givenRunningContainer(running2, "container-id-2", "b-example-1");
givenContainerHasGavLabels(running2);
givenContainersAreRunningForImage("example:latest", running1, running2);

whenMojoExecutes();

thenContainerLookupByImageOccurs("example:latest");
thenListContainersIsNotCalled();
thenContainerIsStopped("container-id-1", false, false);
thenContainerIsNotStopped("container-id-2");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.fabric8.maven.docker.util;

import static io.fabric8.maven.docker.util.ContainerNamingUtil.DEFAULT_CONTAINER_NAME_PATTERN;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -115,28 +117,108 @@ public void testIndexAdvanced() {
}};
Assert.assertEquals("container-2",
ContainerNamingUtil.formatContainerName(
imageConfiguration("jolokia/jolokia_demo","nameAlias", "container-%i"),
imageConfiguration("jolokia/jolokia_demo",null, "container-%i"),
null,
new Date(123456),
Collections.singleton(container1)));
}

@Test
public void testContainersToStopByDefault() {
new Expectations() {{
container1.getName();result = "jolokia_demo-1";
container2.getName();result = "jolokia_demo-n";

}};
Collection<Container> containers = Arrays.asList(container1, container2);
Collection<Container> filtered = ContainerNamingUtil.getContainersToStop(
imageConfiguration("jolokia/jolokia_demo",null, null),
DEFAULT_CONTAINER_NAME_PATTERN,
containers);
Assert.assertEquals(1, filtered.size());
Assert.assertEquals(container1, filtered.iterator().next());
}

@Test
public void testContainersToStopByIndex() {
new Expectations() {{
container1.getName();result = "1";
container2.getName();result = "n";

}};
Collection<Container> containers = Arrays.asList(container1, container2);
Collection<Container> filtered = ContainerNamingUtil.getContainersToStop(
imageConfiguration("jolokia/jolokia_demo",null, "%i"),
DEFAULT_CONTAINER_NAME_PATTERN,
containers);
Assert.assertEquals(1, filtered.size());
Assert.assertEquals(container1, filtered.iterator().next());
}

@Test
public void testContainersToStop() {
public void testContainersToStopByName() {
new Expectations() {{
container1.getName();result = "jolokia_demo";
container2.getName();result = "jolokia_demo2";

}};
Collection<Container> containers = Arrays.asList(container1, container2);
Collection<Container> filtered = ContainerNamingUtil.getContainersToStop(
imageConfiguration("jolokia/jolokia_demo",null, "%n"),
DEFAULT_CONTAINER_NAME_PATTERN,
containers);
Assert.assertEquals(1, filtered.size());
Assert.assertEquals(container1, filtered.iterator().next());
}

@Test
public void testContainersToStopByAlias() {
new Expectations() {{
container1.getName();result = "container-1";
container2.getName();result = "container-2";
}};
Collection<Container> containers = Arrays.asList(container2, container1);

Collection<Container> filtered = ContainerNamingUtil.getContainersToStop(
imageConfiguration("jolokia/jolokia_demo","container-1", "%a"),
DEFAULT_CONTAINER_NAME_PATTERN,
containers);

Assert.assertEquals(1, filtered.size());
Assert.assertEquals(container1, filtered.iterator().next());
}

@Test
public void testContainersToStopByTimestamp() {
new Expectations() {{
container1.getName();result = "1610716931377";
container2.getName();result = "1610716931378";
}};
Collection<Container> containers = Arrays.asList(container1, container2);
Collection<Container> containers = Arrays.asList(container2, container1);

Collection<Container> filtered = ContainerNamingUtil.getContainersToStop(
imageConfiguration("jolokia/jolokia_demo","nameAlias", "container-%i"),
null,
new Date(123456),
containers);
imageConfiguration("jolokia/jolokia_demo",null, "%t"),
DEFAULT_CONTAINER_NAME_PATTERN,
containers);

Assert.assertEquals(2, filtered.size());
}

@Test
public void testContainersToStopByMixed() {
new Expectations() {{
container1.getName();result = "prefix-1-myAlias-jolokia_demo-1610716931377-suffix";
container2.getName();result = "prefix-myAlias-jolokia_demo-1610716931377-suffix";
}};
Collection<Container> containers = Arrays.asList(container2, container1);

Collection<Container> filtered = ContainerNamingUtil.getContainersToStop(
imageConfiguration("jolokia/jolokia_demo","myAlias", "prefix-%i-%a-%n-%t-suffix"),
DEFAULT_CONTAINER_NAME_PATTERN,
containers);

Assert.assertEquals(1, filtered.size());
Assert.assertEquals(container2, filtered.iterator().next());
Assert.assertEquals(container1, filtered.iterator().next());
}

private ImageConfiguration imageConfiguration(String name, String alias, String containerNamePattern) {
Expand Down

0 comments on commit 71e3cc0

Please sign in to comment.