HDDS-31. Fix TestSCMCli. Contributed by Lokesh Jain.

This commit is contained in:
Anu Engineer 2018-05-10 12:43:13 -07:00
parent f8b540049d
commit 48d0b54849
4 changed files with 74 additions and 48 deletions

View File

@ -18,6 +18,10 @@
package org.apache.hadoop.hdds.scm.container.common.helpers; package org.apache.hadoop.hdds.scm.container.common.helpers;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder;
@ -25,6 +29,7 @@ import org.apache.hadoop.hdds.scm.container.ContainerID;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos; import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
import org.apache.hadoop.util.Time; import org.apache.hadoop.util.Time;
import java.io.IOException;
import java.util.Comparator; import java.util.Comparator;
/** /**
@ -32,6 +37,17 @@ import java.util.Comparator;
*/ */
public class ContainerInfo public class ContainerInfo
implements Comparator<ContainerInfo>, Comparable<ContainerInfo> { implements Comparator<ContainerInfo>, Comparable<ContainerInfo> {
private static final ObjectWriter WRITER;
static {
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper
.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
WRITER = mapper.writer();
}
private HddsProtos.LifeCycleState state; private HddsProtos.LifeCycleState state;
private Pipeline pipeline; private Pipeline pipeline;
// Bytes allocated by SCM for clients. // Bytes allocated by SCM for clients.
@ -254,6 +270,16 @@ public class ContainerInfo
return this.compare(this, o); return this.compare(this, o);
} }
/**
* Returns a JSON string of this object.
*
* @return String - json string
* @throws IOException
*/
public String toJsonString() throws IOException {
return WRITER.writeValueAsString(this);
}
/** /**
* Builder class for ContainerInfo. * Builder class for ContainerInfo.
*/ */

View File

