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

RMB-978: TESTCONTAINERS_POSTGRES #1130

Merged
merged 2 commits into from
Sep 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ RMB implementing modules expect a set of environment variables to be passed in a
- DB_RECONNECTINTERVAL
- DB_EXPLAIN_QUERY_THRESHOLD
- DB_ALLOW_SUPPRESS_OPTIMISTIC_LOCKING
- TESTCONTAINERS_POSTGRES

The first five are mandatory, the others are optional.

Expand Down Expand Up @@ -409,6 +410,8 @@ The environment variables `DB_HOST_READER` and `DB_PORT_READER` are for the [Rea

`DB_ALLOW_SUPPRESS_OPTIMISTIC_LOCKING` is a timestamp in the format `2022-12-31T23:59:59Z`. Setting it disables optimistic locking when sending a record that contains `"_version":-1` before that time, after that time `"_version":-1` is rejected. This applies only to tables with `failOnConflictUnlessSuppressed`, see below. The timestamp ensures that disabling this option cannot be forgotten. Suppressing optimistic locking is known to lead to data loss in some cases, don't use in production, you have been warned!

`TESTCONTAINERS_POSTGRES` changes the PostgreSQL container image name used at build time for testing; it is not used at runtime.

See the [Environment Variables](https://github.com/folio-org/okapi/blob/master/doc/guide.md#environment-variables) section of the Okapi Guide for more information on how to deploy environment variables to RMB modules via Okapi.

## Read and write database instances setup
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.folio.postgres.testing;

import java.util.Map;

import org.folio.util.PostgresTester;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.images.builder.Transferable;
import org.folio.util.PostgresTester;

public class PostgresTesterContainer implements PostgresTester {
public static final String DEFAULT_IMAGE_NAME = "postgres:12-alpine";
Expand Down Expand Up @@ -36,6 +38,8 @@ public class PostgresTesterContainer implements PostgresTester {
*/
public static final int SIMULATED_ASYNC_REPLICATION_LAG_MILLISECONDS = 300;

private static final String IMAGE_NAME = getImageName(System.getenv());
steveellis marked this conversation as resolved.
Show resolved Hide resolved

private static final int READY_MESSAGE_TIMES = 2;

private static final Logger LOG = LoggerFactory.getLogger(PostgresTesterContainer.class);
Expand All @@ -59,10 +63,23 @@ public PostgresTesterContainer(String dockerImageName) {
}

/**
* Create postgres container with default image Postgres 12.
* Create postgres container with the image name configured by environment variable
* TESTCONTAINERS_POSTGRES, or the default {@link #DEFAULT_IMAGE_NAME} if undefined.
*/
public PostgresTesterContainer() {
this(DEFAULT_IMAGE_NAME);
this(IMAGE_NAME);
}

static String getImageName(Map<String, String> env) {
return env.getOrDefault("TESTCONTAINERS_POSTGRES", DEFAULT_IMAGE_NAME);
}

/**
* The image name configured by environment variable TESTCONTAINERS_POSTGRES,
* or the default {@link #DEFAULT_IMAGE_NAME} if undefined.
*/
public static String getImageName() {
return IMAGE_NAME;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.folio.postgres.testing;

import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import org.folio.util.PostgresTester;
Expand All @@ -13,16 +13,32 @@

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

import java.sql.Statement;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

public class PostgresTesterContainerTest {

@Test
public void imageName() {
assertEquals(PostgresTesterContainer.DEFAULT_IMAGE_NAME, PostgresTesterContainer.getImageName());
}

@Test
public void imageNameEnvEmpty() {
assertEquals(PostgresTesterContainer.DEFAULT_IMAGE_NAME, PostgresTesterContainer.getImageName(Map.of()));
}

@Test
public void imageNameEnv() {
var env = Map.of("TESTCONTAINERS_POSTGRES", "postgres:15.4-alpine3.18");
assertEquals("postgres:15.4-alpine3.18", PostgresTesterContainer.getImageName(env));
}

@Test
public void testStartClose() {
PostgresTester tester = new PostgresTesterContainer();
Expand Down