|
| 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 | +} |
0 commit comments