HADOOP-15513. Add additional test cases to cover some corner cases for FileUtil#symlink. Contributed by Giovanni Matteo Fumarola.

This commit is contained in:
Inigo Goiri 2018-06-06 11:44:17 -07:00
parent d901be6795
commit db81f85e84
2 changed files with 161 additions and 0 deletions

View File

@ -1033,6 +1033,13 @@ public static class HardLink extends org.apache.hadoop.fs.HardLink {
* @return 0 on success
*/
public static int symLink(String target, String linkname) throws IOException{
if (target == null || linkname == null) {
LOG.warn("Can not create a symLink with a target = " + target
+ " and link =" + linkname);
return 1;
}
// Run the input paths through Java's File so that they are converted to the
// native OS form
File targetFile = new File(

View File

@ -968,6 +968,160 @@ public void testSymlinkLength() throws Exception {
Assert.assertFalse(link.exists());
}
/**
* This test validates the correctness of
* {@link FileUtil#symLink(String, String)} in case of null pointer inputs.
*
* @throws IOException
*/
@Test
public void testSymlinkWithNullInput() throws IOException {
Assert.assertFalse(del.exists());
del.mkdirs();
File file = new File(del, FILE);
File link = new File(del, "_link");
// Create the same symbolic link
// The operation should fail and returns 1
int result = FileUtil.symLink(null, null);
Assert.assertEquals(1, result);
// Create the same symbolic link
// The operation should fail and returns 1
result = FileUtil.symLink(file.getAbsolutePath(), null);
Assert.assertEquals(1, result);
// Create the same symbolic link
// The operation should fail and returns 1
result = FileUtil.symLink(null, link.getAbsolutePath());
Assert.assertEquals(1, result);
file.delete();
link.delete();
}
/**
* This test validates the correctness of
* {@link FileUtil#symLink(String, String)} in case the file already exists.
*
* @throws IOException
*/
@Test
public void testSymlinkFileAlreadyExists() throws IOException {
Assert.assertFalse(del.exists());
del.mkdirs();
File file = new File(del, FILE);
File link = new File(del, "_link");
// Create a symbolic link
// The operation should succeed
int result1 =
FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath());
Assert.assertEquals(0, result1);
// Create the same symbolic link
// The operation should fail and returns 1
result1 = FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath());
Assert.assertEquals(1, result1);
file.delete();
link.delete();
}
/**
* This test validates the correctness of
* {@link FileUtil#symLink(String, String)} in case the file and the link are
* the same file.
*
* @throws IOException
*/
@Test
public void testSymlinkSameFile() throws IOException {
Assert.assertFalse(del.exists());
del.mkdirs();
File file = new File(del, FILE);
// Create a symbolic link
// The operation should succeed
int result =
FileUtil.symLink(file.getAbsolutePath(), file.getAbsolutePath());
Assert.assertEquals(0, result);
file.delete();
}
/**
* This test validates the correctness of
* {@link FileUtil#symLink(String, String)} in case we want to use a link for
* 2 different files.
*
* @throws IOException
*/
@Test
public void testSymlink2DifferentFile() throws IOException {
Assert.assertFalse(del.exists());
del.mkdirs();
File file = new File(del, FILE);
File fileSecond = new File(del, FILE + "_1");
File link = new File(del, "_link");
// Create a symbolic link
// The operation should succeed
int result =
FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath());
Assert.assertEquals(0, result);
// The operation should fail and returns 1
result =
FileUtil.symLink(fileSecond.getAbsolutePath(), link.getAbsolutePath());
Assert.assertEquals(1, result);
file.delete();
fileSecond.delete();
link.delete();
}
/**
* This test validates the correctness of
* {@link FileUtil#symLink(String, String)} in case we want to use a 2
* different links for the same file.
*
* @throws IOException
*/
@Test
public void testSymlink2DifferentLinks() throws IOException {
Assert.assertFalse(del.exists());
del.mkdirs();
File file = new File(del, FILE);
File link = new File(del, "_link");
File linkSecond = new File(del, "_link_1");
// Create a symbolic link
// The operation should succeed
int result =
FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath());
Assert.assertEquals(0, result);
// The operation should succeed
result =
FileUtil.symLink(file.getAbsolutePath(), linkSecond.getAbsolutePath());
Assert.assertEquals(0, result);
file.delete();
link.delete();
linkSecond.delete();
}
private void doUntarAndVerify(File tarFile, File untarDir)
throws IOException {
if (untarDir.exists() && !FileUtil.fullyDelete(untarDir)) {