Exemplo n.º 1
0
    def __init__(self, config):
        # zoopy is really friggin' loud without this
        zookeeper.set_debug_level(0)

        connection_string = config["hostsource"]["connection-string"]
        self.client = ZookeeperClient(connection_string, session_timeout=3000)
        self.user = config["hostsource"]["user"]
        self.password = config["hostsource"]["password"]
Exemplo n.º 2
0
Arquivo: zk.py Projeto: calston/rhumba
    def connect(self):
        client = ZookeeperClient(self.zk_url, 5000)
        log.msg('Connecting to %s' % self.zk_url)

        self.client = yield client.connect()
        
        if not self.config.get('noexpire', False):
            self.t = task.LoopingCall(self.expireResults)
            self.t.start(5.0)

        yield self.setupPaths()
Exemplo n.º 3
0
 def expire_session(self):
     assert self.client.connected
     self.client2 = ZookeeperClient(self.client.servers)
     yield self.client2.connect(client_id=self.client.client_id)
     yield self.client2.close()
     # It takes some time to propagate (1/3 session time as ping)
     yield self.sleep(2)
Exemplo n.º 4
0
 def tearDown(self):
     self.client.close()
     self.client2 = ZookeeperClient("127.0.0.1:2181")
     yield self.client2.connect()
     utils.deleteTree(handle=self.client2.handle)
     yield self.client2.close()
     super(SessionClientExpireTests, self).tearDown()
Exemplo n.º 5
0
def main():
    """
    bootstrap function
    """
    params = sys.argv[1:]
    parser = argparse.ArgumentParser(description='Log producer with embedded https server ')
    parser.add_argument('-z','--zkServer',help='address of the zookeeper server', 
                                          default="localhost:2181", required=True)
    parser.add_argument('-a','--host',help='the hostname/ip to bind to, defaults to localhost', 
                                          default="localhost", required = False)
    parser.add_argument('-p','--port',help='the port to listen in', type=int, 
                                          default="8991", required=False)
    parser.add_argument('-c','--certDir',help='the directory to store https  certificat and key', 
                                          default="/tmp/", required=False)
    
    args = parser.parse_args(params)
    zkAddr = args.zkServer
    host = args.host
    port = args.port
    certDir = args.certDir
    try:
        zc = RetryClient(ZookeeperClient(zkAddr))
        d = zc.connect()
        d.addCallback(cb_connected, zc, host, port, certDir)
        d.addErrback(log.msg)
    except:
        print "Can't connect to Zookeeper!"
        return
        
    reactor.run()
Exemplo n.º 6
0
def main():
    """
    bootstrap function
    """
    params = sys.argv[1:]
    parser = argparse.ArgumentParser(description='A log consumer')
    parser.add_argument('-z',
                        '--zkServer',
                        help='address of the zookeeper server',
                        default="fd88:9fde:bd6e:f57a:0:1d7b:9dea:802:29017",
                        required=True)
    parser.add_argument('-n',
                        '--normalizer',
                        help=' path to pylogs parser normalizers',
                        default="/home/lahoucine/src/pylogsparser/normalizers",
                        required=True)

    args = parser.parse_args(params)

    zkAddr = args.zkServer
    normalizer = args.normalizer
    try:
        zc = RetryClient(ZookeeperClient(zkAddr))
        d = zc.connect()
        d.addCallback(cb_connected, zc, normalizer)
        d.addErrback(log.msg)
    except:
        print "Can't connect to Zookeeper"
        return

    reactor.run()
Exemplo n.º 7
0
    def setUp(self):
        super(SecurityTests, self).setUp()
        self.clients = []

        self.test_cleanup_connection = ZookeeperClient("127.0.0.1:2181", 2000)
        self.access_control_test_cleanup_entry = self.make_ac(
            self.ident_unittest, all=True, admin=True)
        return self.open_and_authenticate(
            self.test_cleanup_connection, self.ident_unittest)
Exemplo n.º 8
0
def main():
    """
    the aim of this code is to start an instance of solr, and publishing
    its configuration (address + port) into the configuration tree managed
    by Zookeeper.
    the znode created for this configuration must be a Ephemeral.
    with that type of znode, the configuration added exist only if the client
    is up.
    """
    params = sys.argv[1:]

    parser = argparse.ArgumentParser(description='Solr bootstrap')
    parser.add_argument('-z',
                        '--zkaddr',
                        help='zookeeper address',
                        required=True)
    parser.add_argument('-c', '--cores', help='cores path',
                        required=False)  #TO DO a remplacer par conf
    parser.add_argument('-m', '--solrhome', help='solr home',
                        required=True)  #TO DO a remplacer par conf
    parser.add_argument('-s', '--solrpath', help='solr path', required=True)
    parser.add_argument('-p', '--port', help='listening port', required=True)
    parser.add_argument('-i', '--ip', help='listening ip', required=True)
    parser.add_argument('-j', '--java', help='java home', required=True)
    parser.add_argument('-n',
                        '--numshard',
                        help='number shard',
                        required=False)

    args = parser.parse_args(params)

    zk = args.zkaddr
    cores = args.cores
    solrpath = args.solrpath
    solrhome = args.solrhome
    port = args.port
    ip = args.ip
    java = args.java
    numsh = args.numshard
    try:
        zc = RetryClient(ZookeeperClient(zk))
        d = zc.connect()
        d.addCallback(cb_connected, zc, cores, solrpath, solrhome, numsh, port,
                      ip, java, zk)
        d.addErrback(log.msg)
    except:
        print "Can't connect to Zokeeper!"
        return
    reactor.run()
Exemplo n.º 9
0
    def setUp(self):
        super(RetryClientConnectionLossTest, self).setUp()

        from twisted.internet import reactor
        self.proxy = ProxyFactory("127.0.0.1", 2181)
        self.proxy_port = reactor.listenTCP(0, self.proxy)
        host = self.proxy_port.getHost()
        self.proxied_client = RetryClient(ZookeeperClient(
            "%s:%s" % (host.host, host.port)))
        self.direct_client = ZookeeperClient("127.0.0.1:2181", 3000)
        self.session_events = []

        def session_event_collector(conn, event):
            self.session_events.append(event)

        self.proxied_client.set_session_callback(session_event_collector)
        return self.direct_client.connect()
Exemplo n.º 10
0
    def expire_session(self, wait=True, retry=0):
        assert self.client.connected
        # if wait:
        #    d = self.client.subscribe_new_session()
        client_id = self.client.client_id
        self.client2 = ZookeeperClient(self.client.servers)
        yield self.client2.connect(client_id=client_id)
        yield self.client2.close()

        # It takes some time to propagate (1/3 session time as ping)
        if wait:
            yield self.sleep(2)
            client_new_id = self.client.client_id
            # Crappy workaround to c libzk bug/issue see http://goo.gl/9ei5c
            # Works most of the time.. but bound it when it doesn't. lame!
            if client_id[0] == client_new_id[0] and retry < 10:
                yield self.expire_session(wait, retry + 1)
