r26596: Fixed upgrade.py.
authorJelmer Vernooij <jelmer@samba.org>
Tue, 25 Dec 2007 22:36:44 +0000 (16:36 -0600)
committerStefan Metzmacher <metze@samba.org>
Wed, 26 Dec 2007 17:57:08 +0000 (11:57 -0600)
Added blackbox tests for provision and upgrade Python scripts.
Clean up temporary files created by the Python tests.

source/lib/registry/tests/bindings.py
source/param/param.i
source/param/param_wrap.c
source/scripting/python/samba/__init__.py
source/scripting/python/samba/provision.py
source/scripting/python/samba/tests/__init__.py
source/scripting/python/samba/tests/provision.py
source/scripting/python/samba/upgrade.py
source/selftest/samba4_tests.sh
source/setup/provision.py
source/setup/upgrade.py

index 906deed7e94b22ca3b4b2a870fd0d6a911f6c6b9..314cf778a1609a13f78a8b3b08ef6db5a9a27802 100644 (file)
@@ -35,7 +35,12 @@ class HelperTests(unittest.TestCase):
 class HiveTests(samba.tests.TestCaseInTempDir):
     def setUp(self):
         super(HiveTests, self).setUp()
-        self.hive = registry.open_ldb(os.path.join(self.tempdir, "ldb_new.ldb"))
+        self.hive_path = os.path.join(self.tempdir, "ldb_new.ldb")
+        self.hive = registry.open_ldb(self.hive_path)
+
+    def tearDown(self):
+        del self.hive
+        os.unlink(self.hive_path)
 
     def test_ldb_new(self):
         self.assertTrue(self.hive is not None)
