Skip to content
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-postgresql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@
<scope>test</scope>
Comment thread
vlad-lyutenko marked this conversation as resolved.
Outdated
</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 @@ -83,7 +83,7 @@ public static void main(String[] args)
throws Exception
{
DistributedQueryRunner queryRunner = createPostgreSqlQueryRunner(
new TestingPostgreSqlServer(),
new TestingPostgreSqlServer(true),
ImmutableMap.of("http-server.http.port", "8080"),
ImmutableMap.of(),
TpchTable.getTables());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.jdbc.RemoteDatabaseEvent.Status.CANCELLED;
import static io.trino.plugin.jdbc.RemoteDatabaseEvent.Status.RUNNING;
import static io.trino.testing.containers.TestContainers.exposeFixedPorts;
import static java.lang.String.format;
import static java.util.function.Predicate.not;
import static org.testcontainers.containers.PostgreSQLContainer.POSTGRESQL_PORT;
Expand All @@ -50,6 +51,11 @@ public class TestingPostgreSqlServer
private final PostgreSQLContainer<?> dockerContainer;

public TestingPostgreSqlServer()
{
this(false);
}

public TestingPostgreSqlServer(boolean shouldExposeFixedPorts)
{
// Use the oldest supported PostgreSQL version
dockerContainer = new PostgreSQLContainer<>("postgres:10.20")
Expand All @@ -58,6 +64,9 @@ public TestingPostgreSqlServer()
.withUsername(USER)
.withPassword(PASSWORD)
.withCommand("postgres", "-c", "log_destination=stderr", "-c", "log_statement=all");
if (shouldExposeFixedPorts) {
exposeFixedPorts(dockerContainer);
}
dockerContainer.start();

execute("CREATE SCHEMA tpch");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@

Comment thread
vlad-lyutenko marked this conversation as resolved.
Outdated
package io.trino.testing.containers;

import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.PortBinding;
import com.google.common.collect.ImmutableList;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.TestcontainersConfiguration;

import java.io.Closeable;

import static com.github.dockerjava.api.model.Ports.Binding.bindPort;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.lang.Boolean.parseBoolean;
import static java.lang.System.getenv;
import static java.util.Objects.requireNonNull;
import static org.testcontainers.utility.MountableFile.forClasspathResource;

public final class TestContainers
Expand Down Expand Up @@ -55,4 +61,20 @@ public static String getPathFromClassPathResource(String resourcePath)
// and mounted from there.
.getResolvedPath();
}

public static void exposeFixedPorts(GenericContainer<?> container)
{
checkState(System.getenv("CONTINUOUS_INTEGRATION") == null, "" +
"Exposing fixed ports should not be used in regular test code. This could break parallel test execution. " +
"This method is supposed to be invoked from local development helpers only e.g. QueryRunner.main(), " +
"hence it should never run on CI");

ImmutableList<PortBinding> portBindings = container.getExposedPorts().stream()
.map(exposedPort -> new PortBinding(bindPort(exposedPort), new ExposedPort(exposedPort)))
.collect(toImmutableList());

container.withCreateContainerCmdModifier(cmd -> cmd
.withHostConfig(requireNonNull(cmd.getHostConfig(), "hostConfig is null")
.withPortBindings(portBindings)));
Comment thread
vlad-lyutenko marked this conversation as resolved.
Outdated
}
}