Exemplo n.º 11
0
class SessionClientExpireTests(ZookeeperTestCase):
    """Verify expiration behavior."""

    def setUp(self):
        super(SessionClientExpireTests, self).setUp()
        self.client = managed.ManagedClient("127.0.0.1:2181", 3000)
        self.client2 = None
        return self.client.connect()

    @inlineCallbacks
    def tearDown(self):
        self.client.close()

        self.client2 = ZookeeperClient("127.0.0.1:2181")
        yield self.client2.connect()
        utils.deleteTree(handle=self.client2.handle)
        yield self.client2.close()
        super(SessionClientExpireTests, self).tearDown()

    @inlineCallbacks
    def expire_session(self):
        assert self.client.connected
        self.client2 = ZookeeperClient(self.client.servers)
        yield self.client2.connect(client_id=self.client.client_id)
        yield self.client2.close()
        # It takes some time to propagate (1/3 session time as ping)
        yield self.sleep(2)

    @inlineCallbacks
    def test_session_expiration_conn(self):
        session_id = self.client.client_id[0]
        yield self.client.create("/fo-1", "abc")
        yield self.expire_session()
        yield self.client.exists("/")
        self.assertNotEqual(session_id, self.client.client_id[0])

    @inlineCallbacks
    def test_session_expiration_notification(self):
        session_id = self.client.client_id[0]
        c_d, w_d = self.client.get_and_watch("/")
        yield c_d
        d = self.client.subscribe_new_session()
        self.assertFalse(d.called)
        yield self.expire_session()
        yield d
        yield w_d
        self.assertNotEqual(session_id, self.client.client_id[0])

    @inlineCallbacks
    def test_session_expiration_conn_watch(self):
        session_id = self.client.client_id[0]
        yield self.client.create("/fo-1", "abc")
        yield self.expire_session()
        yield self.client.exists("/")
        self.assertNotEqual(session_id, self.client.client_id[0])

    @inlineCallbacks
    def test_invoked_watch_gc(self):
        c_d, w_d = yield self.client.get_children_and_watch("/")
        yield c_d
        yield self.client.create("/foo")
        yield w_d
        yield self.expire_session()
        yield self.client.create("/foo2")
        # Nothing should blow up
        yield self.sleep(0.2)

    @inlineCallbacks
    def test_ephemeral_and_watch_recreate(self):
        # Create some ephemeral nodes
        yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
        yield self.client.create("/fo-2", "def", flags=zookeeper.EPHEMERAL)

        # Create some watches
        g_d, g_w_d = self.client.get_and_watch("/fo-1")
        yield g_d

        c_d, c_w_d = self.client.get_children_and_watch("/")
        yield g_d

        e_d, e_w_d = self.client.get_children_and_watch("/fo-2")
        yield e_d

        # Expire the session
        yield self.expire_session()

        # Poof

        # Ephemerals back
        c, s = yield self.client.get("/fo-1")
        self.assertEqual(c, "abc")

        c, s = yield self.client.get("/fo-2")
        self.assertEqual(c, "def")

        # Watches triggered
        yield DeferredList(
            [g_w_d, c_w_d, e_w_d],
            fireOnOneErrback=True, consumeErrors=True)

        self.assertEqual(
            [str(d.result) for d in (g_w_d, c_w_d, e_w_d)],
            ["<ClientEvent session at '/fo-1' state: connected>",
             "<ClientEvent session at '/' state: connected>",
             "<ClientEvent session at '/fo-2' state: connected>"])

    @inlineCallbacks
    def test_ephemeral_no_track_sequence_nodes(self):
        """ Ephemeral tracking ignores sequence nodes.
        """
        yield self.client.create("/music", "abc")
        yield self.client.create(
            "/music/u2-", "abc",
            flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE)
        yield self.expire_session()
        children = yield self.client.get_children("/music")
        self.assertEqual(children, [])

    @inlineCallbacks
    def test_ephemeral_content_modification(self):
        yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
        yield self.client.set("/fo-1", "def")
        yield self.expire_session()
        c, s = yield self.client.get("/fo-1")
        self.assertEqual(c, "def")

    @inlineCallbacks
    def test_ephemeral_acl_modification(self):
        yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
        acl = [test_client.PUBLIC_ACL,
               dict(scheme="digest",
                    id="zebra:moon",
                    perms=zookeeper.PERM_ALL)]
        yield self.client.set_acl("/fo-1", acl)
        yield self.expire_session()
        n_acl, stat = yield self.client.get_acl("/fo-1")
        self.assertEqual(acl, n_acl)

    @inlineCallbacks
    def test_ephemeral_deletion(self):
        yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
        yield self.client.delete("/fo-1")
        yield self.expire_session()
        self.assertFalse((yield self.client.exists("/fo-1")))
Exemplo n.º 12
0
class RetryClientConnectionLossTest(ZookeeperTestCase):

    def setUp(self):
        super(RetryClientConnectionLossTest, self).setUp()

        from twisted.internet import reactor
        self.proxy = ProxyFactory("127.0.0.1", 2181)
        self.proxy_port = reactor.listenTCP(0, self.proxy)
        host = self.proxy_port.getHost()
        self.proxied_client = RetryClient(ZookeeperClient(
            "%s:%s" % (host.host, host.port)))
        self.direct_client = ZookeeperClient("127.0.0.1:2181", 3000)
        self.session_events = []

        def session_event_collector(conn, event):
            self.session_events.append(event)

        self.proxied_client.set_session_callback(session_event_collector)
        return self.direct_client.connect()

    @inlineCallbacks
    def tearDown(self):
        import zookeeper
        zookeeper.set_debug_level(0)
        if self.proxied_client.connected:
            yield self.proxied_client.close()
        if not self.direct_client.connected:
            yield self.direct_client.connect()
        utils.deleteTree(handle=self.direct_client.handle)
        yield self.direct_client.close()
        self.proxy.lose_connection()
        yield self.proxy_port.stopListening()

    @inlineCallbacks
    def test_get_children_and_watch(self):
        yield self.proxied_client.connect()

        # Setup tree
        cpath = "/test-tree"
        yield self.direct_client.create(cpath)

        # Block the request (drops all packets.)
        self.proxy.set_blocked(True)
        child_d, watch_d = self.proxied_client.get_children_and_watch(cpath)

        # Unblock and disconnect
        self.proxy.set_blocked(False)
        self.proxy.lose_connection()

        # Call goes through
        self.assertEqual((yield child_d), [])
        self.assertEqual(len(self.session_events), 2)

        # And we have reconnect events
        self.assertEqual(self.session_events[-1].state_name, "connected")

        yield self.direct_client.create(cpath + "/abc")

        # The original watch is still active
        yield watch_d

    @inlineCallbacks
    def test_exists_and_watch(self):
        yield self.proxied_client.connect()

        cpath = "/test-tree"

        # Block the request
        self.proxy.set_blocked(True)
        exists_d, watch_d = self.proxied_client.exists_and_watch(cpath)

        # Create the node
        yield self.direct_client.create(cpath)

        # Unblock and disconnect
        self.proxy.set_blocked(False)
        self.proxy.lose_connection()

        # Call gets retried, see the latest state
        self.assertTrue((yield exists_d))
        self.assertEqual(len(self.session_events), 2)

        # And we have reconnect events
        self.assertEqual(self.session_events[-1].state_name, "connected")

        yield self.direct_client.delete(cpath)

        # The original watch is still active
        yield watch_d

    @inlineCallbacks
    def test_get_and_watch(self):
        yield self.proxied_client.connect()

        # Setup tree
        cpath = "/test-tree"
        yield self.direct_client.create(cpath)

        # Block the request (drops all packets.)
        self.proxy.set_blocked(True)
        get_d, watch_d = self.proxied_client.get_and_watch(cpath)

        # Unblock and disconnect
        self.proxy.set_blocked(False)
        self.proxy.lose_connection()

        # Call goes through
        content, stat = yield get_d
        self.assertEqual(content, '')
        self.assertEqual(len(self.session_events), 2)

        # And we have reconnect events
        self.assertEqual(self.session_events[-1].state_name, "connected")

        yield self.direct_client.delete(cpath)

        # The original watch is still active
        yield watch_d

    @inlineCallbacks
    def test_set(self):
        yield self.proxied_client.connect()

        # Setup tree
        cpath = "/test-tree"
        yield self.direct_client.create(cpath, json.dumps({"a": 1, "c": 2}))

        def update_node(content, stat):
            data = json.loads(content)
            data["a"] += 1
            data["b"] = 0
            return json.dumps(data)

        # Block the request (drops all packets.)
        self.proxy.set_blocked(True)
        mod_d = retry_change(self.proxied_client, cpath, update_node)

        # Unblock and disconnect
        self.proxy.set_blocked(False)
        self.proxy.lose_connection()

        # Call goes through, contents verified.
        yield mod_d
        content, stat = yield self.direct_client.get(cpath)
        self.assertEqual(json.loads(content),
                         {"a": 2, "b": 0, "c": 2})
Exemplo n.º 13
0
class SessionClientExpireTests(ZookeeperTestCase):
    """Verify expiration behavior."""

    def setUp(self):
        super(SessionClientExpireTests, self).setUp()
        self.client = managed.ManagedClient("127.0.0.1:2181", 3000)
        self.client2 = None
        self.output = self.capture_log(level=logging.DEBUG)
        return self.client.connect()

    @inlineCallbacks
    def tearDown(self):
        self.client.close()
        self.client2 = ZookeeperClient("127.0.0.1:2181")
        yield self.client2.connect()
        utils.deleteTree(handle=self.client2.handle)
        yield self.client2.close()
        super(SessionClientExpireTests, self).tearDown()

    @inlineCallbacks
    def expire_session(self, wait=True, retry=0):
        assert self.client.connected
        # if wait:
        #    d = self.client.subscribe_new_session()
        client_id = self.client.client_id
        self.client2 = ZookeeperClient(self.client.servers)
        yield self.client2.connect(client_id=client_id)
        yield self.client2.close()

        # It takes some time to propagate (1/3 session time as ping)
        if wait:
            yield self.sleep(2)
            client_new_id = self.client.client_id
            # Crappy workaround to c libzk bug/issue see http://goo.gl/9ei5c
            # Works most of the time.. but bound it when it doesn't. lame!
            if client_id[0] == client_new_id[0] and retry < 10:
                yield self.expire_session(wait, retry + 1)

    @inlineCallbacks
    def test_session_expiration_conn(self):
        d = self.client.subscribe_new_session()
        session_id = self.client.client_id[0]
        yield self.client.create("/fo-1", "abc")
        yield self.expire_session(wait=True)
        yield d
        stat = yield self.client.exists("/")
        self.assertTrue(stat)
        self.assertNotEqual(session_id, self.client.client_id[0])

    @inlineCallbacks
    def test_session_expiration_notification(self):
        session_id = self.client.client_id[0]
        c_d, w_d = self.client.get_and_watch("/")
        yield c_d
        d = self.client.subscribe_new_session()
        self.assertFalse(d.called)
        yield self.expire_session(wait=True)
        yield d
        yield w_d
        self.assertNotEqual(session_id, self.client.client_id[0])

    @inlineCallbacks
    def test_invoked_watch_gc(self):
        c_d, w_d = yield self.client.get_children_and_watch("/")
        yield c_d
        yield self.client.create("/foo")
        yield w_d
        yield self.expire_session()
        yield self.client.create("/foo2")
        # Nothing should blow up
        yield self.sleep(0.2)

    @inlineCallbacks
    def test_app_usage_error_bypass_retry(self):
        """App errors shouldn't trigger a reconnect."""
        output = self.capture_log(level=logging.DEBUG)
        yield self.assertFailure(self.client.get("/abc"), zookeeper.NoNodeException)
        self.assertNotIn("Persistent retry error", output.getvalue())

    @inlineCallbacks
    def test_ephemeral_and_watch_recreate(self):
        # Create some ephemeral nodes
        yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
        yield self.client.create("/fo-2", "def", flags=zookeeper.EPHEMERAL)

        # Create some watches
        g_d, g_w_d = self.client.get_and_watch("/fo-1")
        yield g_d

        c_d, c_w_d = self.client.get_children_and_watch("/")
        yield g_d

        e_d, e_w_d = self.client.get_children_and_watch("/fo-2")
        yield e_d

        # Expire the session
        yield self.expire_session()

        # Poof

        # Ephemerals back
        c, s = yield self.client.get("/fo-1")
        self.assertEqual(c, "abc")

        c, s = yield self.client.get("/fo-2")
        self.assertEqual(c, "def")

        # Watches triggered
        yield DeferredList([g_w_d, c_w_d, e_w_d], fireOnOneErrback=True, consumeErrors=True)

        h = self.client.handle
        self.assertEqual(
            [str(d.result) for d in (g_w_d, c_w_d, e_w_d)],
            [
                "<ClientEvent session at '/fo-1' state: connected handle:%d>" % h,
                "<ClientEvent session at '/' state: connected handle:%d>" % h,
                "<ClientEvent session at '/fo-2' state: connected handle:%d>" % h,
            ],
        )

    @inlineCallbacks
    def test_ephemeral_no_track_sequence_nodes(self):
        """ Ephemeral tracking ignores sequence nodes.
        """
        yield self.client.create("/music", "abc")
        yield self.client.create("/music/u2-", "abc", flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE)
        yield self.expire_session()
        children = yield self.client.get_children("/music")
        self.assertEqual(children, [])

    @inlineCallbacks
    def test_ephemeral_content_modification(self):
        yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
        yield self.client.set("/fo-1", "def")
        yield self.expire_session()
        c, s = yield self.client.get("/fo-1")
        self.assertEqual(c, "def")

    @inlineCallbacks
    def test_ephemeral_acl_modification(self):
        yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
        acl = [test_client.PUBLIC_ACL, dict(scheme="digest", id="zebra:moon", perms=zookeeper.PERM_ALL)]
        yield self.client.set_acl("/fo-1", acl)
        yield self.expire_session()
        n_acl, stat = yield self.client.get_acl("/fo-1")
        self.assertEqual(acl, n_acl)

    @inlineCallbacks
    def test_ephemeral_deletion(self):
        yield self.client.create("/fo-1", "abc", flags=zookeeper.EPHEMERAL)
        yield self.client.delete("/fo-1")
        yield self.expire_session()
        self.assertFalse((yield self.client.exists("/fo-1")))
Exemplo n.º 14
0
 def new_connection(node_stat):
     self.assertFalse(node_stat)
     self.client2 = ZookeeperClient("127.0.0.1:2181")
     return self.client2.connect()