index 375c3d0da442c921a1f100efdb9e181dc5bb0c5a..353fa3ced62da553326b27526976fd85cf58ccc3 100644 (file)
@@ -59,6 +59,8 @@ typedef struct loadparm_context {
         bool is_myname(const char *name) { return lp_is_myname($self, name); }
         int use(struct param_context *param_ctx) { return param_use($self, param_ctx); }
         bool set(const char *parm_name, const char *parm_value) {
+            if (parm_value == NULL)
+                return false;
             return lp_set_cmdline($self, parm_name, parm_value);
         }
 
index 5f7ff880ffb2ab934189945a211e5628f1417f42..ca9fbb77b83412cf896af03756b276c929bc7f7e 100644 (file)
@@ -2643,6 +2643,8 @@ SWIGINTERN bool loadparm_context_is_mydomain(loadparm_context *self,char const *
 SWIGINTERN bool loadparm_context_is_myname(loadparm_context *self,char const *name){ return lp_is_myname(self, name); }
 SWIGINTERN int loadparm_context_use(loadparm_context *self,struct param_context *param_ctx){ return param_use(self, param_ctx); }
 SWIGINTERN bool loadparm_context_set(loadparm_context *self,char const *parm_name,char const *parm_value){
+            if (parm_value == NULL)
+                return false;
             return lp_set_cmdline(self, parm_name, parm_value);
         }
 SWIGINTERN PyObject *loadparm_context_get(loadparm_context *self,char const *param_name,char const *service_name){
index 5b345341330954695e62af143598e5afc41625dc..359457d81500e67d3122df1913ed57dac76daf04 100644 (file)
@@ -164,9 +164,9 @@ class Ldb(ldb.Ldb):
             self.add(msg)
 
     def modify_ldif(self, ldif):
-        for (changetype, msg) in ldb.parse_ldif(data):
-            assert changetype == CHANGETYPE_MODIFY
-            ldb.modify(msg)
+        for (changetype, msg) in self.parse_ldif(ldif):
+            assert changetype == ldb.CHANGETYPE_MODIFY
+            self.modify(msg)
 
 
 def substitute_var(text, values):
index a4e6c6a21422838210585d231169fa85b0329ef2..c4a3bb7fd60f7ad11e00ec1a8f23b6ee1912b6a7 100644 (file)
@@ -95,7 +95,7 @@ def setup_add_ldif(ldb, ldif_path, subst_vars=None):
 
     assert "${" not in data
 
-    ldb.load_ldif_add(data)
+    ldb.add_ldif(data)
 
 
 def setup_modify_ldif(ldb, ldif_path, substvars=None):
@@ -140,7 +140,7 @@ def setup_file(template, fname, substvars):
     open(f, 'w').write(data)
 
 
-def provision_default_paths(lp, dnsdomain):
+def provision_paths_from_lp(lp, dnsdomain):
     """Set the default paths for provisioning.
 
     :param lp: Loadparm context.
@@ -605,7 +605,7 @@ def provision(lp, setup_dir, message, blank, paths, session_info,
         realm = lp.get("realm")
     else:
         if lp.get("realm").upper() != realm.upper():
-            raise Error("realm '%s' in smb.conf must match chosen realm '%s'\n" %
+            raise Exception("realm '%s' in smb.conf must match chosen realm '%s'\n" %
                 (lp.get("realm"), realm))
 
     assert realm is not None
index 5885a3b507911ed9b47b4f8d651f8d358f43bf55..5e1ff87c2b51d94c1dc37987705b96feab5cbfda 100644 (file)
@@ -67,7 +67,12 @@ class SubstituteVarTestCase(unittest.TestCase):
 
 class LdbExtensionTests(TestCaseInTempDir):
     def test_searchone(self):
-        l = samba.Ldb(self.tempdir + "/searchone.ldb")
-        l.add({"dn": "foo=dc", "bar": "bla"})
-        self.assertEquals("bla", l.searchone(ldb.Dn(l, "foo=dc"), "bar"))
+        path = self.tempdir + "/searchone.ldb"
+        l = samba.Ldb(path)
+        try:
+            l.add({"dn": "foo=dc", "bar": "bla"})
+            self.assertEquals("bla", l.searchone(ldb.Dn(l, "foo=dc"), "bar"))
+        finally:
+            del l
+            os.unlink(path)
 
index bf7182dbd335c4cc26e5cad934c5ea8d9515608e..f5a0339c1f5de73fec3622f3dc9f0f2a038a769e 100644 (file)
@@ -29,10 +29,14 @@ def setup_path(file):
 
 class ProvisionTestCase(samba.tests.TestCaseInTempDir):
     def test_setup_secretsdb(self):
-        ldb = setup_secretsdb(os.path.join(self.tempdir, "secrets.ldb"), 
-                              setup_path, None, None, None)
-        self.assertEquals("LSA Secrets",
+        path = os.path.join(self.tempdir, "secrets.ldb")
+        ldb = setup_secretsdb(path, setup_path, None, None, None)
+        try:
+            self.assertEquals("LSA Secrets",
                  ldb.searchone(Dn(ldb, "CN=LSA Secrets"), "CN"))
+        finally:
+            del ldb
+            os.unlink(path)
 
 
 class Disabled:
index c13351bc63d817fa8b25d113311500f46ea3f72e..05a63d9326e03650ec2a61905d3e0c70b932a54d 100644 (file)
@@ -8,7 +8,6 @@
 """Support code for upgrading from Samba 3 to Samba 4."""
 
 from provision import findnss, provision
-import provision
 import grp
 import pwd
 import uuid
@@ -255,7 +254,7 @@ maxVersion: %llu
 
     return ldif
 
-def upgrade_provision(samba3, setup_dir, message, credentials, session_info, paths):
+def upgrade_provision(samba3, setup_dir, message, credentials, session_info, lp, paths):
     oldconf = samba3.get_conf()
 
     if oldconf.get("domain logons") == "True":
@@ -266,7 +265,11 @@ def upgrade_provision(samba3, setup_dir, message, credentials, session_info, pat
         else:
             serverrole = "member server"
 
+    lp.set("server role", serverrole)
     domainname = oldconf.get("workgroup")
+    if domainname:
+        domainname = str(domainname)
+    lp.set("workgroup", domainname)
     realm = oldconf.get("realm")
     netbiosname = oldconf.get("netbios name")
 
@@ -279,18 +282,19 @@ def upgrade_provision(samba3, setup_dir, message, credentials, session_info, pat
     if realm is None:
         realm = domainname.lower()
         message("No realm specified in smb.conf file, assuming '%s'\n" % realm)
+    lp.set("realm", realm)
 
     domainguid = secrets_db.get_domain_guid(domainname)
-    domainsid = secrets_db.get_sid(domainsid)
+    domainsid = secrets_db.get_sid(domainname)
     if domainsid is None:
         message("Can't find domain secrets for '%s'; using random SID\n" % domainname)
     
     if netbiosname is not None:
         machinepass = secrets_db.get_machine_password(netbiosname)
     else:
-        netbiosname = None
+        machinepass = None
     
-    provision(lp, setup_dir, message, blank=True, paths=path, session_info=session_info, 
+    provision(lp=lp, setup_dir=setup_dir, message=message, blank=True, ldapbackend=None, paths=paths, session_info=session_info, 
               credentials=credentials, realm=realm, domain=domainname, 
               domainsid=domainsid, domainguid=domainguid, machinepass=machinepass, serverrole=serverrole)
 
@@ -500,18 +504,6 @@ data: %d
 
     return ret
 
-def upgrade_verify(subobj, samba3, paths, message):
-    message("Verifying account policies")
-
-    samldb = Ldb(paths.samdb)
-
-    for account in samba3.samaccounts:
-        msg = samldb.search("(&(sAMAccountName=" + account.nt_username + ")(objectclass=user))")
-        assert(len(msg) >= 1)
-    
-    # FIXME
-
-
 
 def enable_samba3sam(samdb):
     samdb.modify("""
index 59c7635c196191b37672e6f14ee2d8dff8ac646d..309b2c067ef69b1f909a3b1351cdf8889fe681bd 100755 (executable)
@@ -297,7 +297,11 @@ then
        plantest "samba3.python" none PYTHONPATH=bin/python:scripting/python $PYTHON scripting/bin/subunitrun samba.tests.samba3
        plantest "samba3sam.python" none PYTHONPATH=bin/python:scripting/python $PYTHON dsdb/samdb/ldb_modules/tests/samba3sam.py `pwd` $DATADIR/samba3/
        plantest "ldap.python" dc $PYTHON $samba4srcdir/lib/ldb/tests/python/ldap.py \$SERVER -U\$USERNAME%\$PASSWORD
+       plantest "blackbox.samba3dump" none $PYTHON scripting/bin/samba3dump $samba4srcdir/../testdata/samba3
        rm -rf $PREFIX/upgrade
        mkdir -p $PREFIX/upgrade
-       plantest "blackbox.upgrade" none $PYTHON setup/upgrade.py $CONFIGURATION --verify --targetdir=$PREFIX ../testdata/samba3 ../testdata/samba3/smb.conf
+       plantest "blackbox.upgrade" none $PYTHON setup/upgrade.py $CONFIGURATION --targetdir=$PREFIX ../testdata/samba3 ../testdata/samba3/smb.conf
+       rm -rf $PREFIX/provision
+       mkdir -p $PREFIX/provision
+       plantest "blackbox.provision.py" none $PYTHON ./setup/provision.py $CONFIGURATION --domain=FOO --realm=foo --targetdir=$PREFIX/provision
 fi
index a16dde718dc90b67231b67d5b0c6cfc68697dd40..f6f032da707cc8e9c2d3d4f9947d446c4fa3c8e3 100755 (executable)
@@ -21,7 +21,7 @@
 
 import getopt
 import optparse
-import sys
+import os, sys
 
 # Add path to the library for in-tree use
 sys.path.append("scripting/python")
@@ -32,7 +32,7 @@ from auth import system_session
 import samba.getopt as options
 import param
 from samba.provision import (provision,  
-                             provision_default_paths, provision_ldapbase)
+                             provision_paths_from_lp, provision_ldapbase)
 
 parser = optparse.OptionParser("provision [options]")
 parser.add_option_group(options.SambaOptions(parser))
@@ -93,6 +93,8 @@ parser.add_option("--server-role", type="choice", metavar="ROLE",
                help="Set server role to provision for (default standalone)")
 parser.add_option("--partitions-only", 
                help="Configure Samba's partitions, but do not modify them (ie, join a BDC)", action="store_true")
+parser.add_option("--targetdir", type="string", metavar="DIR", 
+                         help="Set target directory")
 
 opts = parser.parse_args()[0]
 
@@ -101,29 +103,29 @@ def message(text):
        if not opts.quiet:
                print text
 
-hostname = opts.host_name
-
-if opts.realm is None or opts.domain is None or opts.host_name is None:
+if opts.realm is None or opts.domain is None:
        if opts.realm is None:
                print >>sys.stderr, "No realm set"
        if opts.domain is None:
                print >>sys.stderr, "No domain set"
-       if opts.host_name is None:
-               print >>sys.stderr, "No host name set"
        parser.print_usage()
        sys.exit(1)
 
 # cope with an initially blank smb.conf 
 lp = param.LoadParm()
-lp.load(opts.configfile)
+if opts.configfile:
+    lp.load(opts.configfile)
+if opts.targetdir is not None:
+    lp.set("private dir", os.path.abspath(opts.targetdir))
+    lp.set("lock dir", os.path.abspath(opts.targetdir))
 lp.set("realm", opts.realm)
 lp.set("workgroup", opts.domain)
-lp.set("server role", opts.server_role)
+lp.set("server role", opts.server_role or "domain controller")
 
 if opts.aci is not None:
        print "set ACI: %s" % opts.aci
 
-paths = provision_default_paths(lp, opts.realm.lower())
+paths = provision_paths_from_lp(lp, opts.realm.lower())
 paths.smbconf = opts.configfile
 
 if opts.ldap_backend:
index 186ad3772b54f6d28a0d2cc02596a8fa406401a9..c531e28ed5ee4b7fefd204eb045fcd33a5dd8bea 100755 (executable)
@@ -11,7 +11,7 @@ sys.path.append("scripting/python")
 import param
 import samba
 import samba.getopt as options
-from samba.provision import provision_default_paths
+from auth import system_session
 
 parser = optparse.OptionParser("upgrade [options] <libdir> <smbconf>")
 parser.add_option_group(options.SambaOptions(parser))
@@ -22,7 +22,6 @@ parser.add_option("--setupdir", type="string", metavar="DIR",
                help="directory with setup files")
 parser.add_option("--realm", type="string", metavar="REALM", help="set realm")
 parser.add_option("--quiet", help="Be quiet")
-parser.add_option("--verify", help="Verify resulting configuration")
 parser.add_option("--blank", 
                help="do not add users or groups, just the structure")
 parser.add_option("--targetdir", type="string", metavar="DIR", 
@@ -51,6 +50,7 @@ else:
 samba3 = Samba3(libdir, smbconf)
 
 from samba.upgrade import upgrade_provision
+from samba.provision import provision_paths_from_lp
 
 message("Provisioning\n")
 
@@ -60,9 +60,12 @@ if setup_dir is None:
 
 creds = credopts.get_credentials()
 lp = param.LoadParm()
-lp.load(opts.configfile)
-upgrade_provision(samba3, setup_dir, message, credentials=creds, session_info=system_session())
-
-if opts.verify:
-       message("Verifying...\n")
-       ret = upgrade_verify(subobj, samba3, paths, message)
+if opts.configfile:
+    lp.load(opts.configfile)
+if opts.targetdir is not None:
+    lp.set("private dir", os.path.abspath(opts.targetdir))
+    lp.set("lock dir", os.path.abspath(opts.targetdir))
+paths = provision_paths_from_lp(lp, "")
+paths.smbconf = opts.configfile
+upgrade_provision(samba3, setup_dir, message, credentials=creds, session_info=system_session(), 
+                  lp=lp, paths=paths)