Beispiel #1
0
    def test_ephemeral_sequential(self):
        z = pookeeper.allocate(self.hosts)

        z.create('/root', CREATOR_ALL_ACL, Persistent())

        random_data = _random_data()
        result = z.create('/root/pookie', CREATOR_ALL_ACL, EphemeralSequential(), data=random_data)
        result = z.create('/root/pookie', CREATOR_ALL_ACL, EphemeralSequential(), data=random_data)
        result = z.create('/root/pookie', CREATOR_ALL_ACL, EphemeralSequential(), data=random_data)

        children, _ = z.get_children('/root')
        children = sorted(children)
        assert len(children) == 3
        assert int(children[0][len('/root/pookie'):]) == 0
        assert int(children[1][len('/root/pookie'):]) == 1
        assert int(children[2][len('/root/pookie'):]) == 2

        z.close()

        z = pookeeper.allocate(self.hosts)

        children, _ = z.get_children('/root')
        assert len(children) == 0

        pookeeper.delete(z, '/root')

        assert not z.exists('/root')

        z.close()
Beispiel #2
0
    def test_ephemeral(self):
        z = pookeeper.allocate(self.hosts)

        random_data = _random_data()
        z.create('/pookie', CREATOR_ALL_ACL, Ephemeral(), data=random_data)

        z.close()

        z = pookeeper.allocate(self.hosts)

        assert not z.exists('/pookie')

        z.close()
Beispiel #3
0
    def test_get_children_default_watcher(self):
        watcher = mock()
        z = pookeeper.allocate(self.hosts, watcher=watcher)

        z.create('/pookie', CREATOR_ALL_ACL, Persistent(), data=_random_data())
        z.get_children('/pookie', watch=True)

        z.create('/pookie/bear', CREATOR_ALL_ACL, Persistent(), data=_random_data())
        z.get_children('/pookie', watch=True)

        z.set_data('/pookie', _random_data())
        z.set_data('/pookie/bear', _random_data())

        # One is for when we do and the other is for when we don't chroot
        z.get_children('/pookie', watch=True)
        z.get_children('/pookie/bear', watch=True)

        z.delete('/pookie/bear')
        z.delete('/pookie')

        z.close()

        mockito.verify(watcher).session_connected(matchers.any(long), matchers.any(str), False)
        mockito.verify(watcher, times=2).children_changed('/pookie')
        mockito.verify(watcher).node_deleted('/pookie/bear')
        mockito.verify(watcher).connection_closed()
        verifyNoMoreInteractions(watcher)
Beispiel #4
0
    def test_session_resumption(self):
        """ Test session reconnect

        disconnect the client by killing the socket, not sending the session
        disconnect to the server as usual. This allows the test to verify
        disconnect handling
        """
        client = DropableClient34(self.hosts)
        client.create('/e', CREATOR_ALL_ACL, Ephemeral())
        client.drop()

        try:
            client.exists('/e')
            assert False, 'Connection dropped for test'
        except ConnectionDroppedForTest:
            pass

        resumed_client = pookeeper.allocate(self.hosts, session_timeout=client.session_timeout, session_id=client.session_id, session_passwd=client.session_passwd)
        stat = resumed_client.exists('/e')
        assert stat is not None

        resumed_client.close()

        try:
            resumed_client.exists('/e')
            assert False, 'Should have raised SessionExpiredError'
        except SessionExpiredError:
            pass

        new_client = self._get_client(session_timeout=resumed_client.session_timeout, session_id=resumed_client.session_id, session_passwd=resumed_client.session_passwd)
        stat = new_client.exists('/e')
        assert stat is None
        new_client.close()
Beispiel #5
0
    def test_persistent(self):
        z = pookeeper.allocate(self.hosts)

        random_data = _random_data()
        z.create('/pookie', CREATOR_ALL_ACL, Persistent(), data=random_data)

        z.close()

        z = pookeeper.allocate(self.hosts)

        data, stat = z.get_data('/pookie')
        assert data == random_data

        z.delete('/pookie', stat.version)

        assert not z.exists('/pookie')

        z.close()
