From 885eec5ef2628e41d59f4017745df44e7b519533 Mon Sep 17 00:00:00 2001 From: Pedro Igor Date: Tue, 29 Jan 2019 15:17:58 -0200 Subject: [PATCH] [KEYCLOAK-8348] - Containerize database tests --- misc/DatabaseTesting.md | 21 +++++- .../DefaultJpaConnectionProviderFactory.java | 3 +- pom.xml | 3 +- testsuite/integration-arquillian/pom.xml | 51 ++++++++++++++ .../integration-arquillian/tests/base/pom.xml | 66 ++++++++++++++++++- .../base/src/test/resources/arquillian.xml | 10 +++ 6 files changed, 150 insertions(+), 4 deletions(-) diff --git a/misc/DatabaseTesting.md b/misc/DatabaseTesting.md index 96d130e883..3ffcf2074f 100644 --- a/misc/DatabaseTesting.md +++ b/misc/DatabaseTesting.md @@ -51,5 +51,24 @@ Run tests: Stop MySQl: - docker rm -f mariadb + docker rm -f mariadb +Using built-in profiles to run database tests using docker containers +------- + +The project provides specific profiles to run database tests using containers. The supported databases and their respective profiles are: + +* `db-mysql` +* `db-postgres` +* `db-mariadb` + +As an example, to run tests using a MySQL docker container: + + mvn -f testsuite/integration-arquillian clean verify -Pdb-mysql,jpa + +If you want to run tests using a pre-configured Keycloak distribution (instead of Undertow): + + mvn -f testsuite/integration-arquillian clean verify -Pdb-mysql,jpa,auth-server-wildfly + +Note that you must always activate the `jpa` profile. + diff --git a/model/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java b/model/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java index 83c25b6fd2..57f0cf631d 100755 --- a/model/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java +++ b/model/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java @@ -22,6 +22,7 @@ import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatfor import org.jboss.logging.Logger; import org.keycloak.Config; import org.keycloak.ServerStartupError; +import org.keycloak.common.util.StringPropertyReplacer; import org.keycloak.connections.jpa.updater.JpaUpdaterProvider; import org.keycloak.connections.jpa.util.JpaUtils; import org.keycloak.models.KeycloakSession; @@ -375,7 +376,7 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide return dataSource.getConnection(); } else { Class.forName(config.get("driver")); - return DriverManager.getConnection(config.get("url"), config.get("user"), config.get("password")); + return DriverManager.getConnection(StringPropertyReplacer.replaceProperties(config.get("url"), System.getProperties()), config.get("user"), config.get("password")); } } catch (Exception e) { throw new RuntimeException("Failed to connect to database", e); diff --git a/pom.xml b/pom.xml index b71468391b..ce321ba028 100755 --- a/pom.xml +++ b/pom.xml @@ -113,7 +113,7 @@ 4.2.0 7.1.0 9.3-1100-jdbc41 - 1.3.7 + 2.2.4 1.0.2.Final 1.0.0.Final 4.0.4 @@ -141,6 +141,7 @@ 1.1.0.Final 1.6.5 1.5 + 0.28.0 -Xms512m -Xmx2048m -XX:MetaspaceSize=96m -XX:MaxMetaspaceSize=256m diff --git a/testsuite/integration-arquillian/pom.xml b/testsuite/integration-arquillian/pom.xml index 81f2aeb547..234c3f22f7 100644 --- a/testsuite/integration-arquillian/pom.xml +++ b/testsuite/integration-arquillian/pom.xml @@ -72,6 +72,9 @@ 1.9.8.Final 2.5.5.Final 3.4.3.Final + + + true @@ -320,6 +323,54 @@ + + db-mysql + + com.mysql.jdbc.Driver + keycloak + keycloak + keycloak + jdbc:mysql://${auth.server.db.host}/${keycloak.connectionsJpa.database} + mysql + mysql-connector-java + ${mysql.version} + mysql:5.7.25 + 3306 + false + + + + db-postgres + + org.postgresql.Driver + keycloak + keycloak + keycloak + jdbc:postgresql://${auth.server.db.host}/${keycloak.connectionsJpa.database} + org.postgresql + postgresql + ${postgresql.version} + postgres:9.6.11 + 5432 + false + + + + db-mariadb + + org.mariadb.jdbc.Driver + keycloak + keycloak + keycloak + jdbc:mariadb://${auth.server.db.host}/${keycloak.connectionsJpa.database} + org.mariadb.jdbc + mariadb-java-client + ${mariadb.version} + mariadb:10.2.21 + 3306 + false + + diff --git a/testsuite/integration-arquillian/tests/base/pom.xml b/testsuite/integration-arquillian/tests/base/pom.xml index 9c77f44a22..bd020d7532 100644 --- a/testsuite/integration-arquillian/tests/base/pom.xml +++ b/testsuite/integration-arquillian/tests/base/pom.xml @@ -193,10 +193,16 @@ testsuiteVmId + ${docker.container.testdb.ip} + ${keycloak.connectionsJpa.driver} + ${keycloak.connectionsJpa.url} + ${keycloak.connectionsJpa.database} + ${keycloak.connectionsJpa.user} + ${keycloak.connectionsJpa.password} - + maven-resources-plugin @@ -304,6 +310,64 @@ + + io.fabric8 + docker-maven-plugin + ${docker.maven.plugin.version} + + ${docker.database.skip} + + + + start-db-container + process-test-classes + + start + + + true + + + testdb + ${docker.database.image} + + + ${docker.database.port} + + + + ${keycloak.connectionsJpa.database} + ${keycloak.connectionsJpa.user} + ${keycloak.connectionsJpa.password} + true + + + ${keycloak.connectionsJpa.database} + ${keycloak.connectionsJpa.user} + ${keycloak.connectionsJpa.password} + + + + + ${docker.database.port} + + + + + + + + + + + stop-db-container + test + + stop + + + + diff --git a/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml b/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml index 75bd384a09..a158020adf 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml +++ b/testsuite/integration-arquillian/tests/base/src/test/resources/arquillian.xml @@ -127,6 +127,7 @@ ${kie.maven.settings} -Dauth.server.truststore=${auth.server.truststore} -Dauth.server.truststore.password=${auth.server.truststore.password} + -Dauth.server.db.host=${auth.server.db.host} ${auth.server.jboss.jvm.debug.args} @@ -154,6 +155,7 @@ ${auth.server.profile} -Dauth.server.truststore=${auth.server.truststore} -Dauth.server.truststore.password=${auth.server.truststore.password} + -Dauth.server.db.host=${auth.server.db.host} ${auth.server.memory.settings} @@ -179,6 +181,7 @@ ${auth.server.profile} -Dauth.server.truststore=${auth.server.truststore} -Dauth.server.truststore.password=${auth.server.truststore.password} + -Dauth.server.db.host=${auth.server.db.host} ${auth.server.memory.settings} @@ -258,6 +261,7 @@ ${auth.server.profile} -Dauth.server.truststore=${auth.server.truststore} -Dauth.server.truststore.password=${auth.server.truststore.password} + -Dauth.server.db.host=${auth.server.db.host} ${auth.server.memory.settings} @@ -287,6 +291,7 @@ ${auth.server.profile} -Dauth.server.truststore=${auth.server.truststore} -Dauth.server.truststore.password=${auth.server.truststore.password} + -Dauth.server.db.host=${auth.server.db.host} ${auth.server.memory.settings} @@ -423,6 +428,7 @@ -Dauth.server.truststore.password=${auth.server.truststore.password} -Dkeycloak.connectionsInfinispan.default.remoteStoreSecurityEnabled=${keycloak.connectionsInfinispan.default.remoteStoreSecurityEnabled} -Dkeycloak.connectionsInfinispan.hotrodProtocolVersion=${keycloak.connectionsInfinispan.hotrodProtocolVersion} + -Dauth.server.db.host=${auth.server.db.host} ${auth.server.memory.settings} @@ -452,6 +458,7 @@ -Dauth.server.truststore.password=${auth.server.truststore.password} -Dkeycloak.connectionsInfinispan.default.remoteStoreSecurityEnabled=${keycloak.connectionsInfinispan.default.remoteStoreSecurityEnabled} -Dkeycloak.connectionsInfinispan.hotrodProtocolVersion=${keycloak.connectionsInfinispan.hotrodProtocolVersion} + -Dauth.server.db.host=${auth.server.db.host} ${auth.server.memory.settings} @@ -482,6 +489,7 @@ -Dauth.server.truststore.password=${auth.server.truststore.password} -Dkeycloak.connectionsInfinispan.default.remoteStoreSecurityEnabled=${keycloak.connectionsInfinispan.default.remoteStoreSecurityEnabled} -Dkeycloak.connectionsInfinispan.hotrodProtocolVersion=${keycloak.connectionsInfinispan.hotrodProtocolVersion} + -Dauth.server.db.host=${auth.server.db.host} ${auth.server.memory.settings} @@ -511,6 +519,7 @@ -Dauth.server.truststore.password=${auth.server.truststore.password} -Dkeycloak.connectionsInfinispan.default.remoteStoreSecurityEnabled=${keycloak.connectionsInfinispan.default.remoteStoreSecurityEnabled} -Dkeycloak.connectionsInfinispan.hotrodProtocolVersion=${keycloak.connectionsInfinispan.hotrodProtocolVersion} + -Dauth.server.db.host=${auth.server.db.host} ${auth.server.memory.settings} @@ -551,6 +560,7 @@ ${auth.server.memory.settings} -Dauth.server.truststore=${auth.server.truststore} -Dauth.server.truststore.password=${auth.server.truststore.password} + -Dauth.server.db.host=${auth.server.db.host} ${auth.server.management.port} ${auth.server.jboss.startup.timeout}