HDFS-9410. Some tests should always reset sysout and syserr. Contributed by Xiao Chen.

(cherry picked from commit cccf88480b)
This commit is contained in:
Walter Su 2015-11-13 14:21:14 +08:00
parent 875aec3177
commit 0f5aae5fd8
12 changed files with 179 additions and 123 deletions

View File

@ -1447,6 +1447,9 @@ Release 2.8.0 - UNRELEASED
HDFS-9396. Total files and directories on jmx and web UI on standby is HDFS-9396. Total files and directories on jmx and web UI on standby is
uninitialized. (kihwal) uninitialized. (kihwal)
HDFS-9410. Some tests should always reset sysout and syserr.
(Xiao Chen via waltersu4549)
Release 2.7.3 - UNRELEASED Release 2.7.3 - UNRELEASED
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -902,9 +902,9 @@ public class TestDFSShell {
assertEquals(dirs, in.nextLong()); assertEquals(dirs, in.nextLong());
assertEquals(files, in.nextLong()); assertEquals(files, in.nextLong());
} finally { } finally {
System.setOut(oldOut);
if (in!=null) in.close(); if (in!=null) in.close();
IOUtils.closeStream(out); IOUtils.closeStream(out);
System.setOut(oldOut);
System.out.println("results:\n" + results); System.out.println("results:\n" + results);
} }
} }
@ -1667,9 +1667,9 @@ public class TestDFSShell {
assertEquals(returnvalue, shell.run(new String[]{"-lsr", root})); assertEquals(returnvalue, shell.run(new String[]{"-lsr", root}));
results = bytes.toString(); results = bytes.toString();
} finally { } finally {
IOUtils.closeStream(out);
System.setOut(oldOut); System.setOut(oldOut);
System.setErr(oldErr); System.setErr(oldErr);
IOUtils.closeStream(out);
} }
System.out.println("results:\n" + results); System.out.println("results:\n" + results);
return results; return results;
@ -3057,15 +3057,16 @@ public class TestDFSShell {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos); PrintStream ps = new PrintStream(baos);
System.setErr(ps); System.setErr(ps);
try {
runCmd(shell, "-ls", "/.reserved");
assertEquals(0, baos.toString().length());
runCmd(shell, "-ls", "/.reserved"); runCmd(shell, "-ls", "/.reserved/raw/.reserved");
assertEquals(0, baos.toString().length()); assertTrue(baos.toString().contains("No such file or directory"));
} finally {
runCmd(shell, "-ls", "/.reserved/raw/.reserved"); System.setErr(syserr);
assertTrue(baos.toString().contains("No such file or directory")); cluster.shutdown();
}
System.setErr(syserr);
cluster.shutdown();
} }
@Test (timeout = 30000) @Test (timeout = 30000)
@ -3131,13 +3132,15 @@ public class TestDFSShell {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos); PrintStream ps = new PrintStream(baos);
System.setErr(ps); System.setErr(ps);
try {
FsShell shell = new FsShell(); FsShell shell = new FsShell();
shell.setConf(conf); shell.setConf(conf);
runCmd(shell, "-cp", src.toString(), "/.reserved"); runCmd(shell, "-cp", src.toString(), "/.reserved");
assertTrue(baos.toString().contains("Invalid path name /.reserved")); assertTrue(baos.toString().contains("Invalid path name /.reserved"));
System.setErr(syserr); } finally {
cluster.shutdown(); System.setErr(syserr);
cluster.shutdown();
}
} }
@Test (timeout = 30000) @Test (timeout = 30000)
@ -3152,13 +3155,15 @@ public class TestDFSShell {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos); PrintStream ps = new PrintStream(baos);
System.setErr(ps); System.setErr(ps);
try {
FsShell shell = new FsShell(); FsShell shell = new FsShell();
shell.setConf(conf); shell.setConf(conf);
runCmd(shell, "-chmod", "777", "/.reserved"); runCmd(shell, "-chmod", "777", "/.reserved");
assertTrue(baos.toString().contains("Invalid path name /.reserved")); assertTrue(baos.toString().contains("Invalid path name /.reserved"));
System.setErr(syserr); } finally {
cluster.shutdown(); System.setErr(syserr);
cluster.shutdown();
}
} }
@Test (timeout = 30000) @Test (timeout = 30000)
@ -3173,13 +3178,15 @@ public class TestDFSShell {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos); PrintStream ps = new PrintStream(baos);
System.setErr(ps); System.setErr(ps);
try {
FsShell shell = new FsShell(); FsShell shell = new FsShell();
shell.setConf(conf); shell.setConf(conf);
runCmd(shell, "-chown", "user1", "/.reserved"); runCmd(shell, "-chown", "user1", "/.reserved");
assertTrue(baos.toString().contains("Invalid path name /.reserved")); assertTrue(baos.toString().contains("Invalid path name /.reserved"));
System.setErr(syserr); } finally {
cluster.shutdown(); System.setErr(syserr);
cluster.shutdown();
}
} }
@Test (timeout = 30000) @Test (timeout = 30000)