Exemplo n.º 15
0
class ClientTests(ZookeeperTestCase):

    def setUp(self):
        super(ClientTests, self).setUp()
        self.client = ZookeeperClient("127.0.0.1:2181", 3000)
        self.client2 = None

    def tearDown(self):
        if self.client.connected:
            utils.deleteTree(handle=self.client.handle)
            self.client.close()

        if self.client2 and self.client2.connected:
            self.client2.close()

        super(ClientTests, self).tearDown()

    def test_wb_connect_after_timeout(self):
        """
        Test an odd error scenario. If the zookeeper client succeeds in
        connecting after a timeout, the connection should be closed, as
        the connect deferred has already fired.
        """
        mock_client = self.mocker.patch(self.client)
        mock_client.close()

        def close_state():
            # Ensure the client state variable is correct after the close call.
            self.client.connected = False

        self.mocker.call(close_state)
        self.mocker.replay()

        task = DelayedCall(1, lambda: 1, None, None, None, None)
        task.called = True

        d = Deferred()
        d.errback(ConnectionTimeoutException())

        self.client._cb_connected(
            task, d, None, zookeeper.CONNECTED_STATE, "/")

        self.failUnlessFailure(d, ConnectionTimeoutException)
        return d

    def test_wb_reconnect_after_timeout_and_close(self):
        """
        Another odd error scenario, if a client instance has has
        connect and closed methods invoked in succession multiple
        times, and a previous callback connect timeouts, the callback
        of a previous connect can be invoked by a subsequent connect,
        with a CONNECTING_STATE. Verify this does not attempt to
        invoke the connect deferred again.
        """
        d = Deferred()
        d.callback(True)

        task = DelayedCall(1, lambda: 1, None, None, None, None)
        task.called = True

        self.assertEqual(
            self.client._cb_connected(
                task, d, None, zookeeper.CONNECTING_STATE, ""),
            None)

    def test_connect(self):
        """
        The client can connect to a zookeeper instance.
        """
        d = self.client.connect()

        def check_connected(client):
            self.assertEquals(client.connected, True)
            self.assertEquals(client.state, zookeeper.CONNECTED_STATE)
        d.addCallback(check_connected)
        return d

    def test_close(self):
        """
        Test that the connection is closed, also for the first
        connection when the zookeeper handle is 0.
        """

        def _fake_init(*_):
            return 0

        mock_init = self.mocker.replace("zookeeper.init")
        mock_init(ARGS)
        self.mocker.call(_fake_init)

        def _fake_close(handle):
            return zookeeper.OK

        mock_close = self.mocker.replace("zookeeper.close")
        mock_close(0)
        self.mocker.call(_fake_close)

        self.mocker.replay()

        # Avoid unclean reactor by letting the callLater go through,
        # but we do not care about the timeout.
        def _silence_timeout(failure):
            failure.trap(ConnectionTimeoutException)
        self.client.connect(timeout=0).addErrback(_silence_timeout)

        d = maybeDeferred(self.client.close)

        def _verify(result):
            self.mocker.verify()
        d.addCallback(_verify)
        return d

    def test_client_event_repr(self):
        event = ClientEvent(zookeeper.SESSION_EVENT,
                            zookeeper.EXPIRED_SESSION_STATE, '', 0)
        self.assertEqual(
            repr(event),
            "<ClientEvent session at '' state: expired handle:0>")

    def test_client_event_attributes(self):
        event = ClientEvent(4, 'state', 'path', 0)
        self.assertEqual(event.type, 4)
        self.assertEqual(event.connection_state, 'state')
        self.assertEqual(event.path, 'path')
        self.assertEqual(event.handle, 0)
        self.assertEqual(event, (4, 'state', 'path', 0))

    def test_client_use_while_disconnected_returns_failure(self):
        return self.assertFailure(
            self.client.exists("/"), NotConnectedException)

    def test_create_ephemeral_node_and_close_connection(self):
        """
        The client can create transient nodes that are destroyed when the
        client is closed and the session is destroyed on the zookeeper servers.
        """
        d = self.client.connect()

        def test_create_ephemeral_node(client):
            d = self.client.create(
                "/foobar-transient", "rabbit", flags=zookeeper.EPHEMERAL)
            return d

        def check_node_path(path):
            self.assertEqual(path, "/foobar-transient")
            return path

        def close_connection(path):
            return self.client.close()

        def new_connection(close_result):
            self.client2 = new_client = ZookeeperClient("127.0.0.1:2181")
            return new_client.connect()

        def check_node_doesnt_exist(connected):
            self.assertRaises(
                zookeeper.NoNodeException,
                zookeeper.get,
                connected.handle,
                "/foobar-transient")
            self.client2.close()

        d.addCallback(test_create_ephemeral_node)
        d.addCallback(check_node_path)
        d.addCallback(close_connection)
        d.addCallback(new_connection)
        d.addCallback(check_node_doesnt_exist)
        return d

    def test_create_node(self):
        """
        We can create a node in zookeeper, with a given path
        """
        d = self.client.connect()

        def create_ephemeral_node(connected):
            d = self.client.create(
                "/foobar", "rabbit", flags=zookeeper.EPHEMERAL)
            return d

        def verify_node_path_and_content(path):
            self.assertEqual(path, "/foobar")
            self.assertNotEqual(
                zookeeper.exists(self.client.handle, path), None)
            data, stat = zookeeper.get(self.client.handle, path)
            self.assertEqual(data, "rabbit")

        d.addCallback(create_ephemeral_node)
        d.addCallback(verify_node_path_and_content)
        return d

    def test_create_persistent_node_and_close(self):
        """
        The client creates persistent nodes by default that exist independently
        of the client session.
        """
        d = self.client.connect()

        def test_create_ephemeral_node(client):
            d = self.client.create(
                "/foobar-persistent", "rabbit")
            return d

        def check_node_path(path):
            self.assertEqual(path, "/foobar-persistent")
            self.assertNotEqual(
                zookeeper.exists(self.client.handle, path), None)
            return path

        def close_connection(path):
            self.client.close()
            self.client2 = new_client = ZookeeperClient("127.0.0.1:2181")
            return new_client.connect()

        def check_node_exists(client):
            data, stat = zookeeper.get(client.handle, "/foobar-persistent")
            self.assertEqual(data, "rabbit")

        d.addCallback(test_create_ephemeral_node)
        d.addCallback(check_node_path)
        d.addCallback(close_connection)
        d.addCallback(check_node_exists)
        return d

    def test_get(self):
        """
        The client can retrieve a node's data via its get method.
        """
        d = self.client.connect()

        def create_node(client):
            d = self.client.create(
                "/foobar-transient", "rabbit", flags=zookeeper.EPHEMERAL)
            return d

        def get_contents(path):
            return self.client.get(path)

        def verify_contents((data, stat)):
            self.assertEqual(data, "rabbit")

        d.addCallback(create_node)
        d.addCallback(get_contents)
        d.addCallback(verify_contents)
        return d

    def test_get_with_error(self):
        """
        On get error the deferred's errback is raised.
        """
        d = self.client.connect()

        def get_contents(client):
            return client.get("/foobar-transient")

        def verify_failure(failure):
            self.assertTrue(
                isinstance(failure.value, zookeeper.NoNodeException))

        def assert_failure(extra):
            self.fail("get should have failed")

        d.addCallback(get_contents)
        d.addCallback(verify_failure)
        d.addErrback(verify_failure)
        return d

    def test_get_with_watcher(self):
        """
        The client can specify a callable watcher when invoking get. The
        watcher will be called back when the client path is modified in
        another session.
        """
        d = self.client.connect()
        watch_deferred = Deferred()

        def create_node(client):
            return self.client.create("/foobar-watched", "rabbit")

        def get_node(path):
            data, watch = self.client.get_and_watch(path)
            watch.chainDeferred(watch_deferred)
            return data

        def new_connection(data):
            self.client2 = ZookeeperClient("127.0.0.1:2181")
            return self.client2.connect()

        def trigger_watch(client):
            zookeeper.set(self.client2.handle, "/foobar-watched", "abc")
            return watch_deferred

        def verify_watch(event):
            self.assertEqual(event.path, "/foobar-watched")
            self.assertEqual(event.type, zookeeper.CHANGED_EVENT)

        d.addCallback(create_node)
        d.addCallback(get_node)
        d.addCallback(new_connection)
        d.addCallback(trigger_watch)
        d.addCallback(verify_watch)
        return d

    def test_get_with_watcher_and_delete(self):
        """
        The client can specify a callable watcher when invoking get. The
        watcher will be called back when the client path is modified in
        another session.
        """
        d = self.client.connect()

        def create_node(client):
            return self.client.create("/foobar-watched", "rabbit")

        def get_node(path):
            data, watch = self.client.get_and_watch(path)
            return data.addCallback(lambda x: (watch,))

        def new_connection((watch,)):
            self.client2 = ZookeeperClient("127.0.0.1:2181")
            return self.client2.connect().addCallback(
                lambda x, y=None, z=None: (x, watch))

        def trigger_watch((client, watch)):
            zookeeper.delete(self.client2.handle, "/foobar-watched")
            self.client2.close()
            return watch

        def verify_watch(event):
            self.assertEqual(event.path, "/foobar-watched")
            self.assertEqual(event.type, zookeeper.DELETED_EVENT)

        d.addCallback(create_node)
        d.addCallback(get_node)
        d.addCallback(new_connection)
        d.addCallback(trigger_watch)
        d.addCallback(verify_watch)
        return d

    def test_delete(self):
        """
        The client can delete a node via its delete method.
        """
        d = self.client.connect()

        def create_node(client):
            return self.client.create(
                "/foobar-transient", "rabbit", flags=zookeeper.EPHEMERAL)

        def verify_exists(path):
            self.assertNotEqual(
                zookeeper.exists(self.client.handle, path), None)
            return path

        def delete_node(path):
            return self.client.delete(path)

        def verify_not_exists(*args):
            self.assertEqual(
                zookeeper.exists(self.client.handle, "/foobar-transient"),
                None)

        d.addCallback(create_node)
        d.addCallback(verify_exists)
        d.addCallback(delete_node)
        d.addCallback(verify_not_exists)
        return d

    def test_exists_with_existing(self):
        """
        The exists method returns node stat information for an existing node.
        """
        d = self.client.connect()

        def create_node(client):
            return self.client.create(
                "/foobar-transient", "rabbit", flags=zookeeper.EPHEMERAL)

        def check_exists(path):
            return self.client.exists(path)

        def verify_exists(node_stat):
            self.assertEqual(node_stat["dataLength"], 6)
            self.assertEqual(node_stat["version"], 0)

        d.addCallback(create_node)
        d.addCallback(check_exists)
        d.addCallback(verify_exists)
        return d

    def test_exists_with_error(self):
        """
        On error exists invokes the errback with the exception.
        """
        d = self.client.connect()

        def inject_error(result_code, d, extra_codes=None, path=None):
            error = SyntaxError()
            d.errback(error)
            return error

        def check_exists(client):
            mock_client = self.mocker.patch(client)
            mock_client._check_result(
                ANY, DEFERRED_MATCH, extra_codes=(zookeeper.NONODE,),
                path="/zebra-moon")
            self.mocker.call(inject_error)
            self.mocker.replay()
            return client.exists("/zebra-moon")

        def verify_failure(failure):
            self.assertTrue(isinstance(failure.value, SyntaxError))

        d.addCallback(check_exists)
        d.addErrback(verify_failure)
        return d

    def test_exists_with_nonexistant(self):
        """
        The exists method returns None when the value node doesn't exist.
        """
        d = self.client.connect()

        def check_exists(client):
            return self.client.exists("/abcdefg")

        def verify_exists(node_stat):
            self.assertEqual(node_stat, None)

        d.addCallback(check_exists)
        d.addCallback(verify_exists)
        return d

    def test_exist_watch_with_node_change(self):
        """
        Setting an exist watches on existing node will also respond to
        node changes.
        """
        d = self.client.connect()

        def create_node(client):
            return client.create("/rome")

        def check_exists(path):
            existsd, w = self.client.exists_and_watch(path)
            w.addCallback(node_watcher)
            return existsd

        def node_watcher(event):
            self.assertEqual(event.type_name, "changed")

        def verify_exists(node_stat):
            self.assertTrue(node_stat)
            return self.client.set("/rome", "magic")

        d.addCallback(create_node)
        d.addCallback(check_exists)
        d.addCallback(verify_exists)
        return d

    def test_exists_with_watcher_and_close(self):
        """
        Closing a connection with an watch outstanding behaves correctly.
        """
        d = self.client.connect()

        def node_watcher(event):
            client = getattr(self, "client", None)
            if client is not None and client.connected:
                self.fail("Client should be disconnected")

        def create_node(client):
            return client.create("/syracuse")

        def check_exists(path):
            # shouldn't fire till unit test cleanup
            d, w = self.client.exists_and_watch(path)
            w.addCallback(node_watcher)
            return d

        def verify_exists(result):
            self.assertTrue(result)

        d.addCallback(create_node)
        d.addCallback(check_exists)
        d.addCallback(verify_exists)
        return d

    def test_exists_with_nonexistant_watcher(self):
        """
        The exists method can also be used to set an optional watcher on a
        node. The watch can be set on a node that does not yet exist.
        """
        d = self.client.connect()
        node_path = "/animals"
        watcher_deferred = Deferred()

        def create_container(path):
            return self.client.create(node_path, "")

        def check_exists(path):
            exists, watch = self.client.exists_and_watch(
                "%s/wooly-mammoth" % node_path)
            watch.chainDeferred(watcher_deferred)
            return exists

        def new_connection(node_stat):
            self.assertFalse(node_stat)
            self.client2 = ZookeeperClient("127.0.0.1:2181")
            return self.client2.connect()

        def create_node(client):
            self.assertEqual(client.connected, True)
            return self.client2.create(
                "%s/wooly-mammoth" % node_path, "extinct")

        def shim(path):
            return watcher_deferred

        def verify_watch(event):
            self.assertEqual(event.path, "%s/wooly-mammoth" % node_path)
            self.assertEqual(event.type, zookeeper.CREATED_EVENT)

        d.addCallback(create_container)
        d.addCallback(check_exists)
        d.addCallback(new_connection)
        d.addCallback(create_node)
        d.addCallback(shim)
        d.addCallback(verify_watch)

        return d

    def test_create_sequence_node(self):
        """
        The client can create a monotonically increasing sequence nodes.
        """
        d = self.client.connect()

        def create_node(client):
            return self.client.create("/seq-a")

        def create_seq_node(path):
            return self.client.create(
                "/seq-a/seq-", flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE)

        def get_children(path):
            return self.client.get_children("/seq-a")

        def verify_children(children):
            self.assertEqual(children, ["seq-0000000000", "seq-0000000001"])

        d.addCallback(create_node)
        d.addCallback(create_seq_node)
        d.addCallback(create_seq_node)
        d.addCallback(get_children)
        d.addCallback(verify_children)
        return d

    def test_create_duplicate_node(self):
        """
        Attempting to create a node that already exists results in a failure.
        """
        d = self.client.connect()

        def create_node(client):
            return self.client.create("/abc")

        def create_duplicate(path):
            return self.client.create("/abc")

        def verify_fails(*args):
            self.fail("Invoked Callback")

        def verify_succeeds(failure):
            self.assertTrue(failure)
            self.assertEqual(failure.value.args, ("node exists",))

        d.addCallback(create_node)
        d.addCallback(create_duplicate)
        d.addCallback(verify_fails)
        d.addErrback(verify_succeeds)
        return d

    def test_delete_nonexistant_node(self):
        """
        Attempting to delete a node that already exists results in a failure.
        """
        d = self.client.connect()

        def delete_node(client):
            return client.delete("/abcd")

        def verify_fails(*args):
            self.fail("Invoked Callback")

        def verify_succeeds(failure):
            self.assertTrue(failure)
            self.assertEqual(
                failure.value.args, ("no node /abcd",))

        d.addCallback(delete_node)
        d.addCallback(verify_fails)
        d.addErrback(verify_succeeds)
        return d

    def test_set(self):
        """
        The client can be used to set contents of a node.
        """
        d = self.client.connect()

        def create_node(client):
            return client.create("/zebra", "horse")

        def set_node(path):
            return self.client.set("/zebra", "mammal")

        def verify_contents(junk):
            self.assertEqual(zookeeper.get(self.client.handle, "/zebra")[0],
                             "mammal")

        d.addCallback(create_node)
        d.addCallback(set_node)
        d.addCallback(verify_contents)
        return d

    def test_set_nonexistant(self):
        """
        if the client is used to set the contents of a nonexistant node
        an error is raised.
        """
        d = self.client.connect()

        def set_node(client):
            return client.set("/xy1")

        def verify_fails(*args):
            self.fail("Invoked Callback")

        def verify_succeeds(failure):
            self.assertTrue(failure)
            self.assertTrue(
                failure.value.args, ("no node /xy1"))

        d.addCallback(set_node)
        d.addCallback(verify_fails)
        d.addErrback(verify_succeeds)
        return d

    def test_get_children(self):

        d = self.client.connect()

        def create_nodes(client):
            zookeeper.create(
                self.client.handle, "/tower", "", [PUBLIC_ACL], 0)
            zookeeper.create(
                self.client.handle, "/tower/london", "", [PUBLIC_ACL], 0)
            zookeeper.create(
                self.client.handle, "/tower/paris", "", [PUBLIC_ACL], 0)

            return client

        def get_children(client):
            return client.get_children("/tower")

        def verify_children(children):
            self.assertEqual(children, ["paris", "london"])

        d.addCallback(create_nodes)
        d.addCallback(get_children)
        d.addCallback(verify_children)
        return d

    def test_get_children_with_error(self):
        """If the result of an api call is an error, its propgated.
        """
        d = self.client.connect()

        def get_children(client):
            # Get the children of a nonexistant node
            return client.get_children("/tower")

        def verify_failure(failure):
            self.assertTrue(isinstance(failure, Failure))
            self.assertTrue(
                isinstance(failure.value, zookeeper.NoNodeException))

        d.addCallback(get_children)
        d.addBoth(verify_failure)
        return d

    # seems to be a segfault on this one, must be running latest zk
    def test_get_children_with_watch(self):
        """
        The get_children method optionally takes a watcher callable which will
        be notified when the node is modified, or a child deleted or added.
        """
        d = self.client.connect()
        watch_deferred = Deferred()

        def create_node(client):
            return client.create("/jupiter")

        def get_children(path):
            ids, watch = self.client.get_children_and_watch(path)
            watch.chainDeferred(watch_deferred)
            return ids

        def new_connection(children):
            self.assertFalse(children)
            self.client2 = ZookeeperClient("127.0.0.1:2181")
            return self.client2.connect()

        def trigger_watch(client):
            zookeeper.create(
                self.client2.handle, "/jupiter/io", "", [PUBLIC_ACL], 0)
            return watch_deferred

        def verify_observed(data):
            self.assertTrue(data)

        d.addCallback(create_node)
        d.addCallback(get_children)
        d.addCallback(new_connection)
        d.addCallback(trigger_watch)
        d.addCallback(verify_observed)
        return d

    def test_get_children_with_watch_container_deleted(self):
        """
        Establishing a child watch on a path, and then deleting the path,
        will fire a child event watch on the container. This seems a little
        counterintutive, but zookeeper docs state they do this as a signal
        the container will never have any children. And logically you'd
        would want to fire, so that in case the container node gets recreated
        later and the watch fires, you don't want to the watch to fire then,
        as its a technically a different container.
        """

        d = self.client.connect()
        watch_deferred = Deferred()

        def create_node(client):
            return self.client.create("/prison")

        def get_children(path):
            childd, w = self.client.get_children_and_watch(path)
            w.addCallback(verify_watch)
            return childd

        def delete_node(children):
            return self.client.delete("/prison")

        def verify_watch(event):
            self.assertTrue(event.type_name, "child")
            watch_deferred.callback(None)

        d.addCallback(create_node)
        d.addCallback(get_children)
        d.addCallback(delete_node)

        return watch_deferred

    test_get_children_with_watch_container_deleted.timeout = 5

    def test_get_no_children(self):
        """
        Getting children of a node without any children returns an empty list.
        """
        d = self.client.connect()

        def create_node(client):
            return self.client.create("/tower")

        def get_children(path):
            return self.client.get_children(path)

        def verify_children(children):
            self.assertEqual(children, [])

        d.addCallback(create_node)
        d.addCallback(get_children)
        d.addCallback(verify_children)
        return d

    def test_get_children_nonexistant(self):
        """
        Getting children of a nonexistant node raises a no node exception.
        """
        d = self.client.connect()

        def get_children(client):
            return client.get_children("/tower")

        d.addCallback(get_children)
        self.failUnlessFailure(d, zookeeper.NoNodeException)
        return d

    def test_add_auth(self):
        """
        The connection can have zero or more authentication infos. This
        authentication infos are used when accessing nodes to veriy access
        against the node's acl.
        """
        d = self.client.connect()

        credentials = "mary:apples"
        user, password = credentials.split(":")
        identity = "%s:%s" % (
            user,
            base64.b64encode(hashlib.new('sha1', credentials).digest()))
        acl = {'id': identity, 'scheme': 'digest', 'perms': zookeeper.PERM_ALL}
        failed = []

        def add_auth_one(client):
            d = client.add_auth("digest", "bob:martini")
            # a little hack to avoid slowness around adding auth
            # see https://issues.apache.org/jira/browse/ZOOKEEPER-770
            # by pushing an additional message send/response cycle
            # we don't have to wait for the io thread to timeout
            # on the socket.
            client.exists("/orchard")
            return d

        def create_node(client):
            return client.create("/orchard", "apple trees", acls=[acl])

        def try_node_access(path):
            return self.client.set("/orchard", "bar")

        def node_access_failed(failure):
            self.assertEqual(failure.value.args, ("not authenticated /orchard",))
            failed.append(True)
            return

        def add_auth_two(result):
            d = self.client.add_auth("digest", credentials)
            # a little hack to avoid slowness around adding auth
            # see https://issues.apache.org/jira/browse/ZOOKEEPER-770
            self.client.get_children("/orchard")
            return d

        def verify_node_access(stat):
            self.assertEqual(stat['version'], 1)
            self.assertEqual(stat['dataLength'], 3)
            self.assertTrue(failed)  # we should have hit the errback

        d.addCallback(add_auth_one)
        d.addCallback(create_node)
        d.addCallback(try_node_access)
        d.addErrback(node_access_failed)
        d.addCallback(add_auth_two)
        d.addCallback(try_node_access)
        d.addCallback(verify_node_access)

        return d

    def test_add_auth_with_error(self):
        """
        On add_auth error the deferred errback is invoked with the exception.
        """
        d = self.client.connect()

        def _fake_auth(handle, scheme, identity, callback):
            callback(0, zookeeper.AUTHFAILED)
            return 0

        mock_auth = self.mocker.replace("zookeeper.add_auth")
        mock_auth(ANY, ANY, ANY, ANY)
        self.mocker.call(_fake_auth)
        self.mocker.replay()

        def add_auth(client):
            d = self.client.add_auth("digest", "mary:lamb")
            return d

        def verify_failure(failure):
            self.assertTrue(
                isinstance(failure.value, zookeeper.AuthFailedException))

        def assert_failed(result):
            self.fail("should not get here")

        d.addCallback(add_auth)
        d.addCallback(assert_failed)
        d.addErrback(verify_failure)
        return d

    def test_set_acl(self):
        """
        The client can be used to set an ACL on a node.
        """

        d = self.client.connect()

        acl = [PUBLIC_ACL,
               dict(scheme="digest",
                    id="zebra:moon",
                    perms=zookeeper.PERM_ALL)]

        def create_node(client):
            return client.create("/moose")

        def set_acl(path):
            return self.client.set_acl(path, acl)

        def verify_acl(junk):
            self.assertEqual(
                zookeeper.get_acl(self.client.handle, "/moose")[1],
                acl)

        d.addCallback(create_node)
        d.addCallback(set_acl)
        d.addCallback(verify_acl)
        return d

    def test_set_acl_with_error(self):
        """
        on error set_acl invokes the deferred's errback with an exception.
        """
        d = self.client.connect()

        acl = dict(scheme="digest", id="a:b", perms=zookeeper.PERM_ALL)

        def set_acl(client):
            return client.set_acl("/zebra-moon22", [acl])

        def verify_failure(failure):
            self.assertTrue(
                isinstance(failure.value, zookeeper.NoNodeException))

        d.addCallback(set_acl)
        d.addErrback(verify_failure)
        return d

    def test_get_acl(self):
        """
        The client can be used to get an ACL on a node.
        """
        d = self.client.connect()

        def create_node(client):
            return client.create("/moose")

        def get_acl(path):
            return self.client.get_acl(path)

        def verify_acl((acls, stat)):
            self.assertEqual(acls, [PUBLIC_ACL])

        d.addCallback(create_node)
        d.addCallback(get_acl)
        d.addCallback(verify_acl)
        return d

    def test_get_acl_error(self):
        """
        On error the acl callback invokes the deferred errback with the
        exception.
        """
        d = self.client.connect()

        def inject_error(result, d):
            error = zookeeper.ZooKeeperException()
            d.errback(error)
            return error

        def get_acl(path):
            # Get the ACL of a nonexistant node
            return self.client.get_acl("/moose")

        def verify_failure(failure):
            self.assertTrue(isinstance(failure, Failure))
            self.assertTrue(
                isinstance(failure.value, zookeeper.ZooKeeperException))

        d.addCallback(get_acl)
        d.addBoth(verify_failure)
        return d

    def test_client_id(self):
        """
        The client exposes a client id which is useful when examining
        the server logs.
        """
        # if we're not connected returns none
        self.assertEqual(self.client.client_id, None)
        d = self.client.connect()

        def verify_client_id(client):
            self.assertTrue(isinstance(self.client.client_id, tuple))
            self.assertTrue(isinstance(self.client.client_id[0], long))
            self.assertTrue(isinstance(self.client.client_id[1], str))

        d.addCallback(verify_client_id)
        return d

    def test_sync(self):
        """
        The sync method on the client flushes the connection to leader.

        In practice this seems hard to test functionally, but we at
        least verify the method executes without issue.
        """
        d = self.client.connect()

        def create_node(client):
            return client.create("/abc")

        def client_sync(path):
            return self.client.sync(path)

        def verify_sync(result):
            self.assertTrue(
                zookeeper.exists(self.client.handle, "/abc"))

        d.addCallback(create_node)
        d.addCallback(client_sync)
        d.addCallback(verify_sync)
        return d

    def test_property_servers(self):
        """
        The servers property of the client, shows which if any servers
        it might be connected, else it returns.
        """
        self.assertEqual(self.client.servers, None)
        d = self.client.connect()

        def verify_servers(client):
            self.assertEqual(client.servers, "127.0.0.1:2181")

        d.addCallback(verify_servers)
        return d

    def test_property_session_timeout(self):
        """
        The negotiated session timeout is available as a property on the
        client. If the client isn't connected, the value is None.
        """
        self.assertEqual(self.client.session_timeout, None)
        d = self.client.connect()

        def verify_session_timeout(client):
            self.assertIn(client.session_timeout, (4000, 10000))

        d.addCallback(verify_session_timeout)
        return d

    def test_property_unrecoverable(self):
        """
        The unrecoverable property specifies whether the connection can be
        recovered or must be discarded.
        """
        d = self.client.connect()

        def verify_recoverable(client):
            self.assertEqual(client.unrecoverable, False)
            return client

        d.addCallback(verify_recoverable)
        return d

    def test_invalid_watcher(self):
        """
        Setting an invalid watcher raises a syntaxerror.
        """
        d = self.client.connect()

        def set_invalid_watcher(client):
            return client.set_connection_watcher(1)

        def verify_invalid(failure):
            self.assertEqual(failure.value.args, ("Invalid Watcher 1",))
            self.assertTrue(isinstance(failure.value, SyntaxError))

        d.addCallback(set_invalid_watcher)
        d.addErrback(verify_invalid)
        return d

    def test_connect_with_server(self):
        """
        A client's servers can be specified in the connect method.
        """
        d = self.client.connect("127.0.0.1:2181")

        def verify_connected(client):
            self.assertTrue(client.connected)

        d.addCallback(verify_connected)
        return d

    def test_connect_with_error(self):
        """
        An error in the connect invokes the deferred errback with exception.
        """

        def _fake_init(handle, callback, timeout):
            callback(0, 0, zookeeper.ASSOCIATING_STATE, "")
            return 0
        mock_init = self.mocker.replace("zookeeper.init")
        mock_init(ANY, ANY, ANY)
        self.mocker.call(_fake_init)
        self.mocker.replay()

        d = self.client.connect()

        def verify_error(failure):
            self.assertFalse(self.client.connected)
            self.assertTrue(isinstance(failure.value, ConnectionException))
            self.assertEqual(failure.value.args[0], "connection error")

        def assert_failed(any):
            self.fail("should not be invoked")

        d.addCallback(assert_failed)
        d.addErrback(verify_error)
        return d

    test_connect_with_error.timeout = 5

    def test_connect_timeout(self):
        """
        A timeout in seconds can be specified on connect, if the client hasn't
        connected before then, then an errback is invoked with a timeout
        exception.
        """
        # Connect to a non standard port with nothing at the remote side.
        d = self.client.connect("127.0.0.1:2182", timeout=0.2)

        def verify_timeout(failure):
            self.assertTrue(
                isinstance(failure.value, ConnectionTimeoutException))

        def assert_failure(any):
            self.fail("should not be reached")

        d.addCallback(assert_failure)
        d.addErrback(verify_timeout)
        return d

    def test_connect_ensured(self):
        """
        All of the client apis (with the exception of connect) attempt
        to ensure the client is connected before executing an operation.
        """
        self.assertFailure(
            self.client.get_children("/abc"), zookeeper.ZooKeeperException)

        self.assertFailure(
            self.client.create("/abc"), zookeeper.ZooKeeperException)

        self.assertFailure(
            self.client.set("/abc", "123"), zookeeper.ZooKeeperException)

    def test_connect_multiple_raises(self):
        """
        Attempting to connect on a client that is already connected raises
        an exception.
        """
        d = self.client.connect()

        def connect_again(client):
            d = client.connect()
            self.failUnlessFailure(d, zookeeper.ZooKeeperException)
            return d

        d.addCallback(connect_again)
        return d

    def test_bad_result_raises_error(self):
        """
        A not OK return from zookeeper api method result raises an exception.
        """
        mock_acreate = self.mocker.replace("zookeeper.acreate")
        mock_acreate(ANY, ANY, ANY, ANY, ANY, ANY)
        self.mocker.result(-100)
        self.mocker.replay()

        d = self.client.connect()

        def verify_failure(client):
            d = client.create("/abc")
            self.failUnlessFailure(d, zookeeper.ZooKeeperException)

        d.addCallback(verify_failure)
        return d

    def test_connection_watcher(self):
        """
        A connection watcher can be set that receives notices on when
        the connection state changes. Technically zookeeper would also
        use this as a global watcher for node watches, but zkpython
        doesn't expose that api, as its mostly considered legacy.

        its out of scope to simulate a connection level event within unit tests
        such as the server restarting.
        """
        d = self.client.connect()

        observed = []

        def watch(*args):
            observed.append(args)

        def set_global_watcher(client):
            client.set_connection_watcher(watch)
            return client

        def close_connection(client):
            return client.close()

        def verify_observed(stat):
            self.assertFalse(observed)

        d.addCallback(set_global_watcher)
        d.addCallback(close_connection)
        d.addCallback(verify_observed)

        return d

    def test_close_not_connected(self):
        """
        If the client is not connected, closing returns None.
        """
        self.assertEqual(self.client.close(), None)

    def test_invalid_connection_error_callback(self):
        self.assertRaises(TypeError,
                          self.client.set_connection_error_callback,
                          None)

    def test_invalid_session_callback(self):
        self.assertRaises(TypeError,
                          self.client.set_session_callback,
                          None)
