diff --git a/README.md b/README.md index 26050b3d..685a6ea4 100644 --- a/README.md +++ b/README.md @@ -507,6 +507,10 @@ The Secret is exposed by mounting it into the container file system. Checks that the application can read configuration from a Secret. The Secret is obtained directly from the Kubernetes API server. +### `lifecycle-application` + +Verifies lifecycle application features like `@QuarkusMain` and `@CommandLineArguments`. + ### `sql-db` Verifies that the application can connect to a SQL database and persist data using Hibernate ORM with Panache. diff --git a/lifecycle-application/pom.xml b/lifecycle-application/pom.xml new file mode 100644 index 00000000..538a60ab --- /dev/null +++ b/lifecycle-application/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + + io.quarkus.ts.openshift + parent + 1.0.0-SNAPSHOT + .. + + + lifecycle-application + jar + + Quarkus OpenShift TS: Lifecycle Application + + + + io.quarkus + quarkus-openshift + + + io.quarkus + quarkus-resteasy + + + io.quarkus + quarkus-smallrye-health + + + io.quarkus.ts.openshift + app-metadata + + + + io.quarkus + quarkus-junit5 + test + + + io.quarkus.ts.openshift + common + test + + + io.rest-assured + rest-assured + test + + + + + + + io.quarkus + quarkus-maven-plugin + + + org.apache.maven.plugins + maven-failsafe-plugin + + + + diff --git a/lifecycle-application/src/main/java/io/quarkus/ts/openshift/lifecycle/ArgsResource.java b/lifecycle-application/src/main/java/io/quarkus/ts/openshift/lifecycle/ArgsResource.java new file mode 100644 index 00000000..06c83ac7 --- /dev/null +++ b/lifecycle-application/src/main/java/io/quarkus/ts/openshift/lifecycle/ArgsResource.java @@ -0,0 +1,25 @@ +package io.quarkus.ts.openshift.lifecycle; + +import io.quarkus.runtime.annotations.CommandLineArguments; + +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Path("/args") +public class ArgsResource { + @Inject + @CommandLineArguments + String[] args; + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String get() { + return Stream.of(args).collect(Collectors.joining(",")); + } +} diff --git a/lifecycle-application/src/main/java/io/quarkus/ts/openshift/lifecycle/Main.java b/lifecycle-application/src/main/java/io/quarkus/ts/openshift/lifecycle/Main.java new file mode 100644 index 00000000..ecda781e --- /dev/null +++ b/lifecycle-application/src/main/java/io/quarkus/ts/openshift/lifecycle/Main.java @@ -0,0 +1,18 @@ +package io.quarkus.ts.openshift.lifecycle; + +import io.quarkus.runtime.Quarkus; +import io.quarkus.runtime.annotations.QuarkusMain; +import org.jboss.logging.Logger; + +@QuarkusMain +public class Main { + + public static final String ARGUMENTS_FROM_MAIN = "Received arguments: "; + + private static final Logger LOG = Logger.getLogger(Main.class); + + public static void main(String... args) { + LOG.info(ARGUMENTS_FROM_MAIN + String.join(",", args)); + Quarkus.run(args); + } +} diff --git a/lifecycle-application/src/main/resources/application.properties b/lifecycle-application/src/main/resources/application.properties new file mode 100644 index 00000000..1e8bb770 --- /dev/null +++ b/lifecycle-application/src/main/resources/application.properties @@ -0,0 +1,8 @@ +quarkus.openshift.expose=true + +# We can't append arguments to the Java commend +# Not sure if the quarkus.openshift.arguments will be the right property to do this or there will be a new one. +# Related to https://github.com/quarkusio/quarkus/pull/15670#issuecomment-800857624. +# quarkus.openshift.arguments=ARG1,ARG2 + +quarkus.s2i.base-jvm-image=registry.access.redhat.com/ubi8/openjdk-11:latest \ No newline at end of file diff --git a/lifecycle-application/src/test/java/io/quarkus/ts/openshift/lifecycle/LifecycleApplicationOpenShiftIT.java b/lifecycle-application/src/test/java/io/quarkus/ts/openshift/lifecycle/LifecycleApplicationOpenShiftIT.java new file mode 100644 index 00000000..47c91ca6 --- /dev/null +++ b/lifecycle-application/src/test/java/io/quarkus/ts/openshift/lifecycle/LifecycleApplicationOpenShiftIT.java @@ -0,0 +1,71 @@ +package io.quarkus.ts.openshift.lifecycle; + +import io.fabric8.kubernetes.api.model.PodList; +import io.fabric8.openshift.client.OpenShiftClient; +import io.quarkus.ts.openshift.common.OpenShiftTest; +import io.quarkus.ts.openshift.common.injection.TestResource; +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; + +import java.io.BufferedReader; +import java.io.StringReader; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; + +import static io.restassured.RestAssured.when; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@OpenShiftTest +public class LifecycleApplicationOpenShiftIT { + + private static final String LOGGING_PROPERTY = "-Djava.util.logging.manager=org.jboss.logmanager.LogManager"; + private static final String RUNTIME_LABEL = "app.openshift.io/runtime"; + private static final String RUNTIME_QUARKUS = "quarkus"; + private static final List EXPECTED_ARGUMENTS = Arrays.asList("ARG1", "ARG2"); + + @TestResource + private OpenShiftClient openshiftClient; + + @Test + public void shouldArgumentsNotContainLoggingProperty() { + String actualArguments = when().get("/args") + .then().statusCode(200).extract().asString(); + + assertFalse(StringUtils.contains(actualArguments, LOGGING_PROPERTY), + "Actual arguments contain unexpected properties: " + actualArguments); + // Can't provide arguments to Java command: https://github.com/quarkusio/quarkus/pull/15670#issuecomment-800857624 + // assertExpectedArguments(actualArguments); + } + + @Test + public void shouldPrintMessagesFromQuarkusMain() { + PodList pods = openshiftClient.pods().withLabel(RUNTIME_LABEL, RUNTIME_QUARKUS).list(); + assertEquals(1, pods.getItems().size(), "Found more than one pod with Quarkus runtime"); + + String actualLog = openshiftClient.pods().withName(pods.getItems().get(0).getMetadata().getName()).getLog(); + Optional argumentsLineOpt = toLines(actualLog).filter(line -> line.contains(Main.ARGUMENTS_FROM_MAIN)) + .findFirst(); + + assertTrue(argumentsLineOpt.isPresent(), + "Pod log does not contain the received arguments. Actual content: " + actualLog); + String argumentsLine = argumentsLineOpt.get(); + assertFalse(argumentsLine.contains(LOGGING_PROPERTY), + "Pod log contain unexpected properties. Actual content: " + argumentsLine); + // Can't provide arguments to Java command: https://github.com/quarkusio/quarkus/pull/15670#issuecomment-800857624 + // assertExpectedArguments(argumentsLine); + } + + private void assertExpectedArguments(String actualArguments) { + EXPECTED_ARGUMENTS.forEach(arg -> assertTrue(actualArguments.contains(arg), + "Expected argument " + arg + " was not found in actual arguments: " + actualArguments)); + } + + private static final Stream toLines(String str) { + return new BufferedReader(new StringReader(str)).lines(); + } + +} diff --git a/pom.xml b/pom.xml index 7eb20726..ff27d8bb 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,7 @@ configmap/file-system config-secret/api-server config-secret/file-system + lifecycle-application messaging/artemis messaging/artemis-jta messaging/amqp-reactive