View File

@ -97,10 +97,14 @@ public class TestFsShellPermission {
ByteArrayOutputStream baout = new ByteArrayOutputStream(); ByteArrayOutputStream baout = new ByteArrayOutputStream();
PrintStream out = new PrintStream(baout, true); PrintStream out = new PrintStream(baout, true);
PrintStream old = System.out; PrintStream old = System.out;
System.setOut(out); int ret;
int ret = shell.run(args); try {
out.close(); System.setOut(out);
System.setOut(old); ret = shell.run(args);
out.close();
} finally {
System.setOut(old);
}
return String.valueOf(ret); return String.valueOf(ret);
} }

View File

@ -990,13 +990,18 @@ public class TestQuota {
conf.set(FS_DEFAULT_NAME_KEY, "hdfs://127.0.0.1:8020"); conf.set(FS_DEFAULT_NAME_KEY, "hdfs://127.0.0.1:8020");
DFSAdmin admin = new DFSAdmin(conf); DFSAdmin admin = new DFSAdmin(conf);
ByteArrayOutputStream err = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream();
System.setErr(new PrintStream(err)); PrintStream oldErr = System.err;
String[] args = { "-setSpaceQuota", "100", "-storageType", "COLD", try {
"/testDir" }; System.setErr(new PrintStream(err));
admin.run(args); String[] args =
String errOutput = new String(err.toByteArray(), Charsets.UTF_8); { "-setSpaceQuota", "100", "-storageType", "COLD", "/testDir" };
assertTrue(errOutput.contains(StorageType.getTypesSupportingQuota() admin.run(args);
.toString())); String errOutput = new String(err.toByteArray(), Charsets.UTF_8);
assertTrue(
errOutput.contains(StorageType.getTypesSupportingQuota().toString()));
} finally {
System.setErr(oldErr);
}
} }
/** /**

View File

@ -232,12 +232,14 @@ public class TestClusterId {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream stdErr = new PrintStream(baos); PrintStream stdErr = new PrintStream(baos);
System.setErr(stdErr); System.setErr(stdErr);
try {
NameNode.createNameNode(argv, config);
NameNode.createNameNode(argv, config); // Check if usage is printed
assertTrue(baos.toString("UTF-8").contains("Usage: hdfs namenode"));
// Check if usage is printed } finally {
assertTrue(baos.toString("UTF-8").contains("Usage: hdfs namenode")); System.setErr(origErr);
System.setErr(origErr); }
// check if the version file does not exists. // check if the version file does not exists.
File version = new File(hdfsDir, "current/VERSION"); File version = new File(hdfsDir, "current/VERSION");
@ -258,12 +260,14 @@ public class TestClusterId {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream stdErr = new PrintStream(baos); PrintStream stdErr = new PrintStream(baos);
System.setErr(stdErr); System.setErr(stdErr);
try {
NameNode.createNameNode(argv, config);
NameNode.createNameNode(argv, config); // Check if usage is printed
assertTrue(baos.toString("UTF-8").contains("Usage: hdfs namenode"));
// Check if usage is printed } finally {
assertTrue(baos.toString("UTF-8").contains("Usage: hdfs namenode")); System.setErr(origErr);
System.setErr(origErr); }
// check if the version file does not exists. // check if the version file does not exists.
File version = new File(hdfsDir, "current/VERSION"); File version = new File(hdfsDir, "current/VERSION");
@ -285,12 +289,14 @@ public class TestClusterId {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream stdErr = new PrintStream(baos); PrintStream stdErr = new PrintStream(baos);
System.setErr(stdErr); System.setErr(stdErr);
try {
NameNode.createNameNode(argv, config);
NameNode.createNameNode(argv, config); // Check if usage is printed
assertTrue(baos.toString("UTF-8").contains("Usage: hdfs namenode"));
// Check if usage is printed } finally {
assertTrue(baos.toString("UTF-8").contains("Usage: hdfs namenode")); System.setErr(origErr);
System.setErr(origErr); }
// check if the version file does not exists. // check if the version file does not exists.
File version = new File(hdfsDir, "current/VERSION"); File version = new File(hdfsDir, "current/VERSION");

View File

@ -72,18 +72,21 @@ public class TestMetadataVersionOutput {
final PrintStream origOut = System.out; final PrintStream origOut = System.out;
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintStream stdOut = new PrintStream(baos); final PrintStream stdOut = new PrintStream(baos);
System.setOut(stdOut);
try { try {
NameNode.createNameNode(new String[] { "-metadataVersion" }, conf); System.setOut(stdOut);
} catch (Exception e) { try {
assertExceptionContains("ExitException", e); NameNode.createNameNode(new String[] { "-metadataVersion" }, conf);
} } catch (Exception e) {
assertExceptionContains("ExitException", e);
}
/* Check if meta data version is printed correctly. */ /* Check if meta data version is printed correctly. */
final String verNumStr = HdfsServerConstants.NAMENODE_LAYOUT_VERSION + ""; final String verNumStr = HdfsServerConstants.NAMENODE_LAYOUT_VERSION + "";
assertTrue(baos.toString("UTF-8"). assertTrue(baos.toString("UTF-8").
contains("HDFS Image Version: " + verNumStr)); contains("HDFS Image Version: " + verNumStr));
assertTrue(baos.toString("UTF-8"). assertTrue(baos.toString("UTF-8").
contains("Software format version: " + verNumStr)); contains("Software format version: " + verNumStr));
System.setOut(origOut); } finally {
System.setOut(origOut);
}
} }
} }

