[ldap test] use get_available_port from cw.devtools.httptest stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 11 Feb 2011 16:45:43 +0100
branchstable
changeset 6986 18343456ee49
parent 6985 dda970c8a315
child 6987 d62d4ba5ef3e
[ldap test] use get_available_port from cw.devtools.httptest
server/test/unittest_ldapuser.py
--- a/server/test/unittest_ldapuser.py	Fri Feb 11 13:38:40 2011 +0100
+++ b/server/test/unittest_ldapuser.py	Fri Feb 11 16:45:43 2011 +0100
@@ -27,6 +27,7 @@
 from logilab.common.testlib import TestCase, unittest_main, mock_object
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.devtools.repotest import RQLGeneratorTC
+from cubicweb.devtools.httptest import get_available_port
 
 from cubicweb.server.sources.ldapuser import *
 
@@ -102,19 +103,7 @@
 
 
     #ldapuri = 'ldapi://' + join(basedir, "ldapi").replace('/', '%2f')
-    for port in range(9000, 9100):
-        try:
-            socket().bind(('localhost', port))
-        except socketerror, e:
-            if e.errno == 98: # Address already in use
-                pass
-            else:
-                raise
-        else:
-            break
-    else:
-        raise Exception("Can't find a free TCP port on localhost")
-
+    port = get_available_port(xrange(9000, 9100))
     host = 'localhost:%s' % port
     ldapuri = 'ldap://%s' % host
     cmdline = ["/usr/sbin/slapd", "-f",  slapdconf,  "-h",  ldapuri, "-d", "0"]
@@ -132,7 +121,11 @@
     global slapd_process
     if slapd_process.returncode is None:
         print "terminating slapd"
-        slapd_process.terminate()
+        if hasattr(slapd_process, 'terminate'):
+            slapd_process.terminate()
+        else:
+            import os, signal
+            os.kill(slapd_process.pid, signal.SIGTERM)
         slapd_process.wait()
         print "DONE"