|
@@ -18,22 +18,11 @@
|
|
|
|
|
|
package org.apache.hadoop.hdfs;
|
|
|
|
|
|
-import static org.junit.Assert.*;
|
|
|
-
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
-import java.io.PrintStream;
|
|
|
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
import org.apache.hadoop.fs.FileStatus;
|
|
|
-import org.apache.hadoop.fs.FsShell;
|
|
|
import org.apache.hadoop.fs.Path;
|
|
|
-import org.apache.hadoop.hdfs.DistributedFileSystem;
|
|
|
-import org.apache.hadoop.hdfs.HdfsConfiguration;
|
|
|
-import org.apache.hadoop.hdfs.tools.DFSAdmin;
|
|
|
-import org.apache.hadoop.util.StringUtils;
|
|
|
-import org.apache.hadoop.util.Tool;
|
|
|
-
|
|
|
import org.junit.After;
|
|
|
import org.junit.AfterClass;
|
|
|
import org.junit.Before;
|
|
@@ -89,136 +78,87 @@ public class TestSnapshotCommands {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void toolRun(Tool tool, String cmd, int retcode, String contain)
|
|
|
- throws Exception {
|
|
|
- String [] cmds = StringUtils.split(cmd, ' ');
|
|
|
- System.out.flush();
|
|
|
- System.err.flush();
|
|
|
- PrintStream origOut = System.out;
|
|
|
- PrintStream origErr = System.err;
|
|
|
- String output = null;
|
|
|
- int ret = 0;
|
|
|
- try {
|
|
|
- ByteArrayOutputStream bs = new ByteArrayOutputStream(1024);
|
|
|
- PrintStream out = new PrintStream(bs);
|
|
|
- System.setOut(out);
|
|
|
- System.setErr(out);
|
|
|
- ret = tool.run(cmds);
|
|
|
- System.out.flush();
|
|
|
- System.err.flush();
|
|
|
- out.close();
|
|
|
- output = bs.toString();
|
|
|
- } finally {
|
|
|
- System.setOut(origOut);
|
|
|
- System.setErr(origErr);
|
|
|
- }
|
|
|
- System.out.println("Output for command: " + cmd + " retcode: " + ret);
|
|
|
- if (output != null) {
|
|
|
- System.out.println(output);
|
|
|
- }
|
|
|
- assertEquals(retcode, ret);
|
|
|
- if (contain != null) {
|
|
|
- assertTrue(output.contains(contain));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void FsShellRun(String cmd, int retcode, String contain)
|
|
|
- throws Exception {
|
|
|
- FsShell shell = new FsShell(new Configuration(conf));
|
|
|
- toolRun(shell, cmd, retcode, contain);
|
|
|
- }
|
|
|
-
|
|
|
- private void DFSAdminRun(String cmd, int retcode, String contain)
|
|
|
- throws Exception {
|
|
|
- DFSAdmin admin = new DFSAdmin(new Configuration(conf));
|
|
|
- toolRun(admin, cmd, retcode, contain);
|
|
|
- }
|
|
|
-
|
|
|
- private void FsShellRun(String cmd) throws Exception {
|
|
|
- FsShellRun(cmd, 0, null);
|
|
|
- }
|
|
|
-
|
|
|
@Test
|
|
|
public void testAllowSnapshot() throws Exception {
|
|
|
// Idempotent test
|
|
|
- DFSAdminRun("-allowSnapshot /sub1", 0, "Allowing snaphot on /sub1 succeeded");
|
|
|
+ DFSTestUtil.DFSAdminRun("-allowSnapshot /sub1", 0, "Allowing snaphot on /sub1 succeeded", conf);
|
|
|
// allow normal dir success
|
|
|
- FsShellRun("-mkdir /sub2");
|
|
|
- DFSAdminRun("-allowSnapshot /sub2", 0, "Allowing snaphot on /sub2 succeeded");
|
|
|
+ DFSTestUtil.FsShellRun("-mkdir /sub2", conf);
|
|
|
+ DFSTestUtil.DFSAdminRun("-allowSnapshot /sub2", 0, "Allowing snaphot on /sub2 succeeded", conf);
|
|
|
// allow non-exists dir failed
|
|
|
- DFSAdminRun("-allowSnapshot /sub3", -1, null);
|
|
|
+ DFSTestUtil.DFSAdminRun("-allowSnapshot /sub3", -1, null, conf);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testCreateSnapshot() throws Exception {
|
|
|
// test createSnapshot
|
|
|
- FsShellRun("-createSnapshot /sub1 sn0", 0, "Created snapshot /sub1/.snapshot/sn0");
|
|
|
- FsShellRun("-createSnapshot /sub1 sn0", 1, "there is already a snapshot with the same name \"sn0\"");
|
|
|
- FsShellRun("-rmr /sub1/sub1sub2");
|
|
|
- FsShellRun("-mkdir /sub1/sub1sub3");
|
|
|
- FsShellRun("-createSnapshot /sub1 sn1", 0, "Created snapshot /sub1/.snapshot/sn1");
|
|
|
+ DFSTestUtil.FsShellRun("-createSnapshot /sub1 sn0", 0, "Created snapshot /sub1/.snapshot/sn0", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-createSnapshot /sub1 sn0", 1, "there is already a snapshot with the same name \"sn0\"", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-rmr /sub1/sub1sub2", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-mkdir /sub1/sub1sub3", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-createSnapshot /sub1 sn1", 0, "Created snapshot /sub1/.snapshot/sn1", conf);
|
|
|
// check snapshot contents
|
|
|
- FsShellRun("-ls /sub1", 0, "/sub1/sub1sub1");
|
|
|
- FsShellRun("-ls /sub1", 0, "/sub1/sub1sub3");
|
|
|
- FsShellRun("-ls /sub1/.snapshot", 0, "/sub1/.snapshot/sn0");
|
|
|
- FsShellRun("-ls /sub1/.snapshot", 0, "/sub1/.snapshot/sn1");
|
|
|
- FsShellRun("-ls /sub1/.snapshot/sn0", 0, "/sub1/.snapshot/sn0/sub1sub1");
|
|
|
- FsShellRun("-ls /sub1/.snapshot/sn0", 0, "/sub1/.snapshot/sn0/sub1sub2");
|
|
|
- FsShellRun("-ls /sub1/.snapshot/sn1", 0, "/sub1/.snapshot/sn1/sub1sub1");
|
|
|
- FsShellRun("-ls /sub1/.snapshot/sn1", 0, "/sub1/.snapshot/sn1/sub1sub3");
|
|
|
+ DFSTestUtil.FsShellRun("-ls /sub1", 0, "/sub1/sub1sub1", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-ls /sub1", 0, "/sub1/sub1sub3", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-ls /sub1/.snapshot", 0, "/sub1/.snapshot/sn0", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-ls /sub1/.snapshot", 0, "/sub1/.snapshot/sn1", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-ls /sub1/.snapshot/sn0", 0, "/sub1/.snapshot/sn0/sub1sub1", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-ls /sub1/.snapshot/sn0", 0, "/sub1/.snapshot/sn0/sub1sub2", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-ls /sub1/.snapshot/sn1", 0, "/sub1/.snapshot/sn1/sub1sub1", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-ls /sub1/.snapshot/sn1", 0, "/sub1/.snapshot/sn1/sub1sub3", conf);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testMkdirUsingReservedName() throws Exception {
|
|
|
// test can not create dir with reserved name: .snapshot
|
|
|
- FsShellRun("-ls /");
|
|
|
- FsShellRun("-mkdir /.snapshot", 1, "File exists");
|
|
|
- FsShellRun("-mkdir /sub1/.snapshot", 1, "File exists");
|
|
|
+ DFSTestUtil.FsShellRun("-ls /", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-mkdir /.snapshot", 1, "File exists", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-mkdir /sub1/.snapshot", 1, "File exists", conf);
|
|
|
// mkdir -p ignore reserved name check if dir already exists
|
|
|
- FsShellRun("-mkdir -p /sub1/.snapshot");
|
|
|
- FsShellRun("-mkdir -p /sub1/sub1sub1/.snapshot", 1, "mkdir: \".snapshot\" is a reserved name.");
|
|
|
+ DFSTestUtil.FsShellRun("-mkdir -p /sub1/.snapshot", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-mkdir -p /sub1/sub1sub1/.snapshot", 1, "mkdir: \".snapshot\" is a reserved name.", conf);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testRenameSnapshot() throws Exception {
|
|
|
- FsShellRun("-createSnapshot /sub1 sn.orig");
|
|
|
- FsShellRun("-renameSnapshot /sub1 sn.orig sn.rename");
|
|
|
- FsShellRun("-ls /sub1/.snapshot", 0, "/sub1/.snapshot/sn.rename");
|
|
|
- FsShellRun("-ls /sub1/.snapshot/sn.rename", 0, "/sub1/.snapshot/sn.rename/sub1sub1");
|
|
|
- FsShellRun("-ls /sub1/.snapshot/sn.rename", 0, "/sub1/.snapshot/sn.rename/sub1sub2");
|
|
|
+ DFSTestUtil.FsShellRun("-createSnapshot /sub1 sn.orig", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-renameSnapshot /sub1 sn.orig sn.rename", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-ls /sub1/.snapshot", 0, "/sub1/.snapshot/sn.rename", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-ls /sub1/.snapshot/sn.rename", 0, "/sub1/.snapshot/sn.rename/sub1sub1", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-ls /sub1/.snapshot/sn.rename", 0, "/sub1/.snapshot/sn.rename/sub1sub2", conf);
|
|
|
|
|
|
//try renaming from a non-existing snapshot
|
|
|
- FsShellRun("-renameSnapshot /sub1 sn.nonexist sn.rename", 1,
|
|
|
- "renameSnapshot: The snapshot sn.nonexist does not exist for directory /sub1");
|
|
|
+ DFSTestUtil.FsShellRun("-renameSnapshot /sub1 sn.nonexist sn.rename", 1,
|
|
|
+ "renameSnapshot: The snapshot sn.nonexist does not exist for directory /sub1", conf);
|
|
|
|
|
|
//try renaming to existing snapshots
|
|
|
- FsShellRun("-createSnapshot /sub1 sn.new");
|
|
|
- FsShellRun("-renameSnapshot /sub1 sn.new sn.rename", 1,
|
|
|
- "renameSnapshot: The snapshot sn.rename already exists for directory /sub1");
|
|
|
- FsShellRun("-renameSnapshot /sub1 sn.rename sn.new", 1,
|
|
|
- "renameSnapshot: The snapshot sn.new already exists for directory /sub1");
|
|
|
+ DFSTestUtil.FsShellRun("-createSnapshot /sub1 sn.new", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-renameSnapshot /sub1 sn.new sn.rename", 1,
|
|
|
+ "renameSnapshot: The snapshot sn.rename already exists for directory /sub1", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-renameSnapshot /sub1 sn.rename sn.new", 1,
|
|
|
+ "renameSnapshot: The snapshot sn.new already exists for directory /sub1", conf);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testDeleteSnapshot() throws Exception {
|
|
|
- FsShellRun("-createSnapshot /sub1 sn1");
|
|
|
- FsShellRun("-deleteSnapshot /sub1 sn1");
|
|
|
- FsShellRun("-deleteSnapshot /sub1 sn1", 1,
|
|
|
- "deleteSnapshot: Cannot delete snapshot sn1 from path /sub1: the snapshot does not exist.");
|
|
|
+ DFSTestUtil.FsShellRun("-createSnapshot /sub1 sn1", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-deleteSnapshot /sub1 sn1", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-deleteSnapshot /sub1 sn1", 1,
|
|
|
+ "deleteSnapshot: Cannot delete snapshot sn1 from path /sub1: the snapshot does not exist.", conf);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testDisallowSnapshot() throws Exception {
|
|
|
- FsShellRun("-createSnapshot /sub1 sn1");
|
|
|
+ DFSTestUtil.FsShellRun("-createSnapshot /sub1 sn1", conf);
|
|
|
// cannot delete snapshotable dir
|
|
|
- FsShellRun("-rmr /sub1", 1, "The directory /sub1 cannot be deleted since /sub1 is snapshottable and already has snapshots");
|
|
|
- DFSAdminRun("-disallowSnapshot /sub1", -1,
|
|
|
- "disallowSnapshot: The directory /sub1 has snapshot(s). Please redo the operation after removing all the snapshots.");
|
|
|
- FsShellRun("-deleteSnapshot /sub1 sn1");
|
|
|
- DFSAdminRun("-disallowSnapshot /sub1", 0, "Disallowing snaphot on /sub1 succeeded");
|
|
|
+ DFSTestUtil.FsShellRun("-rmr /sub1", 1, "The directory /sub1 cannot be deleted since /sub1 is snapshottable and already has snapshots", conf);
|
|
|
+ DFSTestUtil.DFSAdminRun("-disallowSnapshot /sub1", -1,
|
|
|
+ "disallowSnapshot: The directory /sub1 has snapshot(s). Please redo the operation after removing all the snapshots.", conf);
|
|
|
+ DFSTestUtil.FsShellRun("-deleteSnapshot /sub1 sn1", conf);
|
|
|
+ DFSTestUtil.DFSAdminRun("-disallowSnapshot /sub1", 0, "Disallowing snaphot on /sub1 succeeded", conf);
|
|
|
// Idempotent test
|
|
|
- DFSAdminRun("-disallowSnapshot /sub1", 0, "Disallowing snaphot on /sub1 succeeded");
|
|
|
+ DFSTestUtil.DFSAdminRun("-disallowSnapshot /sub1", 0, "Disallowing snaphot on /sub1 succeeded", conf);
|
|
|
// now it can be deleted
|
|
|
- FsShellRun("-rmr /sub1");
|
|
|
+ DFSTestUtil.FsShellRun("-rmr /sub1", conf);
|
|
|
}
|
|
|
}
|