KCC: ldif_utils: ldif_to_samdb doesn't need creds; begin selftest
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Mon, 27 Apr 2015 23:38:51 +0000 (11:38 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Sat, 30 May 2015 19:05:24 +0000 (21:05 +0200)
The tests are based on the testdata/ldif-utils-test-multisite.ldif
which describes a multisite windows network. It was constructed by
Garming Sam.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/ldif_utils.py
python/samba/tests/ldif_utils.py [new file with mode: 0644]
selftest/tests.py
source4/scripting/bin/samba_kcc

index 55d2dd3cd2bdd03a27bc9cc546511843509b23a4..47f4fd0fa7865076c7ac19d1df649667e8bc9a5c 100644 (file)
@@ -38,7 +38,7 @@ def write_search_result(samdb, f, res):
         f.write("%s" % lstr)
 
 
-def ldif_to_samdb(dburl, lp, creds, ldif_file, forced_local_dsa=None):
+def ldif_to_samdb(dburl, lp, ldif_file, forced_local_dsa=None):
     """Routine to import all objects and attributes that are relevent
     to the KCC algorithms from a previously exported LDIF file.
 
@@ -83,8 +83,7 @@ dsServiceName: CN=NTDS Settings,%s
     # We have an abbreviated list of options here because we have built
     # an abbreviated database.  We use the rootdse and extended-dn
     # modules only during this re-open
-    samdb = SamDB(url=dburl, session_info=system_session(),
-                  credentials=creds, lp=lp,
+    samdb = SamDB(url=dburl, session_info=system_session(), lp=lp,
                   options=["modules:rootdse,extended_dn_in,"
                            "extended_dn_out_ldb"])
     return samdb
diff --git a/python/samba/tests/ldif_utils.py b/python/samba/tests/ldif_utils.py
new file mode 100644 (file)
index 0000000..975810a
--- /dev/null
@@ -0,0 +1,99 @@
+# Unix SMB/CIFS implementation. Tests for graph_utils.py routines
+# Copyright (C) Andrew Bartlett 2015
+#
+# Written by Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+"""Tests for samba.ldif_utils"""
+
+import samba
+import os
+import sys
+from tempfile import mkdtemp
+import shutil
+
+import samba.tests
+from samba import ldif_utils
+from samba import ldb
+
+from samba.param import LoadParm
+from samba.credentials import Credentials
+from samba.samdb import SamDB
+import itertools
+
+MULTISITE_LDIF = os.path.join(os.environ['SRCDIR_ABS'],
+                              "testdata/ldif-utils-test-multisite.ldif")
+
+MULTISITE_LDIF_DSAS = (
+    ("CN=WIN08,CN=Servers,CN=Site-4,CN=Sites,CN=Configuration,DC=ad,DC=samba,DC=example,DC=com",
+     "Site-4"),
+    ("CN=WIN07,CN=Servers,CN=Site-4,CN=Sites,CN=Configuration,DC=ad,DC=samba,DC=example,DC=com",
+     "Site-4"),
+    ("CN=WIN06,CN=Servers,CN=Site-3,CN=Sites,CN=Configuration,DC=ad,DC=samba,DC=example,DC=com",
+     "Site-3"),
+    ("CN=WIN09,CN=Servers,CN=Site-5,CN=Sites,CN=Configuration,DC=ad,DC=samba,DC=example,DC=com",
+     "Site-5"),
+    ("CN=WIN10,CN=Servers,CN=Site-5,CN=Sites,CN=Configuration,DC=ad,DC=samba,DC=example,DC=com",
+     "Site-5"),
+    ("CN=WIN02,CN=Servers,CN=Site-2,CN=Sites,CN=Configuration,DC=ad,DC=samba,DC=example,DC=com",
+     "Site-2"),
+    ("CN=WIN04,CN=Servers,CN=Site-2,CN=Sites,CN=Configuration,DC=ad,DC=samba,DC=example,DC=com",
+     "Site-2"),
+    ("CN=WIN03,CN=Servers,CN=Site-2,CN=Sites,CN=Configuration,DC=ad,DC=samba,DC=example,DC=com",
+     "Site-2"),
+    ("CN=WIN05,CN=Servers,CN=Site-2,CN=Sites,CN=Configuration,DC=ad,DC=samba,DC=example,DC=com",
+     "Site-2"),
+    ("CN=WIN01,CN=Servers,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=ad,DC=samba,DC=example,DC=com",
+     "Default-First-Site-Name"),
+)
+
+class LdifUtilTests(samba.tests.TestCase):
+    def setUp(self):
+        super(LdifUtilTests, self).setUp()
+        self.lp = LoadParm()
+        self.creds = Credentials()
+        self.creds.guess(self.lp)
+        #self.creds.set_machine_account(self.lp)
+        self.tmpdir = mkdtemp()
+
+    def tearDown(self):
+        #shutil.rmtree(self.tmpdir)
+        pass
+
+    def test_write_search_url(self):
+        pass
+        #write_search_result(samdb, f, res)
+
+    def test_ldif_to_samdb(self):
+        dburl = os.path.join(self.tmpdir, "ldap")
+        samdb = ldif_utils.ldif_to_samdb(dburl, self.lp, MULTISITE_LDIF)
+        self.assertIsInstance(samdb, SamDB)
+
+    def test_ldif_to_samdb_forced_local_dsa(self):
+        for dsa, site in MULTISITE_LDIF_DSAS:
+            dburl = os.path.join(self.tmpdir, "ldif-to-samba-forced-local-dsa"
+                                 "-%s" % dsa)
+            samdb = ldif_utils.ldif_to_samdb(dburl, self.lp, MULTISITE_LDIF,
+                                             forced_local_dsa=dsa)
+            self.assertIsInstance(samdb, SamDB)
+            self.assertEqual(samdb.server_site_name(), site)
+            dn = ldb.Dn(samdb, "<GUID=%s>" % samdb.get_ntds_GUID())
+            print dn
+
+
+    def samdb_to_ldif_file(self):
+        #samdb_to_ldif_file(samdb, dburl, lp, creds, ldif_file):
+        pass
index 8cf35aca20a84f4e0212c9883a28f603e80daa34..e6a92079a5d76739226d91daffe8df41304bc0a7 100644 (file)
@@ -97,4 +97,5 @@ planpythontestsuite("none", "samba.tests.xattr")
 planpythontestsuite("none", "samba.tests.ntacls")
 planpythontestsuite("none", "samba.tests.policy")
 planpythontestsuite("none", "samba.tests.graph_utils")
+planpythontestsuite("none", "samba.tests.ldif_utils")
 plantestsuite("wafsamba.duplicate_symbols", "none", [os.path.join(srcdir(), "buildtools/wafsamba/test_duplicate_symbol.sh")])
index 8e7ec84a6f9ecdc3a7cac254c50c6abff553403f..8213ce8802404f74aa2864b2e70a51a82b621d95 100755 (executable)
@@ -2589,7 +2589,7 @@ class KCC(object):
         :param ldif_file: path to the ldif file to import
         """
         try:
-            self.samdb = ldif_utils.ldif_to_samdb(dburl, lp, creds, ldif_file,
+            self.samdb = ldif_utils.ldif_to_samdb(dburl, lp, ldif_file,
                                                   opts.forced_local_dsa)
         except ldif_utils.LdifError, e:
             print e