Skip to content

Commit 9a5c5fc

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 603cde0 commit 9a5c5fc

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

Lib/slapdtest/_slapdtest.py

Lines changed: 45 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,35 @@ 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+
path = os.environ.get("PATH", os.defpath).split(os.pathsep)
120+
121+
if sys.platform == 'win32':
122+
if os.curdir not in path:
123+
path.insert(0, os.curdir)
124+
# include path extension (.exe)
125+
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
126+
files = [cmd + ext for ext in pathext]
127+
else:
128+
# always include sbin for slapd binary
129+
for sbin in ['/sbin', '/usr/sbin', '/usr/local/sbin']:
130+
if sbin not in path:
131+
path.append(sbin)
132+
files = [cmd]
133+
134+
for directory in path:
135+
for name in files:
136+
name = os.path.join(directory, name)
137+
if os.path.exists(name) and os.access(name, os.F_OK | os.X_OK):
138+
return name
139+
return None
140+
141+
112142
def combined_logger(
113143
log_name,
114144
log_level=logging.WARN,
@@ -172,8 +202,6 @@ class SlapdObject(object):
172202
)
173203

174204
TMPDIR = os.environ.get('TMP', os.getcwd())
175-
SBINDIR = os.environ.get('SBIN', '/usr/sbin')
176-
BINDIR = os.environ.get('BIN', '/usr/bin')
177205
if 'SCHEMA' in os.environ:
178206
SCHEMADIR = os.environ['SCHEMA']
179207
elif os.path.isdir("/etc/openldap/schema"):
@@ -182,12 +210,12 @@ class SlapdObject(object):
182210
SCHEMADIR = "/etc/ldap/schema"
183211
else:
184212
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')
213+
PATH_LDAPADD = _which('ldapadd')
214+
PATH_LDAPDELETE = _which('ldapdelete')
215+
PATH_LDAPMODIFY = _which('ldapmodify')
216+
PATH_LDAPWHOAMI = _which('ldapwhoami')
217+
PATH_SLAPD = os.environ.get('SLAPD', _which('slapd'))
218+
PATH_SLAPTEST = _which('slaptest')
191219

192220
# time in secs to wait before trying to access slapd via LDAP (again)
193221
_start_sleep = 1.5
@@ -223,13 +251,15 @@ def __init__(self):
223251
self.clientkey = os.path.join(HERE, 'certs/client.key')
224252

225253
def _check_requirements(self):
226-
binaries = [
227-
self.PATH_LDAPADD, self.PATH_LDAPMODIFY, self.PATH_LDAPWHOAMI,
228-
self.PATH_SLAPD, self.PATH_SLAPTEST
229-
]
230-
for binary in binaries:
231-
if not os.path.isfile(binary):
232-
raise ValueError('Binary {} is missing.'.format(binary))
254+
for name in dir(self):
255+
if not name.startswith('PATH_'):
256+
continue
257+
value = getattr(self, name)
258+
if value is None or not os.path.isfile(value):
259+
cmd = name[5:].lower()
260+
raise ValueError(
261+
"Command '{}' not found in PATH".format(cmd)
262+
)
233263
if self.SCHEMADIR is None:
234264
raise ValueError('SCHEMADIR is None, ldap schemas are missing.')
235265

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy