Skip to content

Commit 2c63f23

Browse files
author
jensens
committed
improvements in test shutdown and use hashlib no (BBB for py 2.4 included)
git-svn-id: https://svn.plone.org/svn/collective/bda.ldap/trunk@106784 db7f04ef-aaf3-0310-a811-c281ed44c4ad
1 parent 8bc3832 commit 2c63f23

File tree

8 files changed

+133
-62
lines changed

8 files changed

+133
-62
lines changed

README.txt

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ TODO
9898
Changes
9999
-------
100100

101+
* 1.3.3 (unreleased)
102+
* improved stop mechanism of ldap server in tests (jensens, 2009-12-16)
103+
* remove deprecation warning: use `hashlib` for md5 and fallback to `md5`
104+
with python2.4. (jensens, 2009-12-16)
105+
101106
* 1.3.2 (rnix, 2009-09-02)
102107
* handle timeout of cache, workaround atm
103108

bootstrap.py

+67-20
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,84 @@
1616
Simply run this script in a directory containing a buildout.cfg.
1717
The script accepts buildout command-line options, so you can
1818
use the -c option to specify an alternate configuration file.
19-
20-
$Id: bootstrap.py 72703 2007-02-20 11:49:26Z jim $
2119
"""
2220

2321
import os, shutil, sys, tempfile, urllib2
22+
from optparse import OptionParser
2423

2524
tmpeggs = tempfile.mkdtemp()
2625

27-
ez = {}
28-
exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
26+
is_jython = sys.platform.startswith('java')
27+
28+
# parsing arguments
29+
parser = OptionParser()
30+
parser.add_option("-v", "--version", dest="version",
31+
help="use a specific zc.buildout version")
32+
33+
options, args = parser.parse_args()
34+
35+
if options.version is not None:
36+
VERSION = '==%s' % options.version
37+
else:
38+
VERSION = ''
39+
40+
args = args + ['bootstrap']
41+
42+
to_reload = False
43+
try:
44+
import pkg_resources
45+
if not hasattr(pkg_resources, '_distribute'):
46+
to_reload = True
47+
raise ImportError
48+
except ImportError:
49+
ez = {}
50+
exec urllib2.urlopen('http://python-distribute.org/distribute_setup.py'
2951
).read() in ez
30-
ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)
52+
ez['use_setuptools'](to_dir=tmpeggs, download_delay=0, no_fake=True)
3153

32-
import pkg_resources
54+
if to_reload:
55+
reload(pkg_resources)
56+
else:
57+
import pkg_resources
3358

34-
cmd = 'from setuptools.command.easy_install import main; main()'
3559
if sys.platform == 'win32':
36-
cmd = '"%s"' % cmd # work around spawn lamosity on windows
37-
38-
ws = pkg_resources.working_set
39-
assert os.spawnle(
40-
os.P_WAIT, sys.executable, sys.executable,
41-
'-c', cmd, '-mqNxd', tmpeggs, 'zc.buildout',
42-
dict(os.environ,
43-
PYTHONPATH=
44-
ws.find(pkg_resources.Requirement.parse('setuptools')).location
45-
),
46-
) == 0
60+
def quote(c):
61+
if ' ' in c:
62+
return '"%s"' % c # work around spawn lamosity on windows
63+
else:
64+
return c
65+
else:
66+
def quote (c):
67+
return c
68+
69+
cmd = 'from setuptools.command.easy_install import main; main()'
70+
ws = pkg_resources.working_set
71+
72+
requirement = 'distribute'
73+
74+
if is_jython:
75+
import subprocess
76+
77+
assert subprocess.Popen([sys.executable] + ['-c', quote(cmd), '-mqNxd',
78+
quote(tmpeggs), 'zc.buildout' + VERSION],
79+
env=dict(os.environ,
80+
PYTHONPATH=
81+
ws.find(pkg_resources.Requirement.parse(requirement)).location
82+
),
83+
).wait() == 0
84+
85+
else:
86+
assert os.spawnle(
87+
os.P_WAIT, sys.executable, quote (sys.executable),
88+
'-c', quote (cmd), '-mqNxd', quote (tmpeggs), 'zc.buildout' + VERSION,
89+
dict(os.environ,
90+
PYTHONPATH=
91+
ws.find(pkg_resources.Requirement.parse(requirement)).location
92+
),
93+
) == 0
4794

4895
ws.add_entry(tmpeggs)
49-
ws.require('zc.buildout')
96+
ws.require('zc.buildout' + VERSION)
5097
import zc.buildout.buildout
51-
zc.buildout.buildout.main(sys.argv[1:] + ['bootstrap'])
98+
zc.buildout.buildout.main(args)
5299
shutil.rmtree(tmpeggs)

buildout.cfg

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[buildout]
22
extends = ldap.cfg
33
develop = .
4-
parts = test py omelette
4+
parts = test py omelette dependencytree
55

66
[test]
77
recipe = zc.recipe.testrunner
@@ -12,8 +12,9 @@ environment = testenv
1212

1313
[testenv]
1414
TESTLDIF = ${buildout:directory}/testenv/data.ldif
15-
SLAPDBIN = ${buildout:parts-directory}/openldap/libexec/slapd
16-
LDAPADDBIN = ${buildout:parts-directory}/openldap/bin/ldapadd
15+
SLAPDBIN = ${openldap:location}/libexec/slapd
16+
LDAPADDBIN = ${openldap:location}/bin/ldapadd
17+
LDAPPIDFILE = ${openldap:location}/var/run/slapd.pid
1718
DBDIR = ${buildout:parts-directory}/openldap/var/openldap-data
1819

1920
[py]
@@ -25,3 +26,10 @@ eggs = ${test:eggs}
2526
recipe = collective.recipe.omelette
2627
eggs = ${test:eggs}
2728
ignore-develop = True
29+
30+
[dependencytree]
31+
recipe = zc.recipe.egg
32+
eggs =
33+
${test:eggs}
34+
tl.eggdeps
35+
ignore-develop = True

ldap.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# this build needs (on debian based systems):
33
# apt-get install libsasl2-dev libssl-dev libdb4.4-dev
44
recipe = zc.recipe.cmmi>=1.1.5
5-
url = ftp://sunsite.cnlab-switch.ch/mirror/OpenLDAP/openldap-release/openldap-2.4.13.tgz
5+
url = ftp://sunsite.cnlab-switch.ch/mirror/OpenLDAP/openldap-release/openldap-2.4.20.tgz
66
extra_options = --with-sasl --with-tls --enable-slapd=yes
77
patch = ${buildout:directory}/patches/getpeereid.patch
88
patch_options = -p0

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
extras_require={
3838
'test': [
3939
'interlude',
40-
'bda.cache [test]',
40+
'zope.configuration',
41+
'zope.testing',
4142
]
4243
},
4344
entry_points="""

