s4-python: Remove env from non-executable test scripts.
[Samba/vl.git] / source4 / scripting / python / samba / tests / provision.py
blob6ea3e60fb0091834734a9a3aacaa38617561565f
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2012
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 """Tests for samba.provision."""
20 import os
21 from samba.provision import (
22 ProvisionNames,
23 ProvisionPaths,
24 ProvisionResult,
25 determine_netbios_name,
26 sanitize_server_role,
27 setup_secretsdb,
28 findnss,
30 import samba.tests
31 from samba.tests import env_loadparm, TestCase
33 def create_dummy_secretsdb(path, lp=None):
34 """Create a dummy secrets database for use in tests.
36 :param path: Path to store the secrets db
37 :param lp: Optional loadparm context. A simple one will
38 be generated if not specified.
39 """
40 if lp is None:
41 lp = env_loadparm()
42 paths = ProvisionPaths()
43 paths.secrets = path
44 paths.private_dir = os.path.dirname(path)
45 paths.keytab = "no.keytab"
46 paths.dns_keytab = "no.dns.keytab"
47 secrets_ldb = setup_secretsdb(paths, None, None, lp=lp)
48 secrets_ldb.transaction_commit()
49 return secrets_ldb
52 class ProvisionTestCase(samba.tests.TestCaseInTempDir):
53 """Some simple tests for individual functions in the provisioning code.
54 """
56 def test_setup_secretsdb(self):
57 path = os.path.join(self.tempdir, "secrets.ldb")
58 paths = ProvisionPaths()
59 paths.secrets = path
60 paths.private_dir = os.path.dirname(path)
61 paths.keytab = "no.keytab"
62 paths.dns_keytab = "no.dns.keytab"
63 ldb = setup_secretsdb(paths, None, None, lp=env_loadparm())
64 try:
65 self.assertEquals("LSA Secrets",
66 ldb.searchone(basedn="CN=LSA Secrets", attribute="CN"))
67 finally:
68 del ldb
69 os.unlink(path)
72 class FindNssTests(TestCase):
73 """Test findnss() function."""
75 def test_nothing(self):
76 def x(y):
77 raise KeyError
78 self.assertRaises(KeyError, findnss, x, [])
80 def test_first(self):
81 self.assertEquals("bla", findnss(lambda x: "bla", ["bla"]))
83 def test_skip_first(self):
84 def x(y):
85 if y != "bla":
86 raise KeyError
87 return "ha"
88 self.assertEquals("ha", findnss(x, ["bloe", "bla"]))
91 class Disabled(object):
93 def test_setup_templatesdb(self):
94 raise NotImplementedError(self.test_setup_templatesdb)
96 def test_setup_registry(self):
97 raise NotImplementedError(self.test_setup_registry)
99 def test_setup_samdb_rootdse(self):
100 raise NotImplementedError(self.test_setup_samdb_rootdse)
102 def test_setup_samdb_partitions(self):
103 raise NotImplementedError(self.test_setup_samdb_partitions)
105 def test_create_phpldapadmin_config(self):
106 raise NotImplementedError(self.test_create_phpldapadmin_config)
108 def test_provision_dns(self):
109 raise NotImplementedError(self.test_provision_dns)
111 def test_provision_ldapbase(self):
112 raise NotImplementedError(self.test_provision_ldapbase)
114 def test_provision_guess(self):
115 raise NotImplementedError(self.test_provision_guess)
117 def test_join_domain(self):
118 raise NotImplementedError(self.test_join_domain)
120 def test_vampire(self):
121 raise NotImplementedError(self.test_vampire)
124 class SanitizeServerRoleTests(TestCase):
126 def test_same(self):
127 self.assertEquals("standalone", sanitize_server_role("standalone"))
128 self.assertEquals("member server",
129 sanitize_server_role("member server"))
131 def test_invalid(self):
132 self.assertRaises(ValueError, sanitize_server_role, "foo")
134 def test_valid(self):
135 self.assertEquals("standalone", sanitize_server_role("ROLE_STANDALONE"))
138 class DummyLogger(object):
140 def __init__(self):
141 self.entries = []
143 def info(self, text, *args):
144 self.entries.append(("INFO", text % args))
147 class ProvisionResultTests(TestCase):
149 def report_logger(self, result):
150 logger = DummyLogger()
151 result.report_logger(logger)
152 return logger.entries
154 def base_result(self):
155 result = ProvisionResult()
156 result.server_role = "domain controller"
157 result.names = ProvisionNames()
158 result.names.hostname = "hostnaam"
159 result.names.domain = "DOMEIN"
160 result.names.dnsdomain = "dnsdomein"
161 result.domainsid = "S1-1-1"
162 result.paths = ProvisionPaths()
163 return result
165 def test_basic_report_logger(self):
166 result = self.base_result()
167 entries = self.report_logger(result)
168 self.assertEquals(entries, [
169 ('INFO', 'Once the above files are installed, your Samba4 server '
170 'will be ready to use'),
171 ('INFO', 'Server Role: domain controller'),
172 ('INFO', 'Hostname: hostnaam'),
173 ('INFO', 'NetBIOS Domain: DOMEIN'),
174 ('INFO', 'DNS Domain: dnsdomein'),
175 ('INFO', 'DOMAIN SID: S1-1-1')])
177 def test_report_logger_phpldapadmin(self):
178 result = self.base_result()
179 result.paths.phpldapadminconfig = "/some/ldapconfig"
180 entries = self.report_logger(result)
181 self.assertEquals(entries[-1],
182 ("INFO", "A phpLDAPadmin configuration file suitable for administering the Samba 4 LDAP server has been created in /some/ldapconfig."))
184 def test_report_logger_adminpass(self):
185 result = self.base_result()
186 result.adminpass_generated = True
187 result.adminpass = "geheim"
188 entries = self.report_logger(result)
189 self.assertEquals(entries[1],
190 ("INFO", 'Admin password: geheim'))
193 class DetermineNetbiosNameTests(TestCase):
195 def test_limits_to_15(self):
196 self.assertEquals("A" * 15, determine_netbios_name("a" * 30))
198 def test_strips_invalid(self):
199 self.assertEquals("BLABLA", determine_netbios_name("bla/bla"))