Sfoglia il codice sorgente

HDFS-11949. Add testcase for ensuring that FsShell cann't move file to the target directory that file exists. Contributed by legend.

Wei-Chiu Chuang 6 anni fa
parent
commit
1e92db5a1e

+ 36 - 11
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFsShellCopy.java

@@ -189,7 +189,7 @@ public class TestFsShellCopy {
     // copy to new file, then again
     prepPut(dstPath, false, false);
     checkPut(0, srcPath, dstPath, useWindowsPath);
-    if (lfs.isFile(srcPath)) {
+    if (lfs.getFileStatus(srcPath).isFile()) {
       checkPut(1, srcPath, dstPath, useWindowsPath);
     } else { // directory works because it copies into the dir
       // clear contents so the check won't think there are extra paths
@@ -228,11 +228,11 @@ public class TestFsShellCopy {
     if (create) {
       if (isDir) {
         lfs.mkdirs(dst);
-        assertTrue(lfs.isDirectory(dst));
+        assertTrue(lfs.getFileStatus(dst).isDirectory());
       } else {
         lfs.mkdirs(new Path(dst.getName()));
         lfs.create(dst).close();
-        assertTrue(lfs.isFile(dst));
+        assertTrue(lfs.getFileStatus(dst).isFile());
       }
     }
   }
@@ -253,7 +253,7 @@ public class TestFsShellCopy {
     
     Path target;
     if (lfs.exists(dest)) {
-      if (lfs.isDirectory(dest)) {
+      if (lfs.getFileStatus(dest).isDirectory()) {
         target = new Path(pathAsString(dest), src.getName());
       } else {
         target = dest;
@@ -276,7 +276,8 @@ public class TestFsShellCopy {
     
     if (exitCode == 0) {
       assertTrue(lfs.exists(target));
-      assertTrue(lfs.isFile(src) == lfs.isFile(target));
+      assertTrue(lfs.getFileStatus(src).isFile() ==
+          lfs.getFileStatus(target).isFile());
       assertEquals(1, lfs.listStatus(lfs.makeQualified(target).getParent()).length);      
     } else {
       assertEquals(targetExists, lfs.exists(target));
@@ -293,7 +294,7 @@ public class TestFsShellCopy {
 
     argv = new String[]{ "-put", srcPath.toString(), dstPath.toString() };
     assertEquals(0, shell.run(argv));
-    assertTrue(lfs.exists(dstPath) && lfs.isFile(dstPath));
+    assertTrue(lfs.exists(dstPath) && lfs.getFileStatus(dstPath).isFile());
 
     lfs.delete(dstPath, true);
     assertFalse(lfs.exists(dstPath));
@@ -319,7 +320,7 @@ public class TestFsShellCopy {
           "-put", srcPath.toString(), dstPath.toString()+suffix };
       assertEquals(0, shell.run(argv));
       assertTrue(lfs.exists(subdirDstPath));
-      assertTrue(lfs.isFile(subdirDstPath));
+      assertTrue(lfs.getFileStatus(subdirDstPath).isFile());
     }
 
     // ensure .. is interpreted as a dir
@@ -329,7 +330,7 @@ public class TestFsShellCopy {
     argv = new String[]{ "-put", srcPath.toString(), dotdotDst };
     assertEquals(0, shell.run(argv));
     assertTrue(lfs.exists(subdirDstPath));
-    assertTrue(lfs.isFile(subdirDstPath));
+    assertTrue(lfs.getFileStatus(subdirDstPath).isFile());
   }
   
   @Test
@@ -442,9 +443,33 @@ public class TestFsShellCopy {
     assertEquals(0, exit);
     assertFalse(lfs.exists(srcFile));
     assertTrue(lfs.exists(target));
-    assertTrue(lfs.isFile(target));
+    assertTrue(lfs.getFileStatus(target).isFile());
   }
-  
+
+  @Test
+  public void testMoveFileFromLocalDestExists() throws Exception{
+    Path testRoot = new Path(testRootDir, "testPutFile");
+    lfs.delete(testRoot, true);
+    lfs.mkdirs(testRoot);
+
+    Path target = new Path(testRoot, "target");
+    Path srcFile = new Path(testRoot, new Path("srcFile"));
+    lfs.createNewFile(srcFile);
+
+    int exit = shell.run(new String[]{
+        "-moveFromLocal", srcFile.toString(), target.toString()});
+    assertEquals(0, exit);
+    assertFalse(lfs.exists(srcFile));
+    assertTrue(lfs.exists(target));
+    assertTrue(lfs.getFileStatus(target).isFile());
+
+    lfs.createNewFile(srcFile);
+    exit = shell.run(new String[]{
+        "-moveFromLocal", srcFile.toString(), target.toString()});
+    assertEquals(1, exit);
+    assertTrue(lfs.exists(srcFile));
+  }
+
   @Test
   public void testMoveDirFromLocal() throws Exception {    
     Path testRoot = new Path(testRootDir, "testPutDir");
@@ -502,7 +527,7 @@ public class TestFsShellCopy {
     shellRun(0, "-moveFromLocal", winSrcFile, target.toString());
     assertFalse(lfs.exists(srcFile));
     assertTrue(lfs.exists(target));
-    assertTrue(lfs.isFile(target));
+    assertTrue(lfs.getFileStatus(target).isFile());
   }
 
   @Test