Updated Branches: refs/heads/master b4ce6f88e -> 49aa56ba5
ACCUMULO-527 Add -zi and -zh options for shell. Signed-off-by: Keith Turner <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/7ba6f8d7 Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/7ba6f8d7 Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/7ba6f8d7 Branch: refs/heads/master Commit: 7ba6f8d749928df49fefd435a42410e1d74a2e50 Parents: b4ce6f8 Author: Bill Havanki <[email protected]> Authored: Wed Sep 18 18:47:34 2013 -0400 Committer: Keith Turner <[email protected]> Committed: Thu Sep 19 14:42:21 2013 -0400 ---------------------------------------------------------------------- core/pom.xml | 15 ++ .../apache/accumulo/core/util/shell/Shell.java | 45 ++-- .../core/util/shell/ShellOptionsJC.java | 14 ++ .../core/util/shell/ShellSetInstanceTest.java | 206 +++++++++++++++++++ 4 files changed, 268 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/7ba6f8d7/core/pom.xml ---------------------------------------------------------------------- diff --git a/core/pom.xml b/core/pom.xml index 640f4a8..7e2addf 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -118,6 +118,21 @@ <artifactId>junit</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.easymock</groupId> + <artifactId>easymock</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-api-easymock</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-module-junit4</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> <pluginManagement> http://git-wip-us.apache.org/repos/asf/accumulo/blob/7ba6f8d7/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java b/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java index 72f6df2..1c996f3 100644 --- a/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java +++ b/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java @@ -375,29 +375,50 @@ public class Shell extends ShellOptions { return configError; } + /** + * Sets the instance used by the shell based on the given options. + * + * @param options shell options + */ protected void setInstance(ShellOptionsJC options) { - // should only be one instance option set + // should only be one set of instance options set instance = null; if (options.isFake()) { instance = new MockInstance("fake"); - } else if (options.isHdfsZooInstance()) { - @SuppressWarnings("deprecation") - AccumuloConfiguration deprecatedSiteConfiguration = AccumuloConfiguration.getSiteConfiguration(); - instance = getDefaultInstance(deprecatedSiteConfiguration); + } else { + String instanceName, hosts; + if (options.isHdfsZooInstance()) { + instanceName = hosts = null; } else if (options.getZooKeeperInstance().size() > 0) { List<String> zkOpts = options.getZooKeeperInstance(); - instance = new ZooKeeperInstance(zkOpts.get(0), zkOpts.get(1)); + instanceName = zkOpts.get(0); + hosts = zkOpts.get(1); } else { - @SuppressWarnings("deprecation") - AccumuloConfiguration deprecatedSiteConfiguration = AccumuloConfiguration.getSiteConfiguration(); - instance = getDefaultInstance(deprecatedSiteConfiguration); + instanceName = options.getZooKeeperInstanceName(); + hosts = options.getZooKeeperHosts(); + } + instance = getZooInstance(instanceName, hosts); } } - private static Instance getDefaultInstance(AccumuloConfiguration conf) { - String keepers = conf.get(Property.INSTANCE_ZK_HOST); + private static Instance getZooInstance(String instanceName, String keepers) { + UUID instanceId = null; + if (instanceName == null || keepers == null) { + @SuppressWarnings("deprecation") + AccumuloConfiguration conf = AccumuloConfiguration.getSiteConfiguration(); + if (instanceName == null) { Path instanceDir = new Path(conf.get(Property.INSTANCE_DFS_DIR), "instance_id"); - return new ZooKeeperInstance(UUID.fromString(ZooUtil.getInstanceIDFromHdfs(instanceDir)), keepers); + instanceId = UUID.fromString(ZooUtil.getInstanceIDFromHdfs(instanceDir)); + } + if (keepers == null) { + keepers = conf.get(Property.INSTANCE_ZK_HOST); + } + } + if (instanceId != null) { + return new ZooKeeperInstance(instanceId, keepers); + } else { + return new ZooKeeperInstance(instanceName, keepers); + } } public Connector getConnector() { http://git-wip-us.apache.org/repos/asf/accumulo/blob/7ba6f8d7/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java b/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java index c3890db..cb1f1c8 100644 --- a/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java +++ b/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java @@ -161,6 +161,12 @@ public class ShellOptionsJC { @Parameter(names = {"-z", "--zooKeeperInstance"}, description = "use a zookeeper instance with the given instance name and list of zoo hosts", arity = 2) private List<String> zooKeeperInstance = new ArrayList<String>(); + @Parameter(names = {"-zi", "--zooKeeperInstanceName"}, description="use a zookeeper instance with the given instance name") + private String zooKeeperInstanceName; + + @Parameter(names = {"-zh", "--zooKeeperHosts"}, description="use a zookeeper instance with the given list of zoo hosts") + private String zooKeeperHosts; + @Parameter(names = "--auth-timeout", description = "minutes the shell can be idle without re-entering a password") private int authTimeout = 60; // TODO Add validator for positive number @@ -222,6 +228,14 @@ public class ShellOptionsJC { return zooKeeperInstance; } + public String getZooKeeperInstanceName() { + return zooKeeperInstanceName; + } + + public String getZooKeeperHosts() { + return zooKeeperHosts; + } + public int getAuthTimeout() { return authTimeout; } http://git-wip-us.apache.org/repos/asf/accumulo/blob/7ba6f8d7/core/src/test/java/org/apache/accumulo/core/util/shell/ShellSetInstanceTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/util/shell/ShellSetInstanceTest.java b/core/src/test/java/org/apache/accumulo/core/util/shell/ShellSetInstanceTest.java new file mode 100644 index 0000000..8abd439 --- /dev/null +++ b/core/src/test/java/org/apache/accumulo/core/util/shell/ShellSetInstanceTest.java @@ -0,0 +1,206 @@ +/* + * 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.accumulo.core.util.shell; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; +import static org.powermock.api.easymock.PowerMock.*; + +import java.io.FileDescriptor; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Collections; +import java.util.UUID; +import java.util.List; + +import jline.console.ConsoleReader; + +import org.apache.accumulo.core.conf.AccumuloConfiguration; +import org.apache.accumulo.core.conf.Property; +import org.apache.accumulo.core.client.Instance; +import org.apache.accumulo.core.client.ZooKeeperInstance; +import org.apache.accumulo.core.client.mock.MockInstance; +import org.apache.accumulo.core.zookeeper.ZooUtil; +import org.apache.hadoop.fs.Path; +import org.apache.log4j.Level; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({Shell.class, AccumuloConfiguration.class, ZooUtil.class}) +public class ShellSetInstanceTest { + public static class TestOutputStream extends OutputStream { + StringBuilder sb = new StringBuilder(); + + @Override + public void write(int b) throws IOException { + sb.append((char) (0xff & b)); + } + + public String get() { + return sb.toString(); + } + + public void clear() { + sb.setLength(0); + } + } + + @BeforeClass + public static void setupClass() { + // This is necessary because PowerMock messes with Hadoop's ability to + // determine the current user (see security.UserGroupInformation). + System.setProperty("HADOOP_USER_NAME", "test"); + } + @AfterClass + public static void teardownClass() { + System.clearProperty("HADOOP_USER_NAME"); + } + + private TestOutputStream output; + private Shell shell; + + @Before + public void setup() throws IOException { + Shell.log.setLevel(Level.OFF); + output = new TestOutputStream(); + shell = new Shell(new ConsoleReader(new FileInputStream(FileDescriptor.in), output)); + shell.setLogErrorsToConsole(); + } + + @Test + public void testSetInstance_Fake() throws Exception { + ShellOptionsJC opts = createMock(ShellOptionsJC.class); + expect(opts.isFake()).andReturn(true); + replay(opts); + MockInstance theInstance = createMock(MockInstance.class); + expectNew(MockInstance.class, "fake").andReturn(theInstance); + replay(theInstance, MockInstance.class); + + shell.setInstance(opts); + verify(theInstance, MockInstance.class); + } + @Test + public void testSetInstance_HdfsZooInstance_Explicit() throws Exception { + testSetInstance_HdfsZooInstance(true, false, false); + } + @Test + public void testSetInstance_HdfsZooInstance_InstanceGiven() throws Exception { + testSetInstance_HdfsZooInstance(false, true, false); + } + @Test + public void testSetInstance_HdfsZooInstance_HostsGiven() throws Exception { + testSetInstance_HdfsZooInstance(false, false, true); + } + @Test + public void testSetInstance_HdfsZooInstance_Implicit() throws Exception { + testSetInstance_HdfsZooInstance(false, false, false); + } + private void testSetInstance_HdfsZooInstance(boolean explicitHdfs, boolean onlyInstance, boolean onlyHosts) + throws Exception { + ShellOptionsJC opts = createMock(ShellOptionsJC.class); + expect(opts.isFake()).andReturn(false); + expect(opts.isHdfsZooInstance()).andReturn(explicitHdfs); + if (!explicitHdfs) { + expect(opts.getZooKeeperInstance()) + .andReturn(Collections.<String>emptyList()); + if (onlyInstance) { + expect(opts.getZooKeeperInstanceName()).andReturn("instance"); + } else { + expect(opts.getZooKeeperInstanceName()).andReturn(null); + } + if (onlyHosts) { + expect(opts.getZooKeeperHosts()).andReturn("host1,host2"); + } else { + expect(opts.getZooKeeperHosts()).andReturn(null); + } + } + replay(opts); + + AccumuloConfiguration conf = createMock(AccumuloConfiguration.class); + mockStatic(AccumuloConfiguration.class); + expect(AccumuloConfiguration.getSiteConfiguration()).andReturn(conf); + replay(AccumuloConfiguration.class); + + if (!onlyHosts) { + expect(conf.get(Property.INSTANCE_ZK_HOST)).andReturn("host1,host2"); + } + if (!onlyInstance) { + expect(conf.get(Property.INSTANCE_DFS_DIR)).andReturn("/dfs"); + } + replay(conf); + UUID randomUUID = null; + if (!onlyInstance) { + mockStatic(ZooUtil.class); + randomUUID = UUID.randomUUID(); + expect(ZooUtil.getInstanceIDFromHdfs(anyObject(Path.class))) + .andReturn(randomUUID.toString()); + replay(ZooUtil.class); + } + + ZooKeeperInstance theInstance = createMock(ZooKeeperInstance.class); + if (!onlyInstance) { + expectNew(ZooKeeperInstance.class, randomUUID, "host1,host2") + .andReturn(theInstance); + } else { + expectNew(ZooKeeperInstance.class, "instance", "host1,host2") + .andReturn(theInstance); + } + replay(theInstance, ZooKeeperInstance.class); + + shell.setInstance(opts); + verify(theInstance, ZooKeeperInstance.class); + } + @Test + public void testSetInstance_ZKInstance_DashZ() throws Exception { + testSetInstance_ZKInstance(true); + } + @Test + public void testSetInstance_ZKInstance_DashZIandZH() throws Exception { + testSetInstance_ZKInstance(false); + } + private void testSetInstance_ZKInstance(boolean dashZ) throws Exception { + ShellOptionsJC opts = createMock(ShellOptionsJC.class); + expect(opts.isFake()).andReturn(false); + expect(opts.isHdfsZooInstance()).andReturn(false); + if (dashZ) { + List<String> zl = new java.util.ArrayList<String>(); + zl.add("instance"); zl.add("host1,host2"); + expect(opts.getZooKeeperInstance()).andReturn(zl); + expectLastCall().anyTimes(); + } else { + expect(opts.getZooKeeperInstance()).andReturn(Collections.<String>emptyList()); + expect(opts.getZooKeeperInstanceName()).andReturn("instance"); + expect(opts.getZooKeeperHosts()).andReturn("host1,host2"); + } + replay(opts); + + ZooKeeperInstance theInstance = createMock(ZooKeeperInstance.class); + expectNew(ZooKeeperInstance.class, "instance", "host1,host2") + .andReturn(theInstance); + replay(theInstance, ZooKeeperInstance.class); + + shell.setInstance(opts); + verify(theInstance, ZooKeeperInstance.class); + } +}
