Add replacement addCleanup.
[samba.git] / python / samba / tests / __init__.py
index 952b6eecdd147eb01ebbb0dec1d29ee8cfdaacf0..65a727aaf6c606e69ad9b475070b8efb08aeb009 100644 (file)
@@ -23,21 +23,19 @@ import samba
 import samba.auth
 from samba import param
 from samba.samdb import SamDB
+from samba import credentials
 import subprocess
 import tempfile
+import unittest
 
-samba.ensure_external_module("mimeparse", "mimeparse")
-samba.ensure_external_module("extras", "extras")
-samba.ensure_external_module("testtools", "testtools")
+try:
+    from unittest import SkipTest
+except ImportError:
+    class SkipTest(Exception):
+        """Test skipped."""
 
-# Other modules import these two classes from here, for convenience:
-from testtools.testcase import (
-    TestCase as TesttoolsTestCase,
-    TestSkipped,
-    )
 
-
-class TestCase(TesttoolsTestCase):
+class TestCase(unittest.TestCase):
     """A Samba test case."""
 
     def setUp(self):
@@ -55,8 +53,31 @@ class TestCase(TesttoolsTestCase):
     def get_credentials(self):
         return cmdline_credentials
 
+    # These functions didn't exist before Python2.7:
+    if not getattr(unittest.TestCase, "skipTest", None):
+        def skipTest(self, reason):
+            raise SkipTest(reason)
+
+    if not getattr(unittest.TestCase, "assertIs", None):
+        def assertIs(self, a, b):
+            self.assertTrue(a is b)
+
+    if not getattr(unittest.TestCase, "assertIsNot", None):
+        def assertIsNot(self, a, b):
+            self.assertTrue(a is not b)
+
+    if not getattr(unittest.TestCase, "addCleanup", None):
+        def addCleanup(self, fn, *args, **kwargs):
+            self._cleanups = getattr(self, "_cleanups", []) + [
+                (fn, args, kwargs)]
+
+        def tearDown(self):
+            super(TestCase, self).tearDown()
+            for (fn, args, kwargs) in reversed(getattr(self, "_cleanups", [])):
+                fn(*args, **kwargs)
+
 
-class LdbTestCase(TesttoolsTestCase):
+class LdbTestCase(unittest.TestCase):
     """Trivial test case for running tests against a LDB."""
 
     def setUp(self):
@@ -234,6 +255,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)