From 465c3e4036e7bfff7b1ac1c6070061d469e49e2f Mon Sep 17 00:00:00 2001 From: Steve Martinelli Date: Mon, 2 May 2016 12:55:34 -0700 Subject: [PATCH] enable ldap tests for py3 by moving to pyldap and a new py3 friendly version of ldappool, we can now run keystone's ldap tests with py3. Change-Id: I0da97db0859079e3a134c6ec1de0c4b4f16f7035 --- keystone/tests/unit/__init__.py | 18 ------------------ keystone/tests/unit/common/test_ldap.py | 2 +- keystone/tests/unit/fakeldap.py | 11 ++++++----- tests-py3-blacklist.txt | 4 ---- 4 files changed, 7 insertions(+), 28 deletions(-) diff --git a/keystone/tests/unit/__init__.py b/keystone/tests/unit/__init__.py index 0e92ca65a5..4697eb05e0 100644 --- a/keystone/tests/unit/__init__.py +++ b/keystone/tests/unit/__init__.py @@ -13,24 +13,6 @@ # under the License. import oslo_i18n -import six - - -if six.PY3: - # NOTE(dstanek): This block will monkey patch libraries that are not - # yet supported in Python3. We do this that that it is possible to - # execute any tests at all. Without monkey patching modules the - # tests will fail with import errors. - - import sys - from unittest import mock # noqa: our import detection is naive? - - sys.modules['ldap'] = mock.Mock() - sys.modules['ldap.controls'] = mock.Mock() - sys.modules['ldap.dn'] = mock.Mock() - sys.modules['ldap.filter'] = mock.Mock() - sys.modules['ldap.modlist'] = mock.Mock() - sys.modules['ldappool'] = mock.Mock() # NOTE(dstanek): oslo_i18n.enable_lazy() must be called before diff --git a/keystone/tests/unit/common/test_ldap.py b/keystone/tests/unit/common/test_ldap.py index 75e9d18da6..25d1124a62 100644 --- a/keystone/tests/unit/common/test_ldap.py +++ b/keystone/tests/unit/common/test_ldap.py @@ -422,7 +422,7 @@ class CommonLdapTestCase(unit.BaseTestCase): 'cn': ['junk'], 'sn': [uuid.uuid4().hex], 'mail': [uuid.uuid4().hex], - 'binary_attr': ['\x00\xFF\x00\xFF'] + 'binary_attr': [b'\x00\xFF\x00\xFF'] } ), ] py_result = ks_ldap.convert_ldap_result(result) diff --git a/keystone/tests/unit/fakeldap.py b/keystone/tests/unit/fakeldap.py index 07dc84e7a9..4d9a0c44de 100644 --- a/keystone/tests/unit/fakeldap.py +++ b/keystone/tests/unit/fakeldap.py @@ -80,7 +80,6 @@ def _internal_attr(attr_name, value_or_values): name, val, i = part[0] name = core.utf8_decode(name) name = name.upper() - name = core.utf8_encode(name) norm.append([(name, val, i)]) return core.utf8_decode(ldap.dn.dn2str(norm)) @@ -299,7 +298,8 @@ class FakeLdap(core.LDAPHandler): if server_fail: raise ldap.SERVER_DOWN whos = ['cn=Admin', CONF.ldap.user] - if who in whos and cred in ['password', CONF.ldap.password]: + if (core.utf8_decode(who) in whos and + core.utf8_decode(cred) in ['password', CONF.ldap.password]): return try: @@ -316,7 +316,7 @@ class FakeLdap(core.LDAPHandler): core.utf8_decode(who)) raise ldap.INAPPROPRIATE_AUTH - if cred != db_password: + if cred != core.utf8_encode(db_password): LOG.debug('bind fail: password for who=%s does not match', core.utf8_decode(who)) raise ldap.INVALID_CREDENTIALS @@ -524,7 +524,8 @@ class FakeLdap(core.LDAPHandler): match_attrs = attrs.copy() match_attrs[id_attr] = [id_val] attrs_checked = set() - if not filterstr or _match_query(filterstr, match_attrs, + if not filterstr or _match_query(core.utf8_decode(filterstr), + match_attrs, attrs_checked): if (filterstr and (scope != ldap.SCOPE_BASE) and @@ -532,7 +533,7 @@ class FakeLdap(core.LDAPHandler): raise AssertionError('No objectClass in search filter') # filter the attributes by attrlist attrs = {k: v for k, v in attrs.items() - if not attrlist or k in attrlist} + if not attrlist or k in core.utf8_decode(attrlist)} objects.append((dn, attrs)) return objects diff --git a/tests-py3-blacklist.txt b/tests-py3-blacklist.txt index 69f585e34a..8605b3a1ac 100644 --- a/tests-py3-blacklist.txt +++ b/tests-py3-blacklist.txt @@ -1,7 +1,3 @@ -keystone.tests.unit.common.test_ldap -keystone.tests.unit.identity.backends.test_ldap -keystone.tests.unit.test_backend_ldap -keystone.tests.unit.test_backend_ldap_pool keystone.tests.unit.test_v3_auth keystone.tests.unit.test_v3_credential keystone.tests.unit.test_v3_oauth1