HDFS-12981. renameSnapshot a Non-Existent snapshot to itself should throw error. Contributed by Kitti Nanasi.

This commit is contained in:
Xiao Chen 2018-05-07 15:34:22 -07:00
parent 3a43ac2851
commit 696a4be0da
3 changed files with 24 additions and 3 deletions

View File

@ -119,14 +119,14 @@ public ReadOnlyList<Snapshot> getSnapshotList() {
*/
public void renameSnapshot(String path, String oldName, String newName)
throws SnapshotException {
if (newName.equals(oldName)) {
return;
}
final int indexOfOld = searchSnapshot(DFSUtil.string2Bytes(oldName));
if (indexOfOld < 0) {
throw new SnapshotException("The snapshot " + oldName
+ " does not exist for directory " + path);
} else {
if (newName.equals(oldName)) {
return;
}
final byte[] newNameBytes = DFSUtil.string2Bytes(newName);
int indexOfNew = searchSnapshot(newNameBytes);
if (indexOfNew >= 0) {

View File

@ -155,6 +155,11 @@ public void testRenameSnapshot() throws Exception {
DFSTestUtil.FsShellRun("-renameSnapshot /sub1 sn.nonexist sn.rename", 1,
"renameSnapshot: The snapshot sn.nonexist does not exist for directory /sub1", conf);
//try renaming a non-existing snapshot to itself
DFSTestUtil.FsShellRun("-renameSnapshot /sub1 sn.nonexist sn.nonexist", 1,
"renameSnapshot: The snapshot sn.nonexist " +
"does not exist for directory /sub1", conf);
//try renaming to existing snapshots
DFSTestUtil.FsShellRun("-createSnapshot /sub1 sn.new", conf);
DFSTestUtil.FsShellRun("-renameSnapshot /sub1 sn.new sn.rename", 1,

View File

@ -184,6 +184,22 @@ public void testRenameNonExistingSnapshot() throws Exception {
hdfs.renameSnapshot(sub1, "wrongName", "s2");
}
/**
* Test rename a non-existing snapshot to itself.
*/
@Test (timeout=60000)
public void testRenameNonExistingSnapshotToItself() throws Exception {
DFSTestUtil.createFile(hdfs, file1, BLOCKSIZE, REPLICATION, seed);
// Create snapshot for sub1
SnapshotTestHelper.createSnapshot(hdfs, sub1, "s1");
exception.expect(SnapshotException.class);
String error = "The snapshot wrongName does not exist for directory "
+ sub1.toString();
exception.expectMessage(error);
hdfs.renameSnapshot(sub1, "wrongName", "wrongName");
}
/**
* Test rename a snapshot to another existing snapshot
*/