Skip to content

Commit f01b66e

Browse files
committed
Use PATH to lookup test binaries
Instead of hard-coded paths /usr/bin and /usr/sbin, the SlapdObject test helper now uses PATH env var to find test binaries. For slapd server binary, sbin paths are automatically added to lookup PATH in case they are missing. Signed-off-by: Christian Heimes <cheimes@redhat.com>
1 parent 50e9c2e commit f01b66e

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

Lib/slapdtest/_slapdtest.py

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import os
1111
import socket
12+
import sys
1213
import time
1314
import subprocess
1415
import logging
@@ -109,6 +110,46 @@ def requires_ldapi():
109110
return identity
110111

111112

113+
def _which(cmd):
114+
"""Specialized which command based on shutil.which() from Python 3.6.
115+
116+
* simplified
117+
* always adds /sbin directories to path
118+
"""
119+
120+
def _access_check(fn):
121+
return (os.path.exists(fn) and os.access(fn, os.F_OK | os.X_OK)
122+
and not os.path.isdir(fn))
123+
124+
# Path with directory part skips PATH lookup.
125+
if os.path.dirname(cmd):
126+
if _access_check(cmd):
127+
return cmd
128+
return None
129+
130+
path = os.environ.get("PATH", os.defpath).split(os.pathsep)
131+
132+
if sys.platform == 'win32':
133+
if os.curdir not in path:
134+
path.insert(0, os.curdir)
135+
# include path extension (.exe)
136+
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
137+
files = [cmd + ext for ext in pathext]
138+
else:
139+
# always include sbin for slapd binary
140+
for sbin in ['/usr/local/sbin', '/sbin', '/usr/sbin']:
141+
if sbin not in path:
142+
path.append(sbin)
143+
files = [cmd]
144+
145+
for directory in path:
146+
for name in files:
147+
name = os.path.join(directory, name)
148+
if _access_check(name):
149+
return name
150+
return None
151+
152+
112153
def combined_logger(
113154
log_name,
114155
log_level=logging.WARN,
@@ -172,8 +213,6 @@ class SlapdObject(object):
172213
)
173214

174215
TMPDIR = os.environ.get('TMP', os.getcwd())
175-
SBINDIR = os.environ.get('SBIN', '/usr/sbin')
176-
BINDIR = os.environ.get('BIN', '/usr/bin')
177216
if 'SCHEMA' in os.environ:
178217
SCHEMADIR = os.environ['SCHEMA']
179218
elif os.path.isdir("/etc/openldap/schema"):
@@ -182,12 +221,14 @@ class SlapdObject(object):
182221
SCHEMADIR = "/etc/ldap/schema"
183222
else:
184223
SCHEMADIR = None
185-
PATH_LDAPADD = os.path.join(BINDIR, 'ldapadd')
186-
PATH_LDAPDELETE = os.path.join(BINDIR, 'ldapdelete')
187-
PATH_LDAPMODIFY = os.path.join(BINDIR, 'ldapmodify')
188-
PATH_LDAPWHOAMI = os.path.join(BINDIR, 'ldapwhoami')
189-
PATH_SLAPD = os.environ.get('SLAPD', os.path.join(SBINDIR, 'slapd'))
190-
PATH_SLAPTEST = os.path.join(SBINDIR, 'slaptest')
224+
# _check_requirements turns paths into absolute paths
225+
PATH_LDAPADD = 'ldapadd'
226+
PATH_LDAPDELETE = 'ldapdelete'
227+
PATH_LDAPMODIFY = 'ldapmodify'
228+
PATH_LDAPWHOAMI = 'ldapwhoami'
229+
# The following two binaries are usually in /usr/sbin.
230+
PATH_SLAPD = os.environ.get('SLAPD', 'slapd')
231+
PATH_SLAPTEST = 'slaptest'
191232

192233
# time in secs to wait before trying to access slapd via LDAP (again)
193234
_start_sleep = 1.5
@@ -223,13 +264,19 @@ def __init__(self):
223264
self.clientkey = os.path.join(HERE, 'certs/client.key')
224265

225266
def _check_requirements(self):
226-
binaries = [
227-
self.PATH_LDAPADD, self.PATH_LDAPMODIFY, self.PATH_LDAPWHOAMI,
228-
self.PATH_SLAPD, self.PATH_SLAPTEST
267+
names = [
268+
"PATH_LDAPADD", "PATH_LDAPMODIFY", "PATH_LDAPDELETE",
269+
"PATH_LDAPWHOAMI", "PATH_SLAPD", "PATH_SLAPTEST",
229270
]
230-
for binary in binaries:
231-
if not os.path.isfile(binary):
232-
raise ValueError('Binary {} is missing.'.format(binary))
271+
for name in names:
272+
value = getattr(self, name)
273+
binary = _which(value)
274+
if binary is None:
275+
raise ValueError(
276+
"Command '{}' not found in PATH".format(value)
277+
)
278+
else:
279+
setattr(self, name, binary)
233280
if self.SCHEMADIR is None:
234281
raise ValueError('SCHEMADIR is None, ldap schemas are missing.')
235282

@@ -359,7 +406,7 @@ def _start_slapd(self):
359406
self.PATH_SLAPD,
360407
'-f', self._slapd_conf,
361408
'-F', self.testrundir,
362-
'-h', '%s' % ' '.join(urls),
409+
'-h', ' '.join(urls),
363410
]
364411
if self._log.isEnabledFor(logging.DEBUG):
365412
slapd_args.extend(['-d', '-1'])

0 commit comments

Comments
 (0)