Beispiel #6
0
    def test_close(self):
        watcher = WatcherCounter()
        client = pookeeper.allocate(self.hosts, session_timeout=3.0, watcher=watcher)
        client.sync('/')
        client.close()

        assert watcher._session_connected == 1
        assert not watcher._session_expired
        assert not watcher._auth_failed
        assert not watcher._connection_dropped
        assert watcher._connection_closed == 1
Beispiel #7
0
    def test_ping(self):
        """ Make sure client connection is kept alive by behind the scenes pinging
        """

        client = pookeeper.allocate(self.hosts, session_timeout=0.8)
        client.sync('/')

        time.sleep(5)

        client.get_children('/')
        client.close()
Beispiel #8
0
    def test_negotiated_session_timeout(self):
        """ Verify access to the negotiated session timeout
        """
        TICK_TIME = 2.0

        # validate typical case - requested == negotiated
        client = pookeeper.allocate(self.hosts, session_timeout=TICK_TIME * 4)
        client.sync('/')
        assert TICK_TIME * 4 == client.negotiated_session_timeout
        client.close()

        # validate lower limit
        client = pookeeper.allocate(self.hosts, session_timeout=TICK_TIME)
        client.sync('/')
        assert TICK_TIME * 2 == client.negotiated_session_timeout
        client.close()

        # validate upper limit
        client = pookeeper.allocate(self.hosts, session_timeout=TICK_TIME * 30)
        client.sync('/')
        assert TICK_TIME * 20 == client.negotiated_session_timeout
        client.close()
Beispiel #9
0
    def test_acls(self):
        z = pookeeper.allocate(self.hosts)

        z.create('/pookie', CREATOR_ALL_ACL + READ_ACL_UNSAFE, Persistent())
        acls, stat = z.get_acls('/pookie')
        assert len(acls) == 2
        for acl in acls:
            assert acl in set(CREATOR_ALL_ACL + READ_ACL_UNSAFE)

        z.delete('/pookie', stat.version)

        assert not z.exists('/pookie')

        z.close()
Beispiel #10
0
    def test_bugus_auth(self):
        z = pookeeper.allocate(self.hosts, auth_data=set([('bogus', 'authdata')]))
        try:
            z.exists('/zookeeper')
            assert False, 'Allocation should have thrown an AuthFailedError'
        except AuthFailedError:
            pass

        try:
            z.exists('/zookeeper')
            assert False, 'Allocation should have thrown an AuthFailedError'
        except AuthFailedError:
            pass

        z.close()
Beispiel #11
0
    def test_set_data_watcher(self):
        watcher = mock()
        z = pookeeper.allocate(self.hosts)

        z.create('/pookie', CREATOR_ALL_ACL, Ephemeral(), data=_random_data())

        stat = z.exists('/pookie')
        stat = z.set_data('/pookie', _random_data(), stat.version)
        z.get_data('/pookie', watcher=watcher)
        stat = z.set_data('/pookie', _random_data(), stat.version)
        z.get_data('/pookie', watcher=watcher)
        z.delete('/pookie', stat.version)

        z.close()

        inorder.verify(watcher).data_changed('/pookie')
        inorder.verify(watcher).node_deleted('/pookie')
        verifyNoMoreInteractions(watcher)
Beispiel #12
0
    def test_set_data_default_watcher(self):
        watcher = mock()
        z = pookeeper.allocate(self.hosts, watcher=watcher)

        z.create('/pookie', CREATOR_ALL_ACL, Ephemeral(), data=_random_data())

        stat = z.exists('/pookie')
        stat = z.set_data('/pookie', _random_data(), stat.version)
        z.get_data('/pookie', watch=True)
        stat = z.set_data('/pookie', _random_data(), stat.version)
        z.get_data('/pookie', watch=True)
        z.delete('/pookie', stat.version)

        z.close()

        inorder.verify(watcher).session_connected(matchers.any(long), matchers.any(str), False)
        inorder.verify(watcher).data_changed('/pookie')
        inorder.verify(watcher).node_deleted('/pookie')
        inorder.verify(watcher).connection_closed()
        verifyNoMoreInteractions(watcher)
