Skip to content

Commit

Permalink
Supply databases with Testcontainers.
Browse files Browse the repository at this point in the history
Signed-off-by: Miquel Simon <[email protected]>
  • Loading branch information
miquelsi committed Jul 17, 2024
1 parent 2e3122d commit be375bd
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 15 deletions.
5 changes: 5 additions & 0 deletions test-poc/base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<groupId>org.jboss.logmanager</groupId>
<artifactId>jboss-logmanager</artifactId>
</dependency>
<!-- JDBC Drivers -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mcr.microsoft.com/mssql/server:latest
21 changes: 20 additions & 1 deletion test-poc/framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@
<version>${selenium.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mssqlserver</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.junit.rules;

@SuppressWarnings("unused")
public interface TestRule {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.junit.runners.model;

@SuppressWarnings("unused")
public class Statement {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

public abstract class AbstractDatabaseSupplier implements Supplier<TestDatabase, KeycloakTestDatabase> {

protected static final String DEFAULT_DB_USERNAME = "keycloak";
protected static final String DEFAULT_DB_PASSWORD = "Password1!";

@Override
public Class<KeycloakTestDatabase> getAnnotationClass() {
return KeycloakTestDatabase.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class DatabaseConfig {

private String vendor;
private String containerImage;
private String urlHost;
private String url;
private String username;
private String password;

Expand All @@ -29,12 +29,12 @@ public DatabaseConfig containerImage(String containerImage) {
return this;
}

public String getUrlHost() {
return urlHost;
public String getUrl() {
return url;
}

public DatabaseConfig urlHost(String urlHost) {
this.urlHost = urlHost;
public DatabaseConfig url(String url) {
this.url = url;
return this;
}

Expand All @@ -61,8 +61,8 @@ public Map<String, String> toConfig() {
if (vendor != null) {
config.put("db", vendor);
}
if (urlHost != null) {
config.put("db-url-host", urlHost);
if (url != null) {
config.put("db-url", url);
}
if (username != null) {
config.put("db-username", username);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.keycloak.test.framework.database;

public class MSSQLServerDatabaseSupplier extends AbstractDatabaseSupplier {
public static final String VENDOR = "mssql";

@Override
TestDatabase getTestDatabase() {
DatabaseConfig databaseConfig = new DatabaseConfig()
.vendor(VENDOR)
.containerImage("mcr.microsoft.com/mssql/server:latest");
return new TestDatabase(databaseConfig);
}

@Override
public String getAlias() {
return VENDOR;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.keycloak.test.framework.database;

public class MariaDBDatabaseSupplier extends AbstractDatabaseSupplier {
public static final String VENDOR = "mariadb";

@Override
TestDatabase getTestDatabase() {
DatabaseConfig databaseConfig = new DatabaseConfig()
.vendor(VENDOR)
.username(DEFAULT_DB_USERNAME)
.password(DEFAULT_DB_PASSWORD)
.containerImage("mariadb:latest");
return new TestDatabase(databaseConfig);
}

@Override
public String getAlias() {
return VENDOR;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.keycloak.test.framework.database;

public class MySQLDatabaseSupplier extends AbstractDatabaseSupplier {

public static final String VENDOR = "mysql";

@Override
TestDatabase getTestDatabase() {
DatabaseConfig databaseConfig = new DatabaseConfig()
.vendor(VENDOR)
.username(DEFAULT_DB_USERNAME)
.password(DEFAULT_DB_PASSWORD)
.containerImage("mysql:latest");
return new TestDatabase(databaseConfig);
}

@Override
public String getAlias() {
return VENDOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class PostgresDatabaseSupplier extends AbstractDatabaseSupplier {
TestDatabase getTestDatabase() {
DatabaseConfig databaseConfig = new DatabaseConfig()
.vendor(VENDOR)
.username("keycloak")
.password("keycloak")
.containerImage("the-postgres-container:the-version");
.username(DEFAULT_DB_USERNAME)
.password(DEFAULT_DB_PASSWORD)
.containerImage("postgres:latest");
return new TestDatabase(databaseConfig);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
package org.keycloak.test.framework.database;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.MSSQLServerContainer;
import org.testcontainers.containers.MariaDBContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

import java.time.Duration;
import java.util.Map;

public class TestDatabase {

private DatabaseConfig databaseConfig;
private final DatabaseConfig databaseConfig;

private GenericContainer<?> container;

public TestDatabase(DatabaseConfig databaseConfig) {
this.databaseConfig = databaseConfig;
}

public void start() {
if (databaseConfig.getContainerImage() != null) {
// TODO Start container
container = createContainer();
container.withStartupTimeout(Duration.ofMinutes(5)).start();
databaseConfig.url(getJdbcUrl());
if (container instanceof MSSQLServerContainer) {
databaseConfig.username(((JdbcDatabaseContainer<?>) container).getUsername());
databaseConfig.password(((JdbcDatabaseContainer<?>) container).getPassword());
}
}
}

public void stop() {
if (databaseConfig.getContainerImage() != null) {
// TODO Stop container
} else if (databaseConfig.getVendor().equals("dev-mem")) {
container.stop();
container = null;
} else if ("dev-mem".equals(databaseConfig.getVendor())) {
// TODO Stop in-mem H2 database
}
}
Expand All @@ -28,4 +46,41 @@ public Map<String, String> getServerConfig() {
return databaseConfig.toConfig();
}

public String getJdbcUrl() {
return ((JdbcDatabaseContainer<?>)container).getJdbcUrl();
}

private JdbcDatabaseContainer<?> configureJdbcContainer(JdbcDatabaseContainer<?> jdbcDatabaseContainer) {
if (jdbcDatabaseContainer instanceof MSSQLServerContainer) {
return jdbcDatabaseContainer;
}

return jdbcDatabaseContainer
.withDatabaseName("keycloak")
.withUsername(databaseConfig.getUsername())
.withPassword(databaseConfig.getPassword());
}

private GenericContainer<?> createContainer() {
return switch (databaseConfig.getVendor()) {
case PostgresDatabaseSupplier.VENDOR -> {
DockerImageName POSTGRES = DockerImageName.parse(databaseConfig.getContainerImage()).asCompatibleSubstituteFor("postgres");
yield configureJdbcContainer(new PostgreSQLContainer<>(POSTGRES));
}
case MariaDBDatabaseSupplier.VENDOR -> {
DockerImageName MARIADB = DockerImageName.parse(databaseConfig.getContainerImage()).asCompatibleSubstituteFor("mariadb");
yield configureJdbcContainer(new MariaDBContainer<>(MARIADB));
}
case MySQLDatabaseSupplier.VENDOR -> {
DockerImageName MYSQL = DockerImageName.parse(databaseConfig.getContainerImage()).asCompatibleSubstituteFor("mysql");
yield configureJdbcContainer(new MySQLContainer<>(MYSQL));
}
case MSSQLServerDatabaseSupplier.VENDOR -> {
DockerImageName MSSQL = DockerImageName.parse(databaseConfig.getContainerImage()).asCompatibleSubstituteFor("sqlserver");
yield configureJdbcContainer(new MSSQLServerContainer<>(MSSQL));
}
default -> throw new RuntimeException("Unsupported database: " + databaseConfig.getVendor());
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ org.keycloak.test.framework.webdriver.ChromeWebDriverSupplier
org.keycloak.test.framework.webdriver.FirefoxWebDriverSupplier
org.keycloak.test.framework.database.DevMemDatabaseSupplier
org.keycloak.test.framework.database.DevFileDatabaseSupplier
org.keycloak.test.framework.database.MySQLDatabaseSupplier
org.keycloak.test.framework.database.PostgresDatabaseSupplier
org.keycloak.test.framework.database.MariaDBDatabaseSupplier
org.keycloak.test.framework.database.MSSQLServerDatabaseSupplier
org.keycloak.test.framework.page.PageSupplier

0 comments on commit be375bd

Please sign in to comment.