Skip to content

Commit

Permalink
Add a test for generating and running a sample Quarkus app
Browse files Browse the repository at this point in the history
  • Loading branch information
aloubyansky committed Sep 25, 2024
1 parent 8be6b0d commit 91dc105
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 0 deletions.
1 change: 1 addition & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@

<modules>
<module>platform-generator</module>
<module>sample-app</module>
</modules>
</project>
68 changes: 68 additions & 0 deletions integration-tests/sample-app/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-platform-bom-it-parent</artifactId>
<version>0.0.114-SNAPSHOT</version>
</parent>

<artifactId>quarkus-platform-sample-app-test</artifactId>
<name>Quarkus - Platform BOM Tools - IT - Sample Application Generator</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bootstrap-app-model</artifactId>
<version>${quarkus.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.logmanager</groupId>
<artifactId>jboss-logmanager</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
<project.version>${project.version}</project.version>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>${quarkus.version}</quarkus.platform.version>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package io.quarkus.platform.test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

import io.quarkus.bootstrap.util.IoUtils;
import io.quarkus.bootstrap.util.PropertyUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;

/**
* This test generates, builds and tests a sample Quarkus application (unit and integration tests).
*/
public class SampleProjectGenerationTest {

private static final Logger log = Logger.getLogger(SampleProjectGenerationTest.class);

private static final String SAMPLE_APP = "sample-app";
private static final String GENERATE_APP_TIMEOUT = "sample-app.generate-timeout";
private static final String BUILD_APP_TIMEOUT = "sample-app.build-timeout";

@Test
public void testSampleProjectGeneration() throws Exception {
final Path projectDir = generateSampleAppWithMaven();
buildApp(projectDir);
}

private static void buildApp(Path projectDir) throws IOException {

final Path cliLog = projectDir.resolve("build.log");
final Path cliErrors = projectDir.resolve("build-errors.log");
final long startTime = System.currentTimeMillis();
Process process = new ProcessBuilder()
.directory(projectDir.toFile())
.command("./mvnw", "verify", "-DskipITs=false")
.redirectOutput(cliLog.toFile())
.redirectError(cliErrors.toFile())
.start();
log.info("Building the application with '" + process.info().commandLine().orElse("<command not available>") + "'");
try {
if (!process.waitFor(getBuildAppTimeout(), TimeUnit.SECONDS)) {
try {
process.destroy();
} catch (Exception e) {
log.error("Failed to destroy the process", e);
}
fail("The process has exceeded the time out limit of " + getBuildAppTimeout() + " seconds");
}
} catch (InterruptedException e) {
log.error("Interrupted waiting for the process to complete", e);
}
process = process.onExit().join();
log.infof("Done in %.1f seconds", ((double) (System.currentTimeMillis() - startTime)) / 1000);

if (process.exitValue() != 0) {
var message = Files.readString(cliErrors);
if (message.isEmpty()) {
fail(Files.readString(cliLog));
}
fail(Files.readString(cliErrors));
}
}

private static String getMvnPath() {
var mavenHome = System.getProperty("maven.home");
if (mavenHome == null) {
return "mvn";
}
return Path.of(mavenHome).resolve("bin").resolve(
PropertyUtils.isWindows() ? "mvn.bat" : "mvn").toString();
}

private static Path generateSampleAppWithMaven() throws IOException {
final String quarkusPlatformGroupId = getRequiredProperty("quarkus.platform.group-id");
final String quarkusPlatformVersion = getRequiredProperty("quarkus.platform.version");

final Path workDir = Path.of("").normalize().toAbsolutePath().resolve("target").resolve("sample-app");
IoUtils.recursiveDelete(workDir);
Files.createDirectories(workDir);
final Path cliLog = workDir.resolve("cli.log");
final Path cliErrors = workDir.resolve("cli-errors.log");
final long startTime = System.currentTimeMillis();
Process process = new ProcessBuilder()
.directory(workDir.toFile())
.command(getMvnPath(),
quarkusPlatformGroupId + ":quarkus-maven-plugin:" + quarkusPlatformVersion + ":create",
"-DplatformGroupId=" + quarkusPlatformGroupId,
"-DplatformVersion=" + quarkusPlatformVersion,
"-DprojectGroupId=org.acme",
"-DprojectArtifactId=" + SAMPLE_APP,
"-DprojectVersion=1.0-SNAPSHOT",
"-DquarkusRegistryClient=false")
.redirectOutput(cliLog.toFile())
.redirectError(cliErrors.toFile())
.start();
log.info("Generating application with '" + process.info().commandLine().orElse("<command not available>") + "'");
try {
if (!process.waitFor(getGenerateAppTimeout(), TimeUnit.SECONDS)) {
try {
process.destroy();
} catch (Exception e) {
log.error("Failed to destroy the process", e);
}
fail("The process has exceeded the time out limit of " + getGenerateAppTimeout() + " seconds");
}
} catch (InterruptedException e) {
log.error("Interrupted waiting for the process to complete", e);
}
process = process.onExit().join();
log.infof("Done in %.1f seconds", ((double) (System.currentTimeMillis() - startTime)) / 1000);

if (process.exitValue() != 0) {
var message = Files.readString(cliErrors);
if (message.isEmpty()) {
fail(Files.readString(cliLog));
}
fail(Files.readString(cliErrors));
}
var projectDir = workDir.resolve(SAMPLE_APP);
assertThat(projectDir).exists();
return projectDir;
}

private static String getRequiredProperty(String name) {
var value = System.getProperty(name);
if (value == null) {
fail("Required property " + name + " is not set");
}
return value;
}

private static long getBuildAppTimeout() {
var propValue = System.getProperty(BUILD_APP_TIMEOUT);
if (propValue == null) {
return 30;
}
return Long.parseLong(propValue);
}

private static long getGenerateAppTimeout() {
var propValue = System.getProperty(GENERATE_APP_TIMEOUT);
if (propValue == null) {
return 10;
}
return Long.parseLong(propValue);
}
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<jakarta-inject.version>2.0.1</jakarta-inject.version>
<maven-gpg-plugin.version>3.1.0</maven-gpg-plugin.version>
<compiler-plugin.version>3.12.1</compiler-plugin.version>
<maven-jar-plugin.version>3.4.2</maven-jar-plugin.version>
<maven-javadoc-plugin.version>3.6.3</maven-javadoc-plugin.version>
<maven-source-plugin.version>3.3.0</maven-source-plugin.version>
<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
Expand Down Expand Up @@ -260,6 +261,10 @@
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
Expand Down

0 comments on commit 91dc105

Please sign in to comment.