-
-
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.
Migrate all tests to use Postgres testcontainers instead of H2 (#573)
- Loading branch information
Showing
65 changed files
with
471 additions
and
581 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
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
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
106 changes: 0 additions & 106 deletions
106
src/test/java/org/dependencytrack/AbstractPostgresEnabledTest.java
This file was deleted.
Oops, something went wrong.
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
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
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; | ||
import org.testcontainers.utility.TestcontainersConfiguration; | ||
|
||
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); | ||
} | ||
} | ||
|
||
public void stopWhenNotReusing() { | ||
if (!TestcontainersConfiguration.getInstance().environmentSupportsReuse() || !isShouldBeReused()) { | ||
stop(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.