You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2016/06/16 22:52:47 UTC
incubator-geode git commit: GEODE-1556: refactor and create unit
tests for GFSH hostname
Repository: incubator-geode
Updated Branches:
refs/heads/develop 8a132221f -> 28d2ce069
GEODE-1556: refactor and create unit tests for GFSH hostname
* extract hostname methods to HostName class
* create HostNameTest
* This closes #165 [klund@apache.org]
Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/28d2ce06
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/28d2ce06
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/28d2ce06
Branch: refs/heads/develop
Commit: 28d2ce069681f3fc246b4f9405869ed51d1007ff
Parents: 8a13222
Author: gmeilen <gr...@gmail.com>
Authored: Thu Jun 16 15:33:53 2016 -0700
Committer: Kirk Lund <kl...@apache.org>
Committed: Thu Jun 16 15:37:42 2016 -0700
----------------------------------------------------------------------
.../gemfire/internal/lang/SystemUtils.java | 11 ++
.../gemfire/internal/util/HostName.java | 71 +++++++++++
.../management/internal/cli/shell/Gfsh.java | 40 +-----
.../gemfire/internal/util/HostNameTest.java | 121 +++++++++++++++++++
4 files changed, 205 insertions(+), 38 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/28d2ce06/geode-core/src/main/java/com/gemstone/gemfire/internal/lang/SystemUtils.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/lang/SystemUtils.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/lang/SystemUtils.java
index 1c52a10..07ef612 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/lang/SystemUtils.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/lang/SystemUtils.java
@@ -49,6 +49,7 @@ public class SystemUtils {
public static final String LINUX_OS_NAME = "Linux";
public static final String MAC_OSX_NAME = "Mac";
public static final String WINDOWS_OS_NAME = "Windows";
+ public static final String SOLARIS_OS_NAME = "SunOS";
/**
* Utility method to determine whether the installed Java Runtime Environment (JRE) is minimally at the specified,
@@ -199,6 +200,16 @@ public class SystemUtils {
return isOS(WINDOWS_OS_NAME);
}
+ /**
+ * Utility method that determines whether the Java application process is executing in a Sun Solaris
+ * operating system environment.
+ *
+ * @return a boolean value indicating whether the Java application process is executing in Solaris.
+ * @see #isOS(String)
+ */
+ public static boolean isSolaris() {
+ return isOS(SOLARIS_OS_NAME);
+ }
/**
* Returns true if the specified location is in the JVM classpath. This may
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/28d2ce06/geode-core/src/main/java/com/gemstone/gemfire/internal/util/HostName.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/util/HostName.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/util/HostName.java
new file mode 100644
index 0000000..582240f
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/util/HostName.java
@@ -0,0 +1,71 @@
+/*
+ * 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 com.gemstone.gemfire.internal.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Scanner;
+
+import com.gemstone.gemfire.internal.lang.SystemUtils;
+
+public class HostName {
+
+ static final String COMPUTER_NAME_PROPERTY = "COMPUTERNAME";
+ static final String HOSTNAME_PROPERTY = "HOSTNAME";
+
+ private static final String HOSTNAME = "hostname";
+ private static final String START_OF_STRING = "\\A";
+ private static final String UNKNOWN = "unknown";
+
+ public String determineHostName() {
+ String hostname = getHostNameFromEnv();
+ if (isEmpty(hostname)) {
+ hostname = execHostName();
+ }
+ assert !isEmpty(hostname);
+ return hostname;
+ }
+
+ String execHostName() {
+ String hostname;
+ try {
+ Process process = new ProcessBuilder(HOSTNAME).start();
+ try (InputStream stream = process.getInputStream();
+ Scanner s = new Scanner(stream).useDelimiter(START_OF_STRING);) {
+ hostname = s.hasNext() ? s.next().trim() : UNKNOWN;
+ }
+ } catch (IOException hostnameBinaryNotFound) {
+ hostname = UNKNOWN;
+ }
+ return hostname;
+ }
+
+ String getHostNameFromEnv() {
+ final String hostname;
+ if (SystemUtils.isWindows()) {
+ hostname = System.getenv(COMPUTER_NAME_PROPERTY);
+ } else {
+ hostname = System.getenv(HOSTNAME_PROPERTY);
+ }
+ return hostname;
+ }
+
+ private boolean isEmpty(String value) {
+ return value == null || value.isEmpty();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/28d2ce06/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
index da13bd9..a6f11bf 100755
--- a/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
@@ -41,6 +41,7 @@ import com.gemstone.gemfire.internal.Banner;
import com.gemstone.gemfire.internal.GemFireVersion;
import com.gemstone.gemfire.internal.lang.ClassUtils;
import com.gemstone.gemfire.internal.process.signal.AbstractSignalNotificationHandler;
+import com.gemstone.gemfire.internal.util.HostName;
import com.gemstone.gemfire.internal.util.SunAPINotFoundException;
import com.gemstone.gemfire.management.cli.CommandProcessingException;
import com.gemstone.gemfire.management.cli.Result;
@@ -280,7 +281,7 @@ public class Gfsh extends JLineShell {
private void initializeEnvironment() {
env.put(ENV_SYS_USER, System.getProperty("user.name"));
env.put(ENV_SYS_USER_HOME, System.getProperty("user.home"));
- env.put(ENV_SYS_HOST_NAME, determineHostName());
+ env.put(ENV_SYS_HOST_NAME, new HostName().determineHostName());
env.put(ENV_SYS_CLASSPATH, System.getProperty("java.class.path"));
env.put(ENV_SYS_JAVA_VERSION, System.getProperty("java.version"));
env.put(ENV_SYS_OS, System.getProperty("os.name"));
@@ -295,9 +296,6 @@ public class Gfsh extends JLineShell {
readonlyAppEnv.add(ENV_APP_LOG_FILE);
env.put(ENV_APP_PWD, System.getProperty("user.dir"));
readonlyAppEnv.add(ENV_APP_PWD);
-// Enable when "use region" command is required. See #46110
-// env.put(CliConstants.ENV_APP_CONTEXT_PATH, CliConstants.DEFAULT_APP_CONTEXT_PATH);
-// readonlyAppEnv.add(CliConstants.ENV_APP_CONTEXT_PATH);
env.put(ENV_APP_FETCH_SIZE, String.valueOf(DEFAULT_APP_FETCH_SIZE));
env.put(ENV_APP_LAST_EXIT_STATUS, String.valueOf(DEFAULT_APP_LAST_EXIT_STATUS));
readonlyAppEnv.add(ENV_APP_LAST_EXIT_STATUS);
@@ -307,40 +305,6 @@ public class Gfsh extends JLineShell {
env.put(ENV_APP_RESULT_VIEWER, String.valueOf(DEFAULT_APP_RESULT_VIEWER));
}
- private static String execReadToString(String execCommand) throws IOException {
- Process proc = Runtime.getRuntime().exec(execCommand);
- try (InputStream stream = proc.getInputStream()) {
- try (Scanner s = new Scanner(stream).useDelimiter("\\A")) {
- return s.hasNext() ? s.next().trim() : "";
- }
- }
- }
-
- private String determineHostName()
- {
- // Windows
- String hostname = System.getenv("COMPUTERNAME");
- if (hostname == null) {
- // Unix / Mac / Cygwin sometimes has this set
- hostname = System.getenv("HOSTNAME");
- }
- if (hostname == null) {
- try {
- // Unix / Mac / Windows / Cygwin
- hostname = execReadToString("hostname");
- }
- catch (IOException io) {
- // happens if hostname binary is not found.
- hostname = "unknown";
- }
- }
- if (hostname == null || hostname.isEmpty()) {
- // if it still isn't set, default it.
- hostname = "unknown";
- }
- return hostname;
- }
-
public static Gfsh getInstance(boolean launchShell, String[] args, GfshConfig gfshConfig)
throws ClassNotFoundException, IOException
{
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/28d2ce06/geode-core/src/test/java/com/gemstone/gemfire/internal/util/HostNameTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/util/HostNameTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/util/HostNameTest.java
new file mode 100644
index 0000000..98a87fc
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/util/HostNameTest.java
@@ -0,0 +1,121 @@
+/*
+ * 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 com.gemstone.gemfire.internal.util;
+
+import static com.gemstone.gemfire.internal.lang.SystemUtils.*;
+import static com.gemstone.gemfire.internal.util.HostName.*;
+import static org.assertj.core.api.Assertions.*;
+
+import java.io.IOException;
+
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.contrib.java.lang.system.EnvironmentVariables;
+import org.junit.contrib.java.lang.system.RestoreSystemProperties;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import junitparams.JUnitParamsRunner;
+import junitparams.Parameters;
+
+@Category(UnitTest.class)
+@RunWith(JUnitParamsRunner.class)
+public class HostNameTest {
+
+ private static final String EXPECTED_HOSTNAME = "expected-hostname";
+ private static final String UNKNOWN = "unknown";
+
+ @Rule
+ public final EnvironmentVariables env = new EnvironmentVariables();
+
+ @Rule
+ public final RestoreSystemProperties sysProps = new RestoreSystemProperties();
+
+ @Test
+ public void execHostNameShouldNeverReturnNull() throws IOException {
+ String result = new HostName().execHostName();
+ assertThat(result).isNotNull();
+ }
+
+ @Test
+ @Parameters({ MAC_OSX_NAME, LINUX_OS_NAME, SOLARIS_OS_NAME, WINDOWS_OS_NAME })
+ public void shouldExecHostNameIfEnvValueNotAvailableOnOS(String osName) throws IOException {
+ setHostNamePropertiesNull(osName);
+ String result = new HostName().determineHostName();
+ assertThat(result).isNotNull();
+ }
+
+ @Test
+ @Parameters({ MAC_OSX_NAME, LINUX_OS_NAME, SOLARIS_OS_NAME, WINDOWS_OS_NAME })
+ public void shouldUseComputerNameIfAvailableOnOS(String osName) throws IOException {
+ setHostNameProperties(osName);
+ String result = new HostName().determineHostName();
+ assertThat(result).isEqualTo(EXPECTED_HOSTNAME);
+ }
+
+ @Test
+ @Parameters({ MAC_OSX_NAME, LINUX_OS_NAME, SOLARIS_OS_NAME, WINDOWS_OS_NAME })
+ public void shouldBeNullIfEnvValueNotAvailableOnOS(String osName) throws IOException {
+ setHostNamePropertiesNull(osName);
+ String result = new HostName().getHostNameFromEnv();
+ assertThat(result).isEqualTo(null);
+ }
+
+ private void setHostNameProperties(String osName) {
+ System.setProperty("os.name", osName);
+ if (isWindows()) {
+ this.env.set(COMPUTER_NAME_PROPERTY, EXPECTED_HOSTNAME);
+ this.env.set(HOSTNAME_PROPERTY, null);
+ } else {
+ this.env.set(COMPUTER_NAME_PROPERTY, null);
+ this.env.set(HOSTNAME_PROPERTY, EXPECTED_HOSTNAME);
+ }
+
+ assertThat(System.getProperty("os.name")).isEqualTo(osName);
+ if (isWindows()) {
+ assertThat(System.getenv(COMPUTER_NAME_PROPERTY)).isEqualTo(EXPECTED_HOSTNAME);
+ assertThat(System.getenv(HOSTNAME_PROPERTY)).isNull();
+ } else {
+ assertThat(System.getenv(COMPUTER_NAME_PROPERTY)).isNull();
+ assertThat(System.getenv(HOSTNAME_PROPERTY)).isEqualTo(EXPECTED_HOSTNAME);
+ }
+ }
+
+ private void setHostNamePropertiesNull(String osName) {
+ System.setProperty("os.name", osName);
+ if (isWindows()) {
+ this.env.set(COMPUTER_NAME_PROPERTY, null);
+ this.env.set(HOSTNAME_PROPERTY, null);
+ } else {
+ this.env.set(COMPUTER_NAME_PROPERTY, null);
+ this.env.set(HOSTNAME_PROPERTY, null);
+ }
+
+ assertThat(System.getProperty("os.name")).isEqualTo(osName);
+ if (isWindows()) {
+ assertThat(System.getenv(COMPUTER_NAME_PROPERTY)).isNull();
+ assertThat(System.getenv(HOSTNAME_PROPERTY)).isNull();
+ } else {
+ assertThat(System.getenv(COMPUTER_NAME_PROPERTY)).isNull();
+ assertThat(System.getenv(HOSTNAME_PROPERTY)).isNull();
+ }
+ }
+
+
+}