s4-dsdb-test: Implement samdb_connect_env() to rely solely on environment
authorKamen Mazdrashki <kamenim@samba.org>
Wed, 5 Nov 2014 05:26:25 +0000 (06:26 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 3 Feb 2015 04:02:11 +0000 (05:02 +0100)
this is to help me port Python tests to be more Unit test alike
and remove all global handling
Starting from a new test suite - tombstone_reanimation.py

Andrew Bartlett rose his concerns that passing parameters
through environment may make tests hard to trace for
failures. However, passing parameters on command line
is not Unit test alike either. After discussing this with him
offline, we agreed to continue this approach, but prefix
environment variables with "TEST_". So that an env var
should not be used by coincidence.

Change-Id: I29445c42cdcafede3897c8dd1f1529222a74afc9
Signed-off-by: Kamen Mazdrashki <kamenim@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
python/samba/tests/__init__.py

index 952b6eecdd147eb01ebbb0dec1d29ee8cfdaacf0..5b45865a815cbc44335acc8f54b437281cc7c979 100644 (file)
@@ -23,6 +23,7 @@ import samba
 import samba.auth
 from samba import param
 from samba.samdb import SamDB
+from samba import credentials
 import subprocess
 import tempfile
 
@@ -234,6 +235,26 @@ def connect_samdb_ex(samdb_url, lp=None, session_info=None, credentials=None,
     return (sam_db, res[0])
 
 
+def connect_samdb_env(env_url, env_username, env_password, lp=None):
+    """Connect to SamDB by getting URL and Credentials from environment
+
+    :param env_url: Environment variable name to get lsb url from
+    :param env_username: Username environment variable
+    :param env_password: Password environment variable
+    :return: sam_db_connection
+    """
+    samdb_url = env_get_var_value(env_url)
+    creds = credentials.Credentials()
+    if lp is None:
+        # guess Credentials parameters here. Otherwise workstation
+        # and domain fields are NULL and gencache code segfalts
+        lp = param.LoadParm()
+        creds.guess(lp)
+    creds.set_username(env_get_var_value(env_username))
+    creds.set_password(env_get_var_value(env_password))
+    return connect_samdb(samdb_url, credentials=creds, lp=lp)
+
+
 def delete_force(samdb, dn):
     try:
         samdb.delete(dn)