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

Detect leaked containers when running with JUnit #20297

Merged
merged 1 commit into from
Jan 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions plugin/trino-accumulo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@
</exclusions>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing-containers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing-services</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.trino.plugin.accumulo;

import io.trino.testing.TestingProperties;
import io.trino.testing.containers.junit.ReportLeakedContainers;
import org.apache.accumulo.core.client.AccumuloException;
import org.apache.accumulo.core.client.AccumuloSecurityException;
import org.apache.accumulo.core.client.Connector;
Expand Down Expand Up @@ -63,6 +64,7 @@ private TestingAccumuloServer()
// TODO Change this class to not be a singleton
// https://github.com/trinodb/trino/issues/5842
accumuloContainer.start();
ReportLeakedContainers.ignoreContainerId(accumuloContainer.getContainerId());
}

public String getInstanceName()
Expand Down
6 changes: 6 additions & 0 deletions testing/trino-testing-containers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
<artifactId>trino-testing-services</artifactId>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.rnorth.duct-tape</groupId>
<artifactId>duct-tape</artifactId>
Expand Down
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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.trino.testing.containers.junit.ReportLeakedContainers$Listener