Russole commented on code in PR #10120:
URL: https://github.com/apache/ozone/pull/10120#discussion_r3143888312


##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/OzoneFileSystemTestBase.java:
##########
@@ -473,6 +480,112 @@ void 
nonExplicitlyCreatedPathExistsAfterItsLeafsWereRemoved(Path root)
     fs.delete(source, true);
   }
 
+  void renameFile(String workDirFromFsRoot) throws Exception {
+    FileSystem fs = getFs();
+    Path workDir = pathUnderFsRoot(workDirFromFsRoot);
+    fs.mkdirs(workDir);
+
+    Path file1Source = pathUnderFsRoot(workDirFromFsRoot + "/file1_Copy");
+    Path file1Destin = pathUnderFsRoot(workDirFromFsRoot + "/file1");
+    ContractTestUtils.touch(fs, file1Source);
+    assertTrue(fs.rename(file1Source, file1Destin), "Renamed failed");
+    assertTrue(fs.exists(file1Destin), "Renamed failed");
+
+    verifyRenameFile(workDir, file1Destin);
+  }
+
+  void renameFileToDir(String workDirFromFsRoot) throws Exception {
+    FileSystem fs = getFs();
+    Path workDir = pathUnderFsRoot(workDirFromFsRoot);
+    fs.mkdirs(workDir);
+
+    Path file = pathUnderFsRoot(workDirFromFsRoot + "/file1");
+    ContractTestUtils.touch(fs, file);
+    Path targetDirTree = pathUnderFsRoot("/a/b/c");
+    fs.mkdirs(targetDirTree);
+    assertTrue(fs.rename(file, targetDirTree), "Renamed failed");
+    assertTrue(fs.exists(new Path(targetDirTree, "file1")), "Renamed failed: 
.../a/b/c/file1");
+  }
+
+  void renameToParentDir() throws Exception {
+    FileSystem fs = getFs();
+    final String root = "/root_dir";
+    final String dir1 = root + "/dir1";
+    final String dir2 = dir1 + "/dir2";
+    final Path dir2SourcePath = pathUnderFsRoot(dir2);
+    fs.mkdirs(dir2SourcePath);
+    final Path destRootPath = pathUnderFsRoot(root);
+
+    Path file1Source = pathUnderFsRoot(dir1 + "/file2");
+    ContractTestUtils.touch(fs, file1Source);
+
+    // rename source directory to its parent directory(destination).
+    assertTrue(fs.rename(dir2SourcePath, destRootPath), "Rename failed");
+    final Path expectedPathAfterRename =
+        pathUnderFsRoot(root + "/dir2");
+    assertTrue(fs.exists(expectedPathAfterRename), "Rename failed");
+
+    // rename source file to its parent directory(destination).
+    assertTrue(fs.rename(file1Source, destRootPath), "Rename failed");
+    final Path expectedFilePathAfterRename =
+        pathUnderFsRoot(root + "/file2");
+    assertTrue(fs.exists(expectedFilePathAfterRename), "Rename failed");
+  }
+
+  public void renameDirToItsOwnSubDir() throws Exception {
+    final String root = "/root";
+    final String dir1 = root + "/dir1";
+    FileSystem fs = getFs();
+    final Path dir1Path = pathUnderFsRoot(dir1);
+    // Add a sub-dir1 to the directory to be moved.
+    final Path subDir1 = new Path(dir1Path, "sub_dir1");
+    fs.mkdirs(subDir1);
+    LOG.info("Created dir1 {}", subDir1);
+
+    final Path sourceRoot = pathUnderFsRoot(root);
+    LOG.info("Rename op-> source:{} to destin:{}", sourceRoot, subDir1);
+    try {
+      fs.rename(sourceRoot, subDir1);
+      fail("Should throw exception : Cannot rename a directory to" +
+          " its own subdirectory");
+    } catch (IllegalArgumentException iae) {
+      // expected
+    }
+  }
+
+  void renameDestinationParentDoesNotExist() throws Exception {
+    final String root = "/root_dir";
+    final String dir1 = root + "/dir1";
+    final String dir2 = dir1 + "/dir2";
+    final Path dir2SourcePath = pathUnderFsRoot(dir2);
+    getFs().mkdirs(dir2SourcePath);
+    // (a) parent of dst does not exist.  /root_dir/b/c
+    final Path destinPath = pathUnderFsRoot(root + "/b/c");
+
+    // rename should throw exception
+    try {
+      getFs().rename(dir2SourcePath, destinPath);
+      fail("Should fail as parent of dst does not exist!");
+    } catch (FileNotFoundException fnfe) {
+      //expected
+    }
+    // (b) parent of dst is a file. /root_dir/file1/c
+    Path filePath = pathUnderFsRoot(root + "/file1");
+    ContractTestUtils.touch(getFs(), filePath);
+    Path newDestinPath = new Path(filePath, "c");
+    // rename shouldthrow exception

Review Comment:
   Nit: there is a small typo here. "shouldthrow" should be "should throw".
   ```suggestion
   // rename should throw exception
   ```



##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/OzoneFileSystemTestBase.java:
##########
@@ -473,6 +480,112 @@ void 
nonExplicitlyCreatedPathExistsAfterItsLeafsWereRemoved(Path root)
     fs.delete(source, true);
   }
 
+  void renameFile(String workDirFromFsRoot) throws Exception {
+    FileSystem fs = getFs();
+    Path workDir = pathUnderFsRoot(workDirFromFsRoot);
+    fs.mkdirs(workDir);
+
+    Path file1Source = pathUnderFsRoot(workDirFromFsRoot + "/file1_Copy");
+    Path file1Destin = pathUnderFsRoot(workDirFromFsRoot + "/file1");
+    ContractTestUtils.touch(fs, file1Source);
+    assertTrue(fs.rename(file1Source, file1Destin), "Renamed failed");
+    assertTrue(fs.exists(file1Destin), "Renamed failed");
+
+    verifyRenameFile(workDir, file1Destin);
+  }
+
+  void renameFileToDir(String workDirFromFsRoot) throws Exception {
+    FileSystem fs = getFs();
+    Path workDir = pathUnderFsRoot(workDirFromFsRoot);
+    fs.mkdirs(workDir);
+
+    Path file = pathUnderFsRoot(workDirFromFsRoot + "/file1");
+    ContractTestUtils.touch(fs, file);
+    Path targetDirTree = pathUnderFsRoot("/a/b/c");
+    fs.mkdirs(targetDirTree);
+    assertTrue(fs.rename(file, targetDirTree), "Renamed failed");
+    assertTrue(fs.exists(new Path(targetDirTree, "file1")), "Renamed failed: 
.../a/b/c/file1");
+  }
+
+  void renameToParentDir() throws Exception {
+    FileSystem fs = getFs();
+    final String root = "/root_dir";
+    final String dir1 = root + "/dir1";
+    final String dir2 = dir1 + "/dir2";
+    final Path dir2SourcePath = pathUnderFsRoot(dir2);
+    fs.mkdirs(dir2SourcePath);
+    final Path destRootPath = pathUnderFsRoot(root);
+
+    Path file1Source = pathUnderFsRoot(dir1 + "/file2");
+    ContractTestUtils.touch(fs, file1Source);
+
+    // rename source directory to its parent directory(destination).
+    assertTrue(fs.rename(dir2SourcePath, destRootPath), "Rename failed");
+    final Path expectedPathAfterRename =
+        pathUnderFsRoot(root + "/dir2");
+    assertTrue(fs.exists(expectedPathAfterRename), "Rename failed");
+
+    // rename source file to its parent directory(destination).
+    assertTrue(fs.rename(file1Source, destRootPath), "Rename failed");
+    final Path expectedFilePathAfterRename =
+        pathUnderFsRoot(root + "/file2");
+    assertTrue(fs.exists(expectedFilePathAfterRename), "Rename failed");
+  }
+
+  public void renameDirToItsOwnSubDir() throws Exception {

Review Comment:
   Nit: would it be better to keep this helper package-private, similar to the 
other rename helpers?
   ```suggestion
   void renameDirToItsOwnSubDir() throws Exception {
   ```



##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/OzoneFileSystemTestBase.java:
##########
@@ -473,6 +480,112 @@ void 
nonExplicitlyCreatedPathExistsAfterItsLeafsWereRemoved(Path root)
     fs.delete(source, true);
   }
 
+  void renameFile(String workDirFromFsRoot) throws Exception {
+    FileSystem fs = getFs();
+    Path workDir = pathUnderFsRoot(workDirFromFsRoot);
+    fs.mkdirs(workDir);
+
+    Path file1Source = pathUnderFsRoot(workDirFromFsRoot + "/file1_Copy");
+    Path file1Destin = pathUnderFsRoot(workDirFromFsRoot + "/file1");
+    ContractTestUtils.touch(fs, file1Source);
+    assertTrue(fs.rename(file1Source, file1Destin), "Renamed failed");
+    assertTrue(fs.exists(file1Destin), "Renamed failed");
+
+    verifyRenameFile(workDir, file1Destin);
+  }
+
+  void renameFileToDir(String workDirFromFsRoot) throws Exception {
+    FileSystem fs = getFs();
+    Path workDir = pathUnderFsRoot(workDirFromFsRoot);
+    fs.mkdirs(workDir);
+
+    Path file = pathUnderFsRoot(workDirFromFsRoot + "/file1");
+    ContractTestUtils.touch(fs, file);
+    Path targetDirTree = pathUnderFsRoot("/a/b/c");
+    fs.mkdirs(targetDirTree);
+    assertTrue(fs.rename(file, targetDirTree), "Renamed failed");
+    assertTrue(fs.exists(new Path(targetDirTree, "file1")), "Renamed failed: 
.../a/b/c/file1");
+  }
+
+  void renameToParentDir() throws Exception {
+    FileSystem fs = getFs();
+    final String root = "/root_dir";
+    final String dir1 = root + "/dir1";
+    final String dir2 = dir1 + "/dir2";
+    final Path dir2SourcePath = pathUnderFsRoot(dir2);
+    fs.mkdirs(dir2SourcePath);
+    final Path destRootPath = pathUnderFsRoot(root);
+
+    Path file1Source = pathUnderFsRoot(dir1 + "/file2");
+    ContractTestUtils.touch(fs, file1Source);
+
+    // rename source directory to its parent directory(destination).
+    assertTrue(fs.rename(dir2SourcePath, destRootPath), "Rename failed");
+    final Path expectedPathAfterRename =
+        pathUnderFsRoot(root + "/dir2");
+    assertTrue(fs.exists(expectedPathAfterRename), "Rename failed");
+
+    // rename source file to its parent directory(destination).
+    assertTrue(fs.rename(file1Source, destRootPath), "Rename failed");
+    final Path expectedFilePathAfterRename =
+        pathUnderFsRoot(root + "/file2");
+    assertTrue(fs.exists(expectedFilePathAfterRename), "Rename failed");
+  }
+
+  public void renameDirToItsOwnSubDir() throws Exception {
+    final String root = "/root";
+    final String dir1 = root + "/dir1";
+    FileSystem fs = getFs();
+    final Path dir1Path = pathUnderFsRoot(dir1);
+    // Add a sub-dir1 to the directory to be moved.
+    final Path subDir1 = new Path(dir1Path, "sub_dir1");
+    fs.mkdirs(subDir1);
+    LOG.info("Created dir1 {}", subDir1);
+
+    final Path sourceRoot = pathUnderFsRoot(root);
+    LOG.info("Rename op-> source:{} to destin:{}", sourceRoot, subDir1);
+    try {
+      fs.rename(sourceRoot, subDir1);
+      fail("Should throw exception : Cannot rename a directory to" +
+          " its own subdirectory");
+    } catch (IllegalArgumentException iae) {
+      // expected
+    }
+  }
+
+  void renameDestinationParentDoesNotExist() throws Exception {
+    final String root = "/root_dir";
+    final String dir1 = root + "/dir1";
+    final String dir2 = dir1 + "/dir2";
+    final Path dir2SourcePath = pathUnderFsRoot(dir2);
+    getFs().mkdirs(dir2SourcePath);
+    // (a) parent of dst does not exist.  /root_dir/b/c
+    final Path destinPath = pathUnderFsRoot(root + "/b/c");
+
+    // rename should throw exception
+    try {
+      getFs().rename(dir2SourcePath, destinPath);
+      fail("Should fail as parent of dst does not exist!");
+    } catch (FileNotFoundException fnfe) {
+      //expected
+    }
+    // (b) parent of dst is a file. /root_dir/file1/c
+    Path filePath = pathUnderFsRoot(root + "/file1");
+    ContractTestUtils.touch(getFs(), filePath);
+    Path newDestinPath = new Path(filePath, "c");
+    // rename shouldthrow exception
+    try {
+      getFs().rename(dir2SourcePath, newDestinPath);
+      fail("Should fail as parent of dst is a file!");
+    } catch (IOException e) {
+      //expected
+    }
+  }
+
+  abstract void verifyRenameFile(Path workDir, Path expectedDest) throws 
IOException;
+
+  abstract Path pathUnderFsRoot(String pathUnderFsRoot) throws IOException;

Review Comment:
   Nit: could we remove throws `IOException` from `pathUnderFsRoot`? The 
current implementations do not seem to throw `IOException`, so the simpler 
signature may be enough.



##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractRootedOzoneFileSystemTest.java:
##########
@@ -1731,45 +1731,36 @@ private void checkInvalidPath(Path path) {
   @Test
   void testRenameFile() throws Exception {
     final String dir = "/dir" + RandomUtils.secure().randomInt(0, 1000);
-    Path dirPath = new Path(getBucketPath() + dir);
-    Path file1Source = new Path(getBucketPath() + dir
-        + "/file1_Copy");
-    Path file1Destin = new Path(getBucketPath() + dir + "/file1");
+    Path dirPath = pathUnderFsRoot(dir);
     try {
-      getFs().mkdirs(dirPath);
-
-      ContractTestUtils.touch(getFs(), file1Source);
-      assertTrue(getFs().rename(file1Source, file1Destin), "Renamed failed");
-      assertTrue(getFs().exists(file1Destin), "Renamed failed: /dir/file1");
-      FileStatus[] fStatus = getFs().listStatus(dirPath);
-      assertEquals(1, fStatus.length, "Renamed failed");
+      renameFile(dir);
     } finally {
       // clean up
       fs.delete(dirPath, true);
     }
   }
 
-
+  @Override
+  void verifyRenameFile(Path workDir, Path expectedDest) throws IOException {
+    FileStatus[] fStatus = getFs().listStatus(workDir);
+    assertEquals(1, fStatus.length, "Renamed failed");
+  }

Review Comment:
   Could we also verify `expectedDest` here, or add a short comment explaining 
why `RootedOzoneFileSystem` only checks the number of entries? Since 
`expectedDest` is passed from the base helper, skipping the path assertion 
looks a bit unclear to me.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to