From 9f9fccd221f4a9b76c63776b6883b8add803d50f 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 CompleteOcBuildTraceEnabledITCase to test suite which verifies openshift maven plugin goals with `org.slf4j.simpleLogger.defaultLogLevel` property set to trace Signed-off-by: Rohan Kumar --- .../CompleteOcBuildTraceEnabledITCase.java | 167 ++++++++++++++++++ .../maven/spring/complete/pom.xml | 27 +++ 2 files changed, 194 insertions(+) create mode 100644 it/src/test/java/org/eclipse/jkube/integrationtests/springboot/complete/CompleteOcBuildTraceEnabledITCase.java diff --git a/it/src/test/java/org/eclipse/jkube/integrationtests/springboot/complete/CompleteOcBuildTraceEnabledITCase.java b/it/src/test/java/org/eclipse/jkube/integrationtests/springboot/complete/CompleteOcBuildTraceEnabledITCase.java new file mode 100644 index 00000000..ad12c4c2 --- /dev/null +++ b/it/src/test/java/org/eclipse/jkube/integrationtests/springboot/complete/CompleteOcBuildTraceEnabledITCase.java @@ -0,0 +1,167 @@ +/** + * 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.springboot.complete; + +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.AfterEach; +import org.junit.jupiter.api.BeforeEach; +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.Collections; +import java.util.List; +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.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.Matchers.stringContainsInOrder; +import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ_WRITE; + +@Tag(OPEN_SHIFT) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class CompleteOcBuildTraceEnabledITCase extends Complete implements OpenShiftCase { + @BeforeEach + void setUp() { + System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace"); + } + + @AfterEach + void tearDown() { + System.clearProperty("org.slf4j.simpleLogger.defaultLogLevel"); + } + + @Override + public List getProfiles() { + return Collections.singletonList("OpenShift-Standard"); + } + + @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-")); + final ImageStream is = getOpenShiftClient().imageStreams().withName(getApplication()).get(); + assertThat(is, notNullValue()); + assertThat(is.getStatus().getTags().iterator().next().getTag(), equalTo("latest")); + } + + @Test + @Order(1) + @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/spring-boot-complete-deploymentconfig.yml"), yaml(not(anEmptyMap()))); + assertThat(new File(metaInfDirectory, "jkube/openshift/spring-boot-complete-service.yml"), yaml(not(anEmptyMap()))); + } + + @Test + @Order(2) + @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(3) + @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("Tomcat started on port(s)", "Started CompleteApplication in", "seconds")); + } + + @Test + @Order(4) + @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/projects-to-be-tested/maven/spring/complete/pom.xml b/projects-to-be-tested/maven/spring/complete/pom.xml index efe0175e..8f2d4ced 100644 --- a/projects-to-be-tested/maven/spring/complete/pom.xml +++ b/projects-to-be-tested/maven/spring/complete/pom.xml @@ -184,6 +184,33 @@ + + OpenShift-Standard + + + + org.eclipse.jkube + openshift-maven-plugin + + + + + complete + + + + + + + NodePort + + + + + + + + OpenShift-Docker