-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable reuse of Postgres testcontainer in CI
Signed-off-by: nscuro <[email protected]>
- Loading branch information
Showing
4 changed files
with
54 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/java/org/dependencytrack/PostgresTestContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters