From f231efe53b2f505be37d0fe80d2fb704cbf1c4a6 Mon Sep 17 00:00:00 2001 From: Jose Date: Wed, 17 Mar 2021 08:41:24 +0100 Subject: [PATCH] Added lifecycle application module --- README.md | 4 ++ lifecycle-application/pom.xml | 66 +++++++++++++++++ .../ts/openshift/lifecycle/ArgsResource.java | 25 +++++++ .../quarkus/ts/openshift/lifecycle/Main.java | 18 +++++ .../src/main/resources/application.properties | 8 +++ .../LifecycleApplicationOpenShiftIT.java | 71 +++++++++++++++++++ pom.xml | 1 + 7 files changed, 193 insertions(+) create mode 100644 lifecycle-application/pom.xml create mode 100644 lifecycle-application/src/main/java/io/quarkus/ts/openshift/lifecycle/ArgsResource.java create mode 100644 lifecycle-application/src/main/java/io/quarkus/ts/openshift/lifecycle/Main.java create mode 100644 lifecycle-application/src/main/resources/application.properties create mode 100644 lifecycle-application/src/test/java/io/quarkus/ts/openshift/lifecycle/LifecycleApplicationOpenShiftIT.java diff --git a/README.md b/README.md index e465e9ae..a759de08 100644 --- a/README.md +++ b/README.md @@ -501,6 +501,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 b5ccf7cb..c65af753 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,7 @@ configmap/file-system config-secret/api-server config-secret/file-system + lifecycle-application messaging/artemis messaging/artemis-jta messaging/amqp-reactive