Exemplo n.º 16
0
 def new_connection(data):
     self.client2 = ZookeeperClient("127.0.0.1:2181")
     return self.client2.connect()
Exemplo n.º 17
0
class AutoscalerHostSource(HostSource):
    config_spec = {
        "hostsource": {
            "connection-string": Option(str),
            "user": Option(str, default=None),
            "password": Option(str, default=None),
        },
    }

    def __init__(self, config):
        # zoopy is really friggin' loud without this
        zookeeper.set_debug_level(0)

        connection_string = config["hostsource"]["connection-string"]
        self.client = ZookeeperClient(connection_string, session_timeout=3000)
        self.user = config["hostsource"]["user"]
        self.password = config["hostsource"]["password"]

    @inlineCallbacks
    def _get_host_info(self, hostname):
        base_path = posixpath.join("/server", hostname)

        try:
            node = yield self.client.get(posixpath.join(base_path, "asg"))
        except zookeeper.NoNodeException:
            pool = ""
        else:
            pool = node[0]

        try:
            ipv4 = posixpath.join(base_path, "local-ipv4")
            node = yield self.client.get(ipv4)
        except zookeeper.NoNodeException:
            address = hostname
        else:
            address = node[0]

        returnValue(Host(hostname, hostname, address, pool))

    @inlineCallbacks
    def get_hosts(self):
        try:
            yield self.client.connect()

            if self.user:
                yield self.client.add_auth(
                    "digest", "%s:%s" % (self.user, self.password))

            hostnames = yield self.client.get_children("/server")
            hosts = yield parallel_map(hostnames, self._get_host_info)
            returnValue(hosts)
        except zookeeper.ZooKeeperException as e:
            raise HostSourceError(e)

    @inlineCallbacks
    def should_be_alive(self, host):
        host_root = "/server/%s" % host.name

        try:
            state = yield self.client.get(host_root + "/state")

            if state in ("kicking", "unhealthy"):
                returnValue(False)

            is_autoscaled = yield self.client.exists(host_root + "/asg")
            is_running = yield self.client.exists(host_root + "/running")
            returnValue(not bool(is_autoscaled) or bool(is_running))
        except zookeeper.NoNodeException:
            # ok, it's just a bad node
            returnValue(False)
        except zookeeper.ZooKeeperException as e:
            # fail safe
            logging.warning(
                "autoscaler: failed to check liveliness for %r: %s", host.name,
                e)
            returnValue(True)
Exemplo n.º 18
0
class AutoscalerHostSource(HostSource):
    config_spec = {
        "hostsource": {
            "connection-string": Option(str),
            "user": Option(str, default=None),
            "password": Option(str, default=None),
        },
    }

    def __init__(self, config):
        # zoopy is really friggin' loud without this
        zookeeper.set_debug_level(0)

        connection_string = config["hostsource"]["connection-string"]
        self.client = ZookeeperClient(connection_string, session_timeout=3000)
        self.user = config["hostsource"]["user"]
        self.password = config["hostsource"]["password"]

    @inlineCallbacks
    def _get_host_pool(self, hostname, by_pool):
        path = posixpath.join("/server", hostname, "asg")
        try:
            pool = yield self.client.get(path)
        except zookeeper.NoNodeException:
            pool = ""
        else:
            pool = pool[0]

        by_pool[pool].append(hostname)

    @inlineCallbacks
    def get_hosts(self):
        try:
            yield self.client.connect()

            if self.user:
                yield self.client.add_auth(
                    "digest", "%s:%s" % (self.user, self.password))

            hostnames = yield self.client.get_children("/server")

            by_pool = collections.defaultdict(list)
            yield parallel_map(
                sorted_nicely(hostnames), self._get_host_pool, by_pool)
            pool_aware = interleaved(by_pool)
            returnValue(pool_aware)
        except zookeeper.ZooKeeperException as e:
            raise HostSourceError(e)

    @inlineCallbacks
    def should_be_alive(self, host):
        host_root = "/server/%s" % host

        try:
            state = yield self.client.get(host_root + "/state")

            if state in ("kicking", "unhealthy"):
                returnValue(False)

            is_autoscaled = yield self.client.exists(host_root + "/asg")
            is_running = yield self.client.exists(host_root + "/running")
            returnValue(not bool(is_autoscaled) or bool(is_running))
        except zookeeper.NoNodeException:
            # ok, it's just a bad node
            returnValue(False)
        except zookeeper.ZooKeeperException as e:
            # fail safe
            logging.warning(
                "autoscaler: failed to check liveliness for %r: %s", host, e)
            returnValue(True)
Exemplo n.º 19
0
 def new_connection((watch,)):
     self.client2 = ZookeeperClient("127.0.0.1:2181")
     return self.client2.connect().addCallback(
         lambda x, y=None, z=None: (x, watch))
Exemplo n.º 20
0
class SecurityTests(ZookeeperTestCase):

    ident_bob = "bob:bob"
    ident_alice = "alice:alice"
    ident_eve = "eve:eve"
    ident_chuck = "chuck:chuck"

    ident_unittest = "unittest:unittest"

    def setUp(self):
        super(SecurityTests, self).setUp()
        self.clients = []

        self.test_cleanup_connection = ZookeeperClient("127.0.0.1:2181", 2000)
        self.access_control_test_cleanup_entry = self.make_ac(
            self.ident_unittest, all=True, admin=True)
        return self.open_and_authenticate(
            self.test_cleanup_connection, self.ident_unittest)

    def tearDown(self):
        utils.deleteTree(handle=self.test_cleanup_connection.handle)
        for client in self.clients:
            if client.connected:
                client.close()
        self.test_cleanup_connection.close()

    @inlineCallbacks
    def open_and_authenticate(self, client, credentials):
        """authentication so the test always has access to clean up the
        zookeeper node tree. synchronous auth to avoid using deferred
        during setup."""
        yield client.connect()
        d = client.add_auth("digest", credentials)
        # hack to keep auth fast
        yield client.exists("/")
        yield d
        returnValue(client)

    @inlineCallbacks
    def connect_users(self, *users):
        clients = []
        for name in users:
            ident_user = getattr(self, "ident_%s" % (name), None)
            if ident_user is None:
                raise AttributeError("Invalid User %s" % (name))
            client = ZookeeperClient("127.0.0.1:2181", 3000)
            clients.append(client)
            yield self.open_and_authenticate(client, ident_user)
        self.clients.extend(clients)
        returnValue(clients)

    @inlineCallbacks
    def sync_clients(self, *clients):
        for client in clients:
            yield client.sync()

    def ensure_auth_failure(self, result):
        if isinstance(result, Failure):
            self.assertTrue(isinstance(
                result.value, zookeeper.NoAuthException))
            return
        self.fail("should have raised auth exception")

    def make_acl(self, *access_control_entries):
        """
        Take the variable number of access control entries and return a
        list suitable for passing to the txzookeeper's api as an ACL.

        Also automatically appends the test acess control entry to ensure
        that the test can cleanup regardless of node permissions set within
        a test.
        """
        access_control_list = list(access_control_entries)
        access_control_list.append(self.access_control_test_cleanup_entry)
        return access_control_list

    def make_ac(self, credentials, **kw):
        """
        Given a username:password credential and boolean keyword arguments
        corresponding to permissions construct an access control entry.
        """
        user, password = credentials.split(":")
        identity = "%s:%s" % (
            user,
            base64.b64encode(hashlib.new('sha1', credentials).digest()))

        permissions = None

        for name, perm in (('read', zookeeper.PERM_READ),
                           ('write', zookeeper.PERM_WRITE),
                           ('delete', zookeeper.PERM_DELETE),
                           ('create', zookeeper.PERM_CREATE),
                           ('admin', zookeeper.PERM_ADMIN),
                           ('all', zookeeper.PERM_ALL)):
            if name not in kw:
                continue

            if permissions is None:
                permissions = perm
            else:
                permissions = permissions | perm
        if permissions is None:
            raise SyntaxError("No permissions specified")
        access_control_entry = {
            'id': identity, 'scheme': 'digest', 'perms': permissions}
        return access_control_entry

    @inlineCallbacks
    def test_bob_message_for_alice_with_eve_reading(self):
        """
        If bob creates a message for alice to read, eve cannot read
        it.
        """
        bob, alice, eve = yield self.connect_users(
            "bob", "alice", "eve")
        yield bob.create(
            "/message_inbox", "message for alice",
            self.make_acl(
                self.make_ac(self.ident_bob, write=True, read=True),
                self.make_ac(self.ident_alice, read=True)))

        message_content, message_stat = yield alice.get("/message_inbox")
        self.assertEqual(message_content, "message for alice")

        d = eve.get("/message_inbox")
        d.addBoth(self.ensure_auth_failure)

        yield d

    @inlineCallbacks
    def test_alice_message_box_for_bob_with_eve_deleting(self):
        """
        If alice makes a folder to drop off messages to bob, neither bob nor
        eve can write to it, and bob can only read, and delete the messages.
        The permission for deleting is set on the container node. Bob has
        delete permission only on the on the container, and can delete nodes.
        Even if eve has permission to delete on the message node, without the
        container permission it will not succeed.
        """
        bob, alice, eve = yield self.connect_users("bob", "alice", "eve")

        yield alice.create(
            "/from_alice", "messages from alice",
            self.make_acl(
                self.make_ac(self.ident_alice, create=True, write=True),
                self.make_ac(self.ident_bob, read=True, delete=True))),

        # make sure all the clients have a consistent view
        yield self.sync_clients(alice, bob, eve)

        # bob can't create messages in the mailbox
        d = bob.create("/from_alice/love_letter", "test")
        d.addBoth(self.ensure_auth_failure)

        # alice's message can only be read by bob.
        path = yield alice.create(
            "/from_alice/appreciate_letter", "great",
            self.make_acl(
                self.make_ac(self.ident_eve, delete=True),
                self.make_ac(self.ident_bob, read=True),
                self.make_ac(self.ident_alice, create=True, write=True)))

        message_content, node_stat = yield bob.get(path)
        self.assertEqual(message_content, "great")

        # make sure all the clients have a consistent view
        yield self.sync_clients(alice, bob, eve)

        # eve can neither read nor delete
        d = eve.get(path)
        d.addBoth(self.ensure_auth_failure)
        yield d

        d = eve.delete(path)
        d.addBoth(self.ensure_auth_failure)
        yield d

        # bob can delete the message when he's done reading.
        yield bob.delete(path)

    def test_eve_can_discover_node_path(self):
        """
        One weakness of the zookeeper security model, is that it enables
        discovery of a node existance, its node stats, and its acl to
        any inquiring party.

        The acl is read off the node and then used as enforcement to any
        policy. Ideally it should validate exists and get_acl against
        the read permission on the node.

        Here bob creates a node that only he can read or write to, but
        eve can still get node stat on the node if she knows the path.
        """
        bob, eve = yield self.connect_users("bob", "eve")
        yield bob.create("/bobsafeplace", "",
                         self.make_acl(self.make_ac(self.ident_bob, all=True)))

        yield bob.create("/bobsafeplace/secret-a", "supersecret",
                         self.make_acl(self.make_ac(self.ident_bob, all=True)))

        self.sync_clients(bob, eve)
        d = eve.exists("/bobsafeplace")

        def verify_node_stat(node_stat):
            self.assertEqual(node_stat["dataLength"], len("supersecret"))
            self.assertEqual(node_stat["version"], 0)

        d.addCallback(verify_node_stat)
        yield d

    def test_eve_can_discover_node_acl(self):
        """
        One weakness of the zookeeper security model, is that it enables
        discovery of a node existance, its node stats, and its acl to
        any inquiring party.

        The acl is read off the node and then used as enforcement to any
        policy. Ideally it should validate exists and get_acl against
        the read permission on the node.

        Here bob creates a node that only he can read or write to, but
        eve can still get node stat and acl information on the node if
        she knows the path.
        """
        bob, eve = yield self.connect_users("bob", "eve")
        yield bob.create("/bobsafeplace", "",
                         self.make_acl(self.make_ac(self.ident_bob, all=True)))

        yield bob.create("/bobsafeplace/secret-a", "supersecret",
                         self.make_acl(self.make_ac(self.ident_bob, all=True)))

        self.sync_clients(bob, eve)
        d = eve.get_acl("/bobsafeplace/secret-a")

        def verify_node_stat_and_acl((acl, node_stat)):
            self.assertEqual(node_stat["dataLength"], len("supersecret"))
            self.assertEqual(node_stat["version"], 0)
            self.assertEqual(acl[0]["id"].split(":")[0], "bob")
        d.addCallback(verify_node_stat_and_acl)
        yield d
