Skip to content

Commit

Permalink
Merge pull request #1369 from folio-org/update-postgres
Browse files Browse the repository at this point in the history
Postgres 16, workflow, TESTCONTAINERS_POSTGRES_IMAGE, error reporting
  • Loading branch information
julianladisch authored Dec 19, 2024
2 parents d24aaf2 + 5b6c249 commit b474d98
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: postgres
on:
workflow_dispatch:
inputs:
postgres:
description: "List of postgres container images, to be injected as TESTCONTAINERS_POSTGRES_IMAGE"
default: '["postgres:16-alpine", "postgres:18-alpine"]'
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
postgres: ${{ fromJSON(github.event.inputs.postgres) }}
fail-fast: false
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
cache: maven
- run: mvn --batch-mode verify
env:
TESTCONTAINERS_POSTGRES_IMAGE: ${{ matrix.postgres }}
5 changes: 2 additions & 3 deletions okapi-core/src/test/java/org/folio/okapi/ModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.folio.okapi.common.OkapiLogger;
import org.folio.okapi.common.UrlDecoder;
import org.folio.okapi.common.XOkapiHeaders;
import org.folio.okapi.util.PgTestBase;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
Expand Down Expand Up @@ -135,9 +136,7 @@ public ModuleTest(String value) throws Exception {
switch (value) {
case "postgres":
if (postgresSQLContainer == null) {
postgresSQLContainer = new PostgreSQLContainer<>("postgres:12-alpine")
.withStartupAttempts(STARTUP_ATTEMPTS);
postgresSQLContainer.start();
postgresSQLContainer = PgTestBase.createPostgreSQLContainer();
}
conf.put("postgres_username", postgresSQLContainer.getUsername());
conf.put("postgres_password", postgresSQLContainer.getPassword());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class PostgresHandleTest extends PgTestBase implements WithAssertions {
static void exec(String... command) {
try {
ExecResult execResult = POSTGRESQL_CONTAINER.execInContainer(command);
if (execResult.getExitCode() != 0) {
throw new RuntimeException(String.join(" ", command) + " " + execResult);
}
OkapiLogger.get().debug(() -> String.join(" ", command) + " " + execResult);
} catch (InterruptedException | IOException | UnsupportedOperationException e) {
throw new RuntimeException(e);
Expand All @@ -54,7 +57,7 @@ static void beforeAll() {
MountableFile serverCrtFile = MountableFile.forClasspathResource("server.crt");
POSTGRESQL_CONTAINER.copyFileToContainer(serverKeyFile, KEY_PATH);
POSTGRESQL_CONTAINER.copyFileToContainer(serverCrtFile, CRT_PATH);
exec("chown", "postgres.postgres", KEY_PATH, CRT_PATH);
exec("chown", "postgres:postgres", KEY_PATH, CRT_PATH);
exec("chmod", "400", KEY_PATH, CRT_PATH);
exec("cp", "-p", CONF_PATH, CONF_BAK_PATH);

Expand Down
18 changes: 14 additions & 4 deletions okapi-core/src/test/java/org/folio/okapi/util/PgTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@
* Singleton PostgreSQL instance from testcontainers.org.
*/
public abstract class PgTestBase extends TestBase {
public static final PostgreSQLContainer<?> POSTGRESQL_CONTAINER;
private static final String DEFAULT_POSTGRESQL_IMAGE_NAME = "postgres:16-alpine";
private static final String POSTGRESQL_IMAGE_NAME =
System.getenv().getOrDefault("TESTCONTAINERS_POSTGRES_IMAGE", DEFAULT_POSTGRESQL_IMAGE_NAME);
/**
* Testcontainers startup config workaround for podman:
* https://github.com/testcontainers/testcontainers-java/issues/6640#issuecomment-1431636203
*/
private static final int STARTUP_ATTEMPTS = 3;
public static final PostgreSQLContainer<?> POSTGRESQL_CONTAINER = createPostgreSQLContainer();

static {
POSTGRESQL_CONTAINER = new PostgreSQLContainer<>("postgres:12-alpine");
POSTGRESQL_CONTAINER.start();
public static PostgreSQLContainer<?> createPostgreSQLContainer() {
var container = new PostgreSQLContainer<>(POSTGRESQL_IMAGE_NAME)
.withStartupAttempts(STARTUP_ATTEMPTS);
container.start();
return container;
}

public static PgConnectOptions getPgConnectOptions() {
Expand Down

0 comments on commit b474d98

Please sign in to comment.