Beispiel #13
0
    def test_exists_watcher(self):
        watcher = mock()
        z = pookeeper.allocate(self.hosts)

        assert not z.exists('/pookie', watcher=watcher)
        z.create('/pookie', CREATOR_ALL_ACL, Ephemeral(), data=_random_data())

        stat = z.exists('/pookie', watcher=watcher)
        stat = z.set_data('/pookie', _random_data(), stat.version)
        # This data change will be ignored since the watch has been reset
        z.set_data('/pookie', _random_data(), stat.version)
        stat = z.exists('/pookie', watcher=watcher)
        z.delete('/pookie', stat.version)

        z.close()

        inorder.verify(watcher).node_created('/pookie')
        inorder.verify(watcher).data_changed('/pookie')
        inorder.verify(watcher).node_deleted('/pookie')
        verifyNoMoreInteractions(watcher)
Beispiel #14
0
    def test_state_no_dupuplicate_reporting(self):
        """ Verify state change notifications are not duplicated

        This test makes sure that duplicate state changes are not communicated
        to the client watcher. For example we should not notify state as
        "disconnected" if the watch has already been disconnected. In general
        we don't consider a dup state notification if the event type is
        not "None" (ie non-None communicates an event).
        """

        watcher = WatcherCounter()
        client = pookeeper.allocate(self.hosts, session_timeout=3.0, watcher=watcher)

        client.sync('/')

        self.cluster.stop()

        time.sleep(5)

        assert watcher._session_connected == 1
        assert not watcher._session_expired
        assert not watcher._auth_failed
        assert watcher._connection_dropped == 1
        assert not watcher._connection_closed
Beispiel #15
0
    def test_transaction(self):
        z = pookeeper.allocate(self.hosts)

        # this should fail because /bar does not exist
        z.create('/foo', CREATOR_ALL_ACL, Persistent())
        stat = z.exists('/foo')

        transaction = z.allocate_transaction()
        transaction.create('/pookie', CREATOR_ALL_ACL, Persistent())
        transaction.check('/foo', stat.version)
        transaction.check('/bar', stat.version)
        transaction.delete('/foo', stat.version)
        transaction.commit()

        assert not z.exists('/pookie')
        assert z.exists('/foo')

        # this should succeed
        transaction = z.allocate_transaction()
        transaction.create('/pookie', CREATOR_ALL_ACL, Persistent())
        transaction.check('/foo', stat.version)
        transaction.delete('/foo', stat.version)
        transaction.commit()

        try:
            transaction.create('/pookie', CREATOR_ALL_ACL, Persistent())
            assert False, 'Transaction already committed - create should have failed'
        except ValueError:
            pass
        try:
            transaction.check('/foo', stat.version)
            assert False, 'Transaction already committed - check should have failed'
        except ValueError:
            pass
        try:
            transaction.delete('/foo', stat.version)
            assert False, 'Transaction already committed - delete should have failed'
        except ValueError:
            pass
        try:
            transaction.commit()
            assert False, 'Transaction already committed - commit should have failed'
        except ValueError:
            pass

        stat = z.exists('/pookie')
        z.delete('/pookie', stat.version)
        assert not z.exists('/foo')

        # test with
        z.create('/foo', CREATOR_ALL_ACL, Persistent())
        with z.allocate_transaction() as t:
            t.create('/pookie', CREATOR_ALL_ACL, Persistent())
            t.check('/foo', stat.version)
            t.delete('/foo', stat.version)

        stat = z.exists('/pookie')
        z.delete('/pookie', stat.version)
        assert not z.exists('/foo')

        z.close()