Exemplo n.º 21
0
 def setUp(self):
     super(ClientTests, self).setUp()
     self.client = ZookeeperClient("127.0.0.1:2181", 3000)
     self.client2 = None
Exemplo n.º 22
0
class HippoHostSource(HostSource):
    config_spec = {
        "hostsource": {
            "connection-string": Option(str),
            "user": Option(str, default=None),
            "password": Option(str, default=None),
        },
    }

    def __init__(self, config):
        # zoopy is really friggin' loud without this
        zookeeper.set_debug_level(0)

        connection_string = config["hostsource"]["connection-string"]
        self.client = ZookeeperClient(connection_string, session_timeout=3000)
        self.user = config["hostsource"]["user"]
        self.password = config["hostsource"]["password"]

    @inlineCallbacks
    def _get_host_info(self, instance_id):
        host_node_path = posixpath.join("/hosts", instance_id)
        try:
            host_json, znode = yield self.client.get(host_node_path)
        except zookeeper.NoNodeException:
            returnValue(None)

        host_info = json.loads(host_json)
        tags = host_info["properties"]["tags"]
        try:
            hostname = tags["HostClass"] + instance_id[1:]
        except KeyError:
            try:
                hostname = tags["Name"]
            except KeyError:
                hostname = instance_id

        address = host_info["properties"]["private_ip"]
        pool = tags.get("aws:autoscaling:groupName", "")

        returnValue(Host(instance_id, hostname, address, pool))

    @inlineCallbacks
    def get_hosts(self):
        try:
            yield self.client.connect()

            if self.user:
                yield self.client.add_auth(
                    "digest", "%s:%s" % (self.user, self.password))

            instance_ids = yield self.client.get_children("/hosts")
            hosts = yield parallel_map(instance_ids, self._get_host_info)
            returnValue(filter(None, hosts))
        except zookeeper.ZooKeeperException as e:
            raise HostSourceError(e)

    @inlineCallbacks
    def should_be_alive(self, host):
        instance_root = "/hosts/%s" % host.id

        try:
            yield self.client.get(instance_root)
            returnValue(True)
        except zookeeper.NoNodeException:
            # ok, it's just a bad node
            returnValue(False)
        except zookeeper.ZooKeeperException as e:
            # fail safe
            logging.warning(
                "hippo: failed to check liveliness for %r: %s", host.name, e)
            returnValue(True)
Exemplo n.º 23
0
 def new_connection(children):
     self.assertFalse(children)
     self.client2 = ZookeeperClient("127.0.0.1:2181")
     return self.client2.connect()
Exemplo n.º 24
0
class AutoscalerHostSource(HostSource):
    config_spec = {
        "hostsource": {
            "connection-string": Option(str),
            "user": Option(str, default=None),
            "password": Option(str, default=None),
        },
    }

    def __init__(self, config):
        # zoopy is really friggin' loud without this
        zookeeper.set_debug_level(0)

        connection_string = config["hostsource"]["connection-string"]
        self.client = ZookeeperClient(connection_string, session_timeout=3000)
        self.user = config["hostsource"]["user"]
        self.password = config["hostsource"]["password"]

    @inlineCallbacks
    def _get_host_info(self, hostname):
        base_path = posixpath.join("/server", hostname)

        try:
            node = yield self.client.get(posixpath.join(base_path, "asg"))
        except zookeeper.NoNodeException:
            pool = ""
        else:
            pool = node[0]

        try:
            ipv4 = posixpath.join(base_path, "local-ipv4")
            node = yield self.client.get(ipv4)
        except zookeeper.NoNodeException:
            address = hostname
        else:
            address = node[0]

        returnValue(Host(hostname, hostname, address, pool))

    @inlineCallbacks
    def get_hosts(self):
        try:
            yield self.client.connect()

            if self.user:
                yield self.client.add_auth(
                    "digest", "%s:%s" % (self.user, self.password))

            hostnames = yield self.client.get_children("/server")
            hosts = yield parallel_map(hostnames, self._get_host_info)
            returnValue(hosts)
        except zookeeper.ZooKeeperException as e:
            raise HostSourceError(e)

    @inlineCallbacks
    def should_be_alive(self, host):
        host_root = "/server/%s" % host.name

        try:
            state = yield self.client.get(host_root + "/state")

            if state in ("kicking", "unhealthy"):
                returnValue(False)

            is_autoscaled = yield self.client.exists(host_root + "/asg")
            is_running = yield self.client.exists(host_root + "/running")
            returnValue(not bool(is_autoscaled) or bool(is_running))
        except zookeeper.NoNodeException:
            # ok, it's just a bad node
            returnValue(False)
        except zookeeper.ZooKeeperException as e:
            # fail safe
            logging.warning(
                "autoscaler: failed to check liveliness for %r: %s",
                host.name,
                e
            )
            returnValue(True)