Skip to content

Commit

Permalink
re-factored slapd.py, t_cext.py and t_search.py
Browse files Browse the repository at this point in the history
  • Loading branch information
stroeder committed Apr 26, 2017
1 parent b38ad5e commit 3fa67ea
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 44 deletions.
82 changes: 40 additions & 42 deletions Tests/slapd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
and talking to it with ldapsearch/ldapadd.
"""

import sys
import os
import socket
import time
Expand Down Expand Up @@ -87,46 +86,35 @@ class SlapdObject:
root_pw = 'password'
slapd_loglevel = 'stats stats2'

_log = _LOGGER

# Use /var/tmp to placate apparmour on Ubuntu:
PATH_TMPDIR = '/var/tmp'
PATH_SBINDIR = '/usr/sbin'
PATH_BINDIR = '/usr/bin'
PATH_SCHEMA_CORE = '/etc/openldap/schema/core.schema'
PATH_LDAPADD = os.path.join(PATH_BINDIR, 'ldapadd')
PATH_LDAPSEARCH = os.path.join(PATH_BINDIR, 'ldapsearch')
PATH_LDAPWHOAMI = os.path.join(PATH_BINDIR, 'ldapwhoami')
PATH_SLAPD = os.path.join(PATH_SBINDIR, 'slapd')
PATH_SLAPTEST = os.path.join(PATH_SBINDIR, 'slaptest')

def __init__(self):
self._proc = None
self._port = 0
self._port = find_available_tcp_port(LOCALHOST)
self.ldap_uri = "ldap://%s:%d/" % (LOCALHOST, self._port)
self._log = _LOGGER
self._tmpdir = os.path.join(os.environ.get('TMP', self.PATH_TMPDIR), 'python-ldap-test')
self._slapd_conf = os.path.join(self._tmpdir, "slapd.conf")
self._db_directory = os.path.join(self._tmpdir, "openldap-data")
self._config = self._generate_slapd_conf()
self._log.setLevel(_LOG_LEVEL)

# getters
def get_url(self):
return "ldap://%s:%d/" % self.get_address()

def get_address(self):
if self._port == 0:
self._port = find_available_tcp_port(LOCALHOST)
return (LOCALHOST, self._port)
# init directory structure
delete_directory_content(self._tmpdir)
mkdirs(self._tmpdir)
mkdirs(self._db_directory)

def __del__(self):
self.stop()

def _generate_slapd_conf(self):
"""
returns a string with a complete slapd.conf
"""
# reinitialize database
delete_directory_content(self._db_directory)
# generate slapd.conf lines
def _write_config(self):
"""Writes the slapd.conf file out, and returns the path to it."""
config_dict = {
'path_schema_core': quote(self.PATH_SCHEMA_CORE),
'loglevel': self.slapd_loglevel,
Expand All @@ -136,15 +124,9 @@ def _generate_slapd_conf(self):
'rootdn': quote(self.root_dn),
'rootpw': quote(self.root_pw),
}
return SLAPD_CONF_TEMPLATE % config_dict

def _write_config(self):
"""Writes the slapd.conf file out, and returns the path to it."""
mkdirs(self._tmpdir)
mkdirs(self._db_directory)
self._log.debug("writing config to %s", self._config)
self._log.debug("writing config to %s", self._slapd_conf)
config_file = file(self._slapd_conf, "wb")
config_file.write(self._config)
config_file.write(SLAPD_CONF_TEMPLATE % config_dict)
config_file.close()

def start(self):
Expand All @@ -155,11 +137,12 @@ def start(self):
ok = False
config_path = None
try:
self._write_config()
self._test_configuration()
self._start_slapd()
self._wait_for_slapd()
ok = True
self._log.debug("slapd ready at %s", self.get_url())
self._log.debug("slapd ready at %s", self.ldap_uri)
self.started()
finally:
if not ok:
Expand All @@ -176,7 +159,7 @@ def _start_slapd(self):
self._proc = subprocess.Popen([
self.PATH_SLAPD,
"-f", self._slapd_conf,
"-h", self.get_url(),
"-h", self.ldap_uri,
"-d", "0",
])

Expand All @@ -188,12 +171,12 @@ def _wait_for_slapd(self):
self._stopped()
raise RuntimeError("slapd exited before opening port")
try:
self._log.debug("Connecting to %s", repr(self.get_address()))
s.connect(self.get_address())
s.close()
return
except socket.error:
self._log.debug("Connecting to %s", self.ldap_uri)
self.ldapwhoami()
except RuntimeError:
time.sleep(1)
else:
return

def stop(self):
"""Stops the slapd server, and waits for it to terminate"""
Expand Down Expand Up @@ -232,7 +215,6 @@ def _stopped(self):
self._log.debug("could not remove %s", self._slapd_conf)

def _test_configuration(self):
self._write_config()
self._log.debug("testing configuration")
popen_list = [
self.PATH_SLAPTEST,
Expand All @@ -250,8 +232,24 @@ def _test_configuration(self):
raise RuntimeError("configuration test failed")
self._log.debug("configuration seems ok")
finally:
pass
#os.remove(self._slapd_conf)
os.remove(self._slapd_conf)

def ldapwhoami(self, extra_args=None):
"""Runs ldapwhoami on this slapd instance"""
extra_args = extra_args or []
self._log.debug("whoami")
p = subprocess.Popen(
[
self.PATH_LDAPWHOAMI,
"-x",
"-D", self.root_dn,
"-w", self.root_pw,
"-H", self.ldap_uri
] + extra_args,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
)
if p.wait() != 0:
raise RuntimeError("ldapwhoami process failed")

def ldapadd(self, ldif, extra_args=None):
"""Runs ldapadd on this slapd instance, passing it the ldif content"""
Expand All @@ -263,7 +261,7 @@ def ldapadd(self, ldif, extra_args=None):
"-x",
"-D", self.root_dn,
"-w", self.root_pw,
"-H", self.get_url()
"-H", self.ldap_uri
] + extra_args,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
)
Expand All @@ -286,7 +284,7 @@ def ldapsearch(
"-x",
"-D", self.root_dn,
"-w", self.root_pw,
"-H", self.get_url(),
"-H", self.ldap_uri,
"-b", base,
"-s", scope,
"-LL",
Expand Down
2 changes: 1 addition & 1 deletion Tests/t_cext.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _init(self, reuse_existing=True, bind=True):
Starts a server, and returns a LDAPObject bound to it
"""
server = self._init_server(reuse_existing)
l = _ldap.initialize(server.get_url())
l = _ldap.initialize(server.ldap_uri)
if bind:
# Perform a simple bind
l.set_option(_ldap.OPT_PROTOCOL_VERSION, _ldap.VERSION3)
Expand Down
2 changes: 1 addition & 1 deletion Tests/t_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def setUp(self):
"",
])+"\n")

l = LDAPObject(server.get_url())
l = LDAPObject(server.ldap_uri)
l.protocol_version = 3
l.set_option(ldap.OPT_REFERRALS,0)
l.simple_bind_s(server.root_dn,
Expand Down

0 comments on commit 3fa67ea

Please sign in to comment.