Skip to content

Commit 47131cd

Browse files
adoroszlaisteveloughran
authored andcommitted
HADOOP-17365. Contract test for renaming over existing file is too lenient (#2447)
Contributed by Attila Doroszlai. Change-Id: I21c29256b52449b7fea335704b3afa02e39c6a39
1 parent 8e4b1cd commit 47131cd

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/AbstractContractRenameTest.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,43 @@ public void testRenameFileOverExistingFile() throws Throwable {
104104
assertIsFile(destFile);
105105
boolean renameOverwritesDest = isSupported(RENAME_OVERWRITES_DEST);
106106
boolean renameReturnsFalseOnRenameDestExists =
107-
!isSupported(RENAME_RETURNS_FALSE_IF_DEST_EXISTS);
107+
isSupported(RENAME_RETURNS_FALSE_IF_DEST_EXISTS);
108+
assertFalse(RENAME_OVERWRITES_DEST + " and " +
109+
RENAME_RETURNS_FALSE_IF_DEST_EXISTS + " cannot be both supported",
110+
renameOverwritesDest && renameReturnsFalseOnRenameDestExists);
111+
String expectedTo = "expected rename(" + srcFile + ", " + destFile + ") to ";
112+
108113
boolean destUnchanged = true;
109114
try {
115+
// rename is rejected by returning 'false' or throwing an exception
110116
boolean renamed = rename(srcFile, destFile);
117+
destUnchanged = !renamed;
111118

112119
if (renameOverwritesDest) {
113-
// the filesystem supports rename(file, file2) by overwriting file2
114-
115-
assertTrue("Rename returned false", renamed);
116-
destUnchanged = false;
120+
assertTrue(expectedTo + "overwrite destination, but got false",
121+
renamed);
122+
} else if (renameReturnsFalseOnRenameDestExists) {
123+
assertFalse(expectedTo + "be rejected with false, but destination " +
124+
"was overwritten", renamed);
125+
} else if (renamed) {
126+
String destDirLS = generateAndLogErrorListing(srcFile, destFile);
127+
getLogger().error("dest dir {}", destDirLS);
128+
129+
fail(expectedTo + "be rejected with exception, but got overwritten");
117130
} else {
118-
// rename is rejected by returning 'false' or throwing an exception
119-
if (renamed && !renameReturnsFalseOnRenameDestExists) {
120-
//expected an exception
121-
String destDirLS = generateAndLogErrorListing(srcFile, destFile);
122-
getLogger().error("dest dir {}", destDirLS);
123-
fail("expected rename(" + srcFile + ", " + destFile + " ) to fail," +
124-
" but got success and destination of " + destDirLS);
125-
}
131+
fail(expectedTo + "be rejected with exception, but got false");
126132
}
127133
} catch (FileAlreadyExistsException e) {
134+
// rename(file, file2) should throw exception iff
135+
// it neither overwrites nor returns false
136+
assertFalse(expectedTo + "overwrite destination, but got exception",
137+
renameOverwritesDest);
138+
assertFalse(expectedTo + "be rejected with false, but got exception",
139+
renameReturnsFalseOnRenameDestExists);
140+
128141
handleExpectedException(e);
129142
}
143+
130144
// verify that the destination file is as expected based on the expected
131145
// outcome
132146
verifyFileContents(getFileSystem(), destFile,

0 commit comments

Comments
 (0)