HDFS-10972. Add unit test for HDFS command 'dfsadmin -getDatanodeInfo'. Contributed by Xiaobing Zhou

This commit is contained in:
Mingliang Liu 2016-10-10 17:46:31 -07:00
parent 67d8301e55
commit 5a39a7560a
1 changed files with 110 additions and 14 deletions

View File

@ -23,10 +23,12 @@ import com.google.common.collect.Lists;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.ReconfigurationUtil; import org.apache.hadoop.conf.ReconfigurationUtil;
import org.apache.hadoop.hdfs.DFSConfigKeys; import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdfs.HdfsConfiguration;
import org.apache.hadoop.hdfs.MiniDFSCluster; import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.hdfs.server.common.Storage; import org.apache.hadoop.hdfs.server.common.Storage;
import org.apache.hadoop.hdfs.server.datanode.DataNode; import org.apache.hadoop.hdfs.server.datanode.DataNode;
import org.apache.hadoop.hdfs.server.datanode.StorageLocation; import org.apache.hadoop.hdfs.server.datanode.StorageLocation;
import org.apache.hadoop.util.ToolRunner;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -56,6 +58,10 @@ public class TestDFSAdmin {
private MiniDFSCluster cluster; private MiniDFSCluster cluster;
private DFSAdmin admin; private DFSAdmin admin;
private DataNode datanode; private DataNode datanode;
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
private final ByteArrayOutputStream err = new ByteArrayOutputStream();
private static final PrintStream OLD_OUT = System.out;
private static final PrintStream OLD_ERR = System.err;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -65,12 +71,32 @@ public class TestDFSAdmin {
admin = new DFSAdmin(); admin = new DFSAdmin();
} }
private void redirectStream() {
System.setOut(new PrintStream(out));
System.setErr(new PrintStream(err));
}
private void resetStream() {
out.reset();
err.reset();
}
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
try {
System.out.flush();
System.err.flush();
} finally {
System.setOut(OLD_OUT);
System.setErr(OLD_ERR);
}
if (cluster != null) { if (cluster != null) {
cluster.shutdown(); cluster.shutdown();
cluster = null; cluster = null;
} }
resetStream();
} }
private void restartCluster() throws IOException { private void restartCluster() throws IOException {
@ -85,18 +111,91 @@ public class TestDFSAdmin {
private List<String> getReconfigureStatus(String nodeType, String address) private List<String> getReconfigureStatus(String nodeType, String address)
throws IOException { throws IOException {
ByteArrayOutputStream bufOut = new ByteArrayOutputStream(); ByteArrayOutputStream bufOut = new ByteArrayOutputStream();
PrintStream out = new PrintStream(bufOut); PrintStream outStream = new PrintStream(bufOut);
ByteArrayOutputStream bufErr = new ByteArrayOutputStream(); ByteArrayOutputStream bufErr = new ByteArrayOutputStream();
PrintStream err = new PrintStream(bufErr); PrintStream errStream = new PrintStream(bufErr);
admin.getReconfigurationStatus(nodeType, address, out, err); admin.getReconfigurationStatus(nodeType, address, outStream, errStream);
Scanner scanner = new Scanner(bufOut.toString());
List<String> outputs = Lists.newArrayList(); List<String> outputs = Lists.newArrayList();
while (scanner.hasNextLine()) { scanIntoList(bufOut, outputs);
outputs.add(scanner.nextLine());
}
return outputs; return outputs;
} }
private static void scanIntoList(
final ByteArrayOutputStream baos,
final List<String> list) {
final Scanner scanner = new Scanner(baos.toString());
while (scanner.hasNextLine()) {
list.add(scanner.nextLine());
}
scanner.close();
}
@Test(timeout = 30000)
public void testGetDatanodeInfo() throws Exception {
redirectStream();
final Configuration dfsConf = new HdfsConfiguration();
final int numDn = 2;
/* init cluster */
try (MiniDFSCluster miniCluster = new MiniDFSCluster.Builder(dfsConf)
.numDataNodes(numDn).build()) {
miniCluster.waitActive();
assertEquals(numDn, miniCluster.getDataNodes().size());
final DFSAdmin dfsAdmin = new DFSAdmin(dfsConf);
/* init reused vars */
List<String> outs = null;
int ret;
/**
* test erroneous run
*/
resetStream();
outs = Lists.newArrayList();
/* invoke getDatanodeInfo */
ret = ToolRunner.run(dfsAdmin,
new String[] { "-getDatanodeInfo", "128.0.0.1:1234" });
/* collect outputs */
scanIntoList(out, outs);
/* verify results */
assertEquals(-1, ret);
assertTrue("Unexpected getDatanodeInfo stdout", outs.isEmpty());
/**
* test normal run
*/
for (int i = 0; i < numDn; i++) {
resetStream();
final DataNode dn = miniCluster.getDataNodes().get(i);
/* invoke getDatanodeInfo */
final String addr = String.format("%s:%d",
dn.getXferAddress().getHostString(), dn.getIpcPort());
ret = ToolRunner.run(dfsAdmin,
new String[] { "-getDatanodeInfo", addr });
/* collect outputs */
outs = Lists.newArrayList();
scanIntoList(out, outs);
/* verify results */
assertEquals(0, ret);
assertEquals(
"One line per DataNode like: Uptime: XXX, Software version: x.y.z,"
+ " Config version: core-x.y.z,hdfs-x",
1, outs.size());
assertThat(outs.get(0),
is(allOf(containsString("Uptime:"),
containsString("Software version"),
containsString("Config version"))));
}
}
}
/** /**
* Test reconfiguration and check the status outputs. * Test reconfiguration and check the status outputs.
* @param expectedSuccuss set true if the reconfiguration task should success. * @param expectedSuccuss set true if the reconfiguration task should success.
@ -189,15 +288,12 @@ public class TestDFSAdmin {
String nodeType, String address) String nodeType, String address)
throws IOException { throws IOException {
ByteArrayOutputStream bufOut = new ByteArrayOutputStream(); ByteArrayOutputStream bufOut = new ByteArrayOutputStream();
PrintStream out = new PrintStream(bufOut); PrintStream outStream = new PrintStream(bufOut);
ByteArrayOutputStream bufErr = new ByteArrayOutputStream(); ByteArrayOutputStream bufErr = new ByteArrayOutputStream();
PrintStream err = new PrintStream(bufErr); PrintStream errStream = new PrintStream(bufErr);
admin.getReconfigurableProperties(nodeType, address, out, err); admin.getReconfigurableProperties(nodeType, address, outStream, errStream);
Scanner scanner = new Scanner(bufOut.toString());
List<String> outputs = Lists.newArrayList(); List<String> outputs = Lists.newArrayList();
while (scanner.hasNextLine()) { scanIntoList(bufOut, outputs);
outputs.add(scanner.nextLine());
}
return outputs; return outputs;
} }