src/bda/ldap/base.py

+25-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,23 @@
1010
"""
1111

1212
import logging
13-
import md5
13+
14+
15+
def md5digest(key):
16+
"""needed to support both, python 2.4 and python >=2.5
17+
18+
remove when python 2.4 support is skipped.
19+
"""
20+
try:
21+
# in python >=2.5
22+
import hashlib
23+
except ImportError:
24+
# fallback if python 2.4
25+
import md5
26+
return md5.new(key).hexdigest()
27+
m = hashlib.md5()
28+
m.update(key)
29+
return m.hexdigest()
1430

1531
logger = logging.getLogger('bda.ldap')
1632

@@ -163,7 +179,7 @@ def search(self, queryFilter, scope, baseDN=None,
163179
baseDN,
164180
queryFilter,
165181
scope)
166-
key=md5.new(key).hexdigest() # Hash keys to make them short Gogo.
182+
key = md5digest(key)
167183
args = [baseDN, scope, queryFilter, attrlist, attrsonly]
168184
return self.cache.getData(self._con.search_s, key,
169185
force_reload, args)
@@ -174,7 +190,7 @@ def add(self, dn, data):
174190
"""Insert an entry into directory.
175191
176192
Takes the DN of the entry and the data this object contains. data is a
177-
dictionary and looks ike this:
193+
dictionary and looks like this:
178194
179195
data = {
180196
'uid':'foo',
@@ -213,15 +229,18 @@ def delete(self, deleteDN):
213229
"""
214230
self._con.delete_s(deleteDN)
215231

216-
if __name__ == "__main__":
232+
233+
def main():
217234
"""Use this module from command line for testing the connectivity to the
218235
LDAP Server.
219236
220237
Expects server and port as arguments."""
221-
222238
import sys
223239
if len(sys.argv) < 3:
224240
print 'usage: python base.py [server] [port]'
225241
else:
226242
server, port = sys.argv[1:]
227-
print testLDAPConnectivity(server, int(port))
243+
print testLDAPConnectivity(server, int(port))
244+
245+
if __name__ == "__main__":
246+
main()

src/bda/ldap/tests/stopslapd.txt

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
Stop openldap server.
22

3-
>>> import os
4-
5-
Normally the os increases the pid of the forked process by 1. There might be
6-
the case where some other process is started between the root process and the
7-
forking of the child process, in this case the simple pid + 1 increasing will
8-
fail and we kill the wrong process
9-
10-
XXX: improve this
11-
12-
>>> os.kill(slapdpid + 1, 9)
3+
>>> PIDFILE = os.environ.get('LDAPPIDFILE')
4+
>>> with open(PIDFILE) as pidfile:
5+
... pid = pidfile.readline()
6+
>>> pid = int(pid.strip())
7+
>>> import os
8+
>>> os.kill(pid, 9)
139

1410
Flush openldap db files.
1511

16-
>>> import os
17-
>>> dbdir = os.environ.get('DBDIR')
18-
>>> for file in os.listdir(dbdir):
19-
... os.remove('%s/%s' % (dbdir, file))
20-
>>> os.listdir(dbdir)
21-
[]
12+
>>> import os
13+
>>> dbdir = os.environ.get('DBDIR')
14+
>>> for file in os.listdir(dbdir):
15+
... os.remove('%s/%s' % (dbdir, file))
16+
>>> os.listdir(dbdir)
17+
[]

src/bda/ldap/tests/test_ldap.py

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
__author__ = """Robert Niederreiter <[email protected]>"""
2-
__docformat__ = 'plaintext'
1+
# Copyright 2008-2009, BlueDynamics Alliance, Austria - http://bluedynamics.com
2+
# GNU General Public Licence Version 2 or later
33

44
import time
55
import os
6-
from subprocess import Popen
6+
import subprocess
77
import unittest
88
import interlude
9-
import zope.component
10-
from pprint import pprint
9+
import pprint
1110
from zope.testing import doctest
12-
from zope.app.testing.placelesssetup import setUp, tearDown
13-
from zope.configuration.xmlconfig import XMLConfig
14-
15-
import bda.ldap
1611

1712
optionflags = doctest.NORMALIZE_WHITESPACE | \
1813
doctest.ELLIPSIS | \
@@ -28,8 +23,7 @@
2823
"""
2924

3025
slapdbin = os.environ.get('SLAPDBIN', None)
31-
pr = Popen([slapdbin, '-h', 'ldap://127.0.0.1:12345/'])
32-
slapdpid = pr.pid
26+
pr = subprocess.Popen([slapdbin, '-h', 'ldap://127.0.0.1:12345/'])
3327
time.sleep(1)
3428

3529
TESTFILES = [
@@ -40,19 +34,20 @@
4034
]
4135

4236
def test_suite():
43-
setUp()
37+
from zope.configuration.xmlconfig import XMLConfig
38+
import zope.component
4439
XMLConfig('meta.zcml', zope.component)()
40+
import bda.ldap
4541
XMLConfig('configure.zcml', bda.ldap)()
4642
return unittest.TestSuite([
4743
doctest.DocFileSuite(
4844
file,
4945
optionflags=optionflags,
5046
globs={'interact': interlude.interact,
51-
'pprint': pprint,
52-
'slapdpid': slapdpid},
47+
'pprint': pprint.pprint,
48+
},
5349
) for file in TESTFILES
5450
])
55-
tearDown()
5651

5752
if __name__ == '__main__':
5853
unittest.main(defaultTest='test_suite')

0 commit comments

Comments
 (0)