View File

@ -1044,25 +1044,32 @@ public class TestSnapshotDeletion {
public void testDeleteSnapshotCommandWithIllegalArguments() throws Exception { public void testDeleteSnapshotCommandWithIllegalArguments() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream psOut = new PrintStream(out); PrintStream psOut = new PrintStream(out);
System.setOut(psOut); PrintStream oldOut = System.out;
System.setErr(psOut); PrintStream oldErr = System.err;
FsShell shell = new FsShell(); try {
shell.setConf(conf); System.setOut(psOut);
System.setErr(psOut);
String[] argv1 = {"-deleteSnapshot", "/tmp"}; FsShell shell = new FsShell();
int val = shell.run(argv1); shell.setConf(conf);
assertTrue(val == -1);
assertTrue(out.toString().contains( String[] argv1 = { "-deleteSnapshot", "/tmp" };
argv1[0] + ": Incorrect number of arguments.")); int val = shell.run(argv1);
out.reset(); assertTrue(val == -1);
assertTrue(out.toString()
String[] argv2 = {"-deleteSnapshot", "/tmp", "s1", "s2"}; .contains(argv1[0] + ": Incorrect number of arguments."));
val = shell.run(argv2); out.reset();
assertTrue(val == -1);
assertTrue(out.toString().contains( String[] argv2 = { "-deleteSnapshot", "/tmp", "s1", "s2" };
argv2[0] + ": Incorrect number of arguments.")); val = shell.run(argv2);
psOut.close(); assertTrue(val == -1);
out.close(); assertTrue(out.toString()
.contains(argv2[0] + ": Incorrect number of arguments."));
psOut.close();
out.close();
} finally {
System.setOut(oldOut);
System.setErr(oldErr);
}
} }
/* /*

View File

@ -237,24 +237,31 @@ public class TestSnapshotRename {
public void testRenameSnapshotCommandWithIllegalArguments() throws Exception { public void testRenameSnapshotCommandWithIllegalArguments() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream psOut = new PrintStream(out); PrintStream psOut = new PrintStream(out);
System.setOut(psOut); PrintStream oldOut = System.out;
System.setErr(psOut); PrintStream oldErr = System.err;
FsShell shell = new FsShell(); try {
shell.setConf(conf); System.setOut(psOut);
System.setErr(psOut);
String[] argv1 = {"-renameSnapshot", "/tmp", "s1"}; FsShell shell = new FsShell();
int val = shell.run(argv1); shell.setConf(conf);
assertTrue(val == -1);
assertTrue(out.toString().contains( String[] argv1 = { "-renameSnapshot", "/tmp", "s1" };
argv1[0] + ": Incorrect number of arguments.")); int val = shell.run(argv1);
out.reset(); assertTrue(val == -1);
assertTrue(out.toString()
String[] argv2 = {"-renameSnapshot", "/tmp", "s1", "s2", "s3"}; .contains(argv1[0] + ": Incorrect number of arguments."));
val = shell.run(argv2); out.reset();
assertTrue(val == -1);
assertTrue(out.toString().contains( String[] argv2 = { "-renameSnapshot", "/tmp", "s1", "s2", "s3" };
argv2[0] + ": Incorrect number of arguments.")); val = shell.run(argv2);
psOut.close(); assertTrue(val == -1);
out.close(); assertTrue(out.toString()
.contains(argv2[0] + ": Incorrect number of arguments."));
psOut.close();
out.close();
} finally {
System.setOut(oldOut);
System.setErr(oldErr);
}
} }
} }

View File

@ -43,8 +43,8 @@ public class TestDFSAdminWithHA {
private MiniQJMHACluster cluster; private MiniQJMHACluster cluster;
private Configuration conf; private Configuration conf;
private DFSAdmin admin; private DFSAdmin admin;
private PrintStream originOut; private static final PrintStream oldOut = System.out;
private PrintStream originErr; private static final PrintStream oldErr = System.err;
private static final String NSID = "ns1"; private static final String NSID = "ns1";
private static String newLine = System.getProperty("line.separator"); private static String newLine = System.getProperty("line.separator");
@ -89,18 +89,19 @@ public class TestDFSAdminWithHA {
admin.setConf(conf); admin.setConf(conf);
assertTrue(HAUtil.isHAEnabled(conf, "ns1")); assertTrue(HAUtil.isHAEnabled(conf, "ns1"));
originOut = System.out;
originErr = System.err;
System.setOut(new PrintStream(out)); System.setOut(new PrintStream(out));
System.setErr(new PrintStream(err)); System.setErr(new PrintStream(err));
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
System.out.flush(); try {
System.err.flush(); System.out.flush();
System.setOut(originOut); System.err.flush();
System.setErr(originErr); } finally {
System.setOut(oldOut);
System.setErr(oldErr);
}
if (admin != null) { if (admin != null) {
admin.close(); admin.close();
} }

View File

@ -137,14 +137,19 @@ public class TestJMXGet {
String pattern = "List of all the available keys:"; String pattern = "List of all the available keys:";
PipedOutputStream pipeOut = new PipedOutputStream(); PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut); PipedInputStream pipeIn = new PipedInputStream(pipeOut);
PrintStream oldErr = System.err;
System.setErr(new PrintStream(pipeOut)); System.setErr(new PrintStream(pipeOut));
jmx.printAllValues(); try {
if ((size = pipeIn.available()) != 0) { jmx.printAllValues();
bytes = new byte[size]; if ((size = pipeIn.available()) != 0) {
pipeIn.read(bytes, 0, bytes.length); bytes = new byte[size];
pipeIn.read(bytes, 0, bytes.length);
}
pipeOut.close();
pipeIn.close();
} finally {
System.setErr(oldErr);
} }
pipeOut.close();
pipeIn.close();
return bytes != null ? new String(bytes).contains(pattern) : false; return bytes != null ? new String(bytes).contains(pattern) : false;
} }

View File

@ -103,6 +103,8 @@ public class TestTools {
private void checkOutput(String[] args, String pattern, PrintStream out, private void checkOutput(String[] args, String pattern, PrintStream out,
Class<?> clazz) { Class<?> clazz) {
ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
PrintStream oldOut = System.out;
PrintStream oldErr = System.err;
try { try {
PipedOutputStream pipeOut = new PipedOutputStream(); PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut, PIPE_BUFFER_SIZE); PipedInputStream pipeIn = new PipedInputStream(pipeOut, PIPE_BUFFER_SIZE);
@ -125,6 +127,9 @@ public class TestTools {
assertTrue(new String(outBytes.toByteArray()).contains(pattern)); assertTrue(new String(outBytes.toByteArray()).contains(pattern));
} catch (Exception ex) { } catch (Exception ex) {
fail("checkOutput error " + ex); fail("checkOutput error " + ex);
} finally {
System.setOut(oldOut);
System.setErr(oldErr);
} }
} }

View File

@ -44,9 +44,12 @@ public class TestTraceAdmin {
try { try {
ret = trace.run(cmd); ret = trace.run(cmd);
} finally { } finally {
System.out.flush(); try {
System.setOut(oldStdout); System.out.flush();
System.setErr(oldStderr); } finally {
System.setOut(oldStdout);
System.setErr(oldStderr);
}
} }
return "ret:" + ret + ", " + baos.toString(); return "ret:" + ret + ", " + baos.toString();
} }