forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...ng-containers/src/main/java/io/trino/testing/containers/junit/ReportLeakedContainers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.testing.containers.junit; | ||
|
||
import com.github.dockerjava.api.DockerClient; | ||
import com.github.dockerjava.api.model.Container; | ||
import io.airlift.log.Logger; | ||
import org.junit.platform.launcher.TestExecutionListener; | ||
import org.junit.platform.launcher.TestPlan; | ||
import org.testcontainers.DockerClientFactory; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static com.google.common.base.MoreObjects.toStringHelper; | ||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
import static java.lang.Boolean.getBoolean; | ||
import static java.util.Objects.requireNonNull; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
public final class ReportLeakedContainers | ||
{ | ||
private ReportLeakedContainers() {} | ||
|
||
private static final Logger log = Logger.get(ReportLeakedContainers.class); | ||
private static final boolean DISABLED = getBoolean("ReportLeakedContainers.disabled"); | ||
|
||
private static final Set<String> ignoredIds = Collections.synchronizedSet(new HashSet<>()); | ||
|
||
public static void ignoreContainerId(String containerId) | ||
{ | ||
ignoredIds.add(requireNonNull(containerId, "containerId is null")); | ||
} | ||
|
||
// Separate class so that ReportLeakedContainers.ignoreContainerId can be called without pulling junit platform onto classpath | ||
public static class Listener | ||
implements TestExecutionListener | ||
{ | ||
@Override | ||
public void testPlanExecutionFinished(TestPlan testPlan) | ||
{ | ||
if (DISABLED) { | ||
log.info("ReportLeakedContainers disabled"); | ||
return; | ||
} | ||
log.info("Checking for leaked containers"); | ||
|
||
@SuppressWarnings("resource") // Throws when close is attempted, as this is a global instance. | ||
DockerClient dockerClient = DockerClientFactory.lazyClient(); | ||
|
||
List<Container> containers = dockerClient.listContainersCmd() | ||
.withLabelFilter(Map.of(DockerClientFactory.TESTCONTAINERS_SESSION_ID_LABEL, DockerClientFactory.SESSION_ID)) | ||
.exec() | ||
.stream() | ||
.filter(container -> !ignoredIds.contains(container.getId())) | ||
.collect(toImmutableList()); | ||
|
||
if (!containers.isEmpty()) { | ||
log.error("Leaked containers: %s", containers.stream() | ||
.map(container -> toStringHelper("container") | ||
.add("id", container.getId()) | ||
.add("image", container.getImage()) | ||
.add("imageId", container.getImageId()) | ||
.toString()) | ||
.collect(joining(", ", "[", "]"))); | ||
|
||
// JUnit does not fail on a listener exception. | ||
System.err.println("JVM will be terminated"); | ||
System.exit(1); | ||
} | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...rs/src/main/resources/META-INF/services/org.junit.platform.launcher.TestExecutionListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.trino.testing.containers.junit.ReportLeakedContainers$Listener |