diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ca4bd04198d09e6135604deceab044c6e42a04e0..33ce001a605b906db0b4ada7a83f22f5039fd4cd 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,2 +1,20 @@ +stages: + - build + - test + build: - script: "cd TASMAN-bom; mvn clean install; cd ../TASMAN-core; mvn clean install -P Test -Dmysql_host=localhost -Dmysql_port=3306 -Dmysql_user=tasman_tester -D mysql_password=tasman_tester -Dpostgres_host=localhost -Dpostgres_port=5432 -Dpostgres_user=tasman_tester -Dpostgres_password=tasman_tester -Dpostgres_database=tasman_test" + tags: + - docker + image: maven:3.6.3-openjdk-14 + script: + - cp config.properties.example config.properties + - ./build.sh embedded + +test_backend: + stage: test + tags: + - docker + image: git.ia2.inaf.it:5050/ia2/ia2-devops/maven-otj-pg-embedded + script: + - cd TASMAN-core + - mvn clean install diff --git a/TASMAN-bom/pom.xml b/TASMAN-bom/pom.xml index b13a25f262306ab23c4a785bc595ccda092ed159..e22e45d5d3812f1689a6eaae32d287f55ae34408 100644 --- a/TASMAN-bom/pom.xml +++ b/TASMAN-bom/pom.xml @@ -13,7 +13,6 @@ <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <netbeans.hint.license>inaf-license-netbeans</netbeans.hint.license> - <maven.test.skip>true</maven.test.skip> </properties> <dependencyManagement> diff --git a/TASMAN-core/pom.xml b/TASMAN-core/pom.xml index 49579a714ac7206514cc044f66467b2b9c018f95..48c80f3102ee9919dfccf29b88060eb1ff7ca276 100644 --- a/TASMAN-core/pom.xml +++ b/TASMAN-core/pom.xml @@ -11,6 +11,10 @@ <artifactId>tasman-core</artifactId> <packaging>jar</packaging> + <properties> + <zonky.postgres-binaries.version>12.5.0</zonky.postgres-binaries.version> + </properties> + <dependencies> <dependency> <groupId>org.apache.tomcat</groupId> @@ -34,6 +38,19 @@ <version>9.3-1104-jdbc41</version> <scope>runtime</scope> </dependency> + <!-- Embedded testing databases --> + <dependency> + <groupId>ch.vorburger.mariaDB4j</groupId> + <artifactId>mariaDB4j</artifactId> + <version>2.4.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.opentable.components</groupId> + <artifactId>otj-pg-embedded</artifactId> + <version>0.13.3</version> + <scope>test</scope> + </dependency> </dependencies> <profiles> @@ -57,18 +74,64 @@ </dependencies> </profile> <profile> - <id>test</id> - <properties> - <maven.test.skip>false</maven.test.skip> - </properties> - <build> - <testResources> - <testResource> - <directory>src/test/resources</directory> - <filtering>true</filtering> - </testResource> - </testResources> - </build> + <id>platform-linux</id> + <activation> + <os> + <family>unix</family> + </os> + </activation> + <dependencies> + <dependency> + <groupId>io.zonky.test.postgres</groupId> + <artifactId>embedded-postgres-binaries-linux-amd64</artifactId> + <version>${zonky.postgres-binaries.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + </profile> + <profile> + <id>platform-windows</id> + <activation> + <os> + <family>windows</family> + </os> + </activation> + <dependencies> + <dependency> + <groupId>io.zonky.test.postgres</groupId> + <artifactId>embedded-postgres-binaries-windows-amd64</artifactId> + <version>${zonky.postgres-binaries.version}</version> + <scope>test</scope> + </dependency> + </dependencies> </profile> </profiles> + + <build> + <plugins> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.22.2</version> + </plugin> + <plugin> + <groupId>org.jacoco</groupId> + <artifactId>jacoco-maven-plugin</artifactId> + <version>0.8.6</version> + <executions> + <execution> + <goals> + <goal>prepare-agent</goal> + </goals> + </execution> + <execution> + <id>report</id> + <phase>test</phase> + <goals> + <goal>report</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> </project> \ No newline at end of file diff --git a/TASMAN-core/src/test/java/it/inaf/ia2/tsm/EmbeddedDatabases.java b/TASMAN-core/src/test/java/it/inaf/ia2/tsm/EmbeddedDatabases.java new file mode 100644 index 0000000000000000000000000000000000000000..556b95602f1e9431c4bada5c7d5b865914182bd1 --- /dev/null +++ b/TASMAN-core/src/test/java/it/inaf/ia2/tsm/EmbeddedDatabases.java @@ -0,0 +1,59 @@ +package it.inaf.ia2.tsm; + +import ch.vorburger.mariadb4j.DB; +import ch.vorburger.mariadb4j.DBConfigurationBuilder; +import com.opentable.db.postgres.embedded.UncompressBundleDirectoryResolver; +import com.opentable.db.postgres.embedded.EmbeddedPostgres; +import com.opentable.db.postgres.embedded.PgBinaryResolver; +import it.inaf.ia2.tsm.datalayer.Credentials; +import it.inaf.ia2.tsm.datalayer.DatabaseType; +import java.io.IOException; +import java.io.InputStream; +import org.springframework.core.io.ClassPathResource; + +public class EmbeddedDatabases { + + public static Credentials mariadbCredentials() throws Exception { + + DBConfigurationBuilder configBuilder = DBConfigurationBuilder.newBuilder(); + DB db = DB.newEmbeddedDB(configBuilder.build()); + db.start(); + + Credentials mysqlCredentials = new Credentials(DatabaseType.MYSQL); + mysqlCredentials.setHostname("127.0.0.1"); + mysqlCredentials.setPort(configBuilder.getPort()); + mysqlCredentials.setUsername("root"); + mysqlCredentials.setPassword(""); + + return mysqlCredentials; + } + + public static Credentials postgresCredentials() throws Exception { + + EmbeddedPostgres embeddedPg = EmbeddedPostgres.builder() + .setPgDirectoryResolver(new UncompressBundleDirectoryResolver(new CustomPostgresBinaryResolver())) + .start(); + + Credentials postgresCredentials = new Credentials(DatabaseType.POSTGRES); + postgresCredentials.setHostname("127.0.0.1"); + postgresCredentials.setPort(embeddedPg.getPort()); + postgresCredentials.setUsername("postgres"); + postgresCredentials.setPassword(""); + postgresCredentials.setDatabase("postgres"); + + return postgresCredentials; + } + + private static class CustomPostgresBinaryResolver implements PgBinaryResolver { + + /** + * Loads specific embedded Postgres version. + */ + @Override + public InputStream getPgBinary(String system, String architecture) throws IOException { + ClassPathResource resource = new ClassPathResource(String.format("postgres-%s-%s.txz", system.toLowerCase(), architecture)); + return resource.getInputStream(); + } + } + +} diff --git a/TASMAN-core/src/test/java/it/inaf/ia2/tsm/TestAll.java b/TASMAN-core/src/test/java/it/inaf/ia2/tsm/TestAll.java index cddb76039d653070b4682d7347ce5620af857dd6..af41d325eb56bb8d5ae9c11897b15a4427ed3a7e 100644 --- a/TASMAN-core/src/test/java/it/inaf/ia2/tsm/TestAll.java +++ b/TASMAN-core/src/test/java/it/inaf/ia2/tsm/TestAll.java @@ -28,8 +28,6 @@ import it.inaf.ia2.tsm.datalayer.Credentials; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.sql.Connection; @@ -39,7 +37,6 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Objects; -import java.util.Properties; import java.util.Set; import org.junit.After; import org.junit.AfterClass; @@ -68,29 +65,15 @@ public class TestAll { } @BeforeClass - public static void setUpClass() throws SQLException, IOException { - - Properties props = new Properties(); - try (InputStream in = TestAll.class.getClassLoader().getResourceAsStream("test.properties")) { - props.load(in); - } + public static void setUpClass() throws Exception { dbWrappers = new ArrayList<>(); // MYSQL - Credentials mysqlCredentials = new Credentials(DatabaseType.MYSQL); - mysqlCredentials.setHostname(props.getProperty("mysql_host")); - mysqlCredentials.setPort(Integer.parseInt(props.getProperty("mysql_port"))); - mysqlCredentials.setUsername(props.getProperty("mysql_user")); - mysqlCredentials.setPassword(props.getProperty("mysql_password")); + Credentials mysqlCredentials = EmbeddedDatabases.mariadbCredentials(); // POSTGRES - Credentials postgresCredentials = new Credentials(DatabaseType.POSTGRES); - postgresCredentials.setHostname(props.getProperty("postgres_host")); - postgresCredentials.setPort(Integer.parseInt(props.getProperty("postgres_port"))); - postgresCredentials.setUsername(props.getProperty("postgres_user")); - postgresCredentials.setPassword(props.getProperty("postgres_password")); - postgresCredentials.setDatabase(props.getProperty("postgres_database")); + Credentials postgresCredentials = EmbeddedDatabases.postgresCredentials(); DBWrapper dbWrapper = new DBWrapper(mysqlCredentials); dbWrapper.testConnections(); diff --git a/build.sh b/build.sh index 68dbc97d77943df6f9a174bb00251d5220f76199..8ebce01a7cc3d6d927c72aa45a663ff86423592c 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,6 @@ #!/bin/bash config_file="config.properties" -test_config_file="test.properties" if [ ! -f "$config_file" ]; then echo "$config_file doesn't exist!" @@ -27,7 +26,7 @@ function build_core { cd TASMAN-bom mvn -q clean install cd ../TASMAN-core - mvn -q clean install + mvn -q clean install -DskipTests if [ "$?" -ne 0 ]; then echo "[ERROR] Error in ${FUNCNAME[0]}" exit 1 @@ -40,7 +39,7 @@ function test_core { cd TASMAN-bom mvn -q clean install cd ../TASMAN-core - $(add_properties "mvn clean install -P test" $test_config_file) + mvn clean install if [ "$?" -ne 0 ]; then echo "[ERROR] Error in ${FUNCNAME[0]}" exit 1 @@ -53,7 +52,7 @@ function build_web_glassfish { # build webapp cd TASMAN-webapp - $(add_properties "mvn -q clean install" $config_file) + $(add_properties "mvn -q clean install -DskipTests" $config_file) if [ "$?" -ne 0 ]; then echo "[ERROR] Error in ${FUNCNAME[0]}" exit 1 @@ -67,7 +66,7 @@ function build_web_tomcat { # build webapp cd TASMAN-webapp - $(add_properties "mvn -q clean install -P ServletContainer" $config_file) + $(add_properties "mvn -q clean install -DskipTests -P ServletContainer" $config_file) if [ "$?" -ne 0 ]; then echo "[ERROR] Error in ${FUNCNAME[0]}" exit 1 @@ -81,7 +80,7 @@ function build_web_embedded { # build webapp cd TASMAN-webapp - $(add_properties "mvn -q clean install -P ServletContainer,Jetty" $config_file) + $(add_properties "mvn -q clean install -DskipTests -P ServletContainer,Jetty" $config_file) if [ "$?" -ne 0 ]; then echo "[ERROR] Error in ${FUNCNAME[0]}" exit 1 @@ -93,7 +92,7 @@ function build_web_embedded { cd TASMAN-embedded war_file_path=`dirname ${PWD}`/TASMAN-webapp/target/tasman-webapp-*.war war_file_path=`ls $war_file_path` - mvn clean -q install -Dwar_file_path=$war_file_path + mvn clean -q install -DskipTests -Dwar_file_path=$war_file_path if [ "$?" -ne 0 ]; then echo "[ERROR] Error in ${FUNCNAME[0]}" exit 1 @@ -107,7 +106,7 @@ function build_installer_package { # build webapp cd TASMAN-webapp - mvn -q clean install -P ServletContainer,Jetty + mvn -q clean install -DskipTests -P ServletContainer,Jetty if [ "$?" -ne 0 ]; then echo "[ERROR] Error in ${FUNCNAME[0]}" exit 1 @@ -117,7 +116,7 @@ function build_installer_package { # build embedded cd TASMAN-embedded - mvn -q clean install + mvn -q clean install -DskipTests if [ "$?" -ne 0 ]; then echo "[ERROR] Error in ${FUNCNAME[0]}" exit 1 @@ -162,10 +161,6 @@ case "$1" in build_core ;; "test") - if [ ! -f "$test_config_file" ]; then - echo "$test_config_file doesn't exist!" - exit 1 - fi test_core ;; "glassfish") diff --git a/test.properties.example b/test.properties.example deleted file mode 100644 index 5e0b06c416c31759765b3669301568b86d5129e6..0000000000000000000000000000000000000000 --- a/test.properties.example +++ /dev/null @@ -1,12 +0,0 @@ -# MySQL test database -mysql_host=localhost -mysql_port=3306 -mysql_user= -mysql_password= - -# Postgres test database -postgres_host=localhost -postgres_port=5432 -postgres_user= -postgres_password= -postgres_database=