From 3a698e6aea4c1aac520569376fc0ef87289ff7a2 Mon Sep 17 00:00:00 2001 From: Daryn Sharp Date: Tue, 6 Nov 2012 15:57:58 +0000 Subject: [PATCH] HDFS-1331. dfs -test should work like /bin/test (Andy Isaacson via daryn) git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1406198 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/hadoop/fs/shell/Test.java | 19 +++- .../src/test/resources/testConf.xml | 4 +- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 2 + .../org/apache/hadoop/hdfs/TestDFSShell.java | 101 +++++++++++++++++- 4 files changed, 119 insertions(+), 7 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Test.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Test.java index 9780698b3a..011cdac1f8 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Test.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Test.java @@ -37,16 +37,21 @@ class Test extends FsCommand { } public static final String NAME = "test"; - public static final String USAGE = "-[ezd] "; + public static final String USAGE = "-[defsz] "; public static final String DESCRIPTION = - "If file exists, has zero length, is a directory\n" + - "then return 0, else return 1."; + "Answer various questions about , with result via exit status.\n" + + " -d return 0 if is a directory.\n" + + " -e return 0 if exists.\n" + + " -f return 0 if is a file.\n" + + " -s return 0 if file is greater than zero bytes in size.\n" + + " -z return 0 if file is zero bytes in size.\n" + + "else, return 1."; private char flag; @Override protected void processOptions(LinkedList args) { - CommandFormat cf = new CommandFormat(1, 1, "e", "d", "z"); + CommandFormat cf = new CommandFormat(1, 1, "e", "d", "f", "s", "z"); cf.parse(args); String[] opts = cf.getOpts().toArray(new String[0]); @@ -71,6 +76,12 @@ class Test extends FsCommand { case 'd': test = item.stat.isDirectory(); break; + case 'f': + test = item.stat.isFile(); + break; + case 's': + test = (item.stat.getLen() > 0); + break; case 'z': test = (item.stat.getLen() == 0); break; diff --git a/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml b/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml index ade6e1a04b..65a522b1b7 100644 --- a/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml +++ b/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml @@ -591,11 +591,11 @@ RegexpComparator - ^-test -\[ezd\] <path>:\s+If file exists, has zero length, is a directory( )* + ^-test -\[defsz\] <path>:\sAnswer various questions about <path>, with result via exit status. RegexpComparator - ^( |\t)*then return 0, else return 1.( )* + ^( |\t)*else, return 1.( )* diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 63a9efe6ad..3ff09794d1 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -551,6 +551,8 @@ Release 2.0.3-alpha - Unreleased HDFS-4132. When libwebhdfs is not enabled, nativeMiniDfsClient frees uninitialized memory (Colin Patrick McCabe via todd) + HDFS-1331. dfs -test should work like /bin/test (Andy Isaacson via daryn) + Release 2.0.2-alpha - 2012-09-07 INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java index 3e9026abf5..f24444fb69 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java @@ -1243,7 +1243,106 @@ public class TestDFSShell { } assertEquals(0, val); } - + + // Verify -test -f negative case (missing file) + { + String[] args = new String[3]; + args[0] = "-test"; + args[1] = "-f"; + args[2] = "/test/mkdirs/noFileHere"; + int val = -1; + try { + val = shell.run(args); + } catch (Exception e) { + System.err.println("Exception raised from DFSShell.run " + + e.getLocalizedMessage()); + } + assertEquals(1, val); + } + + // Verify -test -f negative case (directory rather than file) + { + String[] args = new String[3]; + args[0] = "-test"; + args[1] = "-f"; + args[2] = "/test/mkdirs"; + int val = -1; + try { + val = shell.run(args); + } catch (Exception e) { + System.err.println("Exception raised from DFSShell.run " + + e.getLocalizedMessage()); + } + assertEquals(1, val); + } + + // Verify -test -f positive case + { + writeFile(fileSys, myFile); + assertTrue(fileSys.exists(myFile)); + + String[] args = new String[3]; + args[0] = "-test"; + args[1] = "-f"; + args[2] = myFile.toString(); + int val = -1; + try { + val = shell.run(args); + } catch (Exception e) { + System.err.println("Exception raised from DFSShell.run " + + e.getLocalizedMessage()); + } + assertEquals(0, val); + } + + // Verify -test -s negative case (missing file) + { + String[] args = new String[3]; + args[0] = "-test"; + args[1] = "-s"; + args[2] = "/test/mkdirs/noFileHere"; + int val = -1; + try { + val = shell.run(args); + } catch (Exception e) { + System.err.println("Exception raised from DFSShell.run " + + e.getLocalizedMessage()); + } + assertEquals(1, val); + } + + // Verify -test -s negative case (zero length file) + { + String[] args = new String[3]; + args[0] = "-test"; + args[1] = "-s"; + args[2] = "/test/mkdirs/isFileHere"; + int val = -1; + try { + val = shell.run(args); + } catch (Exception e) { + System.err.println("Exception raised from DFSShell.run " + + e.getLocalizedMessage()); + } + assertEquals(1, val); + } + + // Verify -test -s positive case (nonzero length file) + { + String[] args = new String[3]; + args[0] = "-test"; + args[1] = "-s"; + args[2] = myFile.toString(); + int val = -1; + try { + val = shell.run(args); + } catch (Exception e) { + System.err.println("Exception raised from DFSShell.run " + + e.getLocalizedMessage()); + } + assertEquals(0, val); + } + } finally { try { fileSys.close();