comparison MoinMoin/_tests/ldap_testbase.py @ 3651:df024fd0a129

LDAP tests: add checks for the environment needed to run the tests. skip test with reason if environment is unsuitable.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 01 Jun 2008 13:43:48 +0200
parents 23851c20e53f
children 2d5a325ae247
comparison
equal deleted inserted replaced
3650:23851c20e53f 3651:df024fd0a129
34 34
35 @copyright: 2008 by Thomas Waldmann 35 @copyright: 2008 by Thomas Waldmann
36 @license: GNU GPL, see COPYING for details. 36 @license: GNU GPL, see COPYING for details.
37 """ 37 """
38 38
39 SLAPD_EXECUTABLE = 'slapd' # filename of LDAP server executable - if it is not
40 # in your PATH, you have to give full path/filename.
41
39 import os, shutil, tempfile, time 42 import os, shutil, tempfile, time
40 from StringIO import StringIO 43 from StringIO import StringIO
41 import signal 44 import signal
42 import subprocess # needs Python 2.4 45
43 46 try:
44 import ldap, ldif, ldap.modlist 47 import subprocess # needs Python 2.4
48 except ImportError:
49 subprocess = None
50
51 try:
52 import ldap, ldif, ldap.modlist # needs python-ldap
53 except ImportError:
54 ldap = None
55
56
57 def check_environ():
58 """ Check the system environment whether we are able to run.
59 Either return some failure reason if we can't or None if everything
60 looks OK.
61 """
62 if subprocess is None:
63 return "You need at least python 2.4 to use ldap_testbase."
64 if ldap is None:
65 return "You need python-ldap installed to use ldap_testbase."
66 slapd = False
67 try:
68 p = subprocess.Popen([SLAPD_EXECUTABLE, '-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
69 pid = p.pid
70 rc = p.wait()
71 if pid and rc == 1:
72 slapd = True # it works
73 except OSError, err:
74 import errno
75 if not (err.errno == errno.ENOENT or
76 (err.errno == 3 and os.name == 'nt')):
77 raise
78 if not slapd:
79 return "Can't start %s (see SLAPD_EXECUTABLE)." % SLAPD_EXECUTABLE
80 return None
81
45 82
46 class Slapd(object): 83 class Slapd(object):
47 """ Manage a slapd process for testing purposes """ 84 """ Manage a slapd process for testing purposes """
48 def __init__(self, 85 def __init__(self,
49 config=None, # config filename for -f 86 config=None, # config filename for -f
50 executable='slapd', # slapd executable filename 87 executable=SLAPD_EXECUTABLE,
51 debug_flags='', # None, # for -d stats,acl,args,trace,sync,config 88 debug_flags='', # None, # for -d stats,acl,args,trace,sync,config
52 proto='ldap', ip='127.0.0.1', port=3890, # use -h proto://ip:port 89 proto='ldap', ip='127.0.0.1', port=3890, # use -h proto://ip:port
53 service_name='' # defaults to -n executable:port, use None to not use -n 90 service_name='' # defaults to -n executable:port, use None to not use -n
54 ): 91 ):
55 self.executable = executable 92 self.executable = executable
165 f.close() 202 f.close()
166 203
167 def start_slapd(self): 204 def start_slapd(self):
168 """ start a slapd and optionally wait until it talks with us """ 205 """ start a slapd and optionally wait until it talks with us """
169 self.slapd = Slapd(config=self.slapd_conf, port=3890+self.instance) 206 self.slapd = Slapd(config=self.slapd_conf, port=3890+self.instance)
170 self.slapd.start(timeout=self.timeout) 207 started = self.slapd.start(timeout=self.timeout)
208 return started
171 209
172 def load_directory(self, ldif_content): 210 def load_directory(self, ldif_content):
173 """ load the directory with the ldif_content (str) """ 211 """ load the directory with the ldif_content (str) """
174 lo = ldap.initialize(self.slapd.url) 212 lo = ldap.initialize(self.slapd.url)
175 ldap.set_option(ldap.OPT_PROTOCOL_VERSION, ldap.VERSION3) # ldap v2 is outdated 213 ldap.set_option(ldap.OPT_PROTOCOL_VERSION, ldap.VERSION3) # ldap v2 is outdated
188 226
189 def destroy_env(self): 227 def destroy_env(self):
190 """ remove the temporary LDAP server environment """ 228 """ remove the temporary LDAP server environment """
191 shutil.rmtree(self.ldap_dir) 229 shutil.rmtree(self.ldap_dir)
192 230
193 231 try:
194 class LDAPTestBase: 232 import py.test
195 """ Test base class for py.test based tests which need a LDAP server to talk to. 233
196 234 class LDAPTestBase:
197 Inherit your test class from this base class to test LDAP stuff. 235 """ Test base class for py.test based tests which need a LDAP server to talk to.
198 """ 236
199 237 Inherit your test class from this base class to test LDAP stuff.
200 # You MUST define these in your derived class: 238 """
201 slapd_config = None # a string with your slapd.conf template 239
202 ldif_content = None # a string with your ldif contents 240 # You MUST define these in your derived class:
203 basedn = None # your base DN 241 slapd_config = None # a string with your slapd.conf template
204 rootdn = None # root DN 242 ldif_content = None # a string with your ldif contents
205 rootpw = None # root password 243 basedn = None # your base DN
206 244 rootdn = None # root DN
207 def setup_class(self): 245 rootpw = None # root password
208 """ Create LDAP server environment, start slapd """ 246
209 self.ldap_env = LdapEnvironment(self.basedn, self.rootdn, self.rootpw) 247 def setup_class(self):
210 self.ldap_env.create_env(slapd_config=self.slapd_config) 248 """ Create LDAP server environment, start slapd """
211 self.ldap_env.start_slapd() 249 self.ldap_env = LdapEnvironment(self.basedn, self.rootdn, self.rootpw)
212 self.ldap_env.load_directory(ldif_content=self.ldif_content) 250 self.ldap_env.create_env(slapd_config=self.slapd_config)
213 251 started = self.ldap_env.start_slapd()
214 def teardown_class(self): 252 if not started:
215 """ Stop slapd, remove LDAP server environment """ 253 py.test.skip("Failed to start %s process, please see your syslog / log files"
216 self.ldap_env.stop_slapd() 254 " (and check if stopping apparmor helps, in case you use it)." % SLAPD_EXECUTABLE)
217 self.ldap_env.destroy_env() 255 self.ldap_env.load_directory(ldif_content=self.ldif_content)
218 256
219 257 def teardown_class(self):
258 """ Stop slapd, remove LDAP server environment """
259 self.ldap_env.stop_slapd()
260 self.ldap_env.destroy_env()
261
262 except ImportError:
263 pass # obviously py.test not in use
264