This is an automated email from the ASF dual-hosted git repository. jensdeppe pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/geode.git
The following commit(s) were added to refs/heads/develop by this push: new 0ec5e49 GEODE-6473: Create a test which utilizes the geode-management.jar (#3254) 0ec5e49 is described below commit 0ec5e49e6c5285015e836e16bf5ce6e8ecff4ae9 Author: Jens Deppe <jde...@pivotal.io> AuthorDate: Fri Mar 1 08:49:15 2019 -0800 GEODE-6473: Create a test which utilizes the geode-management.jar (#3254) - Also restore more detailed logging in ProcessLogger. --- ...tandaloneClientManagementAPIAcceptanceTest.java | 119 +++++++++++++++++++++ .../resources/ManagementClientCreateRegion.java | 41 +++++++ .../junit/rules/gfsh/internal/ProcessLogger.java | 39 ++++++- 3 files changed, 196 insertions(+), 3 deletions(-) diff --git a/geode-assembly/src/acceptanceTest/java/org/apache/geode/management/internal/rest/StandaloneClientManagementAPIAcceptanceTest.java b/geode-assembly/src/acceptanceTest/java/org/apache/geode/management/internal/rest/StandaloneClientManagementAPIAcceptanceTest.java new file mode 100644 index 0000000..0223a38 --- /dev/null +++ b/geode-assembly/src/acceptanceTest/java/org/apache/geode/management/internal/rest/StandaloneClientManagementAPIAcceptanceTest.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.apache.geode.management.internal.rest; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import org.apache.geode.test.compiler.JarBuilder; +import org.apache.geode.test.junit.rules.gfsh.GfshExecution; +import org.apache.geode.test.junit.rules.gfsh.GfshRule; +import org.apache.geode.test.junit.rules.gfsh.GfshScript; +import org.apache.geode.test.junit.rules.gfsh.internal.ProcessLogger; +import org.apache.geode.util.test.TestUtil; + +public class StandaloneClientManagementAPIAcceptanceTest { + + @Rule + public GfshRule gfsh = new GfshRule(); + + @Rule + public TemporaryFolder tempDir = new TemporaryFolder(); + + @Test + public void clientCreatesRegionUsingClusterManagementService() throws Exception { + JarBuilder jarBuilder = new JarBuilder(); + String filePath = + TestUtil.getResourcePath(this.getClass(), "/ManagementClientCreateRegion.java"); + assertThat(filePath).as("java file resource not found").isNotBlank(); + + File outputJar = new File(tempDir.getRoot(), "output.jar"); + jarBuilder.buildJar(outputJar, new File(filePath)); + + GfshExecution startCluster = + GfshScript.of("start locator", "start server --locators=localhost[10334]") + .withName("startCluster").execute(gfsh); + + assertThat(startCluster.getProcess().exitValue()) + .as("Cluster did not start correctly").isEqualTo(0); + + Process process = launchClientProcess(outputJar); + + boolean exited = process.waitFor(10, TimeUnit.SECONDS); + assertThat(exited).as("Process did not exit within 10 seconds").isTrue(); + assertThat(process.exitValue()).as("Process did not exit with 0 return code").isEqualTo(0); + + GfshExecution listRegionsResult = GfshScript.of("connect", "list regions") + .withName("listRegions").execute(gfsh); + assertThat(listRegionsResult.getOutputText()).contains("REGION1"); + } + + private Process launchClientProcess(File outputJar) throws IOException { + Path javaBin = Paths.get(System.getProperty("java.home"), "bin", "java"); + + ProcessBuilder pBuilder = new ProcessBuilder(); + pBuilder.directory(tempDir.newFolder()); + + StringBuilder classPath = new StringBuilder(); + for (String module : Arrays.asList( + "commons-logging", + "geode-common", + "geode-management", + "jackson-annotations", + "jackson-core", + "jackson-databind", + "spring-beans", + "spring-core", + "spring-web")) { + classPath.append(getJarOrClassesForModule(module)); + classPath.append(File.pathSeparator); + } + + classPath.append(File.pathSeparator); + classPath.append(outputJar.getAbsolutePath()); + + pBuilder.command(javaBin.toString(), "-classpath", classPath.toString(), + "ManagementClientTestCreateRegion", "REGION1"); + + Process process = pBuilder.start(); + new ProcessLogger(process, "clientCreateRegion"); + return process; + } + + private String getJarOrClassesForModule(String module) { + String classPath = Arrays.stream(System.getProperty("java.class.path") + .split(File.pathSeparator)) + .filter(x -> x.contains(module) + && (x.endsWith("/classes") || x.endsWith("/resources") || x.endsWith(".jar"))) + .collect(Collectors.joining(File.pathSeparator)); + + assertThat(classPath).as("no classes found for module: " + module) + .isNotBlank(); + + return classPath; + } +} diff --git a/geode-assembly/src/acceptanceTest/resources/ManagementClientCreateRegion.java b/geode-assembly/src/acceptanceTest/resources/ManagementClientCreateRegion.java new file mode 100644 index 0000000..6ef9b60 --- /dev/null +++ b/geode-assembly/src/acceptanceTest/resources/ManagementClientCreateRegion.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +import org.apache.geode.cache.configuration.RegionConfig; +import org.apache.geode.management.api.ClusterManagementResult; +import org.apache.geode.management.api.ClusterManagementService; +import org.apache.geode.management.client.ClusterManagementServiceProvider; + +public class ManagementClientTestCreateRegion { + public static void main(String[] args) { + String regionName = args[0]; + + ClusterManagementService cms = + ClusterManagementServiceProvider.getService("http://localhost:7070/geode-management"); + + RegionConfig config = new RegionConfig(); + config.setName(regionName); + config.setType("REPLICATE"); + + ClusterManagementResult result = cms.create(config, "cluster"); + + if (!result.isSuccessful()) { + throw new RuntimeException( + "Failure creating region: " + result.getPersistenceStatus().getMessage()); + } + + System.out.println("Successfully created region: " + regionName); + } +} diff --git a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/internal/ProcessLogger.java b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/internal/ProcessLogger.java index 7c6fce0..81be549 100644 --- a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/internal/ProcessLogger.java +++ b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/internal/ProcessLogger.java @@ -22,13 +22,26 @@ import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import org.apache.commons.lang3.SystemUtils; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.ConsoleAppender; +import org.apache.logging.log4j.core.config.Configurator; +import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder; +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory; +import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; public class ProcessLogger { - + private static final LoggerContext LOGGER_CONTEXT = createLoggerContext(); + private final Logger logger; private final Queue<OutputLine> outputLines = new ConcurrentLinkedQueue<>(); public ProcessLogger(Process process, String name) { + this.logger = LOGGER_CONTEXT.getLogger(name); + StreamGobbler stdOutGobbler = new StreamGobbler(process.getInputStream(), this::consumeInfoMessage); StreamGobbler stdErrGobbler = @@ -39,15 +52,35 @@ public class ProcessLogger { } private void consumeInfoMessage(String message) { - System.out.println(message); + logger.info(message); outputLines.add(OutputLine.fromStdOut(message)); } private void consumeErrorMessage(String message) { - System.err.println(message); + logger.error(message); outputLines.add(OutputLine.fromStdErr(message)); } + private static LoggerContext createLoggerContext() { + ConfigurationBuilder<BuiltConfiguration> builder = + ConfigurationBuilderFactory.newConfigurationBuilder(); + builder.setStatusLevel(Level.ERROR); + builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL) + .addAttribute("level", Level.DEBUG)); + AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE") + .addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); + appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", + "[%-5level %d{HH:mm:ss.SSS z}] (%c): %msg%n%throwable")); + appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY, Filter.Result.NEUTRAL) + .addAttribute("marker", "FLOW")); + builder.add(appenderBuilder); + builder.add(builder.newLogger("org.apache.logging.log4j", Level.ERROR) + .add(builder.newAppenderRef("Stdout")).addAttribute("additivity", false)); + builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))); + + return Configurator.initialize(builder.build()); + } + public List<String> getStdOutLines() { return getOutputLines(OutputLine.OutputSource.STD_OUT); }