diff --git a/.github/workflows/ci-test.yaml b/.github/workflows/ci-test.yaml index a0f5e0eca..22463aae3 100644 --- a/.github/workflows/ci-test.yaml +++ b/.github/workflows/ci-test.yaml @@ -41,6 +41,8 @@ jobs: cache: 'maven' - name: Execute unit tests + env: + TESTCONTAINERS_REUSE_ENABLE: "true" run: |- mvn clean mvn test -P enhance diff --git a/src/test/java/org/dependencytrack/PersistenceCapableTest.java b/src/test/java/org/dependencytrack/PersistenceCapableTest.java index 7f9dd9cd9..99d3d729d 100644 --- a/src/test/java/org/dependencytrack/PersistenceCapableTest.java +++ b/src/test/java/org/dependencytrack/PersistenceCapableTest.java @@ -25,16 +25,13 @@ import org.datanucleus.api.jdo.JDOPersistenceManagerFactory; import org.dependencytrack.event.kafka.KafkaProducerInitializer; import org.dependencytrack.persistence.QueryManager; -import org.dependencytrack.persistence.migration.MigrationInitializer; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.contrib.java.lang.system.EnvironmentVariables; -import org.postgresql.ds.PGSimpleDataSource; import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.utility.DockerImageName; import javax.jdo.JDOHelper; import java.sql.Connection; @@ -46,7 +43,7 @@ public abstract class PersistenceCapableTest { @Rule public EnvironmentVariables environmentVariables = new EnvironmentVariables(); - protected static PostgreSQLContainer postgresContainer; + protected static PostgresTestContainer postgresContainer; protected MockProducer kafkaMockProducer; protected QueryManager qm; @@ -54,9 +51,8 @@ public abstract class PersistenceCapableTest { public static void init() throws Exception { Config.enableUnitTests(); - postgresContainer = createPostgresContainer(); + postgresContainer = new PostgresTestContainer(); postgresContainer.start(); - runMigrations(postgresContainer); } @Before @@ -87,17 +83,9 @@ public void after() { @AfterClass public static void tearDownClass() { - if (postgresContainer != null) { - postgresContainer.stop(); - } - } - - @SuppressWarnings("resource") - protected static PostgreSQLContainer createPostgresContainer() { - return new PostgreSQLContainer<>(DockerImageName.parse("postgres:11-alpine")) - .withUsername("dtrack") - .withPassword("dtrack") - .withDatabaseName("dtrack"); +// if (postgresContainer != null) { +// postgresContainer.stop(); +// } } protected static void configurePmf(final PostgreSQLContainer postgresContainer) { @@ -118,14 +106,6 @@ protected static void configurePmf(final PostgreSQLContainer postgresContaine PersistenceManagerFactory.setJdoPersistenceManagerFactory(pmf); } - protected static void runMigrations(final PostgreSQLContainer postgresContainer) throws Exception { - final var dataSource = new PGSimpleDataSource(); - dataSource.setUrl(postgresContainer.getJdbcUrl()); - dataSource.setUser(postgresContainer.getUsername()); - dataSource.setPassword(postgresContainer.getPassword()); - MigrationInitializer.runMigration(dataSource, /* silent */ true); - } - protected static void truncateTables(final PostgreSQLContainer postgresContainer) throws Exception { // Truncate all tables to ensure each test starts from a clean slate. // https://stackoverflow.com/a/63227261 diff --git a/src/test/java/org/dependencytrack/PostgresTestContainer.java b/src/test/java/org/dependencytrack/PostgresTestContainer.java new file mode 100644 index 000000000..99f682f7a --- /dev/null +++ b/src/test/java/org/dependencytrack/PostgresTestContainer.java @@ -0,0 +1,45 @@ +package org.dependencytrack; + +import com.github.dockerjava.api.command.InspectContainerResponse; +import org.dependencytrack.persistence.migration.MigrationInitializer; +import org.postgresql.ds.PGSimpleDataSource; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.utility.DockerImageName; + +public class PostgresTestContainer extends PostgreSQLContainer { + + @SuppressWarnings("resource") + public PostgresTestContainer() { + super(DockerImageName.parse("postgres:11-alpine")); + withUsername("dtrack"); + withPassword("dtrack"); + withDatabaseName("dtrack"); + withLabel("owner", "hyades-apiserver"); + + // NB: Container reuse won't be active unless either: + // - The environment variable TESTCONTAINERS_REUSE_ENABLE=true is set + // - testcontainers.reuse.enable=false is set in ~/.testcontainers.properties + withReuse(true); + } + + @Override + protected void containerIsStarted(final InspectContainerResponse containerInfo, final boolean reused) { + super.containerIsStarted(containerInfo, reused); + + if (reused) { + logger().debug("Reusing container; Migration not necessary"); + return; + } + + final var dataSource = new PGSimpleDataSource(); + dataSource.setUrl(getJdbcUrl()); + dataSource.setUser(getUsername()); + dataSource.setPassword(getPassword()); + + try { + MigrationInitializer.runMigration(dataSource, /* silent */ true); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/test/java/org/dependencytrack/ResourceTest.java b/src/test/java/org/dependencytrack/ResourceTest.java index b7f079afa..89b2e939c 100644 --- a/src/test/java/org/dependencytrack/ResourceTest.java +++ b/src/test/java/org/dependencytrack/ResourceTest.java @@ -37,7 +37,6 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; -import org.testcontainers.containers.PostgreSQLContainer; import javax.json.Json; import javax.json.JsonArray; @@ -49,8 +48,6 @@ import java.util.List; import static org.dependencytrack.PersistenceCapableTest.configurePmf; -import static org.dependencytrack.PersistenceCapableTest.createPostgresContainer; -import static org.dependencytrack.PersistenceCapableTest.runMigrations; import static org.dependencytrack.PersistenceCapableTest.truncateTables; public abstract class ResourceTest extends JerseyTest { @@ -98,7 +95,7 @@ public abstract class ResourceTest extends JerseyTest { protected final String X_API_KEY = "X-Api-Key"; protected final String V1_TAG = "/v1/tag"; - protected static PostgreSQLContainer postgresContainer; + protected static PostgresTestContainer postgresContainer; protected QueryManager qm; protected MockProducer kafkaMockProducer; @@ -111,9 +108,8 @@ public abstract class ResourceTest extends JerseyTest { public static void init() throws Exception { Config.enableUnitTests(); - postgresContainer = createPostgresContainer(); + postgresContainer = new PostgresTestContainer(); postgresContainer.start(); - runMigrations(postgresContainer); } @Before