diff options
author | Kamen Mazdrashki <kamenim@samba.org> | 2010-09-29 02:58:23 +0300 |
---|---|---|
committer | Kamen Mazdrashki <kamenim@samba.org> | 2010-10-03 01:24:56 +0300 |
commit | 7acbb7346fdf96f25b3c465dd070b86694beb12b (patch) | |
tree | 549fd055fd7775bd0c66c4360d9a65a88966f489 /source4/scripting/python | |
parent | 46ac1c3b478d087723962b6a6b6febf38620bcc9 (diff) | |
download | samba-7acbb7346fdf96f25b3c465dd070b86694beb12b.tar.gz samba-7acbb7346fdf96f25b3c465dd070b86694beb12b.tar.bz2 samba-7acbb7346fdf96f25b3c465dd070b86694beb12b.zip |
s4-python-test: Implement global connect_samdb() function
This helper makes proper ldb url to connect to
and is a shorthand for test to create SamDB connections
Diffstat (limited to 'source4/scripting/python')
-rw-r--r-- | source4/scripting/python/samba/tests/__init__.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/source4/scripting/python/samba/tests/__init__.py b/source4/scripting/python/samba/tests/__init__.py index f2c2fbff63..bf7c563c4e 100644 --- a/source4/scripting/python/samba/tests/__init__.py +++ b/source4/scripting/python/samba/tests/__init__.py @@ -22,6 +22,7 @@ import os import ldb import samba +import samba.auth from samba import param import subprocess import tempfile @@ -100,3 +101,48 @@ class BlackboxTestCase(TestCase): parts[0] = os.path.join(bindir, parts[0]) line = " ".join(parts) subprocess.check_call(line, shell=True) + + +def connect_samdb(samdb_url, lp=None, session_info=None, + credentials=None, flags=0, ldb_options=None, ldap_only=False): + """Creates SamDB instance and connects to samdb_url database. + + :param samdb_url: Url for database to connect to. + :param lp: Optional loadparm object + :param session_info: Optional session information + :param credentials: Optional credentials, defaults to anonymous. + :param flags: Optional LDB flags + :param ldap_only: If set, only remote LDAP connection will be created. + + Added value for tests is that we have a shorthand function + to make proper URL for ldb.connect() while using default + parameters for connection based on test environment + """ + samdb_url = samdb_url.lower() + if not "://" in samdb_url: + if not ldap_only and os.path.isfile(samdb_url): + samdb_url = "tdb://%s" % samdb_url + else: + samdb_url = "ldap://%s" % samdb_url + # use 'paged_search' module when connecting remotely + if samdb_url.startswith("ldap://"): + ldb_options = ["modules:paged_searches"] + else: + assert not ldap_only, \ + "Trying to connect to %s while remote connection is required" % samdb_url + + # set defaults for test environment + if not lp: + lp=env_loadparm() + if not session_info: + session_info=samba.auth.system_session() + if not credentials: + credentials=cmdline_credentials + + from samba.samdb import SamDB + return SamDB(url=samdb_url, + lp=lp, + session_info=session_info, + credentials=credentials, + flags=flags, + options=ldb_options) |