sreejasahithi commented on code in PR #10120:
URL: https://github.com/apache/ozone/pull/10120#discussion_r3145231604
##########
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");
+ }
/**
* Rename file to an existed directory.
*/
@Test
void testRenameFileToDir() throws Exception {
final String dir = "/dir" + RandomUtils.secure().randomInt(0, 1000);
- Path dirPath = new Path(getBucketPath() + dir);
- getFs().mkdirs(dirPath);
-
- Path file1Destin = new Path(getBucketPath() + dir + "/file1");
- ContractTestUtils.touch(getFs(), file1Destin);
- Path abcRootPath = new Path(getBucketPath() + "/a/b/c");
- getFs().mkdirs(abcRootPath);
- assertTrue(getFs().rename(file1Destin, abcRootPath), "Renamed failed");
- assertTrue(getFs().exists(new Path(
- abcRootPath, "file1")), "Renamed filed: /a/b/c/file1");
+ renameFileToDir(dir);
getFs().delete(getBucketPath(), true);
}
+ @Override
+ Path pathUnderFsRoot(String p) {
+ return new Path(getBucketPath() + p);
+ }
+
Review Comment:
same here
##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractOzoneFileSystemTest.java:
##########
@@ -1136,16 +1115,12 @@ public void testRenameFile() throws Exception {
@Test
public void testRenameFileToDir() throws Exception {
final String root = "/root";
- Path rootPath = new Path(fs.getUri().toString() + root);
- fs.mkdirs(rootPath);
+ renameFileToDir(root);
+ }
- Path file1Destin = new Path(fs.getUri().toString() + root + "/file1");
- ContractTestUtils.touch(fs, file1Destin);
- Path abcRootPath = new Path(fs.getUri().toString() + "/a/b/c");
- fs.mkdirs(abcRootPath);
- assertTrue(fs.rename(file1Destin, abcRootPath), "Renamed failed");
- assertTrue(fs.exists(new Path(abcRootPath,
- "file1")), "Renamed filed: /a/b/c/file1");
+ @Override
+ protected Path pathUnderFsRoot(String p) {
+ return new Path(getFs().getUri().toString() + p);
Review Comment:
Nit: parameter name does not convey any meaning , could you please change
this to adescriptive name like relativePath.
##########
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
Review Comment:
capture FileSystem fs = getFs() once and reuse it similar to how it is done
in other methods.
--
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]