Skip to content

Commit

Permalink
more re-factoring of slapdtest
Browse files Browse the repository at this point in the history
  • Loading branch information
stroeder committed Apr 27, 2017
1 parent 48b65a4 commit b17af25
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
3 changes: 2 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Lib/slapdtest.py
instances side-by-side (e.g. with replication)
* added authz-regexp mapping to rootdn for user running the test
* internally use SASL/EXTERNAL via LDAPI to bind
* SlapdObject.server_id used as serverID in slapd.conf for MMR

Tests/
* ReconnectLDAPObject is also tested by sub-classing test class
Expand Down Expand Up @@ -1429,4 +1430,4 @@ Released 2.0.0pre02 2002-02-01
----------------------------------------------------------------
Released 1.10alpha3 2000-09-19

$Id: CHANGES,v 1.440 2017/04/27 16:10:27 stroeder Exp $
$Id: CHANGES,v 1.441 2017/04/27 16:45:19 stroeder Exp $
39 changes: 17 additions & 22 deletions Lib/slapdtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
See http://www.python-ldap.org/ for details.
\$Id: slapdtest.py,v 1.6 2017/04/27 16:06:52 stroeder Exp $
\$Id: slapdtest.py,v 1.7 2017/04/27 16:45:19 stroeder Exp $
Python compability note:
This module only works with Python 2.7.x since
Expand Down Expand Up @@ -32,6 +32,7 @@

# a template string for generating simple slapd.conf file
SLAPD_CONF_TEMPLATE = r"""
serverID %(serverid)s
moduleload back_%(database)s
include "%(schema_include)s"
loglevel %(loglevel)s
Expand Down Expand Up @@ -120,12 +121,16 @@ def __init__(self):
self._log = combined_logger('python-ldap-test')
self._proc = None
self._port = self._avail_tcp_port()
self.server_id = self._port % 4096
self.testrundir = os.path.join(self.TMPDIR, 'python-ldap-test-%d' % self._port)
self._slapd_conf = os.path.join(self.testrundir, 'slapd.conf')
self._db_directory = os.path.join(self.testrundir, "openldap-data")
self.ldap_uri = "ldap://%s:%d/" % (LOCALHOST, self._port)
ldapi_path = os.path.join(self.testrundir, 'ldapi')
self.ldapi_uri = "ldapi://%s" % urllib.quote_plus(ldapi_path)
assert self.suffix.startswith("dc=")
assert self.root_dn.startswith("cn=")
assert self.root_dn.endswith("," + self.suffix)

def _setup_rundir(self):
"""
Expand Down Expand Up @@ -170,6 +175,7 @@ def _gen_config(self):
generates a slapd.conf and returns it as one string
"""
config_dict = {
'serverid': hex(self.server_id),
'schema_include': self.INIT_SCHEMA_PATH,
'loglevel': self.slapd_loglevel,
'database': self.database,
Expand Down Expand Up @@ -229,7 +235,7 @@ def _start_slapd(self):
raise RuntimeError("slapd exited before opening port")
time.sleep(self._start_sleep)
try:
self._log.debug("Connecting to %s", self.ldap_uri)
self._log.debug("slapd connection check to %s", self.ldapi_uri)
self.ldapwhoami()
except RuntimeError:
pass
Expand All @@ -245,18 +251,14 @@ def start(self):
# prepare directory structure
self._cleanup_rundir()
self._setup_rundir()
try:
self._write_config()
self._test_config()
self._start_slapd()
self._log.debug(
'slapd with pid=%d listing on %s and %s',
self._proc.pid, self.ldap_uri, self.ldapi_uri
)
self.started()
finally:
pass
#self._cleanup_rundir()
self._write_config()
self._test_config()
self._start_slapd()
self._log.debug(
'slapd with pid=%d listening on %s and %s',
self._proc.pid, self.ldap_uri, self.ldapi_uri
)
self.started()

def stop(self):
"""Stops the slapd server, and waits for it to terminate"""
Expand Down Expand Up @@ -285,10 +287,6 @@ def _stopped(self):
if self._proc is not None:
self._log.info('slapd terminated')
self._proc = None
try:
os.remove(self._slapd_conf)
except os.error:
self._log.debug('could not remove %s', self._slapd_conf)

def _cli_auth_args(self):
if self.cli_sasl_external:
Expand Down Expand Up @@ -346,10 +344,7 @@ def started(self):
By default, this method adds the two initial objects,
the domain object and the root user object.
"""
assert self.suffix.startswith("dc=")
suffix_dc = self.suffix.split(',')[0][3:]
assert self.root_dn.startswith("cn=")
assert self.root_dn.endswith("," + self.suffix)
self._log.debug(
"adding %s and %s",
self.suffix,
Expand All @@ -364,7 +359,7 @@ def started(self):
'o: '+suffix_dc,
'',
'dn: '+self.root_dn,
'objectClass: organizationalRole',
'objectClass: applicationProcess',
'cn: '+self.root_cn,
''
])
Expand Down

0 comments on commit b17af25

Please sign in to comment.