From ca5dbfb0e70fb9a8ec1e7c0847c82ff5c501225d Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Thu, 19 Oct 2023 21:58:57 +0530 Subject: [PATCH] test : Add test to verify OpenShift trace logging works Related to https://github.com/eclipse/jkube/issues/2211 + Add QuarkusOcTraceEnabledITCase to test suite which verifies openshift maven plugin goals with `org.slf4j.simpleLogger.defaultLogLevel` property set to trace + Add new test project `maven/quarkus/rest-trace-logging-enabled` for testing abovementioned integration test Signed-off-by: Rohan Kumar --- .../quarkus/rest/Quarkus.java | 2 +- .../quarkus/rest/QuarkusOcITCase.java | 1 - .../rest/QuarkusOcTraceEnabledITCase.java | 164 ++++++++++++++++++ pom.xml | 1 + .../rest-trace-logging-enabled/pom.xml | 95 ++++++++++ .../rest/CoolApplicationWithTraceEnabled.java | 33 ++++ ...olApplicationWithTraceEnabledResource.java | 38 ++++ ...oolApplicationWithTraceEnabledService.java | 24 +++ 8 files changed, 356 insertions(+), 2 deletions(-) create mode 100644 it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/QuarkusOcTraceEnabledITCase.java create mode 100644 projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/pom.xml create mode 100644 projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabled.java create mode 100644 projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabledResource.java create mode 100644 projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabledService.java diff --git a/it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/Quarkus.java b/it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/Quarkus.java index 5b95b881..4e04359e 100644 --- a/it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/Quarkus.java +++ b/it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/Quarkus.java @@ -59,7 +59,7 @@ public String getApplication() { final Pod assertThatShouldApplyResources() throws Exception { final Pod pod = awaitPod(this).getKubernetesResource(); assertPod(pod).apply(this) - .logContains("quarkus-rest 0.0.0-SNAPSHOT on JVM (powered by Quarkus ", 60) + .logContains(getApplication() + " 0.0.0-SNAPSHOT on JVM (powered by Quarkus ", 60) .logContains(".Final) started in", 10); awaitService(this, pod.getMetadata().getNamespace()) .assertIsNodePort() diff --git a/it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/QuarkusOcITCase.java b/it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/QuarkusOcITCase.java index 1efbd85d..8ee42618 100644 --- a/it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/QuarkusOcITCase.java +++ b/it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/QuarkusOcITCase.java @@ -50,7 +50,6 @@ class QuarkusOcITCase extends Quarkus implements OpenShiftCase { @ResourceLock(value = CLUSTER_RESOURCE_INTENSIVE, mode = READ_WRITE) @DisplayName("oc:build, should create image") void ocBuild() throws Exception { - getOpenShiftClient().imageStreams().delete(); // When final InvocationResult invocationResult = maven("oc:build"); // Then diff --git a/it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/QuarkusOcTraceEnabledITCase.java b/it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/QuarkusOcTraceEnabledITCase.java new file mode 100644 index 00000000..b8e7d08b --- /dev/null +++ b/it/src/test/java/org/eclipse/jkube/integrationtests/quarkus/rest/QuarkusOcTraceEnabledITCase.java @@ -0,0 +1,164 @@ +/** + * Copyright (c) 2019 Red Hat, Inc. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at: + * + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ +package org.eclipse.jkube.integrationtests.quarkus.rest; + +import io.fabric8.openshift.api.model.ImageStream; +import org.apache.maven.shared.invoker.InvocationResult; +import org.eclipse.jkube.integrationtests.OpenShiftCase; +import org.eclipse.jkube.integrationtests.maven.MavenInvocationResult; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.parallel.ResourceLock; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.util.Properties; + +import static org.eclipse.jkube.integrationtests.Locks.CLUSTER_RESOURCE_INTENSIVE; +import static org.eclipse.jkube.integrationtests.Tags.OPEN_SHIFT; +import static org.eclipse.jkube.integrationtests.Tags.OPEN_SHIFT_OSCI; +import static org.eclipse.jkube.integrationtests.assertions.InvocationResultAssertion.assertInvocation; +import static org.eclipse.jkube.integrationtests.assertions.JKubeAssertions.assertJKube; +import static org.eclipse.jkube.integrationtests.assertions.KubernetesListAssertion.assertListResource; +import static org.eclipse.jkube.integrationtests.assertions.YamlAssertion.yaml; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder; +import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ_WRITE; + +@Tag(OPEN_SHIFT) +@Tag(OPEN_SHIFT_OSCI) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class QuarkusOcTraceEnabledITCase extends Quarkus implements OpenShiftCase { + + @Override + public String getProject() { + return "projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled"; + } + + @Override + public String getApplication() { + return "quarkus-rest-trace-logging-enabled"; + } + + @Test + @Order(1) + @ResourceLock(value = CLUSTER_RESOURCE_INTENSIVE, mode = READ_WRITE) + @DisplayName("oc:build, with org.slf4j.simpleLogger.defaultLogLevel=trace, should create image and print trace logs") + void ocBuild() throws Exception { + // Given + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + // When + final InvocationResult invocationResult = maven("clean package oc:build", + properties("org.slf4j.simpleLogger.defaultLogLevel", "trace"), baos); + // Then + assertInvocation(invocationResult); + String ocBuildLog = baos.toString(); + assertThat(ocBuildLog, containsString("[TRACE] -HTTP START-")); + assertThat(ocBuildLog, containsString("[TRACE] -HTTP END-")); + assertThat(ocBuildLog, containsString("[TRACE] -WS START-")); + assertThat(ocBuildLog, containsString("[TRACE] -WS END-")); + ImageStream is = getOpenShiftClient().imageStreams().withName(getApplication()).get(); + + assertThat(is.getStatus().getTags().iterator().next().getTag(), equalTo("latest")); + assertThat(getOpenShiftClient().imageStreams().withName(getApplication()).get(), notNullValue()); + } + + @Test + @Order(2) + @DisplayName("oc:resource, should create manifests") + void ocResource() throws Exception { + // When + final InvocationResult invocationResult = maven("oc:resource"); + // Then + assertInvocation(invocationResult); + final File metaInfDirectory = new File( + String.format("../%s/target/classes/META-INF", getProject())); + assertThat(metaInfDirectory.exists(), equalTo(true)); + assertListResource(new File(metaInfDirectory, "jkube/openshift.yml")); + assertThat(new File(metaInfDirectory, "jkube/openshift/quarkus-rest-trace-logging-enabled-deploymentconfig.yml"), yaml(not(anEmptyMap()))); + assertThat(new File(metaInfDirectory, "jkube/openshift/quarkus-rest-trace-logging-enabled-service.yml"), yaml(not(anEmptyMap()))); + } + + @Test + @Order(3) + @ResourceLock(value = CLUSTER_RESOURCE_INTENSIVE, mode = READ_WRITE) + @DisplayName("oc:apply, with org.slf4j.simpleLogger.defaultLogLevel=trace, should deploy pod and service and print trace logs") + void ocApply() throws Exception { + // Given + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + assertThat(getOpenShiftClient().imageStreams().withName(getApplication()).get(), notNullValue()); + // When + final InvocationResult invocationResult = maven("oc:apply", + properties("org.slf4j.simpleLogger.defaultLogLevel", "trace") + , baos); + // Then + String ocApplyLog = baos.toString(); + assertThat(ocApplyLog, containsString("[TRACE] -HTTP START-")); + assertThat(ocApplyLog, containsString("[TRACE] -HTTP END-")); + assertInvocation(invocationResult); + assertThatShouldApplyResources(); + } + + @Test + @Order(4) + @DisplayName("oc:log, with org.slf4j.simpleLogger.defaultLogLevel=trace, should retrieve log and print trace logs") + void ocLog() throws Exception { + // Given + Properties properties = new Properties(); + properties.put("org.slf4j.simpleLogger.defaultLogLevel", "trace"); + properties.put("jkube.log.follow", "false"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // When + final MavenInvocationResult invocationResult = maven("oc:log", properties, baos); + // Then + String ocLogGoalLog = baos.toString(); + assertThat(ocLogGoalLog, containsString("[TRACE] -HTTP START-")); + assertThat(ocLogGoalLog, containsString("[TRACE] -HTTP END-")); + assertThat(ocLogGoalLog, containsString("[TRACE] -WS START-")); + assertThat(ocLogGoalLog, containsString("[TRACE] -WS END-")); + assertInvocation(invocationResult); + assertThat(invocationResult.getStdOut(), + stringContainsInOrder(String.format("INFO: %s 0.0.0-SNAPSHOT on JVM", getApplication()), " started in ")); + } + + @Test + @Order(5) + @ResourceLock(value = CLUSTER_RESOURCE_INTENSIVE, mode = READ_WRITE) + @DisplayName("oc:undeploy, with org.slf4j.simpleLogger.defaultLogLevel=trace, should delete all applied resources and print trace logs") + void ocUndeploy() throws Exception { + // Given + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + // When + final InvocationResult invocationResult = maven("oc:undeploy", + properties("org.slf4j.simpleLogger.defaultLogLevel", "trace"), baos); + // Then + String ocUndeployLog = baos.toString(); + assertThat(ocUndeployLog, containsString("[TRACE] -HTTP START-")); + assertThat(ocUndeployLog, containsString("[TRACE] -HTTP END-")); + assertInvocation(invocationResult); + assertJKube(this) + .assertThatShouldDeleteAllAppliedResources(); + cleanUpCluster(); + } +} diff --git a/pom.xml b/pom.xml index 0dd60090..15f390d8 100644 --- a/pom.xml +++ b/pom.xml @@ -321,6 +321,7 @@ quarkus projects-to-be-tested/maven/quarkus/rest + projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled diff --git a/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/pom.xml b/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/pom.xml new file mode 100644 index 00000000..749eb71d --- /dev/null +++ b/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/pom.xml @@ -0,0 +1,95 @@ + + + + 4.0.0 + + + org.eclipse.jkube.integration-tests + jkube-integration-tests-project + ${revision} + ../../../../pom.xml + + + quarkus-rest-trace-logging-enabled + ${global.name} :: Quarkus :: Rest :: Trace Logging Enabled + + Quarkus REST JSON project + + + + + + io.quarkus.platform + quarkus-bom + ${quarkus.version} + pom + import + + + + + + + io.quarkus + quarkus-arc + + + io.quarkus + quarkus-resteasy + + + io.quarkus + quarkus-resteasy-jackson + + + + + + + io.quarkus.platform + quarkus-maven-plugin + ${quarkus.version} + true + + + + build + generate-code + generate-code-tests + + + + + + org.eclipse.jkube + openshift-maven-plugin + + + + + NodePort + + + + + + + + + diff --git a/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabled.java b/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabled.java new file mode 100644 index 00000000..e8a622ae --- /dev/null +++ b/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabled.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 Red Hat, Inc. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at: + * + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ +package org.eclipse.jkube.integrationtests.quarkus.rest; + +public class CoolApplicationWithTraceEnabled { + + private final String applicationName; + private final String message; + + public CoolApplicationWithTraceEnabled(String applicationName, String message) { + this.applicationName = applicationName; + this.message = message; + } + + public String getApplicationName() { + return applicationName; + } + + public String getMessage() { + return message; + } +} diff --git a/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabledResource.java b/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabledResource.java new file mode 100644 index 00000000..fa3d8d5b --- /dev/null +++ b/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabledResource.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 Red Hat, Inc. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at: + * + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ +package org.eclipse.jkube.integrationtests.quarkus.rest; + +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +@Path("/") +public class CoolApplicationWithTraceEnabledResource { + + private CoolApplicationWithTraceEnabledService coolApplicationService; + + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response get() { + return Response.ok(coolApplicationService.getCoolApplication()).build(); + } + + @Inject + public void setCoolApplicationService(CoolApplicationWithTraceEnabledService coolApplicationService) { + this.coolApplicationService = coolApplicationService; + } +} diff --git a/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabledService.java b/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabledService.java new file mode 100644 index 00000000..40a90f92 --- /dev/null +++ b/projects-to-be-tested/maven/quarkus/rest-trace-logging-enabled/src/main/java/org/eclipse/jkube/integrationtests/quarkus/rest/CoolApplicationWithTraceEnabledService.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 Red Hat, Inc. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at: + * + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ +package org.eclipse.jkube.integrationtests.quarkus.rest; + +import jakarta.inject.Singleton; + +@Singleton +public class CoolApplicationWithTraceEnabledService { + + CoolApplicationWithTraceEnabled getCoolApplication() { + return new CoolApplicationWithTraceEnabled("JKube", "Subatomic JKube really whips the llama's ass!"); + } +}