Exemple #1
0
 def setUp( self ):
     zktestbase.TestBase.setUp(self)
     try:
         zookeeper.create(self.handle, "/zk-python-existstest","existstest", [ZOO_OPEN_ACL_UNSAFE],zookeeper.EPHEMERAL)
         zookeeper.create(self.handle, "/zk-python-aexiststest","existstest",[ZOO_OPEN_ACL_UNSAFE],zookeeper.EPHEMERAL)
     except:
         pass
Exemple #2
0
    def testmanyhandles(self):
        """
        Test the ability of the module to support many handles.
        """
        # We'd like to do more, but currently the C client doesn't
        # work with > 83 handles (fails to create a pipe) on MacOS 10.5.8
        handles = [ zookeeper.init(self.host) for i in xrange(63) ]

        cv = threading.Condition()
        self.connected = False
        def connection_watcher(handle, type, state, path):
            cv.acquire()
            self.connected = True
            self.assertEqual(zookeeper.CONNECTED_STATE, state)
            self.handle = handle
            cv.notify()
            cv.release()

        cv.acquire()
        ret = zookeeper.init(self.host, connection_watcher)
        cv.wait(15.0)
        cv.release()
        self.assertEqual(self.connected, True, "Connection timed out to " + self.host)

        for i,h in enumerate(handles):
            path = "/zkpython-test-handles-%s" % str(i)
            self.assertEqual(path, zookeeper.create(h, path, "", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL))

        self.assertEqual(True, self.all( zookeeper.close(h) == zookeeper.OK for h in handles ))
Exemple #3
0
    def test_async_delete(self):
        ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"}
        self.assertEqual(self.connected, True)
        ret = zookeeper.create(self.handle, "/zk-python-adeletetest", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL)
        self.assertEqual(ret, "/zk-python-adeletetest")

        self.cv = threading.Condition()
        self.callback_flag = False
        self.rc = -1
        def callback(handle, rc):
            self.cv.acquire()
            self.callback_flag = True
            self.cv.notify()
            self.rc = rc # don't assert this here, as if the assertion fails, the test will block
            self.cv.release()

        self.cv.acquire()
        ret = zookeeper.adelete(self.handle,"/zk-python-adeletetest",-1,callback)
        self.assertEqual(ret, zookeeper.OK, "adelete failed")
        while not self.callback_flag:
            self.cv.wait(15)
        self.cv.release()

        self.assertEqual(self.callback_flag, True, "adelete timed out")
        self.assertEqual(self.rc, zookeeper.OK)
Exemple #4
0
    def test_sync_get_large_datanode(self):
        """
        Test that we can retrieve datanode sizes up to
        1Mb with default parameters (depends on ZooKeeper server).
        """

        data = ''.join(["A" for x in xrange(1024*1023)])
        self.ensureDeleted("/zk-python-test-large-datanode")
        zookeeper.create(self.handle, "/zk-python-test-large-datanode", data,
                         [{"perms":0x1f, "scheme":"world", "id" :"anyone"}])
        (ret,stat) = zookeeper.get(self.handle, "/zk-python-test-large-datanode")
        self.assertEqual(len(ret), 1024*1023,
                         "Should have got 1Mb returned, instead got %s" % len(ret))
        (ret,stat) = zookeeper.get(self.handle, "/zk-python-test-large-datanode",None,500)
        self.assertEqual(len(ret), 500,
                         "Should have got 500 bytes returned, instead got %s" % len(ret))
Exemple #5
0
 def ensureCreated(self,path,data="",flags=zookeeper.EPHEMERAL):
     """
     It's possible not to get the flags you want here if the node already exists
     """
     self.assertEqual(zookeeper.CONNECTED_STATE, zookeeper.state(self.handle), "Not connected!")
     try:
         self.assertEqual(path, zookeeper.create(self.handle, path, data, [ZOO_OPEN_ACL_UNSAFE], flags))
     except zookeeper.NodeExistsException:
         pass
Exemple #6
0
 def test_sync_acl(self):
   self.assertEqual(self.connected, True)
   ret = zookeeper.create(self.handle, "/zk-python-acltest", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL)
   acls = zookeeper.get_acl(self.handle, "/zk-python-acltest")
   self.assertEqual(acls[1], [ZOO_OPEN_ACL_UNSAFE])
   self.assertRaises(zookeeper.InvalidACLException,zookeeper.set_acl,self.handle, "/zk-python-acltest", -1, ZOO_ACL_READ)
   zookeeper.set_acl(self.handle, "/zk-python-acltest", -1, [ZOO_ACL_READ])
   acls = zookeeper.get_acl(self.handle, "/zk-python-acltest")
   self.assertEqual(acls[1], [ZOO_ACL_READ])
