diff options
-rw-r--r-- | source4/scripting/python/samba/__init__.py | 2 | ||||
-rw-r--r-- | source4/scripting/python/samba/provision.py | 60 | ||||
-rw-r--r-- | source4/scripting/python/samba/tests/__init__.py | 17 | ||||
-rw-r--r-- | source4/scripting/python/samba/tests/provision.py | 59 | ||||
-rwxr-xr-x | source4/selftest/samba4_tests.sh | 1 |
5 files changed, 97 insertions, 42 deletions
diff --git a/source4/scripting/python/samba/__init__.py b/source4/scripting/python/samba/__init__.py index 3981464681..9ac283e660 100644 --- a/source4/scripting/python/samba/__init__.py +++ b/source4/scripting/python/samba/__init__.py @@ -129,8 +129,6 @@ def substitute_var(text, values): assert isinstance(value, str), "Value %r for %s is not a string" % (value, name) text = text.replace("${%s}" % name, value) - assert "${" not in text, text - return text diff --git a/source4/scripting/python/samba/provision.py b/source4/scripting/python/samba/provision.py index ebc8288351..04f50e8359 100644 --- a/source4/scripting/python/samba/provision.py +++ b/source4/scripting/python/samba/provision.py @@ -135,18 +135,6 @@ def hostname(): return gethostname().split(".")[0] -def ldb_delete(ldb): - """Delete a LDB file. - - This may be necessary if the ldb is in bad shape, possibly due to being - built from an incompatible previous version of the code, so delete it - completely. - """ - print "Deleting %s\n" % ldb.filename - os.unlink(ldb.filename) - ldb.connect(ldb.filename) - - def open_ldb(session_info, credentials, lp, dbname): assert session_info is not None try: @@ -176,6 +164,13 @@ def setup_add_ldif(ldb, setup_dir, ldif, subst_vars=None): def setup_modify_ldif(ldb, setup_dir, ldif, substvars=None): + """Modify a ldb in the private dir. + + :param ldb: LDB object. + :param setup_dir: Setup directory. + :param ldif: LDIF file path. + :param substvars: Optional dictionary with substitution variables. + """ src = os.path.join(setup_dir, ldif) data = open(src, 'r').read() @@ -199,19 +194,6 @@ def setup_ldb(ldb, setup_dir, ldif, subst_vars=None): ldb.transaction_commit() -def setup_ldb_modify(ldb, setup_dir, ldif, substvars=None): - """Modify a ldb in the private dir.""" - src = os.path.join(setup_dir, ldif) - - data = open(src, 'r').read() - if substvars is not None: - data = substitute_var(data, substvars) - assert not "${" in data - - for (changetype, msg) in ldb.parse_ldif(data): - ldb.modify(msg) - - def setup_file(setup_dir, template, fname, substvars): """Setup a file in the private dir.""" f = fname @@ -328,7 +310,7 @@ def provision_become_dc(setup_dir, subobj, message, paths, lp, session_info, setup_samdb_rootdse(samdb, setup_dir, subobj) message("Erasing data from partitions") - ldb_erase_partitions(subobj, message, samdb, None) + ldb_erase_partitions(subobj.domaindn, message, samdb, None) message("Setting up %s indexes" % paths.samdb) setup_add_ldif(samdb, setup_dir, "provision_index.ldif") @@ -453,8 +435,7 @@ def provision(lp, setup_dir, subobj, message, blank, paths, session_info, smbconfsuffix = "member" else: assert "Invalid server role setting: %s" % lp.get("server role") - setup_file(setup_dir, "provision.smb.conf.%s" % smbconfsuffix, paths.smbconf, - None) + setup_file(setup_dir, "provision.smb.conf.%s" % smbconfsuffix, paths.smbconf) lp.reload() # only install a new shares config db if there is none @@ -462,7 +443,7 @@ def provision(lp, setup_dir, subobj, message, blank, paths, session_info, message("Setting up share.ldb") share_ldb = Ldb(paths.shareconf, session_info=session_info, credentials=credentials, lp=lp) - setup_ldb(share_ldb, setup_dir, "share.ldif", None) + setup_ldb(share_ldb, setup_dir, "share.ldif") message("Setting up %s" % paths.secrets) secrets_ldb = setup_secretsdb(paths.secrets, setup_dir, session_info=session_info, @@ -497,7 +478,7 @@ def provision(lp, setup_dir, subobj, message, blank, paths, session_info, setup_samdb_rootdse(samdb, setup_dir, subobj) message("Erasing data from partitions") - ldb_erase_partitions(subobj, message, samdb, ldapbackend) + ldb_erase_partitions(subobj.domaindn, message, samdb, ldapbackend) except: samdb.transaction_cancel() raise @@ -527,7 +508,7 @@ def provision(lp, setup_dir, subobj, message, blank, paths, session_info, else: domainguid_mod = "" - setup_ldb_modify(samdb, setup_dir, "provision_basedn_modify.ldif", { + setup_modify_ldif(samdb, setup_dir, "provision_basedn_modify.ldif", { "RDN_DC": subobj.rdn_dc, "LDAPTIME": timestring(int(time.time())), "DOMAINSID": str(subobj.domainsid), @@ -547,7 +528,7 @@ def provision(lp, setup_dir, subobj, message, blank, paths, session_info, "EXTENSIBLEOBJECT": "# no objectClass: extensibleObject for local ldb", }) message("Modifying configuration container") - setup_ldb_modify(samdb, setup_dir, "provision_configuration_basedn_modify.ldif", { + setup_modify_ldif(samdb, setup_dir, "provision_configuration_basedn_modify.ldif", { "CONFIGDN": subobj.configdn, "SCHEMADN": subobj.schemadn, }) @@ -559,7 +540,7 @@ def provision(lp, setup_dir, subobj, message, blank, paths, session_info, "EXTENSIBLEOBJECT": "# no objectClass: extensibleObject for local ldb" }) message("Modifying schema container") - setup_ldb_modify(samdb, setup_dir, "provision_schema_basedn_modify.ldif", { + setup_modify_ldif(samdb, setup_dir, "provision_schema_basedn_modify.ldif", { "SCHEMADN": subobj.schemadn, "NETBIOSNAME": subobj.netbiosname, "DEFAULTSITE": subobj.defaultsite, @@ -593,13 +574,13 @@ def provision(lp, setup_dir, subobj, message, blank, paths, session_info, setup_add_ldif(samdb, setup_dir, "provision_users_add.ldif", { "DOMAINDN": subobj.domaindn}) message("Modifying users container") - setup_ldb_modify(samdb, setup_dir, "provision_users_modify.ldif", { + setup_modify_ldif(samdb, setup_dir, "provision_users_modify.ldif", { "DOMAINDN": subobj.domaindn}) message("Adding computers container (permitted to fail)") setup_add_ldif(samdb, setup_dir, "provision_computers_add.ldif", { "DOMAINDN": subobj.domaindn}) message("Modifying computers container") - setup_ldb_modify(samdb, setup_dir, "provision_computers_modify.ldif", { + setup_modify_ldif(samdb, setup_dir, "provision_computers_modify.ldif", { "DOMAINDN": subobj.domaindn}) message("Setting up sam.ldb data") setup_add_ldif(samdb, setup_dir, "provision.ldif", { @@ -807,7 +788,7 @@ def load_schema(setup_dir, samdb, subobj): samdb.attach_schema_from_ldif(head_data, schema_data) -def join_domain(domain, netbios_name, join_type, creds, message): +def join_domain(domain, netbios_name, join_type, creds): ctx = NetContext(creds) joindom = object() joindom.domain = domain @@ -824,8 +805,7 @@ def vampire(domain, session_info, credentials, message): access to our local database (might be remote ldap) """ ctx = NetContext(credentials) - vampire_ctx = object() - machine_creds = credentials_init() + machine_creds = Credentials() machine_creds.set_domain(form.domain) if not machine_creds.set_machine_account(): raise Exception("Failed to access domain join information!") @@ -835,7 +815,7 @@ def vampire(domain, session_info, credentials, message): raise Exception("Migration of remote domain to Samba failed: %s " % vampire_ctx.error_string) -def ldb_erase_partitions(subobj, message, ldb, ldapbackend): +def ldb_erase_partitions(domaindn, message, ldb, ldapbackend): """Erase an ldb, removing all records.""" assert ldb is not None res = ldb.search(Dn(ldb, ""), SCOPE_BASE, "(objectClass=*)", @@ -848,7 +828,7 @@ def ldb_erase_partitions(subobj, message, ldb, ldapbackend): previous_remaining = 1 current_remaining = 0 - if ldapbackend and (basedn == subobj.domaindn): + if ldapbackend and (basedn == domaindn): # Only delete objects that were created by provision anything = "(objectcategory=*)" diff --git a/source4/scripting/python/samba/tests/__init__.py b/source4/scripting/python/samba/tests/__init__.py index d3930e217a..0808469907 100644 --- a/source4/scripting/python/samba/tests/__init__.py +++ b/source4/scripting/python/samba/tests/__init__.py @@ -35,3 +35,20 @@ class LdbTestCase(unittest.TestCase): self.ldb = samba.Ldb(self.filename) +class SubstituteVarTestCase(unittest.TestCase): + def test_empty(self): + self.assertEquals("", samba.substitute_var("", {})) + + def test_nothing(self): + self.assertEquals("foo bar", samba.substitute_var("foo bar", {"bar": "bla"})) + + def test_replace(self): + self.assertEquals("foo bla", samba.substitute_var("foo ${bar}", {"bar": "bla"})) + + def test_broken(self): + self.assertEquals("foo ${bdkjfhsdkfh sdkfh ", + samba.substitute_var("foo ${bdkjfhsdkfh sdkfh ", {"bar": "bla"})) + + def test_unknown_var(self): + self.assertEquals("foo ${bla} gsff", + samba.substitute_var("foo ${bla} gsff", {"bar": "bla"})) diff --git a/source4/scripting/python/samba/tests/provision.py b/source4/scripting/python/samba/tests/provision.py new file mode 100644 index 0000000000..5edfe79084 --- /dev/null +++ b/source4/scripting/python/samba/tests/provision.py @@ -0,0 +1,59 @@ +#!/usr/bin/python + +# Unix SMB/CIFS implementation. +# Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007 +# +# 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/>. +# + +import unittest +import samba.provision + +class ProvisionTestCase(unittest.TestCase): + def test_setup_secretsdb(self): + raise NotImplementedError(self.test_setup_secretsdb) + + def test_setup_templatesdb(self): + raise NotImplementedError(self.test_setup_templatesdb) + + def test_setup_registry(self): + raise NotImplementedError(self.test_setup_registry) + + def test_setup_samdb_rootdse(self): + raise NotImplementedError(self.test_setup_samdb_rootdse) + + def test_setup_samdb_partitions(self): + raise NotImplementedError(self.test_setup_samdb_partitions) + + def test_create_phpldapadmin_config(self): + raise NotImplementedError(self.test_create_phpldapadmin_config) + + def test_provision_dns(self): + raise NotImplementedError(self.test_provision_dns) + + def test_provision_ldapbase(self): + raise NotImplementedError(self.test_provision_ldapbase) + + def test_provision_guess(self): + raise NotImplementedError(self.test_provision_guess) + + def test_join_domain(self): + raise NotImplementedError(self.test_join_domain) + + def test_vampire(self): + raise NotImplementedError(self.test_vampire) + + def test_erase_partitions(self): + raise NotImplementedError(self.test_erase_partitions) + diff --git a/source4/selftest/samba4_tests.sh b/source4/selftest/samba4_tests.sh index f0700a6956..268a3246e5 100755 --- a/source4/selftest/samba4_tests.sh +++ b/source4/selftest/samba4_tests.sh @@ -295,4 +295,5 @@ then plantest "security.python" none PYTHONPATH=bin/python:scripting/python:libcli/security/tests/ scripting/bin/subunitrun bindings plantest "param.python" none PYTHONPATH=bin/python:scripting/python:param/tests scripting/bin/subunitrun bindings plantest "upgrade.python" none PYTHONPATH=bin/python:scripting/python scripting/bin/subunitrun samba.tests.upgrade + plantest "samba.python" none PYTHONPATH=bin/python:scripting/python scripting/bin/subunitrun samba.tests fi |