Author: mahadev Date: Fri Mar 5 03:47:38 2010 New Revision: 919280 URL: http://svn.apache.org/viewvc?rev=919280&view=rev Log: ZOOKEEPER-579. zkpython needs more test coverage for ACL code paths (henry via mahadev)
Added: hadoop/zookeeper/trunk/src/contrib/zkpython/src/test/acl_test.py Modified: hadoop/zookeeper/trunk/CHANGES.txt hadoop/zookeeper/trunk/src/contrib/zkpython/README hadoop/zookeeper/trunk/src/contrib/zkpython/src/c/zookeeper.c Modified: hadoop/zookeeper/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/CHANGES.txt?rev=919280&r1=919279&r2=919280&view=diff ============================================================================== --- hadoop/zookeeper/trunk/CHANGES.txt (original) +++ hadoop/zookeeper/trunk/CHANGES.txt Fri Mar 5 03:47:38 2010 @@ -312,6 +312,9 @@ ZOOKEEPER-640. make build.xml more configurable to ease packaging for linux distros (phunt via mahadev) + ZOOKEEPER-579. zkpython needs more test coverage for ACL code paths (henry + via mahadev) + NEW FEATURES: ZOOKEEPER-539. generate eclipse project via ant target. (phunt via mahadev) Modified: hadoop/zookeeper/trunk/src/contrib/zkpython/README URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/contrib/zkpython/README?rev=919280&r1=919279&r2=919280&view=diff ============================================================================== --- hadoop/zookeeper/trunk/src/contrib/zkpython/README (original) +++ hadoop/zookeeper/trunk/src/contrib/zkpython/README Fri Mar 5 03:47:38 2010 @@ -9,7 +9,7 @@ You will need the Python development headers installed to build the module - on many package-management systems, these can be found in python-devel. -Python >= 2.3 is required. We have tested against 2.3, 2.5 and 2.6. We have not tested against 3.x. +Python >= 2.6 is required. We have tested against 2.6. We have not tested against 3.x. BUILD AND INSTALL: ------------------- Modified: hadoop/zookeeper/trunk/src/contrib/zkpython/src/c/zookeeper.c URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/contrib/zkpython/src/c/zookeeper.c?rev=919280&r1=919279&r2=919280&view=diff ============================================================================== --- hadoop/zookeeper/trunk/src/contrib/zkpython/src/c/zookeeper.c (original) +++ hadoop/zookeeper/trunk/src/contrib/zkpython/src/c/zookeeper.c Fri Mar 5 03:47:38 2010 @@ -464,6 +464,24 @@ PyGILState_Release(gstate); } +int check_is_acl(PyObject *o) { + int i; + if (!PyList_Check(o)) { + return 0; + } + for (i=0;i<PyList_Size(o);++i) { + if (!PyDict_Check(PyList_GetItem(o,i))) { + return 0; + } + } + return 1; +} + +#define CHECK_ACLS(o) if (check_is_acl(o) == 0) {\ + PyErr_SetString(err_to_exception(ZINVALIDACL), zerror(ZINVALIDACL));\ + return NULL;\ + } + /////////////////////////////////////////////////////// // Asynchronous API PyObject *pyzoo_acreate(PyObject *self, PyObject *args) @@ -475,25 +493,23 @@ if (!PyArg_ParseTuple(args, "iss#O|iO", &zkhid, &path, &value, &valuelen, &pyacls, &flags, &completion_callback)) return NULL; CHECK_ZHANDLE(zkhid); - - if (pyacls != Py_None) - parse_acls(&acl, pyacls); + CHECK_ACLS(pyacls); + parse_acls(&acl, pyacls); int err = zoo_acreate( zhandles[zkhid], - path, - value, - valuelen, - pyacls == Py_None ? NULL : &acl, - flags, - string_completion_dispatch, - completion_callback != Py_None ? create_pywatcher(zkhid, completion_callback,0 ) : NULL ); - + path, + value, + valuelen, + pyacls == Py_None ? NULL : &acl, + flags, + string_completion_dispatch, + completion_callback != Py_None ? create_pywatcher(zkhid, completion_callback,0 ) : NULL ); free_acls(&acl); if (err != ZOK) { PyErr_SetString(err_to_exception(err), zerror(err)); return NULL; } - return Py_BuildValue("i", err);; + return Py_BuildValue("i", err); } PyObject *pyzoo_adelete(PyObject *self, PyObject *args) @@ -676,7 +692,7 @@ &pyacl, &completion_callback)) return NULL; CHECK_ZHANDLE(zkhid); - + CHECK_ACLS(pyacl); parse_acls( &aclv, pyacl ); int err = zoo_aset_acl( zhandles[zkhid], path, @@ -736,6 +752,7 @@ return NULL; CHECK_ZHANDLE(zkhid); struct ACL_vector aclv; + CHECK_ACLS(acl); parse_acls(&aclv,acl); zhandle_t *zh = zhandles[zkhid]; int err = zoo_create(zh, path, values, valuelen, &aclv, flags, realbuf, maxbuf_len); Added: hadoop/zookeeper/trunk/src/contrib/zkpython/src/test/acl_test.py URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/contrib/zkpython/src/test/acl_test.py?rev=919280&view=auto ============================================================================== --- hadoop/zookeeper/trunk/src/contrib/zkpython/src/test/acl_test.py (added) +++ hadoop/zookeeper/trunk/src/contrib/zkpython/src/test/acl_test.py Fri Mar 5 03:47:38 2010 @@ -0,0 +1,89 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import zookeeper, zktestbase, unittest, threading + +ZOO_OPEN_ACL_UNSAFE = {"perms":zookeeper.PERM_ALL, "scheme":"world", "id" :"anyone"} +ZOO_ACL_READ = {"perms":zookeeper.PERM_READ, "scheme": "world", + "id":"anyone"} +class ACLTest(zktestbase.TestBase): + """Test whether basic ACL setting and getting work correctly""" + # to do: startup and teardown via scripts? + def setUp(self): + zktestbase.TestBase.setUp(self) + try: + zookeeper.delete(self.handle, "/zk-python-acltest") + zookeeper.delete(self.handle, "/zk-python-aacltest") + except: + pass + + 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]) + + + 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") + +if __name__ == '__main__': + unittest.main()