|
| 1 | +package org.evomaster.client.java.postgres.test.utils; |
| 2 | + |
| 3 | +import org.testcontainers.containers.GenericContainer; |
| 4 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 5 | + |
| 6 | +import java.util.Collections; |
| 7 | + |
| 8 | +/** |
| 9 | + * Shared utility for creating and configuring PostgreSQL Testcontainers instances. |
| 10 | + * Used across modules (client-java/sql, core, e2e tests) to avoid code duplication. |
| 11 | + */ |
| 12 | +public class PostgresContainerUtils { |
| 13 | + |
| 14 | + /** |
| 15 | + * Default version of PostgreSQL used when none is explicitly specified. |
| 16 | + */ |
| 17 | + public static final String DEFAULT_POSTGRES_VERSION = "14"; |
| 18 | + |
| 19 | + /** |
| 20 | + * Creates a new PostgreSQL container using the default version ({@value DEFAULT_POSTGRES_VERSION}). |
| 21 | + * |
| 22 | + * @return A configured {@link GenericContainer} instance ready to be started. |
| 23 | + */ |
| 24 | + public static GenericContainer<?> newContainer() { |
| 25 | + return newContainer(DEFAULT_POSTGRES_VERSION); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Creates a new PostgreSQL container using the specified version. |
| 30 | + * The container is pre-configured with: |
| 31 | + * <ul> |
| 32 | + * <li>Port 5432 exposed</li> |
| 33 | + * <li>Temporary filesystem for the data directory (avoids disk I/O in tests)</li> |
| 34 | + * <li>{@code POSTGRES_HOST_AUTH_METHOD=trust} (no password required)</li> |
| 35 | + * <li>Readiness wait on the "database system is ready to accept connections" log message</li> |
| 36 | + * </ul> |
| 37 | + * |
| 38 | + * @param version The PostgreSQL Docker image version tag (e.g. {@code "14"}, {@code "17"}). |
| 39 | + * @return A configured {@link GenericContainer} instance ready to be started. |
| 40 | + */ |
| 41 | + public static GenericContainer<?> newContainer(String version) { |
| 42 | + return new GenericContainer<>("postgres:" + version) |
| 43 | + .withExposedPorts(5432) |
| 44 | + .withTmpFs(Collections.singletonMap("/var/lib/postgresql/data", "rw")) |
| 45 | + .withEnv("POSTGRES_HOST_AUTH_METHOD", "trust") |
| 46 | + /* |
| 47 | + * A call to getConnection() when the postgres container is still not ready |
| 48 | + * signals a PSQLException with message "FATAL: the database system is starting up". |
| 49 | + * See: https://github.com/testcontainers/testcontainers-java/issues/317 |
| 50 | + */ |
| 51 | + .waitingFor(Wait.forLogMessage(".*database system is ready to accept connections.*\\s", 2)); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Constructs a JDBC URL for a PostgreSQL database hosted in the given container. |
| 56 | + * |
| 57 | + * @param container A running PostgreSQL {@link GenericContainer} with port 5432 mapped. |
| 58 | + * @return JDBC URL of the form {@code jdbc:postgresql://<host>:<port>/postgres}. |
| 59 | + */ |
| 60 | + public static String getJdbcUrl(GenericContainer<?> container) { |
| 61 | + return "jdbc:postgresql://" + container.getHost() + ":" + container.getMappedPort(5432) + "/postgres"; |
| 62 | + } |
| 63 | +} |
0 commit comments