Provide a SamDB TestCase-class that can be used by OpenChange.
[amitay/samba.git] / source4 / scripting / python / samba / tests / samdb.py
index 3745dba6fc27474312f265ae199b02e3cf8f8745..d0b95cf542ec108294c6a18e3d6f6e1c620ebb98 100644 (file)
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
-from auth import system_session
-from credentials import Credentials
+from samba.auth import system_session
+from samba.credentials import Credentials
 import os
-from samba.provision import setup_samdb
+from samba.provision import setup_samdb, guess_names, setup_templatesdb, make_smbconf, find_setup_dir
 from samba.samdb import SamDB
-from samba.tests import get_loadparm, TestCaseInTempDir
-import security
+from samba.tests import TestCaseInTempDir
+from samba.dcerpc import security
 from unittest import TestCase
 import uuid
+from samba import param
+
 
 class SamDBTestCase(TestCaseInTempDir):
+    """Base-class for tests with a Sam Database.
+    
+    This is used by the Samba SamDB-tests, but e.g. also by the OpenChange
+    provisioning tests (which need a Sam).
+    """
+
+    def setup_path(self, relpath):
+        return os.path.join(find_setup_dir(), relpath)
+
     def setUp(self):
         super(SamDBTestCase, self).setUp()
-        invocationid = uuid.random()
+        invocationid = str(uuid.uuid4())
         domaindn = "DC=COM,DC=EXAMPLE"
         self.domaindn = domaindn
         configdn = "CN=Configuration," + domaindn
         schemadn = "CN=Schema," + configdn
-        domainguid = uuid.random()
-        policyguid = uuid.random()
-        setup_path = lambda x: os.path.join("setup", x)
+        domainguid = str(uuid.uuid4())
+        policyguid = str(uuid.uuid4())
         creds = Credentials()
         creds.set_anonymous()
         domainsid = security.random_sid()
-        hostguid = uuid.random()
+        hostguid = str(uuid.uuid4())
         path = os.path.join(self.tempdir, "samdb.ldb")
-        self.samdb = setup_samdb(path, setup_path, system_session(), creds, 
-                                 get_loadparm(), schemadn, configdn, 
-                                 self.domaindn, "example.com", "EXAMPLE.COM", 
-                                 "FOO", lambda x: None, "foo", domaindn, 
-                                 False, domainsid, "# no aci", domainguid, 
-                                 policyguid, "EXAMPLE", True, "secret", 
-                                 "secret", "secret", hostguid, invocationid, 
+        session_info = system_session()
+        
+        hostname="foo"
+        domain="EXAMPLE"
+        dnsdomain="example.com" 
+        serverrole="domain controller"
+
+        smbconf = os.path.join(self.tempdir, "smb.conf")
+        make_smbconf(smbconf, self.setup_path, hostname, domain, dnsdomain, 
+                     serverrole, self.tempdir)
+
+        self.lp = param.LoadParm()
+        self.lp.load(smbconf)
+
+        names = guess_names(lp=self.lp, hostname=hostname, 
+                            domain=domain, dnsdomain=dnsdomain, 
+                            serverrole=serverrole, 
+                            domaindn=self.domaindn, configdn=configdn, 
+                            schemadn=schemadn)
+        setup_templatesdb(os.path.join(self.tempdir, "templates.ldb"), 
+                          self.setup_path, session_info=session_info, 
+                          credentials=creds, lp=self.lp)
+        self.samdb = setup_samdb(path, self.setup_path, session_info, creds, 
+                                 self.lp, names, 
+                                 lambda x: None, domainsid, 
+                                 "# no aci", domainguid, 
+                                 policyguid, False, "secret", 
+                                 "secret", "secret", invocationid, 
                                  "secret", "domain controller")
 
+    def tearDown(self):
+        for f in ['templates.ldb', 'schema.ldb', 'configuration.ldb', 
+                  'users.ldb', 'samdb.ldb', 'smb.conf']:
+            os.remove(os.path.join(self.tempdir, f))
+        super(SamDBTestCase, self).tearDown()
+
+
+class SamDBTests(SamDBTestCase):
+    """Tests for the SamDB implementation."""
+
     def test_add_foreign(self):
         self.samdb.add_foreign(self.domaindn, "S-1-5-7", "Somedescription")