protected boolean processCmd(MyCommandOptions co) throws CliException, IOException, InterruptedException { boolean watch = false; try { watch = processZKCmd(co); exitCode = ExitCode.EXECUTION_FINISHED.getValue(); } catch (CliException ex) { exitCode = ex.getExitCode(); System.err.println(ex.getMessage()); } return watch; }
protected boolean processCmd(MyCommandOptions co) throws KeeperException, IOException, InterruptedException { try { return processZKCmd(co); } catch (IllegalArgumentException e) { System.err.println("Command failed: " + e); } catch (KeeperException.NoNodeException e) { System.err.println("Node does not exist: " + e.getPath()); } catch (KeeperException.NoChildrenForEphemeralsException e) { System.err.println("Ephemerals cannot have children: " + e.getPath()); } catch (KeeperException.NodeExistsException e) { System.err.println("Node already exists: " + e.getPath()); } catch (KeeperException.NotEmptyException e) { System.err.println("Node not empty: " + e.getPath()); } catch (KeeperException.NotReadOnlyException e) { System.err.println("Not a read-only call: " + e.getPath()); }catch (KeeperException.InvalidACLException e) { System.err.println("Acl is not valid : "+e.getPath()); }catch (KeeperException.NoAuthException e) { System.err.println("Authentication is not valid : "+e.getPath()); }catch (KeeperException.BadArgumentsException e) { System.err.println("Arguments are not valid : "+e.getPath()); }catch (KeeperException.BadVersionException e) { System.err.println("version No is not valid : "+e.getPath()); } return false; }
@Test public void testStatCommand() throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); String cmdstring1 = "create -e /node3 data"; String cmdstring2 = "stat /node3"; String cmdstring3 = "delete /node3"; zkMain.cl.parseCommand(cmdstring1); Assert.assertTrue(zkMain.processZKCmd(zkMain.cl)); zkMain.cl.parseCommand(cmdstring2); Assert.assertFalse(zkMain.processZKCmd(zkMain.cl)); zkMain.cl.parseCommand(cmdstring3); Assert.assertFalse(zkMain.processZKCmd(zkMain.cl)); }
zkMain.cl.parseCommand(cmdstring); Assert.assertTrue("Removewatches cmd fails to remove child watches", zkMain.processZKCmd(zkMain.cl)); LOG.info("Waiting for the DataWatchRemoved event"); myWatcher.matches();
/** * Test verifies deletion of NodeChildrenChanged watches */ @Test(timeout = 30000) public void testRemoveNodeChildrenChangedWatches() throws Exception { List<EventType> expectedEvents = new ArrayList<Watcher.Event.EventType>(); expectedEvents.add(EventType.ChildWatchRemoved); MyWatcher myWatcher = new MyWatcher("/testnode1", expectedEvents, 1); zk.create("/testnode1", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); LOG.info("Adding child changed watcher"); zk.getChildren("/testnode1", myWatcher); String cmdstring = "removewatches /testnode1 -c"; LOG.info("Remove watchers using shell command : {}", cmdstring); zkMain.cl.parseCommand(cmdstring); Assert.assertTrue("Removewatches cmd fails to remove child watches", zkMain.processZKCmd(zkMain.cl)); myWatcher.matches(); Assert.assertEquals( "Failed to remove child watches : " + zk.getChildWatches(), 0, zk.getChildWatches().size()); }
/** * Test verifies deletion of NodeDataChanged watches */ @Test(timeout = 30000) public void testRemoveNodeDataChangedWatches() throws Exception { LOG.info("Adding data watcher using getData()"); List<EventType> expectedEvents = new ArrayList<Watcher.Event.EventType>(); expectedEvents.add(EventType.DataWatchRemoved); MyWatcher myWatcher = new MyWatcher("/testnode1", expectedEvents, 1); zk.create("/testnode1", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.getData("/testnode1", myWatcher, null); String cmdstring = "removewatches /testnode1 -d"; LOG.info("Remove watchers using shell command : {}", cmdstring); zkMain.cl.parseCommand(cmdstring); Assert.assertTrue("Removewatches cmd fails to remove data watches", zkMain.processZKCmd(zkMain.cl)); LOG.info("Waiting for the DataWatchRemoved event"); myWatcher.matches(); // verifying that other path data watches are removed Assert.assertEquals( "Data watches are not removed : " + zk.getDataWatches(), 0, zk .getDataWatches().size()); }
String cmdstring1 = "deleteall /a"; zkMain.cl.parseCommand(cmdstring0); Assert.assertFalse(zkMain.processZKCmd(zkMain.cl)); Assert.assertEquals(null, zk.exists("/a/b/v", null)); zkMain.cl.parseCommand(cmdstring1); Assert.assertFalse(zkMain.processZKCmd(zkMain.cl)); Assert.assertNull(zk.exists("/a", null));
Assert.assertTrue( "Removewatches cmd fails to remove child/data watches", zkMain.processZKCmd(zkMain.cl)); LOG.info("Waiting for the WatchRemoved events"); watcherLatch.await(10, TimeUnit.SECONDS);
zkMain.cl.parseCommand(cmdstring); Assert.assertTrue("Removewatches cmd fails to remove child watches", zkMain.processZKCmd(zkMain.cl)); LOG.info("Waiting for the ChildWatchRemoved event"); myWatcher.matches();
@Test public void testSetData() throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); String cmdstring1 = "create -e /node4 data"; String cmdstring2 = "set /node4 " + "data"; String cmdstring3 = "delete /node4"; Stat stat = new Stat(); int version = 0; zkMain.cl.parseCommand(cmdstring1); Assert.assertTrue(zkMain.processZKCmd(zkMain.cl)); stat = zk.exists("/node4", true); version = stat.getVersion(); zkMain.cl.parseCommand(cmdstring2); Assert.assertFalse(zkMain.processZKCmd(zkMain.cl)); stat = zk.exists("/node4", true); Assert.assertEquals(version + 1, stat.getVersion()); zkMain.cl.parseCommand(cmdstring3); Assert.assertFalse(zkMain.processZKCmd(zkMain.cl)); }
@Test public void testSetAclRecursive() throws Exception { final ZooKeeper zk = createClient(); final byte[] EMPTY = new byte[0]; zk.setData("/", EMPTY, -1); zk.create("/a", EMPTY, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/a/b", EMPTY, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/a/b/c", EMPTY, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/a/d", EMPTY, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/e", EMPTY, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); ZooKeeperMain zkMain = new ZooKeeperMain(zk); String setAclCommand = "setAcl -R /a world:anyone:r"; zkMain.cl.parseCommand(setAclCommand); Assert.assertFalse(zkMain.processZKCmd(zkMain.cl)); Assert.assertEquals(Ids.READ_ACL_UNSAFE, zk.getACL("/a", new Stat())); Assert.assertEquals(Ids.READ_ACL_UNSAFE, zk.getACL("/a/b", new Stat())); Assert.assertEquals(Ids.READ_ACL_UNSAFE, zk.getACL("/a/b/c", new Stat())); Assert.assertEquals(Ids.READ_ACL_UNSAFE, zk.getACL("/a/d", new Stat())); // /e is unset, its acl should remain the same. Assert.assertEquals(Ids.OPEN_ACL_UNSAFE, zk.getACL("/e", new Stat())); } }
@Test public void testACLWithExtraAgruments() throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); // create persistent sequential node String cmdstring = "create -s /l data ip:10.18.52.144:cdrwa f g h"; zkMain.cl.parseCommand(cmdstring); Assert.assertTrue( "Not considering the extra arguments after the acls.", zkMain .processZKCmd(zkMain.cl)); }
@Test public void testCreatePersistentNode() throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); String cmdstring = "create /node2"; zkMain.cl.parseCommand(cmdstring); Assert.assertTrue("Not creating Persistent node.", zkMain .processZKCmd(zkMain.cl)); }
@Test public void testDelete() throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); String cmdstring1 = "create -e /node2 data"; String cmdstring2 = "delete /node2"; String cmdstring3 = "ls /node2"; zkMain.cl.parseCommand(cmdstring1); Assert.assertTrue(zkMain.processZKCmd(zkMain.cl)); zkMain.cl.parseCommand(cmdstring2); Assert.assertFalse(zkMain.processZKCmd(zkMain.cl)); zkMain.cl.parseCommand(cmdstring3); Assert.assertFalse("", zkMain.processCmd(zkMain.cl)); }
Assert.assertTrue( "Removewatches cmd fails to remove pre-create watches", zkMain.processZKCmd(zkMain.cl)); myWatcher1.matches(); Assert.assertEquals( zkMain.cl.parseCommand(cmdstring2); Assert.assertTrue("Removewatches cmd fails to remove data watches", zkMain.processZKCmd(zkMain.cl));
zkMain.cl.parseCommand(cmdstring); Assert.assertTrue("Doesn't create node without data", zkMain .processZKCmd(zkMain.cl)); .processZKCmd(zkMain.cl)); .processZKCmd(zkMain.cl)); try { Assert.assertTrue("Created node with wrong option", zkMain .processZKCmd(zkMain.cl)); Assert.fail("Created the node with wrong option should " + "throw Exception.");
@Test public void testStatWhenPathDoesNotExist() throws IOException, InterruptedException, MalformedCommandException { final ZooKeeper zk = createClient(); ZooKeeperMain main = new ZooKeeperMain(zk); String cmdstring = "stat /invalidPath"; main.cl.parseCommand(cmdstring); try { main.processZKCmd(main.cl); Assert.fail("As Node does not exist, command should fail by throwing No Node Exception."); } catch (CliException e) { Assert.assertEquals("Node does not exist: /invalidPath", e.getMessage()); } }
private void testInvalidCommand(String cmdString, int exitCode) throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); zkMain.cl.parseCommand(cmdString); // Verify that the exit code is set properly zkMain.processCmd(zkMain.cl); Assert.assertEquals(exitCode, zkMain.exitCode); // Verify that the correct exception is thrown try { zkMain.processZKCmd(zkMain.cl); Assert.fail(); } catch (CliException e) { return; } Assert.fail("invalid command should throw CliException"); }
@Test public void testInvalidStatCommand() throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); // node doesn't exists String cmdstring1 = "stat /node123"; zkMain.cl.parseCommand(cmdstring1); try { Assert.assertFalse(zkMain.processZKCmd(zkMain.cl)); Assert.fail("Path doesn't exists so, command should fail."); } catch (CliWrapperException e) { Assert.assertEquals(KeeperException.Code.NONODE, ((KeeperException)e.getCause()).code()); } }
protected boolean processCmd(MyCommandOptions co) throws KeeperException, IOException, InterruptedException { try { return processZKCmd(co); } catch (IllegalArgumentException e) { System.err.println("Command failed: " + e); } catch (KeeperException.NoNodeException e) { System.err.println("Node does not exist: " + e.getPath()); } catch (KeeperException.NoChildrenForEphemeralsException e) { System.err.println("Ephemerals cannot have children: " + e.getPath()); } catch (KeeperException.NodeExistsException e) { System.err.println("Node already exists: " + e.getPath()); } catch (KeeperException.NotEmptyException e) { System.err.println("Node not empty: " + e.getPath()); } return false; }