Skip to content

Commit 8fb9730

Browse files
findepilosipiuk
authored andcommitted
Detect leaked containers when running with JUnit
With TestNG, `ManageTestResources` was attempting to prevent resource leaks, including container leaks, in tests. It relied on certain common test patterns to operate (like storing resource on instance fields). This commit attempts to provide similar functionality for JUnit. For now it's limited to containers. As an added bonus, it works regardless of how the test class is written.
1 parent 9b646d8 commit 8fb9730

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

plugin/trino-accumulo/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@
267267
</exclusions>
268268
</dependency>
269269

270+
<dependency>
271+
<groupId>io.trino</groupId>
272+
<artifactId>trino-testing-containers</artifactId>
273+
<scope>test</scope>
274+
</dependency>
275+
270276
<dependency>
271277
<groupId>io.trino</groupId>
272278
<artifactId>trino-testing-services</artifactId>

plugin/trino-accumulo/src/test/java/io/trino/plugin/accumulo/TestingAccumuloServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package io.trino.plugin.accumulo;
1515

1616
import io.trino.testing.TestingProperties;
17+
import io.trino.testing.containers.junit.ReportLeakedContainers;
1718
import org.apache.accumulo.core.client.AccumuloException;
1819
import org.apache.accumulo.core.client.AccumuloSecurityException;
1920
import org.apache.accumulo.core.client.Connector;
@@ -63,6 +64,7 @@ private TestingAccumuloServer()
6364
// TODO Change this class to not be a singleton
6465
// https://github.com/trinodb/trino/issues/5842
6566
accumuloContainer.start();
67+
ReportLeakedContainers.ignoreContainerId(accumuloContainer.getContainerId());
6668
}
6769

6870
public String getInstanceName()

testing/trino-testing-containers/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
<artifactId>trino-testing-services</artifactId>
6262
</dependency>
6363

64+
<dependency>
65+
<groupId>org.junit.platform</groupId>
66+
<artifactId>junit-platform-launcher</artifactId>
67+
<optional>true</optional>
68+
</dependency>
69+
6470
<dependency>
6571
<groupId>org.rnorth.duct-tape</groupId>
6672
<artifactId>duct-tape</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.testing.containers.junit;
15+
16+
import com.github.dockerjava.api.DockerClient;
17+
import com.github.dockerjava.api.model.Container;
18+
import io.airlift.log.Logger;
19+
import org.junit.platform.launcher.TestExecutionListener;
20+
import org.junit.platform.launcher.TestPlan;
21+
import org.testcontainers.DockerClientFactory;
22+
23+
import java.util.Collections;
24+
import java.util.HashSet;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Set;
28+
29+
import static com.google.common.base.MoreObjects.toStringHelper;
30+
import static com.google.common.collect.ImmutableList.toImmutableList;
31+
import static java.lang.Boolean.getBoolean;
32+
import static java.util.Objects.requireNonNull;
33+
import static java.util.stream.Collectors.joining;
34+
35+
public final class ReportLeakedContainers
36+
{
37+
private ReportLeakedContainers() {}
38+
39+
private static final Logger log = Logger.get(ReportLeakedContainers.class);
40+
private static final boolean DISABLED = getBoolean("ReportLeakedContainers.disabled");
41+
42+
private static final Set<String> ignoredIds = Collections.synchronizedSet(new HashSet<>());
43+
44+
public static void ignoreContainerId(String containerId)
45+
{
46+
ignoredIds.add(requireNonNull(containerId, "containerId is null"));
47+
}
48+
49+
// Separate class so that ReportLeakedContainers.ignoreContainerId can be called without pulling junit platform onto classpath
50+
public static class Listener
51+
implements TestExecutionListener
52+
{
53+
@Override
54+
public void testPlanExecutionFinished(TestPlan testPlan)
55+
{
56+
if (DISABLED) {
57+
log.info("ReportLeakedContainers disabled");
58+
return;
59+
}
60+
log.info("Checking for leaked containers");
61+
62+
@SuppressWarnings("resource") // Throws when close is attempted, as this is a global instance.
63+
DockerClient dockerClient = DockerClientFactory.lazyClient();
64+
65+
List<Container> containers = dockerClient.listContainersCmd()
66+
.withLabelFilter(Map.of(DockerClientFactory.TESTCONTAINERS_SESSION_ID_LABEL, DockerClientFactory.SESSION_ID))
67+
.exec()
68+
.stream()
69+
.filter(container -> !ignoredIds.contains(container.getId()))
70+
.collect(toImmutableList());
71+
72+
if (!containers.isEmpty()) {
73+
log.error("Leaked containers: %s", containers.stream()
74+
.map(container -> toStringHelper("container")
75+
.add("id", container.getId())
76+
.add("image", container.getImage())
77+
.add("imageId", container.getImageId())
78+
.toString())
79+
.collect(joining(", ", "[", "]")));
80+
81+
// JUnit does not fail on a listener exception.
82+
System.err.println("JVM will be terminated");
83+
System.exit(1);
84+
}
85+
}
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.trino.testing.containers.junit.ReportLeakedContainers$Listener

0 commit comments

Comments
 (0)