HADOOP-17365. Contract test for renaming over existing file is too lenient (#2447)

Contributed by Attila Doroszlai.

Change-Id: I21c29256b52449b7fea335704b3afa02e39c6a39
This commit is contained in:
Doroszlai, Attila 2020-11-11 22:20:09 +01:00 committed by Steve Loughran
parent 8e4b1cd192
commit 47131cdf7c
No known key found for this signature in database
GPG Key ID: D22CF846DBB162A0
1 changed files with 26 additions and 12 deletions

View File

@ -104,29 +104,43 @@ public abstract class AbstractContractRenameTest extends
assertIsFile(destFile); assertIsFile(destFile);
boolean renameOverwritesDest = isSupported(RENAME_OVERWRITES_DEST); boolean renameOverwritesDest = isSupported(RENAME_OVERWRITES_DEST);
boolean renameReturnsFalseOnRenameDestExists = boolean renameReturnsFalseOnRenameDestExists =
!isSupported(RENAME_RETURNS_FALSE_IF_DEST_EXISTS); isSupported(RENAME_RETURNS_FALSE_IF_DEST_EXISTS);
assertFalse(RENAME_OVERWRITES_DEST + " and " +
RENAME_RETURNS_FALSE_IF_DEST_EXISTS + " cannot be both supported",
renameOverwritesDest && renameReturnsFalseOnRenameDestExists);
String expectedTo = "expected rename(" + srcFile + ", " + destFile + ") to ";
boolean destUnchanged = true; boolean destUnchanged = true;
try { try {
// rename is rejected by returning 'false' or throwing an exception
boolean renamed = rename(srcFile, destFile); boolean renamed = rename(srcFile, destFile);
destUnchanged = !renamed;
if (renameOverwritesDest) { if (renameOverwritesDest) {
// the filesystem supports rename(file, file2) by overwriting file2 assertTrue(expectedTo + "overwrite destination, but got false",
renamed);
assertTrue("Rename returned false", renamed); } else if (renameReturnsFalseOnRenameDestExists) {
destUnchanged = false; assertFalse(expectedTo + "be rejected with false, but destination " +
} else { "was overwritten", renamed);
// rename is rejected by returning 'false' or throwing an exception } else if (renamed) {
if (renamed && !renameReturnsFalseOnRenameDestExists) {
//expected an exception
String destDirLS = generateAndLogErrorListing(srcFile, destFile); String destDirLS = generateAndLogErrorListing(srcFile, destFile);
getLogger().error("dest dir {}", destDirLS); getLogger().error("dest dir {}", destDirLS);
fail("expected rename(" + srcFile + ", " + destFile + " ) to fail," +
" but got success and destination of " + destDirLS); fail(expectedTo + "be rejected with exception, but got overwritten");
} } else {
fail(expectedTo + "be rejected with exception, but got false");
} }
} catch (FileAlreadyExistsException e) { } catch (FileAlreadyExistsException e) {
// rename(file, file2) should throw exception iff
// it neither overwrites nor returns false
assertFalse(expectedTo + "overwrite destination, but got exception",
renameOverwritesDest);
assertFalse(expectedTo + "be rejected with false, but got exception",
renameReturnsFalseOnRenameDestExists);
handleExpectedException(e); handleExpectedException(e);
} }
// verify that the destination file is as expected based on the expected // verify that the destination file is as expected based on the expected
// outcome // outcome
verifyFileContents(getFileSystem(), destFile, verifyFileContents(getFileSystem(), destFile,