@ -44,6 +44,10 @@ public class CreateContainerHandler extends OzoneCommandHandler {
@Override @Override
public void execute(CommandLine cmd) throws IOException { public void execute(CommandLine cmd) throws IOException {
if (cmd.hasOption(HELP_OP)) {
displayHelp();
}
if (!cmd.hasOption(CONTAINER_CREATE)) { if (!cmd.hasOption(CONTAINER_CREATE)) {
throw new IOException("Expecting container create"); throw new IOException("Expecting container create");
} }
@ -57,7 +61,7 @@ public class CreateContainerHandler extends OzoneCommandHandler {
public void displayHelp() { public void displayHelp() {
Options options = new Options(); Options options = new Options();
HelpFormatter helpFormatter = new HelpFormatter(); HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.printHelp(CMD_WIDTH, "hdfs scm -container -create <option>", helpFormatter.printHelp(CMD_WIDTH, "hdfs scm -container -create",
"where <option> is", options, ""); null, options, null);
} }
} }

View File

@ -24,7 +24,6 @@ import org.apache.commons.cli.Options;
import org.apache.hadoop.hdds.scm.cli.OzoneCommandHandler; import org.apache.hadoop.hdds.scm.cli.OzoneCommandHandler;
import org.apache.hadoop.hdds.scm.client.ScmClient; import org.apache.hadoop.hdds.scm.client.ScmClient;
import org.apache.hadoop.hdds.scm.container.common.helpers.ContainerInfo; import org.apache.hadoop.hdds.scm.container.common.helpers.ContainerInfo;
import org.apache.hadoop.hdds.scm.container.common.helpers.Pipeline;
import org.apache.hadoop.ozone.web.utils.JsonUtils; import org.apache.hadoop.ozone.web.utils.JsonUtils;
import java.io.IOException; import java.io.IOException;
@ -87,14 +86,15 @@ public class ListContainerHandler extends OzoneCommandHandler {
// Output data list // Output data list
for (ContainerInfo container : containerList) { for (ContainerInfo container : containerList) {
outputContainerPipeline(container.getPipeline()); outputContainerInfo(container);
} }
} }
private void outputContainerPipeline(Pipeline pipeline) throws IOException { private void outputContainerInfo(ContainerInfo containerInfo)
throws IOException {
// Print container report info. // Print container report info.
logOut("%s", JsonUtils.toJsonStringWithDefaultPrettyPrinter( logOut("%s", JsonUtils.toJsonStringWithDefaultPrettyPrinter(
pipeline.toJsonString())); containerInfo.toJsonString()));
} }
@Override @Override

View File

@ -55,7 +55,6 @@ import static org.apache.hadoop.hdds.scm.cli.ResultCode.EXECUTION_ERROR;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
/** /**
* This class tests the CLI of SCM. * This class tests the CLI of SCM.
@ -126,21 +125,12 @@ public class TestSCMCli {
@Test @Test
public void testCreateContainer() throws Exception { public void testCreateContainer() throws Exception {
long containerID = ContainerTestHelper.getTestContainerID(); ByteArrayOutputStream testContent = new ByteArrayOutputStream();
try { PrintStream testPrintOut = new PrintStream(testContent);
scm.getClientProtocolServer().getContainer(containerID); System.setOut(testPrintOut);
fail("should not be able to get the container"); String[] args = {"-container", "-create"};
} catch (IOException ioe) {
assertTrue(ioe.getMessage().contains(
"Specified key does not exist. key : " + containerID));
}
String[] args = {"-container", "-create", "-c",
Long.toString(containerID)};
assertEquals(ResultCode.SUCCESS, cli.run(args)); assertEquals(ResultCode.SUCCESS, cli.run(args));
ContainerInfo container = scm.getClientProtocolServer() assertEquals("", testContent.toString());
.getContainer(containerID);
assertNotNull(container);
assertEquals(containerID, container.containerID());
} }
private boolean containerExist(long containerID) { private boolean containerExist(long containerID) {
@ -215,9 +205,9 @@ public class TestSCMCli {
ContainerInfo emptyContainer = containerOperationClient ContainerInfo emptyContainer = containerOperationClient
.createContainer(xceiverClientManager.getType(), .createContainer(xceiverClientManager.getType(),
HddsProtos.ReplicationFactor.ONE, containerOwner); HddsProtos.ReplicationFactor.ONE, containerOwner);
containerOperationClient.closeContainer(container.getContainerID(), containerOperationClient.closeContainer(emptyContainer.getContainerID(),
container.getPipeline()); container.getPipeline());
Assert.assertTrue(containerExist(container.getContainerID())); Assert.assertTrue(containerExist(emptyContainer.getContainerID()));
// Successfully delete an empty container. // Successfully delete an empty container.
delCmd = new String[] {"-container", "-delete", "-c", delCmd = new String[] {"-container", "-delete", "-c",
@ -252,7 +242,7 @@ public class TestSCMCli {
DatanodeDetails datanodeDetails = cluster.getHddsDatanodes().get(0) DatanodeDetails datanodeDetails = cluster.getHddsDatanodes().get(0)
.getDatanodeDetails(); .getDatanodeDetails();
String formatStr = String formatStr =
"Container Name: %s\n" + "Container id: %s\n" +
"Container State: %s\n" + "Container State: %s\n" +
"Container DB Path: %s\n" + "Container DB Path: %s\n" +
"Container Path: %s\n" + "Container Path: %s\n" +
@ -261,7 +251,7 @@ public class TestSCMCli {
"Datanodes: [%s]\n"; "Datanodes: [%s]\n";
String formatStrWithHash = String formatStrWithHash =
"Container Name: %s\n" + "Container id: %s\n" +
"Container State: %s\n" + "Container State: %s\n" +
"Container Hash: %s\n" + "Container Hash: %s\n" +
"Container DB Path: %s\n" + "Container DB Path: %s\n" +
@ -271,8 +261,9 @@ public class TestSCMCli {
"Datanodes: [%s]\n"; "Datanodes: [%s]\n";
// Test a non-exist container // Test a non-exist container
String cname = "nonExistContainer"; String containerID =
String[] info = {"-container", "-info", cname}; Long.toString(ContainerTestHelper.getTestContainerID());
String[] info = { "-container", "-info", containerID };
int exitCode = runCommandAndGetOutput(info, null, null); int exitCode = runCommandAndGetOutput(info, null, null);
assertEquals("Expected Execution Error, Did not find that.", assertEquals("Expected Execution Error, Did not find that.",
EXECUTION_ERROR, exitCode); EXECUTION_ERROR, exitCode);
@ -286,14 +277,16 @@ public class TestSCMCli {
readContainer(container.getContainerID(), readContainer(container.getContainerID(),
container.getPipeline()), conf); container.getPipeline()), conf);
info = new String[]{"-container", "-info", "-c", cname}; info = new String[] { "-container", "-info", "-c",
Long.toString(container.getContainerID()) };
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
exitCode = runCommandAndGetOutput(info, out, null); exitCode = runCommandAndGetOutput(info, out, null);
assertEquals("Expected Success, did not find it.", ResultCode.SUCCESS, assertEquals("Expected Success, did not find it.", ResultCode.SUCCESS,
exitCode); exitCode);
String openStatus = data.isOpen() ? "OPEN" : "CLOSED"; String openStatus = data.isOpen() ? "OPEN" : "CLOSED";
String expected = String.format(formatStr, cname, openStatus, String expected =
String.format(formatStr, container.getContainerID(), openStatus,
data.getDBPath(), data.getContainerPath(), "", data.getDBPath(), data.getContainerPath(), "",
datanodeDetails.getHostName(), datanodeDetails.getHostName()); datanodeDetails.getHostName(), datanodeDetails.getHostName());
assertEquals(expected, out.toString()); assertEquals(expected, out.toString());
@ -307,14 +300,16 @@ public class TestSCMCli {
data = ContainerData data = ContainerData
.getFromProtBuf(containerOperationClient.readContainer( .getFromProtBuf(containerOperationClient.readContainer(
container.getContainerID(), container.getPipeline()), conf); container.getContainerID(), container.getPipeline()), conf);
KeyUtils.getDB(data, conf).put(cname.getBytes(), "someKey".getBytes()); KeyUtils.getDB(data, conf)
.put(containerID.getBytes(), "someKey".getBytes());
info = new String[]{"-container", "-info", "-c", cname}; info = new String[] { "-container", "-info", "-c",
Long.toString(container.getContainerID()) };
exitCode = runCommandAndGetOutput(info, out, null); exitCode = runCommandAndGetOutput(info, out, null);
assertEquals(ResultCode.SUCCESS, exitCode); assertEquals(ResultCode.SUCCESS, exitCode);
openStatus = data.isOpen() ? "OPEN" : "CLOSED"; openStatus = data.isOpen() ? "OPEN" : "CLOSED";
expected = String.format(formatStr, cname, openStatus, expected = String.format(formatStr, container.getContainerID(), openStatus,
data.getDBPath(), data.getContainerPath(), "", data.getDBPath(), data.getContainerPath(), "",
datanodeDetails.getHostName(), datanodeDetails.getHostName()); datanodeDetails.getHostName(), datanodeDetails.getHostName());
assertEquals(expected, out.toString()); assertEquals(expected, out.toString());
@ -326,7 +321,8 @@ public class TestSCMCli {
containerOperationClient.closeContainer( containerOperationClient.closeContainer(
container.getContainerID(), container.getPipeline()); container.getContainerID(), container.getPipeline());
info = new String[] {"-container", "-info", "-c", cname}; info = new String[] { "-container", "-info", "-c",
Long.toString(container.getContainerID()) };
exitCode = runCommandAndGetOutput(info, out, null); exitCode = runCommandAndGetOutput(info, out, null);
assertEquals(ResultCode.SUCCESS, exitCode); assertEquals(ResultCode.SUCCESS, exitCode);
data = ContainerData data = ContainerData
@ -334,9 +330,10 @@ public class TestSCMCli {
container.getContainerID(), container.getPipeline()), conf); container.getContainerID(), container.getPipeline()), conf);
openStatus = data.isOpen() ? "OPEN" : "CLOSED"; openStatus = data.isOpen() ? "OPEN" : "CLOSED";
expected = String.format(formatStrWithHash, cname, openStatus, expected = String
data.getHash(), data.getDBPath(), data.getContainerPath(), .format(formatStrWithHash, container.getContainerID(), openStatus,
"", datanodeDetails.getHostName(), datanodeDetails.getHostName()); data.getHash(), data.getDBPath(), data.getContainerPath(), "",
datanodeDetails.getHostName(), datanodeDetails.getHostName());
assertEquals(expected, out.toString()); assertEquals(expected, out.toString());
} }
@ -413,16 +410,16 @@ public class TestSCMCli {
args = new String[] {"-container", "-list", "-start", args = new String[] {"-container", "-list", "-start",
startContainerIDStr}; startContainerIDStr};
exitCode = runCommandAndGetOutput(args, out, err); exitCode = runCommandAndGetOutput(args, out, err);
assertEquals(ResultCode.SUCCESS, exitCode); assertEquals(ResultCode.EXECUTION_ERROR, exitCode);
assertTrue(out.toString().isEmpty()); assertTrue(err.toString().contains(
"java.io.IOException: Expecting container count"));
} }
@Test @Test
public void testCloseContainer() throws Exception { public void testCloseContainer() throws Exception {
long containerID = ContainerTestHelper.getTestContainerID(); long containerID = containerOperationClient
String[] args = {"-container", "-create", "-c", .createContainer(xceiverClientManager.getType(),
Long.toString(containerID)}; HddsProtos.ReplicationFactor.ONE, containerOwner).getContainerID();
assertEquals(ResultCode.SUCCESS, cli.run(args));
ContainerInfo container = scm.getClientProtocolServer() ContainerInfo container = scm.getClientProtocolServer()
.getContainer(containerID); .getContainer(containerID);
assertNotNull(container); assertNotNull(container);
@ -477,7 +474,7 @@ public class TestSCMCli {
String[] args2 = {"-container", "-create", "-help"}; String[] args2 = {"-container", "-create", "-help"};
assertEquals(ResultCode.SUCCESS, cli.run(args2)); assertEquals(ResultCode.SUCCESS, cli.run(args2));
String expected2 = String expected2 =
"usage: hdfs scm -container -create\n"; "usage: hdfs scm -container -create\n\n";
assertEquals(expected2, testContent.toString()); assertEquals(expected2, testContent.toString());
testContent.reset(); testContent.reset();
@ -502,11 +499,10 @@ public class TestSCMCli {
String[] args5 = {"-container", "-list", "-help"}; String[] args5 = {"-container", "-list", "-help"};
assertEquals(ResultCode.SUCCESS, cli.run(args5)); assertEquals(ResultCode.SUCCESS, cli.run(args5));
String expected5 = String expected5 = "usage: hdfs scm -container -list <option>\n"
"usage: hdfs scm -container -list <option>\n" + + "where <option> can be the following\n"
"where <option> can be the following\n" + + " -count <arg> Specify count number, required\n"
" -start <arg> Specify start container id, required\n" + + " -start <arg> Specify start container id\n";
" -count <arg> Specify count number name\n";
assertEquals(expected5, testContent.toString()); assertEquals(expected5, testContent.toString());
testContent.reset(); testContent.reset();