Skip to content

Commit

Permalink
Enable reuse of Postgres testcontainer in CI
Browse files Browse the repository at this point in the history
Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Feb 13, 2024
1 parent 30e076a commit 33f07bc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
cache: 'maven'

- name: Execute unit tests
env:
TESTCONTAINERS_REUSE_ENABLE: "true"
run: |-
mvn clean
mvn test -P enhance
30 changes: 5 additions & 25 deletions src/test/java/org/dependencytrack/PersistenceCapableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,17 +43,16 @@ public abstract class PersistenceCapableTest {
@Rule
public EnvironmentVariables environmentVariables = new EnvironmentVariables();

protected static PostgreSQLContainer<?> postgresContainer;
protected static PostgresTestContainer postgresContainer;
protected MockProducer<byte[], byte[]> kafkaMockProducer;
protected QueryManager qm;

@BeforeClass
public static void init() throws Exception {
Config.enableUnitTests();

postgresContainer = createPostgresContainer();
postgresContainer = new PostgresTestContainer();
postgresContainer.start();
runMigrations(postgresContainer);
}

@Before
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/dependencytrack/PostgresTestContainer.java
Original file line number Diff line number Diff line change
@@ -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<PostgresTestContainer> {

@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);
}
}
}
8 changes: 2 additions & 6 deletions src/test/java/org/dependencytrack/ResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<byte[], byte[]> kafkaMockProducer;
Expand All @@ -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
Expand Down

0 comments on commit 33f07bc

Please sign in to comment.