This is an automated email from the ASF dual-hosted git repository.

khowe 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 fe533c6  GEODE-5741: Modified tests to be independent of environment 
(#2496)
fe533c6 is described below

commit fe533c60edb4c3cea56540f5e873f47b04cb74ff
Author: Kenneth Howe <[email protected]>
AuthorDate: Tue Sep 25 12:06:27 2018 -0700

    GEODE-5741: Modified tests to be independent of environment (#2496)
    
    * GEODE-5741: Modified tests to be independent of environment
    
    Tests using relative paths had depended on running as a user other than 
root.
    The tests expceted that file or directpory creation would fail because the 
user
    would not have permission to write to filesystem root.
    
    A further problem was the several tests had hardcoded '/' in pathnames,
    which is incompatible with running on Windows.
    
    * make tests use relative paths
    * remove system dependent path separators
    * move tests that expect directory creation to fail to StartMemberUtilsTest.
    * On entry to testGeodeOnClasspathIsFirst there may (depending test 
platform)
    be a version of geode-core-*.jar on the system classpath, but at an unknown 
position
    in tthe list. Now, a dummy geode-core-*.jar path is inserted in the system
    classpath at a known, but not the first, position.
---
 .../cli/commands/StartLocatorCommandDUnitTest.java | 39 ++-------------
 .../cli/commands/StartServerCommandDUnitTest.java  | 56 ++++++----------------
 .../cli/commands/StartMemberUtilsTest.java         | 55 +++++++++++++++++----
 .../internal/cli/commands/StartLocatorCommand.java |  3 +-
 .../internal/cli/commands/StartMemberUtils.java    |  4 +-
 .../internal/cli/commands/StartServerCommand.java  |  3 +-
 6 files changed, 71 insertions(+), 89 deletions(-)

diff --git 
a/geode-assembly/src/distributedTest/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandDUnitTest.java
 
b/geode-assembly/src/distributedTest/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandDUnitTest.java
index de802e8..364eaec 100644
--- 
a/geode-assembly/src/distributedTest/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandDUnitTest.java
+++ 
b/geode-assembly/src/distributedTest/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommandDUnitTest.java
@@ -230,37 +230,9 @@ public class StartLocatorCommandDUnitTest {
   }
 
   @Test
-  public void testInMissingRelativeDirectoryWithoutCreatePermissions() {
-    // path to a missing dir that cannot be created due to insufficient 
permissions
-    String readOnlyPathname = "readOnlyDir";
-    File readOnlyDir = new File(readOnlyPathname);
-    final String missingDirPath =
-        Paths.get(readOnlyPathname, "missing", "path", "to", "start", 
"in").toString();
-
-    final String expectedMessage = "Could not create directory .*" + 
missingDirPath
-        + ". Please verify directory path or user permissions.";
-    final String memberName = 
"testInMissingRelativeDirectoryWithoutCreatePermissions-locator";
-
-    try {
-      assertThat(readOnlyDir.mkdir()).isTrue();
-      assertThat(readOnlyDir.setReadOnly()).isTrue();
-
-      CommandStringBuilder command = new CommandStringBuilder(START_LOCATOR)
-          .addOption(START_LOCATOR__MEMBER_NAME, memberName)
-          .addOption(START_LOCATOR__LOCATORS, locatorConnectionString)
-          .addOption(START_LOCATOR__DIR, missingDirPath);
-
-      CommandResult result = gfsh.executeCommand(command.getCommandString());
-
-      assertThat(result.getStatus()).isEqualTo(Result.Status.ERROR);
-      
assertThat(result.getMessageFromContent()).containsPattern(expectedMessage);
-    } finally {
-      FileUtils.deleteQuietly(readOnlyDir);
-    }
-  }
-
-  @Test
   public void testInMissingRelativeDirectoryThatCanBeCreated() {
+    final Integer locatorPort = 
AvailablePortHelper.getRandomAvailableTCPPort();
+
     // path to a missing dir that can be created
     String readWritePathname = "readWriteDir";
     File readWriteDir = new File(readWritePathname);
@@ -273,12 +245,12 @@ public class StartLocatorCommandDUnitTest {
     CommandStringBuilder command = new CommandStringBuilder(START_LOCATOR)
         .addOption(START_LOCATOR__MEMBER_NAME, memberName)
         .addOption(START_LOCATOR__LOCATORS, locatorConnectionString)
-        .addOption(START_LOCATOR__DIR, missingDirPath);
+        .addOption(START_LOCATOR__DIR, missingDirPath)
+        .addOption(START_LOCATOR__PORT, locatorPort.toString());
 
     try {
       CommandResult result = gfsh.executeCommand(command.getCommandString());
 
-
       assertThat(result.getStatus()).isEqualTo(Result.Status.OK);
       
assertThat(result.getMessageFromContent()).containsPattern(expectedMessage);
     } finally {
@@ -325,8 +297,7 @@ public class StartLocatorCommandDUnitTest {
       assertThat(result.getMessageFromContent()).contains(expectedMessage);
 
       // Verify GEODE-2138 (Geode commands do not contain GemFire in output)
-      assertThat(result.getMessageFromContent()).doesNotContain("Gemfire")
-          .doesNotContain("GemFire");
+      
assertThat(result.getMessageFromContent()).doesNotContainPattern("Gem[Ff]ire 
Version");
       
assertThat(result.getMessageFromContent()).containsPattern(expectedVersionPattern);
     } finally {
       String pathToFile = Paths.get(System.getProperty("user.dir"), 
memberName).toString();
diff --git 
a/geode-assembly/src/distributedTest/java/org/apache/geode/management/internal/cli/commands/StartServerCommandDUnitTest.java
 
b/geode-assembly/src/distributedTest/java/org/apache/geode/management/internal/cli/commands/StartServerCommandDUnitTest.java
index 064d76b..6e5b7cc 100644
--- 
a/geode-assembly/src/distributedTest/java/org/apache/geode/management/internal/cli/commands/StartServerCommandDUnitTest.java
+++ 
b/geode-assembly/src/distributedTest/java/org/apache/geode/management/internal/cli/commands/StartServerCommandDUnitTest.java
@@ -42,6 +42,7 @@ import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.nio.file.Paths;
 
+import org.apache.commons.io.FileUtils;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -238,12 +239,17 @@ public class StartServerCommandDUnitTest {
   }
 
   @Test
-  public void testWithMissingStartDirectoryThatCannotBeCreated() {
+  public void testWithMissingStartDirectoryThatCanBeCreated() throws 
IOException {
     final Integer serverPort = AvailablePortHelper.getRandomAvailableTCPPort();
-    final String missingDirPath = "/missing/dir/to/start/in";
-    final String memberName = 
"testWithMissingStartDirectoryThatCannotBeCreated-server";
-    final String expectedError = "Could not create directory " + missingDirPath
-        + ". Please verify directory path or user permissions.";
+
+    // path to a missing dir that can be created
+    String readWritePathname = "readWriteDir";
+    File readWriteDir = new File(readWritePathname);
+    final String missingDirPath =
+        Paths.get(readWritePathname, "missing", "dir", "to", "start", 
"in").toString();
+
+    final String memberName = 
"testWithMissingStartDirectoryThatCanBeCreated-server";
+    final String expectedMessage = "Server in .*" + missingDirPath;
 
     String command = new CommandStringBuilder(START_SERVER)
         .addOption(START_SERVER__NAME, memberName)
@@ -252,34 +258,13 @@ public class StartServerCommandDUnitTest {
         .addOption(START_SERVER__DIR, missingDirPath)
         .getCommandString();
 
-    CommandResult result = gfsh.executeCommand(command);
-
-    assertThat(result.getStatus()).isEqualTo(Result.Status.ERROR);
-    assertThat(result.getMessageFromContent()).contains(expectedError);
-  }
-
-  @Test
-  public void testWithMissingStartDirectoryThatCanBeCreated() throws 
IOException {
-    final Integer serverPort = AvailablePortHelper.getRandomAvailableTCPPort();
-    final String missingDirPath = workingDir.getCanonicalPath() + 
"/missing/dir/to/start/in";
-    final String memberName = 
"testWithMissingStartDirectoryThatCanBeCreated-server";
-    final String expectedMessage = "Server in " + missingDirPath;
-
     try {
-      String command = new CommandStringBuilder(START_SERVER)
-          .addOption(START_SERVER__NAME, memberName)
-          .addOption(START_SERVER__LOCATORS, locatorConnectionString)
-          .addOption(START_SERVER__SERVER_PORT, serverPort.toString())
-          .addOption(START_SERVER__DIR, missingDirPath)
-          .getCommandString();
-
       CommandResult result = gfsh.executeCommand(command);
 
       assertThat(result.getStatus()).isEqualTo(Result.Status.OK);
-      assertThat(result.getMessageFromContent()).contains(expectedMessage);
+      
assertThat(result.getMessageFromContent()).containsPattern(expectedMessage);
     } finally {
-      File toDelete = new File(missingDirPath);
-      deleteServerFiles(toDelete);
+      FileUtils.deleteQuietly(readWriteDir);
     }
   }
 
@@ -373,8 +358,7 @@ public class StartServerCommandDUnitTest {
     assertThat(result.getMessageFromContent()).contains(expectedMessage);
 
     // verify GEODE-2138 (Geode commands do not contain GemFire in output)
-    assertThat(result.getMessageFromContent()).doesNotContain("Gemfire")
-        .doesNotContain("GemFire");
+    
assertThat(result.getMessageFromContent()).doesNotContainPattern("Gem[Ff]ire 
Version");
     
assertThat(result.getMessageFromContent()).containsPattern(expectedVersionPattern);
   }
 
@@ -442,18 +426,6 @@ public class StartServerCommandDUnitTest {
     assertThat(ProcessUtils.isProcessAlive(serverPid)).isFalse();
   }
 
-  private void deleteServerFiles(File toDelete) {
-    File[] nestedToDelete = toDelete.listFiles();
-
-    if (nestedToDelete != null && nestedToDelete.length > 0) {
-      for (File file : nestedToDelete) {
-        deleteServerFiles(file);
-      }
-    }
-
-    toDelete.delete();
-  }
-
   /**
    * Attempts to determine the PID of the running process from the 
ManagementFactory's runtime MBean
    *
diff --git 
a/geode-core/src/integrationTest/java/org/apache/geode/management/internal/cli/commands/StartMemberUtilsTest.java
 
b/geode-core/src/integrationTest/java/org/apache/geode/management/internal/cli/commands/StartMemberUtilsTest.java
index 2f0cd34..21450ef 100644
--- 
a/geode-core/src/integrationTest/java/org/apache/geode/management/internal/cli/commands/StartMemberUtilsTest.java
+++ 
b/geode-core/src/integrationTest/java/org/apache/geode/management/internal/cli/commands/StartMemberUtilsTest.java
@@ -15,9 +15,12 @@
 
 package org.apache.geode.management.internal.cli.commands;
 
-import static org.assertj.core.api.Java6Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
 
 import java.io.File;
 import java.io.FileWriter;
@@ -47,8 +50,19 @@ public class StartMemberUtilsTest {
   public RestoreSystemProperties restorer = new RestoreSystemProperties();
 
   @Test
+  public void workingDirCantBeCreatedThrowsException() {
+    File userSpecifiedDir = spy(new File("cantCreateDir"));
+    when(userSpecifiedDir.exists()).thenReturn(false);
+    when(userSpecifiedDir.mkdirs()).thenReturn(false);
+    assertThatThrownBy(
+        () -> StartMemberUtils.resolveWorkingDir(userSpecifiedDir, new 
File("server1")))
+            .isInstanceOf(IllegalStateException.class)
+            .hasMessageContaining("Could not create directory");
+  }
+
+  @Test
   public void workingDirDefaultsToMemberName() {
-    String workingDir = StartMemberUtils.resolveWorkingDir(null, "server1");
+    String workingDir = StartMemberUtils.resolveWorkingDir(null, new 
File("server1"));
     assertThat(new File(workingDir)).exists();
     assertThat(workingDir).endsWith("server1");
   }
@@ -58,7 +72,8 @@ public class StartMemberUtilsTest {
     File workingDir = temporaryFolder.newFolder("foo");
     FileUtils.deleteQuietly(workingDir);
     String workingDirString = workingDir.getAbsolutePath();
-    String resolvedWorkingDir = 
StartMemberUtils.resolveWorkingDir(workingDirString, "server1");
+    String resolvedWorkingDir =
+        StartMemberUtils.resolveWorkingDir(new File(workingDirString), new 
File("server1"));
     assertThat(new File(resolvedWorkingDir)).exists();
     assertThat(workingDirString).endsWith("foo");
   }
@@ -68,7 +83,7 @@ public class StartMemberUtilsTest {
     Path relativePath = Paths.get("some").resolve("relative").resolve("path");
     assertThat(relativePath.isAbsolute()).isFalse();
     String resolvedWorkingDir =
-        StartMemberUtils.resolveWorkingDir(relativePath.toString(), "server1");
+        StartMemberUtils.resolveWorkingDir(new File(relativePath.toString()), 
new File("server1"));
     
assertThat(resolvedWorkingDir).isEqualTo(relativePath.toAbsolutePath().toString());
   }
 
@@ -87,17 +102,33 @@ public class StartMemberUtilsTest {
     assertEquals(expectedPid, actualPid);
   }
 
+  /**
+   * Verify that the classpath for Geode always has the geode-core jar first 
in the list of paths.
+   * On entry to the test there may (depending test platform) be a version of 
geode-core-*.jar on
+   * the system classpath, but at an unknown position in the list. To make the 
test deterministic, a
+   * dummy geode-core-*.jar path is inserted in the system classpath at a 
known, but not the first,
+   * position.
+   */
   @Test
   public void testGeodeOnClasspathIsFirst() {
     String currentClasspath = System.getProperty("java.class.path");
-    String customGeodeCore = FileSystems
-        .getDefault().getPath("/custom/geode-core-" + 
GemFireVersion.getGemFireVersion() + ".jar")
+    final Path customGeodeJarPATH =
+        Paths.get("/custom", "geode-core-" + 
GemFireVersion.getGemFireVersion() + ".jar");
+    final Path prependJar1 = Paths.get("/prepend", "pre1.jar");
+    final Path prependJar2 = Paths.get("/prepend", "pre2.jar");
+    final Path otherJar1 = Paths.get("/other", "one.jar");
+    final Path otherJar2 = Paths.get("/other", "two.jar");
+    final String customGeodeCore = 
FileSystems.getDefault().getPath(customGeodeJarPATH.toString())
         .toAbsolutePath().toString();
-    System.setProperty("java.class.path", currentClasspath + 
File.pathSeparator + customGeodeCore);
+
+    currentClasspath = prependToClasspath("java.class.path", customGeodeCore, 
currentClasspath);
+    currentClasspath =
+        prependToClasspath("java.class.path", prependJar2.toString(), 
currentClasspath);
+    prependToClasspath("java.class.path", prependJar1.toString(), 
currentClasspath);
 
     String[] otherJars = new String[] {
-        
FileSystems.getDefault().getPath("/other/one.jar").toAbsolutePath().toString(),
-        
FileSystems.getDefault().getPath("/other/two.jar").toAbsolutePath().toString()};
+        
FileSystems.getDefault().getPath(otherJar1.toString()).toAbsolutePath().toString(),
+        
FileSystems.getDefault().getPath(otherJar2.toString()).toAbsolutePath().toString()};
 
     String gemfireClasspath = StartMemberUtils.toClasspath(true, otherJars);
     assertThat(gemfireClasspath).startsWith(customGeodeCore);
@@ -106,6 +137,12 @@ public class StartMemberUtilsTest {
     assertThat(gemfireClasspath).startsWith(customGeodeCore);
   }
 
+  private String prependToClasspath(String systemPropertyKey, String 
prependJarPath,
+      String currentClasspath) {
+    System.setProperty(systemPropertyKey, prependJarPath + File.pathSeparator 
+ currentClasspath);
+    return System.getProperty(systemPropertyKey);
+  }
+
   @Test
   public void testExtensionsJars() throws IOException {
 
diff --git 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommand.java
 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommand.java
index edbab1a..b2e87f2 100644
--- 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommand.java
+++ 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartLocatorCommand.java
@@ -129,7 +129,8 @@ public class StartLocatorCommand extends 
InternalGfshCommand {
       memberName = StartMemberUtils.getNameGenerator().generate('-');
     }
 
-    workingDirectory = StartMemberUtils.resolveWorkingDir(workingDirectory, 
memberName);
+    workingDirectory = StartMemberUtils.resolveWorkingDir(
+        workingDirectory == null ? null : new File(workingDirectory), new 
File(memberName));
 
     if (gemfirePropertiesFile != null && !gemfirePropertiesFile.exists()) {
       return ResultBuilder.createUserErrorResult(
diff --git 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartMemberUtils.java
 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartMemberUtils.java
index 378b27e..05abf00 100644
--- 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartMemberUtils.java
+++ 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartMemberUtils.java
@@ -70,9 +70,9 @@ public class StartMemberUtils {
     }
   }
 
-  static String resolveWorkingDir(String userSpecifiedDir, String memberName) {
+  static String resolveWorkingDir(File userSpecifiedDir, File memberNameDir) {
     File workingDir =
-        (userSpecifiedDir == null) ? new File(memberName) : new 
File(userSpecifiedDir);
+        (userSpecifiedDir == null) ? memberNameDir : userSpecifiedDir;
     String workingDirPath = 
IOUtils.tryGetCanonicalPathElseGetAbsolutePath(workingDir);
     if (!workingDir.exists()) {
       if (!workingDir.mkdirs()) {
diff --git 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartServerCommand.java
 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartServerCommand.java
index 829d89f..61d2539 100644
--- 
a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartServerCommand.java
+++ 
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/StartServerCommand.java
@@ -198,7 +198,8 @@ public class StartServerCommand extends InternalGfshCommand 
{
       }
     }
 
-    workingDirectory = StartMemberUtils.resolveWorkingDir(workingDirectory, 
memberName);
+    workingDirectory = StartMemberUtils.resolveWorkingDir(
+        workingDirectory == null ? null : new File(workingDirectory), new 
File(memberName));
 
     cacheXmlPathname = CliUtil.resolvePathname(cacheXmlPathname);
 

Reply via email to