public void executeLine(String line) throws CliException, InterruptedException, IOException { if (!line.equals("")) { cl.parseCommand(line); addToHistory(commandCount,line); processCmd(cl); commandCount++; } }
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));
/** * 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()); }
zkMain.cl.parseCommand(cmdstring); Assert.assertTrue("Removewatches cmd fails to remove child watches", zkMain.processZKCmd(zkMain.cl));
/** * 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()); }
public void executeLine(String line) throws InterruptedException, IOException, KeeperException { if (!line.equals("")) { cl.parseCommand(line); addToHistory(commandCount,line); processCmd(cl); commandCount++; } }
@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 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 testParseWithMixedQuotes() throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); for (String[] quoteChars : new String[][] {{"'", "\""}, {"\"", "'"}}) { String outerQuotes = quoteChars[0]; String innerQuotes = quoteChars[1]; String cmdstring = String.format("create /node %1$s%2$squoted data%2$s%1$s", outerQuotes, innerQuotes); zkMain.cl.parseCommand(cmdstring); Assert.assertEquals("quotes combine arguments", zkMain.cl.getNumArguments(), 3); Assert.assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); Assert.assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); Assert.assertEquals("quoted data is not taken as third argument", zkMain.cl.getCmdArgument(2), innerQuotes + "quoted data" + innerQuotes); } }
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()); } }
@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()); } }
@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)); }
@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)); }
@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 testParseWithQuotes() throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); for (String quoteChar : new String[] {"'", "\""}) { String cmdstring = String.format("create /node %1$squoted data%1$s", quoteChar); zkMain.cl.parseCommand(cmdstring); Assert.assertEquals("quotes combine arguments", zkMain.cl.getNumArguments(), 3); Assert.assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); Assert.assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); Assert.assertEquals("quoted data is not taken as third argument", zkMain.cl.getCmdArgument(2), "quoted data"); } }
@Test public void testParseWithExtraSpaces() throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); String cmdstring = " ls / "; zkMain.cl.parseCommand(cmdstring); Assert.assertEquals("Spaces also considered as characters", zkMain.cl.getNumArguments(), 2); Assert.assertEquals("ls is not taken as first argument", zkMain.cl.getCmdArgument(0), "ls"); Assert.assertEquals("/ is not taken as second argument", zkMain.cl.getCmdArgument(1), "/"); }
@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 testParseWithMultipleQuotes() throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); String cmdstring = "create /node '' ''"; zkMain.cl.parseCommand(cmdstring); Assert.assertEquals("expected 5 arguments", zkMain.cl.getNumArguments(), 4); Assert.assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); Assert.assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); Assert.assertEquals("empty string is not taken as third argument", zkMain.cl.getCmdArgument(2), ""); Assert.assertEquals("empty string is not taken as fourth argument", zkMain.cl.getCmdArgument(3), ""); }
@Test public void testParseWithEmptyQuotes() throws Exception { final ZooKeeper zk = createClient(); ZooKeeperMain zkMain = new ZooKeeperMain(zk); String cmdstring = "create /node ''"; zkMain.cl.parseCommand(cmdstring); Assert.assertEquals("empty quotes should produce arguments", zkMain.cl.getNumArguments(), 3); Assert.assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); Assert.assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); Assert.assertEquals("empty string is not taken as third argument", zkMain.cl.getCmdArgument(2), ""); }