GEODE-3031: Extracting startLocator and startServer from LauncherLifecycleCommands
* this closes #642 Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/fcce2b0b Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/fcce2b0b Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/fcce2b0b Branch: refs/heads/feature/GEM-1483 Commit: fcce2b0bed7b108db0e61716e14f51915d61c9d4 Parents: 7f86d0c Author: YehEmily <[email protected]> Authored: Mon Jul 17 14:35:50 2017 -0700 Committer: Jinmei Liao <[email protected]> Committed: Tue Jul 25 11:18:56 2017 -0700 ---------------------------------------------------------------------- .../commands/GfshCommandIntegrationTest.java | 177 +++ .../LauncherLifecycleCommandsDUnitTest.java | 72 +- ...auncherLifecycleCommandsIntegrationTest.java | 214 --- .../commands/LauncherLifecycleCommandsTest.java | 575 ------- .../StartLocatorCommandIntegrationTest.java | 85 ++ .../cli/commands/StartLocatorCommandTest.java | 147 ++ .../StartServerCommandIntegrationTest.java | 85 ++ .../cli/commands/StartServerCommandTest.java | 144 ++ .../internal/process/signal/SignalListener.java | 5 +- .../internal/cli/commands/GfshCommand.java | 3 +- .../cli/commands/LauncherLifecycleCommands.java | 1415 ------------------ .../cli/commands/LauncherSignalListener.java | 31 + .../cli/commands/StartLocatorCommand.java | 546 +++++++ .../internal/cli/commands/StartMemberUtils.java | 250 ++++ .../cli/commands/StartServerCommand.java | 594 ++++++++ .../LauncherLifecycleCommandsController.java | 3 +- .../cli/commands/GfshCommandJUnitTest.java | 232 ++- .../commands/LauncherLifecycleCommandsTest.java | 120 -- .../internal/security/TestCommand.java | 2 +- 19 files changed, 2328 insertions(+), 2372 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/fcce2b0b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/GfshCommandIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/GfshCommandIntegrationTest.java b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/GfshCommandIntegrationTest.java new file mode 100644 index 0000000..263b12c --- /dev/null +++ b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/GfshCommandIntegrationTest.java @@ -0,0 +1,177 @@ +/* + * 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.cli.commands; + +import static org.assertj.core.api.Java6Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.jar.Attributes; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +import org.apache.commons.io.FileUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; + +import org.apache.geode.internal.util.IOUtils; +import org.apache.geode.test.junit.categories.IntegrationTest; + +@Category(IntegrationTest.class) +public class GfshCommandIntegrationTest { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Rule + public TestName testName = new TestName(); + + @Test + public void workingDirDefaultsToMemberName() { + StartServerCommand startServer = new StartServerCommand(); + String workingDir = StartMemberUtils.resolveWorkingDir(null, "server1"); + assertThat(new File(workingDir)).exists(); + assertThat(workingDir).endsWith("server1"); + } + + @Test + public void workingDirGetsCreatedIfNecessary() throws Exception { + File workingDir = temporaryFolder.newFolder("foo"); + FileUtils.deleteQuietly(workingDir); + String workingDirString = workingDir.getAbsolutePath(); + + StartServerCommand startServer = new StartServerCommand(); + + String resolvedWorkingDir = StartMemberUtils.resolveWorkingDir(workingDirString, "server1"); + assertThat(new File(resolvedWorkingDir)).exists(); + assertThat(workingDirString).endsWith("foo"); + } + + @Test + public void testWorkingDirWithRelativePath() throws Exception { + Path relativePath = Paths.get("some").resolve("relative").resolve("path"); + assertThat(relativePath.isAbsolute()).isFalse(); + + StartServerCommand startServer = new StartServerCommand(); + + String resolvedWorkingDir = + StartMemberUtils.resolveWorkingDir(relativePath.toString(), "server1"); + + assertThat(resolvedWorkingDir).isEqualTo(relativePath.toAbsolutePath().toString()); + } + + @Test + public void testReadPid() throws IOException { + final int expectedPid = 12345; + + File folder = temporaryFolder.newFolder(); + File pidFile = + new File(folder, getClass().getSimpleName() + "_" + testName.getMethodName() + ".pid"); + + assertTrue(pidFile.createNewFile()); + + pidFile.deleteOnExit(); + writePid(pidFile, expectedPid); + + final int actualPid = StartMemberUtils.readPid(pidFile); + + assertEquals(expectedPid, actualPid); + } + + @Test + public void testGemFireCoreClasspath() throws IOException { + File coreDependenciesJar = new File(StartMemberUtils.CORE_DEPENDENCIES_JAR_PATHNAME); + assertNotNull(coreDependenciesJar); + assertTrue(coreDependenciesJar + " is not a file", coreDependenciesJar.isFile()); + Collection<String> expectedJarDependencies = + Arrays.asList("antlr", "commons-io", "commons-lang", "commons-logging", "geode", + "jackson-annotations", "jackson-core", "jackson-databind", "jansi", "jline", "snappy", + "spring-core", "spring-shell", "jetty-server", "jetty-servlet", "jetty-webapp", + "jetty-util", "jetty-http", "servlet-api", "jetty-io", "jetty-security", "jetty-xml"); + assertJarFileManifestClassPath(coreDependenciesJar, expectedJarDependencies); + } + + private void assertJarFileManifestClassPath(final File dependenciesJar, + final Collection<String> expectedJarDependencies) throws IOException { + JarFile dependenciesJarFile = new JarFile(dependenciesJar); + Manifest manifest = dependenciesJarFile.getManifest(); + + assertNotNull(manifest); + + Attributes attributes = manifest.getMainAttributes(); + + assertNotNull(attributes); + assertTrue(attributes.containsKey(Attributes.Name.CLASS_PATH)); + + String[] actualJarDependencies = attributes.getValue(Attributes.Name.CLASS_PATH).split(" "); + + assertNotNull(actualJarDependencies); + assertTrue( + String.format( + "Expected the actual number of JAR dependencies to be (%1$d); but was (%2$d)!", + expectedJarDependencies.size(), actualJarDependencies.length), + actualJarDependencies.length >= expectedJarDependencies.size()); + // assertTrue(Arrays.asList(actualJarDependencies).containsAll(expectedJarDependencies)); + + List<String> actualJarDependenciesList = new ArrayList<>(Arrays.asList(actualJarDependencies)); + List<String> missingExpectedJarDependenciesList = + new ArrayList<>(expectedJarDependencies.size()); + + for (String expectedJarDependency : expectedJarDependencies) { + boolean containsExpectedJar = false; + + for (int index = 0, size = actualJarDependenciesList.size(); index < size; index++) { + if (actualJarDependenciesList.get(index).toLowerCase() + .contains(expectedJarDependency.toLowerCase())) { + actualJarDependenciesList.remove(index); + containsExpectedJar = true; + break; + } + } + + if (!containsExpectedJar) { + missingExpectedJarDependenciesList.add(expectedJarDependency); + } + } + + assertTrue( + String.format( + "GemFire dependencies JAR file (%1$s) does not contain the expected dependencies (%2$s) in the Manifest Class-Path attribute (%3$s)!", + dependenciesJar, missingExpectedJarDependenciesList, + attributes.getValue(Attributes.Name.CLASS_PATH)), + missingExpectedJarDependenciesList.isEmpty()); + } + + private void writePid(final File pidFile, final int pid) throws IOException { + final FileWriter fileWriter = new FileWriter(pidFile, false); + fileWriter.write(String.valueOf(pid)); + fileWriter.write("\n"); + fileWriter.flush(); + IOUtils.close(fileWriter); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fcce2b0b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsDUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsDUnitTest.java b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsDUnitTest.java index 4455249..6e5d17c 100644 --- a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsDUnitTest.java +++ b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsDUnitTest.java @@ -22,6 +22,39 @@ import static org.apache.geode.test.dunit.Assert.assertNotNull; import static org.apache.geode.test.dunit.Assert.assertTrue; import static org.apache.geode.test.dunit.Wait.waitForCriterion; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.lang.management.ManagementFactory; +import java.net.InetAddress; +import java.nio.charset.Charset; +import java.text.DateFormat; +import java.text.MessageFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.TimeUnit; + +import javax.management.MBeanServerConnection; +import javax.management.ObjectName; +import javax.management.Query; +import javax.management.QueryExp; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; + +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runners.MethodSorters; + import org.apache.geode.cache.Region; import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.ClientCacheFactory; @@ -52,37 +85,6 @@ import org.apache.geode.management.internal.cli.result.CommandResult; import org.apache.geode.management.internal.cli.util.CommandStringBuilder; import org.apache.geode.test.dunit.WaitCriterion; import org.apache.geode.test.junit.categories.DistributedTest; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.junit.runners.MethodSorters; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileOutputStream; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.lang.management.ManagementFactory; -import java.net.InetAddress; -import java.nio.charset.Charset; -import java.text.DateFormat; -import java.text.MessageFormat; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Queue; -import java.util.Set; -import java.util.concurrent.ConcurrentLinkedDeque; -import java.util.concurrent.TimeUnit; -import javax.management.MBeanServerConnection; -import javax.management.ObjectName; -import javax.management.Query; -import javax.management.QueryExp; -import javax.management.remote.JMXConnector; -import javax.management.remote.JMXConnectorFactory; -import javax.management.remote.JMXServiceURL; /** * The LauncherLifecycleCommandsDUnitTest class is a test suite of integration tests testing the @@ -96,7 +98,6 @@ import javax.management.remote.JMXServiceURL; * @see org.apache.geode.internal.AvailablePortHelper * @see org.apache.geode.management.internal.cli.shell.Gfsh * @see org.apache.geode.management.internal.cli.commands.CliCommandTestBase - * @see org.apache.geode.management.internal.cli.commands.LauncherLifecycleCommands * @see org.apache.geode.management.internal.cli.util.CommandStringBuilder * @since GemFire 7.0 */ @@ -148,7 +149,6 @@ public class LauncherLifecycleCommandsDUnitTest extends CliCommandTestBase { @Override public final void postTearDown() throws Exception { - LauncherLifecycleCommands launcherLifecycleCommands = new LauncherLifecycleCommands(); Integer pid; while ((pid = processIds.poll()) != null) { @@ -254,9 +254,6 @@ public class LauncherLifecycleCommandsDUnitTest extends CliCommandTestBase { if (pid != null) { WaitCriterion waitCriteria = new WaitCriterion() { - private LauncherLifecycleCommands launcherLifecycleCommands = - new LauncherLifecycleCommands(); - @Override public boolean done() { return !ProcessUtils.isProcessAlive(pid); @@ -1031,9 +1028,6 @@ public class LauncherLifecycleCommandsDUnitTest extends CliCommandTestBase { final int serverPid = serverState.getPid(); WaitCriterion waitCriteria = new WaitCriterion() { - private LauncherLifecycleCommands launcherLifecycleCommands = - new LauncherLifecycleCommands(); - @Override public boolean done() { return !ProcessUtils.isProcessAlive(serverPid); http://git-wip-us.apache.org/repos/asf/geode/blob/fcce2b0b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsIntegrationTest.java b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsIntegrationTest.java deleted file mode 100644 index c4bff8c..0000000 --- a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsIntegrationTest.java +++ /dev/null @@ -1,214 +0,0 @@ -/* - * 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.cli.commands; - - -import static org.assertj.core.api.Java6Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.jar.Attributes; -import java.util.jar.Attributes.Name; -import java.util.jar.JarFile; -import java.util.jar.Manifest; - -import org.apache.commons.io.FileUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.junit.rules.TemporaryFolder; -import org.junit.rules.TestName; - -import org.apache.geode.internal.util.IOUtils; -import org.apache.geode.test.junit.categories.IntegrationTest; - -/** - * The LauncherLifecycleCommandsIntegrationTest class is a test suite of test cases testing the - * contract and functionality of the lifecycle launcher GemFire shell (Gfsh) commands. - * - * @see org.apache.geode.management.internal.cli.commands.LauncherLifecycleCommands - * @see org.junit.Assert - * @see org.junit.Test - * @since GemFire 7.0 - */ -@Category(IntegrationTest.class) -public class LauncherLifecycleCommandsIntegrationTest { - - private LauncherLifecycleCommands launcherCommands; - - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - - @Rule - public TestName testName = new TestName(); - - @Before - public void setup() { - launcherCommands = new LauncherLifecycleCommands(); - } - - @After - public void tearDown() { - launcherCommands = null; - } - - @Test - public void testGemFireCoreClasspath() throws IOException { - File coreDependenciesJar = new File(LauncherLifecycleCommands.CORE_DEPENDENCIES_JAR_PATHNAME); - - assertNotNull(coreDependenciesJar); - assertTrue(coreDependenciesJar + " is not a file", coreDependenciesJar.isFile()); - - Collection<String> expectedJarDependencies = - Arrays.asList("antlr", "commons-io", "commons-lang", "commons-logging", "geode", - "jackson-annotations", "jackson-core", "jackson-databind", "jansi", "jline", "snappy", - "spring-core", "spring-shell", "jetty-server", "jetty-servlet", "jetty-webapp", - "jetty-util", "jetty-http", "servlet-api", "jetty-io", "jetty-security", "jetty-xml" - - ); - - assertJarFileManifestClassPath(coreDependenciesJar, expectedJarDependencies); - } - - @Test - public void testReadPid() throws IOException { - final int expectedPid = 12345; - - File folder = temporaryFolder.newFolder(); - File pidFile = - new File(folder, getClass().getSimpleName() + "_" + testName.getMethodName() + ".pid"); - - assertTrue(pidFile.createNewFile()); - - pidFile.deleteOnExit(); - writePid(pidFile, expectedPid); - - final int actualPid = getLauncherLifecycleCommands().readPid(pidFile); - - assertEquals(expectedPid, actualPid); - } - - private LauncherLifecycleCommands getLauncherLifecycleCommands() { - return launcherCommands; - } - - private void writePid(final File pidFile, final int pid) throws IOException { - final FileWriter fileWriter = new FileWriter(pidFile, false); - fileWriter.write(String.valueOf(pid)); - fileWriter.write("\n"); - fileWriter.flush(); - IOUtils.close(fileWriter); - } - - private void assertJarFileManifestClassPath(final File dependenciesJar, - final Collection<String> expectedJarDependencies) throws IOException { - JarFile dependenciesJarFile = new JarFile(dependenciesJar); - Manifest manifest = dependenciesJarFile.getManifest(); - - assertNotNull(manifest); - - Attributes attributes = manifest.getMainAttributes(); - - assertNotNull(attributes); - assertTrue(attributes.containsKey(Name.CLASS_PATH)); - - String[] actualJarDependencies = attributes.getValue(Name.CLASS_PATH).split(" "); - - assertNotNull(actualJarDependencies); - assertTrue( - String.format( - "Expected the actual number of JAR dependencies to be (%1$d); but was (%2$d)!", - expectedJarDependencies.size(), actualJarDependencies.length), - actualJarDependencies.length >= expectedJarDependencies.size()); - // assertTrue(Arrays.asList(actualJarDependencies).containsAll(expectedJarDependencies)); - - List<String> actualJarDependenciesList = new ArrayList<>(Arrays.asList(actualJarDependencies)); - List<String> missingExpectedJarDependenciesList = - new ArrayList<>(expectedJarDependencies.size()); - - for (String expectedJarDependency : expectedJarDependencies) { - boolean containsExpectedJar = false; - - for (int index = 0, size = actualJarDependenciesList.size(); index < size; index++) { - if (actualJarDependenciesList.get(index).toLowerCase() - .contains(expectedJarDependency.toLowerCase())) { - actualJarDependenciesList.remove(index); - containsExpectedJar = true; - break; - } - } - - if (!containsExpectedJar) { - missingExpectedJarDependenciesList.add(expectedJarDependency); - } - } - - assertTrue( - String.format( - "GemFire dependencies JAR file (%1$s) does not contain the expected dependencies (%2$s) in the Manifest Class-Path attribute (%3$s)!", - dependenciesJar, missingExpectedJarDependenciesList, - attributes.getValue(Name.CLASS_PATH)), - missingExpectedJarDependenciesList.isEmpty()); - } - - @Test - public void workingDirDefaultsToMemberName() { - LauncherLifecycleCommands launcherLifecycleCommands = new LauncherLifecycleCommands(); - - String workingDir = launcherLifecycleCommands.resolveWorkingDir(null, "server1"); - assertThat(new File(workingDir)).exists(); - assertThat(workingDir).endsWith("server1"); - } - - - @Test - public void workingDirGetsCreatedIfNecessary() throws Exception { - File workingDir = temporaryFolder.newFolder("foo"); - FileUtils.deleteQuietly(workingDir); - String workingDirString = workingDir.getAbsolutePath(); - - LauncherLifecycleCommands launcherLifecycleCommands = new LauncherLifecycleCommands(); - - String resolvedWorkingDir = - launcherLifecycleCommands.resolveWorkingDir(workingDirString, "server1"); - assertThat(new File(resolvedWorkingDir)).exists(); - assertThat(workingDirString).endsWith("foo"); - } - - @Test - public void testWorkingDirWithRelativePath() throws Exception { - Path relativePath = Paths.get("some").resolve("relative").resolve("path"); - assertThat(relativePath.isAbsolute()).isFalse(); - - LauncherLifecycleCommands launcherLifecycleCommands = new LauncherLifecycleCommands(); - - String resolvedWorkingDir = - launcherLifecycleCommands.resolveWorkingDir(relativePath.toString(), "server1"); - - assertThat(resolvedWorkingDir).isEqualTo(relativePath.toAbsolutePath().toString()); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/fcce2b0b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsTest.java ---------------------------------------------------------------------- diff --git a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsTest.java b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsTest.java deleted file mode 100755 index cc74398..0000000 --- a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/LauncherLifecycleCommandsTest.java +++ /dev/null @@ -1,575 +0,0 @@ -/* - * 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.cli.commands; - -import static org.apache.geode.distributed.ConfigurationProperties.HTTP_SERVICE_BIND_ADDRESS; -import static org.apache.geode.distributed.ConfigurationProperties.HTTP_SERVICE_PORT; -import static org.apache.geode.distributed.ConfigurationProperties.LOCATORS; -import static org.apache.geode.distributed.ConfigurationProperties.LOG_FILE; -import static org.apache.geode.distributed.ConfigurationProperties.LOG_LEVEL; -import static org.apache.geode.distributed.ConfigurationProperties.MCAST_PORT; -import static org.apache.geode.distributed.ConfigurationProperties.NAME; -import static org.apache.geode.distributed.ConfigurationProperties.START_DEV_REST_API; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import org.apache.commons.lang.StringUtils; -import org.apache.geode.GemFireException; -import org.apache.geode.cache.server.CacheServer; -import org.apache.geode.distributed.LocatorLauncher; -import org.apache.geode.distributed.ServerLauncher; -import org.apache.geode.distributed.internal.DistributionConfig; -import org.apache.geode.internal.DistributionLocator; -import org.apache.geode.internal.lang.SystemUtils; -import org.apache.geode.internal.util.IOUtils; -import org.apache.geode.management.internal.cli.i18n.CliStrings; -import org.apache.geode.test.junit.categories.UnitTest; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Properties; -import java.util.Set; -import java.util.Stack; - -/** - * The LauncherLifecycleCommandsTest class is a test suite of test cases testing the contract and - * functionality of the lifecycle launcher GemFire shell (Gfsh) commands. - * - * @see org.apache.geode.management.internal.cli.commands.LauncherLifecycleCommands - * @see org.junit.Assert - * @see org.junit.Test - * @since GemFire 7.0 - */ -@Category(UnitTest.class) -@SuppressWarnings("unused") -public class LauncherLifecycleCommandsTest { - - private LauncherLifecycleCommands launcherCommands; - - @Before - public void setup() { - launcherCommands = new LauncherLifecycleCommands(); - } - - @After - public void tearDown() { - launcherCommands = null; - } - - @Test - public void testAddGemFirePropertyFileToCommandLine() { - final List<String> commandLine = new ArrayList<>(); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addGemFirePropertyFile(commandLine, null); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addGemFirePropertyFile(commandLine, StringUtils.EMPTY); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addGemFirePropertyFile(commandLine, " "); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addGemFirePropertyFile(commandLine, - "/path/to/gemfire.properties"); - - assertFalse(commandLine.isEmpty()); - assertTrue(commandLine.contains("-DgemfirePropertyFile=/path/to/gemfire.properties")); - } - - @Test - public void testAddGemFireSystemPropertiesToCommandLine() { - final List<String> commandLine = new ArrayList<>(); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addGemFireSystemProperties(commandLine, new Properties()); - - assertTrue(commandLine.isEmpty()); - - final Properties gemfireProperties = new Properties(); - - gemfireProperties.setProperty(LOCATORS, "localhost[11235]"); - gemfireProperties.setProperty(LOG_LEVEL, "config"); - gemfireProperties.setProperty(LOG_FILE, StringUtils.EMPTY); - gemfireProperties.setProperty(MCAST_PORT, "0"); - gemfireProperties.setProperty(NAME, "machine"); - - getLauncherLifecycleCommands().addGemFireSystemProperties(commandLine, gemfireProperties); - - assertFalse(commandLine.isEmpty()); - assertEquals(4, commandLine.size()); - - for (final String propertyName : gemfireProperties.stringPropertyNames()) { - final String propertyValue = gemfireProperties.getProperty(propertyName); - if (StringUtils.isBlank(propertyValue)) { - for (final String systemProperty : commandLine) { - assertFalse(systemProperty.startsWith( - "-D" + DistributionConfig.GEMFIRE_PREFIX + "".concat(propertyName).concat("="))); - } - } else { - assertTrue(commandLine.contains("-D" + DistributionConfig.GEMFIRE_PREFIX - + "".concat(propertyName).concat("=").concat(propertyValue))); - } - } - } - - - @Test - public void testAddGemFireSystemPropertiesToCommandLineWithRestAPI() { - final List<String> commandLine = new ArrayList<>(); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addGemFireSystemProperties(commandLine, new Properties()); - - assertTrue(commandLine.isEmpty()); - - final Properties gemfireProperties = new Properties(); - - gemfireProperties.setProperty(LOCATORS, "localhost[11235]"); - gemfireProperties.setProperty(LOG_LEVEL, "config"); - gemfireProperties.setProperty(LOG_FILE, StringUtils.EMPTY); - gemfireProperties.setProperty(MCAST_PORT, "0"); - gemfireProperties.setProperty(NAME, "machine"); - - gemfireProperties.setProperty(START_DEV_REST_API, "true"); - gemfireProperties.setProperty(HTTP_SERVICE_PORT, "8080"); - gemfireProperties.setProperty(HTTP_SERVICE_BIND_ADDRESS, "localhost"); - - - getLauncherLifecycleCommands().addGemFireSystemProperties(commandLine, gemfireProperties); - - assertFalse(commandLine.isEmpty()); - assertEquals(7, commandLine.size()); - - for (final String propertyName : gemfireProperties.stringPropertyNames()) { - final String propertyValue = gemfireProperties.getProperty(propertyName); - if (StringUtils.isBlank(propertyValue)) { - for (final String systemProperty : commandLine) { - assertFalse(systemProperty.startsWith( - "-D" + DistributionConfig.GEMFIRE_PREFIX + "".concat(propertyName).concat("="))); - } - } else { - assertTrue(commandLine.contains("-D" + DistributionConfig.GEMFIRE_PREFIX - + "".concat(propertyName).concat("=").concat(propertyValue))); - } - } - } - - @Test - public void testAddInitialHeapToCommandLine() { - final List<String> commandLine = new ArrayList<>(); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addInitialHeap(commandLine, null); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addInitialHeap(commandLine, StringUtils.EMPTY); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addInitialHeap(commandLine, " "); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addInitialHeap(commandLine, "512M"); - - assertFalse(commandLine.isEmpty()); - assertEquals("-Xms512M", commandLine.get(0)); - } - - @Test - public void testAddJvmArgumentsAndOptionsToCommandLine() { - final List<String> commandLine = new ArrayList<>(); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addJvmArgumentsAndOptions(commandLine, null); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addJvmArgumentsAndOptions(commandLine, new String[] {}); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addJvmArgumentsAndOptions(commandLine, - new String[] {"-DmyProp=myVal", "-d64", "-server", "-Xprof"}); - - assertFalse(commandLine.isEmpty()); - assertEquals(4, commandLine.size()); - assertEquals("-DmyProp=myVal", commandLine.get(0)); - assertEquals("-d64", commandLine.get(1)); - assertEquals("-server", commandLine.get(2)); - assertEquals("-Xprof", commandLine.get(3)); - } - - // Fix for Bug #47192 - "Making GemFire (JVM) to exit in case of OutOfMemory" - @Test - public void testAddJvmOptionsForOutOfMemoryErrors() { - final List<String> jvmOptions = new ArrayList<>(1); - - getLauncherLifecycleCommands().addJvmOptionsForOutOfMemoryErrors(jvmOptions); - - if (SystemUtils.isHotSpotVM()) { - if (SystemUtils.isWindows()) { - assertTrue(jvmOptions.contains("-XX:OnOutOfMemoryError=taskkill /F /PID %p")); - } else { - assertTrue(jvmOptions.contains("-XX:OnOutOfMemoryError=kill -KILL %p")); - } - } else if (SystemUtils.isJ9VM()) { - assertEquals(1, jvmOptions.size()); - assertTrue(jvmOptions.contains("-Xcheck:memory")); - } else if (SystemUtils.isJRockitVM()) { - assertEquals(1, jvmOptions.size()); - assertTrue(jvmOptions.contains("-XXexitOnOutOfMemory")); - } else { - assertTrue(jvmOptions.isEmpty()); - } - } - - @Test - public void testAddMaxHeapToCommandLine() { - final List<String> commandLine = new ArrayList<>(); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addMaxHeap(commandLine, null); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addMaxHeap(commandLine, StringUtils.EMPTY); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addMaxHeap(commandLine, " "); - - assertTrue(commandLine.isEmpty()); - - getLauncherLifecycleCommands().addMaxHeap(commandLine, "1024M"); - - assertFalse(commandLine.isEmpty()); - assertEquals(3, commandLine.size()); - assertEquals("-Xmx1024M", commandLine.get(0)); - assertEquals("-XX:+UseConcMarkSweepGC", commandLine.get(1)); - assertEquals("-XX:CMSInitiatingOccupancyFraction=" - + LauncherLifecycleCommands.CMS_INITIAL_OCCUPANCY_FRACTION, commandLine.get(2)); - } - - @Test(expected = AssertionError.class) - public void testReadPidWithNull() { - try { - getLauncherLifecycleCommands().readPid(null); - } catch (AssertionError expected) { - assertEquals("The file from which to read the process ID (pid) cannot be null!", - expected.getMessage()); - throw expected; - } - } - - @Test - @SuppressWarnings("deprecation") - public void testGetClasspath() { - assertEquals(System.getProperty("java.class.path"), - getLauncherLifecycleCommands().getClasspath(null)); - } - - @Test - @SuppressWarnings("deprecation") - public void testGetClasspathWithUserDefinedClasspath() { - assertEquals( - System.getProperty("java.class.path") + File.pathSeparator + "/path/to/user/classes", - getLauncherLifecycleCommands().getClasspath("/path/to/user/classes")); - } - - @Test - public void testGetSystemClasspath() { - assertEquals(System.getProperty("java.class.path"), - getLauncherLifecycleCommands().getSystemClasspath()); - } - - @Test - public void testLocatorClasspathOrder() { - String userClasspath = "/path/to/user/lib/app.jar:/path/to/user/classes"; - - String expectedClasspath = launcherCommands.getGemFireJarPath().concat(File.pathSeparator) - .concat(userClasspath).concat(File.pathSeparator) - .concat(System.getProperty("java.class.path")).concat(File.pathSeparator) - .concat(LauncherLifecycleCommands.CORE_DEPENDENCIES_JAR_PATHNAME); - - String actualClasspath = launcherCommands.getLocatorClasspath(true, userClasspath); - - assertEquals(expectedClasspath, actualClasspath); - } - - @Test - public void testServerClasspathOrder() { - String userClasspath = "/path/to/user/lib/app.jar:/path/to/user/classes"; - - String expectedClasspath = launcherCommands.getGemFireJarPath().concat(File.pathSeparator) - .concat(userClasspath).concat(File.pathSeparator) - .concat(LauncherLifecycleCommands.CORE_DEPENDENCIES_JAR_PATHNAME); - - String actualClasspath = launcherCommands.getServerClasspath(false, userClasspath); - - assertEquals(expectedClasspath, actualClasspath); - } - - @Test - public void testToClasspath() { - final boolean EXCLUDE_SYSTEM_CLASSPATH = false; - final boolean INCLUDE_SYSTEM_CLASSPATH = true; - - String[] jarFilePathnames = - {"/path/to/user/libs/A.jar", "/path/to/user/libs/B.jar", "/path/to/user/libs/C.jar"}; - - String[] userClasspaths = {"/path/to/classes:/path/to/libs/1.jar:/path/to/libs/2.jar", - "/path/to/ext/libs/1.jar:/path/to/ext/classes:/path/to/ext/lib/10.jar"}; - - String expectedClasspath = LauncherLifecycleCommands.GEODE_JAR_PATHNAME - .concat(File.pathSeparator).concat(toClasspath(userClasspaths)).concat(File.pathSeparator) - .concat(toClasspath(jarFilePathnames)); - - assertEquals(expectedClasspath, getLauncherLifecycleCommands() - .toClasspath(EXCLUDE_SYSTEM_CLASSPATH, jarFilePathnames, userClasspaths)); - - expectedClasspath = LauncherLifecycleCommands.GEODE_JAR_PATHNAME.concat(File.pathSeparator) - .concat(toClasspath(userClasspaths)).concat(File.pathSeparator) - .concat(System.getProperty("java.class.path")).concat(File.pathSeparator) - .concat(toClasspath(jarFilePathnames)); - - assertEquals(expectedClasspath, getLauncherLifecycleCommands() - .toClasspath(INCLUDE_SYSTEM_CLASSPATH, jarFilePathnames, userClasspaths)); - - expectedClasspath = LauncherLifecycleCommands.GEODE_JAR_PATHNAME.concat(File.pathSeparator) - .concat(System.getProperty("java.class.path")); - - assertEquals(expectedClasspath, getLauncherLifecycleCommands() - .toClasspath(INCLUDE_SYSTEM_CLASSPATH, null, (String[]) null)); - - assertEquals(LauncherLifecycleCommands.GEODE_JAR_PATHNAME, getLauncherLifecycleCommands() - .toClasspath(EXCLUDE_SYSTEM_CLASSPATH, null, (String[]) null)); - - assertEquals(LauncherLifecycleCommands.GEODE_JAR_PATHNAME, - getLauncherLifecycleCommands().toClasspath(EXCLUDE_SYSTEM_CLASSPATH, new String[0], "")); - } - - @Test - public void testToClassPathOrder() { - String userClasspathOne = "/path/to/user/lib/a.jar:/path/to/user/classes"; - String userClasspathTwo = - "/path/to/user/lib/x.jar:/path/to/user/lib/y.jar:/path/to/user/lib/z.jar"; - - String expectedClasspath = launcherCommands.getGemFireJarPath().concat(File.pathSeparator) - .concat(userClasspathOne).concat(File.pathSeparator).concat(userClasspathTwo) - .concat(File.pathSeparator).concat(System.getProperty("java.class.path")) - .concat(File.pathSeparator).concat(LauncherLifecycleCommands.CORE_DEPENDENCIES_JAR_PATHNAME) - .concat(File.pathSeparator) - .concat(LauncherLifecycleCommands.CORE_DEPENDENCIES_JAR_PATHNAME); - - String actualClasspath = launcherCommands.toClasspath(true, - new String[] {LauncherLifecycleCommands.CORE_DEPENDENCIES_JAR_PATHNAME, - LauncherLifecycleCommands.CORE_DEPENDENCIES_JAR_PATHNAME}, - userClasspathOne, userClasspathTwo); - - assertEquals(expectedClasspath, actualClasspath); - } - - @Test - public void testCreateServerCommandLine() throws Exception { - ServerLauncher serverLauncher = new ServerLauncher.Builder() - .setCommand(ServerLauncher.Command.START).setDisableDefaultServer(true) - .setMemberName("testCreateServerCommandLine").setRebalance(true) - // .setServerBindAddress("localhost") - .setServerPort(41214).setCriticalHeapPercentage(95.5f).setEvictionHeapPercentage(85.0f) - .setSocketBufferSize(1024 * 1024).setMessageTimeToLive(93).build(); - - String[] commandLineElements = launcherCommands.createStartServerCommandLine(serverLauncher, - null, null, new Properties(), null, false, new String[0], false, null, null); - - assertNotNull(commandLineElements); - assertTrue(commandLineElements.length > 0); - - Set<String> expectedCommandLineElements = new HashSet<>(6); - - expectedCommandLineElements.add(serverLauncher.getCommand().getName()); - expectedCommandLineElements.add("--disable-default-server"); - expectedCommandLineElements.add(serverLauncher.getMemberName().toLowerCase()); - expectedCommandLineElements.add("--rebalance"); - // expectedCommandLineElements.add(String.format("--server-bind-address=%1$s", - // serverLauncher.getServerBindAddress().getHostName())); - expectedCommandLineElements - .add(String.format("--server-port=%1$d", serverLauncher.getServerPort())); - expectedCommandLineElements.add(String.format("--critical-heap-percentage=%1$s", - serverLauncher.getCriticalHeapPercentage())); - expectedCommandLineElements.add(String.format("--eviction-heap-percentage=%1$s", - serverLauncher.getEvictionHeapPercentage())); - expectedCommandLineElements - .add(String.format("--socket-buffer-size=%1$d", serverLauncher.getSocketBufferSize())); - expectedCommandLineElements - .add(String.format("--message-time-to-live=%1$d", serverLauncher.getMessageTimeToLive())); - - for (String commandLineElement : commandLineElements) { - expectedCommandLineElements.remove(commandLineElement.toLowerCase()); - } - - assertTrue(String.format("Expected ([]); but was (%1$s)", expectedCommandLineElements), - expectedCommandLineElements.isEmpty()); - } - - @Test - public void testCreateServerCommandLineWithRestAPI() throws Exception { - ServerLauncher serverLauncher = new ServerLauncher.Builder() - .setCommand(ServerLauncher.Command.START).setDisableDefaultServer(true) - .setMemberName("testCreateServerCommandLine").setRebalance(true) - // .setServerBindAddress("localhost") - .setServerPort(41214).setCriticalHeapPercentage(95.5f).setEvictionHeapPercentage(85.0f) - .build(); - - Properties gemfireProperties = new Properties(); - gemfireProperties.setProperty(START_DEV_REST_API, "true"); - gemfireProperties.setProperty(HTTP_SERVICE_PORT, "8080"); - gemfireProperties.setProperty(HTTP_SERVICE_BIND_ADDRESS, "localhost"); - - - String[] commandLineElements = launcherCommands.createStartServerCommandLine(serverLauncher, - null, null, gemfireProperties, null, false, new String[0], false, null, null); - - assertNotNull(commandLineElements); - assertTrue(commandLineElements.length > 0); - - Set<String> expectedCommandLineElements = new HashSet<>(6); - - expectedCommandLineElements.add(serverLauncher.getCommand().getName()); - expectedCommandLineElements.add("--disable-default-server"); - expectedCommandLineElements.add(serverLauncher.getMemberName().toLowerCase()); - expectedCommandLineElements.add("--rebalance"); - // expectedCommandLineElements.add(String.format("--server-bind-address=%1$s", - // serverLauncher.getServerBindAddress().getHostName())); - expectedCommandLineElements - .add(String.format("--server-port=%1$d", serverLauncher.getServerPort())); - expectedCommandLineElements.add(String.format("--critical-heap-percentage=%1$s", - serverLauncher.getCriticalHeapPercentage())); - expectedCommandLineElements.add(String.format("--eviction-heap-percentage=%1$s", - serverLauncher.getEvictionHeapPercentage())); - - expectedCommandLineElements - .add("-d" + DistributionConfig.GEMFIRE_PREFIX + "" + START_DEV_REST_API + "=" + "true"); - expectedCommandLineElements - .add("-d" + DistributionConfig.GEMFIRE_PREFIX + "" + HTTP_SERVICE_PORT + "=" + "8080"); - expectedCommandLineElements.add("-d" + DistributionConfig.GEMFIRE_PREFIX + "" - + HTTP_SERVICE_BIND_ADDRESS + "=" + "localhost"); - - - for (String commandLineElement : commandLineElements) { - expectedCommandLineElements.remove(commandLineElement.toLowerCase()); - } - - assertTrue(String.format("Expected ([]); but was (%1$s)", expectedCommandLineElements), - expectedCommandLineElements.isEmpty()); - } - - /** - * Verify commandline parameters passed for starting locator - * - * @throws Exception - */ - @Test - public void testLocatorCommandLineWithRestAPI() throws Exception { - LocatorLauncher locatorLauncher = - new LocatorLauncher.Builder().setCommand(LocatorLauncher.Command.START) - .setMemberName("testLocatorCommandLineWithRestAPI").setBindAddress("localhost") - .setPort(11111).build(); - - Properties gemfireProperties = new Properties(); - gemfireProperties.setProperty(HTTP_SERVICE_PORT, "8089"); - gemfireProperties.setProperty(HTTP_SERVICE_BIND_ADDRESS, "localhost"); - - - String[] commandLineElements = launcherCommands.createStartLocatorCommandLine(locatorLauncher, - null, null, gemfireProperties, null, false, new String[0], null, null); - - assertNotNull(commandLineElements); - assertTrue(commandLineElements.length > 0); - - Set<String> expectedCommandLineElements = new HashSet<>(6); - - expectedCommandLineElements.add(locatorLauncher.getCommand().getName()); - expectedCommandLineElements.add(locatorLauncher.getMemberName().toLowerCase()); - expectedCommandLineElements.add(String.format("--port=%1$d", locatorLauncher.getPort())); - expectedCommandLineElements - .add("-d" + DistributionConfig.GEMFIRE_PREFIX + "" + HTTP_SERVICE_PORT + "=" + "8089"); - expectedCommandLineElements.add("-d" + DistributionConfig.GEMFIRE_PREFIX + "" - + HTTP_SERVICE_BIND_ADDRESS + "=" + "localhost"); - - - for (String commandLineElement : commandLineElements) { - expectedCommandLineElements.remove(commandLineElement.toLowerCase()); - } - - assertTrue(String.format("Expected ([]); but was (%1$s)", expectedCommandLineElements), - expectedCommandLineElements.isEmpty()); - } - - - @Test - public void testReadPidWithNonExistingFile() { - assertEquals(LauncherLifecycleCommands.INVALID_PID, - getLauncherLifecycleCommands().readPid(new File("/path/to/non_existing/pid.file"))); - } - - private LauncherLifecycleCommands getLauncherLifecycleCommands() { - return launcherCommands; - } - - private String toClasspath(final String... jarFilePathnames) { - String classpath = StringUtils.EMPTY; - - if (jarFilePathnames != null) { - for (final String jarFilePathname : jarFilePathnames) { - classpath += (classpath.isEmpty() ? StringUtils.EMPTY : File.pathSeparator); - classpath += jarFilePathname; - } - } - - return classpath; - } - - private String toPath(Object... pathElements) { - String path = ""; - - for (Object pathElement : pathElements) { - path += (path.isEmpty() ? StringUtils.EMPTY : File.pathSeparator); - path += pathElement; - } - - return path; - } - -} http://git-wip-us.apache.org/repos/asf/geode/blob/fcce2b0b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandIntegrationTest.java b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandIntegrationTest.java new file mode 100644 index 0000000..0700742 --- /dev/null +++ b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandIntegrationTest.java @@ -0,0 +1,85 @@ +/* + * 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.cli.commands; + +import static org.apache.geode.distributed.ConfigurationProperties.ENABLE_CLUSTER_CONFIGURATION; +import static org.apache.geode.distributed.ConfigurationProperties.JMX_MANAGER_HOSTNAME_FOR_CLIENTS; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.util.Properties; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.TemporaryFolder; +import org.mockito.ArgumentCaptor; + +import org.apache.geode.management.internal.cli.shell.Gfsh; +import org.apache.geode.management.internal.cli.util.CommandStringBuilder; +import org.apache.geode.test.dunit.rules.GfshParserRule; +import org.apache.geode.test.junit.categories.IntegrationTest; + +@Category(IntegrationTest.class) +public class StartLocatorCommandIntegrationTest { + private static final String FAKE_HOSTNAME = "someFakeHostname"; + + @Rule + public GfshParserRule commandRule = new GfshParserRule(); + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void startLocatorWorksWithNoOptions() throws Exception { + StartLocatorCommand spy = spyOnStartLocatorCommand("start locator"); + commandRule.executeLastCommandWithInstance(spy); + + ArgumentCaptor<Properties> gemfirePropertiesCaptor = ArgumentCaptor.forClass(Properties.class); + verify(spy).createStartLocatorCommandLine(any(), any(), any(), + gemfirePropertiesCaptor.capture(), any(), any(), any(), any(), any()); + + Properties gemfireProperties = gemfirePropertiesCaptor.getValue(); + assertThat(gemfireProperties).containsKey(ENABLE_CLUSTER_CONFIGURATION); + assertThat(gemfireProperties.get(ENABLE_CLUSTER_CONFIGURATION)).isEqualTo("true"); + } + + @Test + public void startLocatorRespectsJmxManagerHostnameForClients() throws Exception { + String startLocatorCommand = new CommandStringBuilder("start locator") + .addOption(JMX_MANAGER_HOSTNAME_FOR_CLIENTS, FAKE_HOSTNAME).toString(); + + StartLocatorCommand spyCommand = spyOnStartLocatorCommand(startLocatorCommand); + commandRule.executeLastCommandWithInstance(spyCommand); + + ArgumentCaptor<Properties> gemfirePropertiesCaptor = ArgumentCaptor.forClass(Properties.class); + verify(spyCommand).createStartLocatorCommandLine(any(), any(), any(), + gemfirePropertiesCaptor.capture(), any(), any(), any(), any(), any()); + + Properties gemfireProperties = gemfirePropertiesCaptor.getValue(); + assertThat(gemfireProperties).containsKey(JMX_MANAGER_HOSTNAME_FOR_CLIENTS); + assertThat(gemfireProperties.get(JMX_MANAGER_HOSTNAME_FOR_CLIENTS)).isEqualTo(FAKE_HOSTNAME); + } + + private StartLocatorCommand spyOnStartLocatorCommand(String command) { + StartLocatorCommand spy = commandRule.spyCommand(command); + doReturn(mock(Gfsh.class)).when(spy).getGfsh(); + return spy; + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fcce2b0b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandTest.java ---------------------------------------------------------------------- diff --git a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandTest.java b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandTest.java new file mode 100644 index 0000000..5a7c11c --- /dev/null +++ b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandTest.java @@ -0,0 +1,147 @@ +/* + * 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.cli.commands; + +import static org.apache.geode.distributed.ConfigurationProperties.HTTP_SERVICE_BIND_ADDRESS; +import static org.apache.geode.distributed.ConfigurationProperties.HTTP_SERVICE_PORT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import org.apache.geode.distributed.LocatorLauncher; +import org.apache.geode.distributed.internal.DistributionConfig; +import org.apache.geode.internal.lang.SystemUtils; +import org.apache.geode.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class StartLocatorCommandTest { + private StartLocatorCommand locatorCommands; + + @Before + public void setup() { + locatorCommands = new StartLocatorCommand(); + } + + @After + public void tearDown() { + locatorCommands = null; + } + + @Test + public void testLocatorClasspathOrder() { + String userClasspath = "/path/to/user/lib/app.jar:/path/to/user/classes"; + String expectedClasspath = + StartMemberUtils.getGemFireJarPath().concat(File.pathSeparator).concat(userClasspath) + .concat(File.pathSeparator).concat(System.getProperty("java.class.path")) + .concat(File.pathSeparator).concat(StartMemberUtils.CORE_DEPENDENCIES_JAR_PATHNAME); + String actualClasspath = locatorCommands.getLocatorClasspath(true, userClasspath); + assertEquals(expectedClasspath, actualClasspath); + } + + @Test + public void testLocatorCommandLineWithRestAPI() throws Exception { + LocatorLauncher locatorLauncher = + new LocatorLauncher.Builder().setCommand(LocatorLauncher.Command.START) + .setMemberName("testLocatorCommandLineWithRestAPI").setBindAddress("localhost") + .setPort(11111).build(); + + Properties gemfireProperties = new Properties(); + gemfireProperties.setProperty(HTTP_SERVICE_PORT, "8089"); + gemfireProperties.setProperty(HTTP_SERVICE_BIND_ADDRESS, "localhost"); + + String[] commandLineElements = locatorCommands.createStartLocatorCommandLine(locatorLauncher, + null, null, gemfireProperties, null, false, new String[0], null, null); + + assertNotNull(commandLineElements); + assertTrue(commandLineElements.length > 0); + + Set<String> expectedCommandLineElements = new HashSet<>(6); + + expectedCommandLineElements.add(locatorLauncher.getCommand().getName()); + expectedCommandLineElements.add(locatorLauncher.getMemberName().toLowerCase()); + expectedCommandLineElements.add(String.format("--port=%1$d", locatorLauncher.getPort())); + expectedCommandLineElements + .add("-d" + DistributionConfig.GEMFIRE_PREFIX + "" + HTTP_SERVICE_PORT + "=" + "8089"); + expectedCommandLineElements.add("-d" + DistributionConfig.GEMFIRE_PREFIX + "" + + HTTP_SERVICE_BIND_ADDRESS + "=" + "localhost"); + + for (String commandLineElement : commandLineElements) { + expectedCommandLineElements.remove(commandLineElement.toLowerCase()); + } + + assertTrue(String.format("Expected ([]); but was (%1$s)", expectedCommandLineElements), + expectedCommandLineElements.isEmpty()); + } + + @Test + public void testAddJvmOptionsForOutOfMemoryErrors() { + final List<String> jvmOptions = new ArrayList<>(1); + + addJvmOptionsForOutOfMemoryErrors(jvmOptions); + + if (SystemUtils.isHotSpotVM()) { + if (SystemUtils.isWindows()) { + assertTrue(jvmOptions.contains("-XX:OnOutOfMemoryError=taskkill /F /PID %p")); + } else { + assertTrue(jvmOptions.contains("-XX:OnOutOfMemoryError=kill -KILL %p")); + } + } else if (SystemUtils.isJ9VM()) { + assertEquals(1, jvmOptions.size()); + assertTrue(jvmOptions.contains("-Xcheck:memory")); + } else if (SystemUtils.isJRockitVM()) { + assertEquals(1, jvmOptions.size()); + assertTrue(jvmOptions.contains("-XXexitOnOutOfMemory")); + } else { + assertTrue(jvmOptions.isEmpty()); + } + } + + private void addJvmOptionsForOutOfMemoryErrors(final List<String> commandLine) { + if (SystemUtils.isHotSpotVM()) { + if (SystemUtils.isWindows()) { + // ProcessBuilder "on Windows" needs every word (space separated) to be + // a different element in the array/list. See #47312. Need to study why! + commandLine.add("-XX:OnOutOfMemoryError=taskkill /F /PID %p"); + } else { // All other platforms (Linux, Mac OS X, UNIX, etc) + commandLine.add("-XX:OnOutOfMemoryError=kill -KILL %p"); + } + } else if (SystemUtils.isJ9VM()) { + // NOTE IBM states the following IBM J9 JVM command-line option/switch has side-effects on + // "performance", + // as noted in the reference documentation... + // http://publib.boulder.ibm.com/infocenter/javasdk/v6r0/index.jsp?topic=/com.ibm.java.doc.diagnostics.60/diag/appendixes/cmdline/commands_jvm.html + commandLine.add("-Xcheck:memory"); + } else if (SystemUtils.isJRockitVM()) { + // NOTE the following Oracle JRockit JVM documentation was referenced to identify the + // appropriate JVM option to + // set when handling OutOfMemoryErrors. + // http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionXX.html + commandLine.add("-XXexitOnOutOfMemory"); + } + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fcce2b0b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandIntegrationTest.java b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandIntegrationTest.java new file mode 100644 index 0000000..059611d --- /dev/null +++ b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandIntegrationTest.java @@ -0,0 +1,85 @@ +/* + * 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.cli.commands; + +import static org.apache.geode.distributed.ConfigurationProperties.JMX_MANAGER_HOSTNAME_FOR_CLIENTS; +import static org.apache.geode.distributed.ConfigurationProperties.USE_CLUSTER_CONFIGURATION; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.util.Properties; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.TemporaryFolder; +import org.mockito.ArgumentCaptor; + +import org.apache.geode.management.internal.cli.shell.Gfsh; +import org.apache.geode.management.internal.cli.util.CommandStringBuilder; +import org.apache.geode.test.dunit.rules.GfshParserRule; +import org.apache.geode.test.junit.categories.IntegrationTest; + +@Category(IntegrationTest.class) +public class StartServerCommandIntegrationTest { + private static final String FAKE_HOSTNAME = "someFakeHostname"; + + @Rule + public GfshParserRule commandRule = new GfshParserRule(); + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void startServerWorksWithNoOptions() throws Exception { + StartServerCommand spy = spyOnStartServerCommand("start server"); + commandRule.executeLastCommandWithInstance(spy); + + ArgumentCaptor<Properties> gemfirePropertiesCaptor = ArgumentCaptor.forClass(Properties.class); + verify(spy).createStartServerCommandLine(any(), any(), any(), gemfirePropertiesCaptor.capture(), + any(), any(), any(), any(), any(), any()); + + Properties gemfireProperties = gemfirePropertiesCaptor.getValue(); + assertThat(gemfireProperties).containsKey(USE_CLUSTER_CONFIGURATION); + assertThat(gemfireProperties.get(USE_CLUSTER_CONFIGURATION)).isEqualTo("true"); + } + + @Test + public void startServerRespectsJmxManagerHostnameForClients() throws Exception { + String startServerCommand = new CommandStringBuilder("start server") + .addOption(JMX_MANAGER_HOSTNAME_FOR_CLIENTS, FAKE_HOSTNAME).toString(); + + StartServerCommand commandSpy = spyOnStartServerCommand(startServerCommand); + commandRule.executeLastCommandWithInstance(commandSpy); + + ArgumentCaptor<Properties> gemfirePropertiesCaptor = ArgumentCaptor.forClass(Properties.class); + verify(commandSpy).createStartServerCommandLine(any(), any(), any(), + gemfirePropertiesCaptor.capture(), any(), any(), any(), any(), any(), any()); + + Properties gemfireProperties = gemfirePropertiesCaptor.getValue(); + assertThat(gemfireProperties).containsKey(JMX_MANAGER_HOSTNAME_FOR_CLIENTS); + assertThat(gemfireProperties.get(JMX_MANAGER_HOSTNAME_FOR_CLIENTS)).isEqualTo(FAKE_HOSTNAME); + } + + private StartServerCommand spyOnStartServerCommand(String command) { + StartServerCommand spy = commandRule.spyCommand(command); + doReturn(mock(Gfsh.class)).when(spy).getGfsh(); + return spy; + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fcce2b0b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandTest.java ---------------------------------------------------------------------- diff --git a/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandTest.java b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandTest.java new file mode 100644 index 0000000..e704248 --- /dev/null +++ b/geode-assembly/src/test/java/org/apache/geode/management/internal/cli/commands/StartServerCommandTest.java @@ -0,0 +1,144 @@ +/* + * 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.cli.commands; + +import static org.apache.geode.distributed.ConfigurationProperties.HTTP_SERVICE_BIND_ADDRESS; +import static org.apache.geode.distributed.ConfigurationProperties.HTTP_SERVICE_PORT; +import static org.apache.geode.distributed.ConfigurationProperties.START_DEV_REST_API; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.HashSet; +import java.util.Properties; +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import org.apache.geode.distributed.ServerLauncher; +import org.apache.geode.distributed.internal.DistributionConfig; +import org.apache.geode.test.junit.categories.UnitTest; + +@Category(UnitTest.class) +public class StartServerCommandTest { + private StartServerCommand serverCommands; + + @Before + public void setup() { + serverCommands = new StartServerCommand(); + } + + @After + public void tearDown() { + serverCommands = null; + } + + @Test + public void testServerClasspathOrder() { + String userClasspath = "/path/to/user/lib/app.jar:/path/to/user/classes"; + String expectedClasspath = + StartMemberUtils.getGemFireJarPath().concat(File.pathSeparator).concat(userClasspath) + .concat(File.pathSeparator).concat(StartMemberUtils.CORE_DEPENDENCIES_JAR_PATHNAME); + String actualClasspath = serverCommands.getServerClasspath(false, userClasspath); + assertEquals(expectedClasspath, actualClasspath); + } + + @Test + public void testCreateServerCommandLine() throws Exception { + ServerLauncher serverLauncher = new ServerLauncher.Builder() + .setCommand(ServerLauncher.Command.START).setDisableDefaultServer(true) + .setMemberName("testCreateServerCommandLine").setRebalance(true).setServerPort(41214) + .setCriticalHeapPercentage(95.5f).setEvictionHeapPercentage(85.0f) + .setSocketBufferSize(1024 * 1024).setMessageTimeToLive(93).build(); + + String[] commandLineElements = serverCommands.createStartServerCommandLine(serverLauncher, null, + null, new Properties(), null, false, new String[0], false, null, null); + + assertNotNull(commandLineElements); + assertTrue(commandLineElements.length > 0); + + Set<String> expectedCommandLineElements = new HashSet<>(6); + expectedCommandLineElements.add(serverLauncher.getCommand().getName()); + expectedCommandLineElements.add("--disable-default-server"); + expectedCommandLineElements.add(serverLauncher.getMemberName().toLowerCase()); + expectedCommandLineElements.add("--rebalance"); + expectedCommandLineElements + .add(String.format("--server-port=%1$d", serverLauncher.getServerPort())); + expectedCommandLineElements.add(String.format("--critical-heap-percentage=%1$s", + serverLauncher.getCriticalHeapPercentage())); + expectedCommandLineElements.add(String.format("--eviction-heap-percentage=%1$s", + serverLauncher.getEvictionHeapPercentage())); + expectedCommandLineElements + .add(String.format("--socket-buffer-size=%1$d", serverLauncher.getSocketBufferSize())); + expectedCommandLineElements + .add(String.format("--message-time-to-live=%1$d", serverLauncher.getMessageTimeToLive())); + + for (String commandLineElement : commandLineElements) { + expectedCommandLineElements.remove(commandLineElement.toLowerCase()); + } + assertTrue(String.format("Expected ([]); but was (%1$s)", expectedCommandLineElements), + expectedCommandLineElements.isEmpty()); + } + + @Test + public void testCreateServerCommandLineWithRestAPI() throws Exception { + ServerLauncher serverLauncher = new ServerLauncher.Builder() + .setCommand(ServerLauncher.Command.START).setDisableDefaultServer(true) + .setMemberName("testCreateServerCommandLine").setRebalance(true).setServerPort(41214) + .setCriticalHeapPercentage(95.5f).setEvictionHeapPercentage(85.0f).build(); + + Properties gemfireProperties = new Properties(); + gemfireProperties.setProperty(START_DEV_REST_API, "true"); + gemfireProperties.setProperty(HTTP_SERVICE_PORT, "8080"); + gemfireProperties.setProperty(HTTP_SERVICE_BIND_ADDRESS, "localhost"); + + String[] commandLineElements = serverCommands.createStartServerCommandLine(serverLauncher, null, + null, gemfireProperties, null, false, new String[0], false, null, null); + + assertNotNull(commandLineElements); + assertTrue(commandLineElements.length > 0); + + Set<String> expectedCommandLineElements = new HashSet<>(6); + + expectedCommandLineElements.add(serverLauncher.getCommand().getName()); + expectedCommandLineElements.add("--disable-default-server"); + expectedCommandLineElements.add(serverLauncher.getMemberName().toLowerCase()); + expectedCommandLineElements.add("--rebalance"); + expectedCommandLineElements + .add(String.format("--server-port=%1$d", serverLauncher.getServerPort())); + expectedCommandLineElements.add(String.format("--critical-heap-percentage=%1$s", + serverLauncher.getCriticalHeapPercentage())); + expectedCommandLineElements.add(String.format("--eviction-heap-percentage=%1$s", + serverLauncher.getEvictionHeapPercentage())); + + expectedCommandLineElements + .add("-d" + DistributionConfig.GEMFIRE_PREFIX + "" + START_DEV_REST_API + "=" + "true"); + expectedCommandLineElements + .add("-d" + DistributionConfig.GEMFIRE_PREFIX + "" + HTTP_SERVICE_PORT + "=" + "8080"); + expectedCommandLineElements.add("-d" + DistributionConfig.GEMFIRE_PREFIX + "" + + HTTP_SERVICE_BIND_ADDRESS + "=" + "localhost"); + + for (String commandLineElement : commandLineElements) { + expectedCommandLineElements.remove(commandLineElement.toLowerCase()); + } + assertTrue(String.format("Expected ([]); but was (%1$s)", expectedCommandLineElements), + expectedCommandLineElements.isEmpty()); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/fcce2b0b/geode-core/src/main/java/org/apache/geode/internal/process/signal/SignalListener.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/internal/process/signal/SignalListener.java b/geode-core/src/main/java/org/apache/geode/internal/process/signal/SignalListener.java index 4570eb8..501b9db 100644 --- a/geode-core/src/main/java/org/apache/geode/internal/process/signal/SignalListener.java +++ b/geode-core/src/main/java/org/apache/geode/internal/process/signal/SignalListener.java @@ -18,6 +18,7 @@ package org.apache.geode.internal.process.signal; import java.util.EventListener; /** + * <p> * The SignalListener class... * </p> * @@ -26,7 +27,5 @@ import java.util.EventListener; */ @SuppressWarnings("unused") public interface SignalListener extends EventListener { - - public void handle(SignalEvent event); - + void handle(SignalEvent event); } http://git-wip-us.apache.org/repos/asf/geode/blob/fcce2b0b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/GfshCommand.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/GfshCommand.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/GfshCommand.java index 88f730e..c7f53b1 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/GfshCommand.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/GfshCommand.java @@ -48,6 +48,7 @@ import org.apache.geode.management.internal.cli.util.MemberNotFoundException; */ @SuppressWarnings("unused") public interface GfshCommand extends CommandMarker { + default String convertDefaultValue(final String from, final String to) { return CliMetaData.ANNOTATION_DEFAULT_VALUE.equals(from) ? to : from; } @@ -193,8 +194,6 @@ public interface GfshCommand extends CommandMarker { } else { FunctionService.registerFunction(function); } - return function; } - }
