diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-registry/src/main/java/org/apache/hadoop/registry/cli/RegistryCli.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-registry/src/main/java/org/apache/hadoop/registry/cli/RegistryCli.java index bf2b5e5..2aa7f4e 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-registry/src/main/java/org/apache/hadoop/registry/cli/RegistryCli.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-registry/src/main/java/org/apache/hadoop/registry/cli/RegistryCli.java @@ -34,6 +34,7 @@ import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; +import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.PathNotFoundException; import org.apache.hadoop.security.AccessControlException; @@ -57,11 +58,11 @@ public class RegistryCli extends Configured implements Tool { private static final Logger LOG = LoggerFactory.getLogger(RegistryCli.class); - protected final PrintStream sysout; - protected final PrintStream syserr; + protected final PrintStream sysout; + protected final PrintStream syserr; - private RegistryOperations registry; + private RegistryOperations registry; static final String LS_USAGE = "ls pathName"; static final String RESOLVE_USAGE = "resolve pathName"; @@ -78,11 +79,21 @@ public RegistryCli(PrintStream sysout, PrintStream syserr) { - super(new YarnConfiguration()); - this.sysout = sysout; - this.syserr = syserr; + YarnConfiguration conf = new YarnConfiguration(); + super.setConf(conf); + registry = RegistryOperationsFactory.createInstance(conf); + registry.start(); + this.sysout = sysout; + this.syserr = syserr; } + public RegistryCli(RegistryOperations reg, Configuration conf, PrintStream sysout, + PrintStream syserr) { + super(conf); + registry = reg; + this.sysout = sysout; + this.syserr = syserr; + } @SuppressWarnings("UseOfSystemOutOrSystemErr") public static void main(String[] args) throws Exception { @@ -106,10 +117,7 @@ private boolean validatePath(String path) { } @Override public int run(String[] args) throws Exception { - Preconditions.checkArgument(getConf() != null, "null configuration"); - registry = RegistryOperationsFactory.createInstance( - new YarnConfiguration(getConf())); - registry.start(); + Preconditions.checkArgument(getConf() != null, "null configuration"); if (args.length > 0) { if (args[0].equals("ls")) { return ls(args); @@ -121,9 +129,11 @@ public int run(String[] args) throws Exception { return mknode(args); } else if (args[0].equals("rm")) { return rm(args); + } else { + return usageError("Invalid command: " + args[0], USAGE); } } - return usageError("Invalid command: " + args[0], USAGE); + return usageError("No command arg passed.", USAGE); } @SuppressWarnings("unchecked") @@ -182,12 +192,13 @@ public int resolve(String [] args) { + endpoint.addressType + ") are: "); for (Map address : endpoint.addresses) { - sysout.println(" [ "); + sysout.println("[ "); for (Map.Entry entry : address.entrySet()) { - sysout.println(" " + entry.getKey() - + ": \"" + entry.getValue() + "\""); + sysout.print("\t" + entry.getKey() + + ":" + entry.getValue()); } - sysout.println(" ]"); + + sysout.println("\n]"); } sysout.println(); } diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-registry/src/test/java/org/apache/hadoop/registry/cli/TestResgistryCli.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-registry/src/test/java/org/apache/hadoop/registry/cli/TestResgistryCli.java new file mode 100644 index 0000000..d1f01bc --- /dev/null +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-registry/src/test/java/org/apache/hadoop/registry/cli/TestResgistryCli.java @@ -0,0 +1,292 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.registry.cli; + + + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.apache.hadoop.registry.AbstractRegistryTest; +import org.apache.hadoop.registry.operations.TestRegistryOperations; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TestResgistryCli extends AbstractRegistryTest { + protected static final Logger LOG = + LoggerFactory.getLogger(TestRegistryOperations.class); + + + + ByteArrayOutputStream sysOutStream; + private PrintStream sysOut; + ByteArrayOutputStream sysErrStream; + private PrintStream sysErr; + RegistryCli cli; + + + + + @Before + public void setUp() throws Exception { + sysOutStream = new ByteArrayOutputStream(); + sysOut = new PrintStream(sysOutStream); + sysErrStream = new ByteArrayOutputStream(); + sysErr = new PrintStream(sysErrStream); + System.setOut(sysOut); + cli = new RegistryCli(operations, createRegistryConfiguration(), sysOut, sysErr); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testBadCommands() throws Exception { + int result = cli.run(new String[] { }); + assertEquals(-1, result); + result = cli.run(new String[] { "foo"}); + assertEquals(-1, result); + } + + @Test + public void testInvalidNumArgs() throws Exception { + int result; + result = cli.run(new String[] { "ls"}); + assertEquals(-1, result); + result = cli.run(new String[] { "ls", "/path", "/extraPath"}); + assertEquals(-1, result); + result = cli.run(new String[] { "resolve"}); + assertEquals(-1, result); + result = cli.run(new String[] { "resolve", "/path", "/extraPath"}); + assertEquals(-1, result); + result = cli.run(new String[] { "mknode"}); + assertEquals(-1, result); + result = cli.run(new String[] { "mknode", "/path", "/extraPath"}); + assertEquals(-1, result); + result = cli.run(new String[] { "rm"}); + assertEquals(-1, result); + result = cli.run(new String[] { "rm", "/path", "/extraPath"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-inet", "foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-inet", "-api", "-p", "378", "-h", "host", "/foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-inet", "-api", "Api", "-p", "-h", "host", "/foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-inet", "-api", "Api", "-p", "378", "-h", "/foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-inet", "-api", "Api", "-p", "378", "-h", "host"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-api", "Api", "-p", "378", "-h", "host", "/foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-webui", "foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-webui", "-api", "Api", "/foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-webui", "uriString", "-api", "/foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-webui", "uriString", "-api", "Api"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-rest", "foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-rest", "uriString", "-api", "Api"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-rest", "-api", "Api", "/foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-rest", "uriString", "-api", "/foo"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "uriString", "-api", "Api", "/foo"}); + assertEquals(-1, result); + } + + + @Test + public void testBadPath() throws Exception { + int result; + result = cli.run(new String[] { "ls", "NonSlashPath"}); + assertEquals(-1, result); + result = cli.run(new String[] { "ls", "//"}); + assertEquals(-1, result); + result = cli.run(new String[] { "resolve", "NonSlashPath"}); + assertEquals(-1, result); + result = cli.run(new String[] { "resolve", "//"}); + assertEquals(-1, result); + result = cli.run(new String[] { "mknode", "NonSlashPath"}); + assertEquals(-1, result); + result = cli.run(new String[] { "mknode", "//"}); + assertEquals(-1, result); + result = cli.run(new String[] { "rm", "NonSlashPath"}); + assertEquals(-1, result); + result = cli.run(new String[] { "rm", "//"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-inet", "-api", "Api", "-p", "378", "-h", "host", "NonSlashPath"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-inet", "-api", "Api", "-p", "378", "-h", "host", "//"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-webui", "uriString", "-api", "Api", "NonSlashPath"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-webui", "uriString", "-api", "Api", "//"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-rest", "uriString", "-api", "Api", "NonSlashPath"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-rest", "uriString", "-api", "Api", "//"}); + assertEquals(-1, result); + } + + @Test + public void testNotExistingPaths() throws Exception { + int result = cli.run(new String[] { "ls", "/nonexisting_path" }); + assertEquals(-1, result); + result = cli.run(new String[] { "ls", "/NonExistingDir/nonexisting_path" }); + assertEquals(-1, result); + result = cli.run(new String[] { "resolve", "/nonexisting_path" }); + assertEquals(-1, result); + result = cli.run(new String[] { "resolve", "/NonExistingDir/nonexisting_path" }); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-inet", "-api", "Api", "-p", "378", "-h", "host", "/NonExistingDir/nonexisting_path"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-webui", "uriString", "-api", "Api", "/NonExistingDir/nonexisting_path"}); + assertEquals(-1, result); + result = cli.run(new String[] { "bind", "-rest", "uriString", "-api", "Api", "/NonExistingDir/nonexisting_path"}); + assertEquals(-1, result); + + } + + @Test + public void testValidCommands() throws Exception { + int result; + result = cli.run(new String[] { "bind", "-inet", "-api", "Api", "-p", "378", "-h", "host", "/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "rm", "/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/foo"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "bind", "-webui", "uriString", "-api", "Api", "/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "rm", "/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/foo"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "bind", "-rest", "uriString", "-api", "Api", "/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "rm", "/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/foo"}); + assertEquals(-1, result); + + //Test Sub Directories Binds + result = cli.run(new String[] { "mknode", "/subdir"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/subdir"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "bind", "-inet", "-api", "Api", "-p", "378", "-h", "host", "/subdir/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/subdir/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "rm", "/subdir/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/subdir/foo"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "bind", "-webui", "uriString", "-api", "Api", "/subdir/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/subdir/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "rm", "/subdir/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/subdir/foo"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "bind", "-rest", "uriString", "-api", "Api", "/subdir/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/subdir/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "rm", "/subdir/foo"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/subdir/foo"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "rm", "/subdir"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/subdir"}); + assertEquals(-1, result); + + //Test Bind that the dir itself + result = cli.run(new String[] { "mknode", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/dir"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "bind", "-inet", "-api", "Api", "-p", "378", "-h", "host", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "rm", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/dir"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "mknode", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/dir"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "bind", "-webui", "uriString", "-api", "Api", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "rm", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/dir"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "mknode", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/dir"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "bind", "-rest", "uriString", "-api", "Api", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "rm", "/dir"}); + assertEquals(0, result); + result = cli.run(new String[] { "resolve", "/dir"}); + assertEquals(-1, result); + + result = cli.run(new String[] { "rm", "/Nonexitent"}); + assertEquals(0, result); + } +}