changeset 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 0b04ad4db631
files MoinMoin/_tests/ldap_testbase.py MoinMoin/auth/_tests/test_ldap_login.py
diffstat 2 files changed, 80 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/ldap_testbase.py	Sun Jun 01 02:18:38 2008 +0200
+++ b/MoinMoin/_tests/ldap_testbase.py	Sun Jun 01 13:43:48 2008 +0200
@@ -36,18 +36,55 @@
     @license: GNU GPL, see COPYING for details.
 """
 
+SLAPD_EXECUTABLE = 'slapd'  # filename of LDAP server executable - if it is not
+                            # in your PATH, you have to give full path/filename.
+
 import os, shutil, tempfile, time
 from StringIO import StringIO
 import signal
-import subprocess  # needs Python 2.4
 
-import ldap, ldif, ldap.modlist
+try:
+    import subprocess  # needs Python 2.4
+except ImportError:
+    subprocess = None
+
+try:
+    import ldap, ldif, ldap.modlist  # needs python-ldap
+except ImportError:
+    ldap = None
+
+
+def check_environ():
+    """ Check the system environment whether we are able to run.
+        Either return some failure reason if we can't or None if everything
+        looks OK.
+    """
+    if subprocess is None:
+        return "You need at least python 2.4 to use ldap_testbase."
+    if ldap is None:
+        return "You need python-ldap installed to use ldap_testbase."
+    slapd = False
+    try:
+        p = subprocess.Popen([SLAPD_EXECUTABLE, '-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        pid = p.pid
+        rc = p.wait()
+        if pid and rc == 1:
+            slapd = True  # it works
+    except OSError, err:
+        import errno
+        if not (err.errno == errno.ENOENT or
+                (err.errno == 3 and os.name == 'nt')):
+            raise
+    if not slapd:
+        return "Can't start %s (see SLAPD_EXECUTABLE)." % SLAPD_EXECUTABLE
+    return None
+
 
 class Slapd(object):
     """ Manage a slapd process for testing purposes """
     def __init__(self,
                  config=None,  # config filename for -f
-                 executable='slapd',  # slapd executable filename
+                 executable=SLAPD_EXECUTABLE,
                  debug_flags='', # None,  # for -d stats,acl,args,trace,sync,config
                  proto='ldap', ip='127.0.0.1', port=3890,  # use -h proto://ip:port
                  service_name=''  # defaults to -n executable:port, use None to not use -n
@@ -167,7 +204,8 @@
     def start_slapd(self):
         """ start a slapd and optionally wait until it talks with us """
         self.slapd = Slapd(config=self.slapd_conf, port=3890+self.instance)
-        self.slapd.start(timeout=self.timeout)
+        started = self.slapd.start(timeout=self.timeout)
+        return started
 
     def load_directory(self, ldif_content):
         """ load the directory with the ldif_content (str) """
@@ -190,30 +228,37 @@
         """ remove the temporary LDAP server environment """
         shutil.rmtree(self.ldap_dir)
 
-
-class LDAPTestBase:
-    """ Test base class for py.test based tests which need a LDAP server to talk to.
-
-        Inherit your test class from this base class to test LDAP stuff.
-    """
+try:
+    import py.test
 
-    # You MUST define these in your derived class:
-    slapd_config = None  # a string with your slapd.conf template
-    ldif_content = None  # a string with your ldif contents
-    basedn = None  # your base DN
-    rootdn = None  # root DN
-    rootpw = None  # root password
+    class LDAPTestBase:
+        """ Test base class for py.test based tests which need a LDAP server to talk to.
 
-    def setup_class(self):
-        """ Create LDAP server environment, start slapd """
-        self.ldap_env = LdapEnvironment(self.basedn, self.rootdn, self.rootpw)
-        self.ldap_env.create_env(slapd_config=self.slapd_config)
-        self.ldap_env.start_slapd()
-        self.ldap_env.load_directory(ldif_content=self.ldif_content)
+            Inherit your test class from this base class to test LDAP stuff.
+        """
 
-    def teardown_class(self):
-        """ Stop slapd, remove LDAP server environment """
-        self.ldap_env.stop_slapd()
-        self.ldap_env.destroy_env()
+        # You MUST define these in your derived class:
+        slapd_config = None  # a string with your slapd.conf template
+        ldif_content = None  # a string with your ldif contents
+        basedn = None  # your base DN
+        rootdn = None  # root DN
+        rootpw = None  # root password
 
+        def setup_class(self):
+            """ Create LDAP server environment, start slapd """
+            self.ldap_env = LdapEnvironment(self.basedn, self.rootdn, self.rootpw)
+            self.ldap_env.create_env(slapd_config=self.slapd_config)
+            started = self.ldap_env.start_slapd()
+            if not started:
+                py.test.skip("Failed to start %s process, please see your syslog / log files"
+                             " (and check if stopping apparmor helps, in case you use it)." % SLAPD_EXECUTABLE)
+            self.ldap_env.load_directory(ldif_content=self.ldif_content)
 
+        def teardown_class(self):
+            """ Stop slapd, remove LDAP server environment """
+            self.ldap_env.stop_slapd()
+            self.ldap_env.destroy_env()
+
+except ImportError:
+    pass  # obviously py.test not in use
+
--- a/MoinMoin/auth/_tests/test_ldap_login.py	Sun Jun 01 02:18:38 2008 +0200
+++ b/MoinMoin/auth/_tests/test_ldap_login.py	Sun Jun 01 13:43:48 2008 +0200
@@ -6,13 +6,19 @@
     @license: GNU GPL, see COPYING for details.
 """
 
-import ldap
-
 import py.test
 
-from MoinMoin._tests.ldap_testbase import LDAPTestBase, LdapEnvironment
+from MoinMoin._tests.ldap_testbase import LDAPTestBase, LdapEnvironment, check_environ
 from MoinMoin._tests.ldap_testdata import *
 
+# first check if we have python 2.4, python-ldap and slapd:
+msg = check_environ()
+if msg:
+    py.test.skip(msg)
+del msg
+
+import ldap
+
 class TestSimpleLdap(LDAPTestBase):
     basedn = BASEDN
     rootdn = ROOTDN