Explorar o código

ZOOKEEPER-4779: Fix ZKUtilTest which fails to run on WSL

Reviewers: maoling
Author: muthu90tech
Closes #2099 from muthu90tech/ZOOKEEPER-4779
Muthuraj Ramalingakumar hai 1 ano
pai
achega
e2ea0381f4

+ 20 - 0
zookeeper-server/src/main/java/org/apache/zookeeper/Shell.java

@@ -33,10 +33,12 @@ import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
 import java.util.Map;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.commons.io.FileUtils;
 import org.apache.zookeeper.common.Time;
 import org.apache.zookeeper.server.ExitCode;
 import org.slf4j.Logger;
@@ -105,6 +107,24 @@ public abstract class Shell {
         return new String[]{ULIMIT_COMMAND, "-v", String.valueOf(memoryLimit)};
     }
 
+    /**
+     * Check if running in Windows Subsystem for Linux
+     * @return
+     * @throws IOException
+     */
+    public static boolean isWsl() throws IOException {
+        if (WINDOWS) {
+            return false;
+        }
+        final File f = new File("/proc/version");
+        if (!f.exists()) {
+            return false;
+        }
+        final String output = FileUtils.readFileToString(f, StandardCharsets.UTF_8.name());
+        return (output != null)
+                && System.getProperty("os.name").startsWith("Linux") && output.toLowerCase().contains("microsoft");
+    }
+
     /** Set to true on Windows platforms */
     public static final boolean WINDOWS /* borrowed from Path.WINDOWS */ = System.getProperty("os.name").startsWith("Windows");
 

+ 3 - 2
zookeeper-server/src/test/java/org/apache/zookeeper/ZKUtilTest.java

@@ -80,8 +80,9 @@ public class ZKUtilTest extends ClientBase {
 
     @Test
     public void testUnreadableFileInput() throws Exception {
-        //skip this test on Windows, coverage on Linux
-        assumeTrue(!org.apache.zookeeper.Shell.WINDOWS);
+        //skip this test on Windows and WSL, coverage on Linux
+        assumeTrue("Skipping this test on Windows and WSL",
+                !(org.apache.zookeeper.Shell.WINDOWS || org.apache.zookeeper.Shell.isWsl()));
         File file = File.createTempFile("test", ".junit", testData);
         file.setReadable(false, false);
         file.deleteOnExit();