Exemple #7
0
    def create(self, path, data, acls, ephemeral=False, sequential=False):
        """Create a node with the given path.

        The node data will be the given data, and node acl will be the given
        acls.

        The `ephemeral` and `sequential` arguments specify what types of nodes
        will be created.

        An ephemeral node will be removed by the ZooKeeper automatically when
        the session associated with the creation of the node expires.

        The flags argument can also specify to create a sequential node. The
        actual path name of a sequential node will be the given path plus a
        suffix "i" where i is the current sequential number of the node. The
        sequence number is always fixed length of 10 digits, 0 padded. Once
        such a node is created, the sequential number will be incremented by
        one.

        This operation, if successful, will trigger all the watches left on the
        node of the given path by `exists()` and `get_data()` API calls, and
        the watches left on the parent node by `get_children()` API calls.

        The maximum allowable size of the data array is 1 MB (1,048,576 bytes).
        Arrays larger than this will cause a ZooKeeperExecption to be
        thrown. (TODO: what type?)

        :Parameters:
            - `path`: Name of node to create
            - `data`: Data to store in node.
            - `acls`: List of ACLs (ACL objects) for the node.
            - `ephemeral`: Created node should be ephemeral.
            - `sequential`: Created node should be sequential.

        :Exceptions:
            - `NodeExistsException`: if a node with the same path already
              exists (impossible if `sequential` is True)
            - `NoNodeException`: if parent node does not exist
            - `NoChildrenForEphemeralsException`: if parent node of the path is
              ephemeral (ephemeral nodes may not have children)
            - `ZooKeeperException`: *TODO* if size is too big

        :Returns:
            Path for created node
        """
        flags = 0
        if ephemeral:
            flags |= zookeeper.constants.EPHEMERAL
        if sequential:
            flags |= zookeeper.constants.SEQUENCE

	acls = [acl.to_dict() for acl in acls]

        created_path = _zookeeper.create(
            self.zk_handle, path, data, acls, flags)

        return created_path
Exemple #8
0
 def test_sync_create(self):
     self.assertEqual(self.connected, True)
     ret = zookeeper.create(
         self.handle, "/zk-python-createtest", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL
     )
     self.assertEqual(ret, "/zk-python-createtest")
     self.assertRaises(
         zookeeper.NoChildrenForEphemeralsException,
         zookeeper.create,
         self.handle,
         "/zk-python-createtest/invalid-child",
         "",
         [ZOO_OPEN_ACL_UNSAFE],
         zookeeper.EPHEMERAL,
     )
Exemple #9
0
    def test_sync_delete(self):
        ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"}
        self.assertEqual(self.connected, True)
        ret = zookeeper.create(self.handle, "/zk-python-deletetest", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL)
        self.assertEqual(ret, "/zk-python-deletetest")
        ret = zookeeper.delete(self.handle,"/zk-python-deletetest")
        self.assertEqual(ret, zookeeper.OK)
        children = zookeeper.get_children(self.handle, "/")
        self.assertEqual(False, "zk-python-deletetest" in children)

        # test exception
        self.assertRaises(zookeeper.NoNodeException,
                          zookeeper.delete,
                          self.handle,
                          "/zk-python-deletetest")
Exemple #10
0
    def test_sync_create_existing(self):
        self.assertEqual(self.connected, True)
        ret = zookeeper.create(
            self.handle, "/zk-python-createtest-existing", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL
        )
        self.assertEqual(ret, "/zk-python-createtest-existing")

        self.assertRaises(
            zookeeper.NodeExistsException,
            zookeeper.create,
            self.handle,
            "/zk-python-createtest-existing",
            "nodecontents",
            [ZOO_OPEN_ACL_UNSAFE],
            zookeeper.EPHEMERAL,
        )
Exemple #11
0
    def test_async_acl(self):
      self.cv = threading.Condition()
      self.cv = threading.Condition()
      def aget_callback(handle, rc, acl, stat):
        self.cv.acquire()
        self.callback_flag = True
        self.rc = rc
        self.acl = acl
        self.stat = stat
        self.cv.notify()
        self.cv.release()

      def aset_callback(handle, rc):
        self.cv.acquire()
        self.callback_flag = True
        self.rc = rc
        self.cv.notify()
        self.cv.release()

      self.assertEqual(self.connected, True, "Not connected!")
      ret = zookeeper.create(self.handle, "/zk-python-aacltest", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL)

      self.cv.acquire()
      zookeeper.aget_acl(self.handle, "/zk-python-aacltest", aget_callback)
      self.cv.wait(15)
      self.cv.release()

      self.assertEqual(self.callback_flag, True, "aget_acl timed out")
      self.assertEqual(self.rc, zookeeper.OK, "aget failed")
      self.assertEqual(self.acl, [ZOO_OPEN_ACL_UNSAFE], "Wrong ACL returned from aget")

      self.cv.acquire()
      self.callback_flag = False
      zookeeper.aset_acl(self.handle, "/zk-python-aacltest", -1, [ZOO_ACL_READ], aset_callback)
      self.cv.wait(15)
      self.cv.release()

      self.assertEqual(self.callback_flag, True, "aset_acl timed out")
      self.assertEqual(self.rc, zookeeper.OK, "aset failed")
      acls = zookeeper.get_acl(self.handle, "/zk-python-aacltest")
      self.assertEqual(acls[1], [ZOO_ACL_READ], "Wrong ACL returned from get when aset")