python:samba: Use 'binddns dir' in samba-tool and samba_upgradedns
[garming/samba-autobuild/.git] / python / samba / tests / provision.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2012
3 #
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.
8 #
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.
13 #
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/>.
16 #
17
18 """Tests for samba.provision."""
19
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,
29     )
30 import samba.tests
31 from samba.tests import env_loadparm, TestCase
32
33 def create_dummy_secretsdb(path, lp=None):
34     """Create a dummy secrets database for use in tests.
35
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.binddns_dir = os.path.dirname(path)
46     paths.keytab = "no.keytab"
47     paths.dns_keytab = "no.dns.keytab"
48     secrets_ldb = setup_secretsdb(paths, None, None, lp=lp)
49     secrets_ldb.transaction_commit()
50     return secrets_ldb
51
52
53 class ProvisionTestCase(samba.tests.TestCaseInTempDir):
54     """Some simple tests for individual functions in the provisioning code.
55     """
56
57     def test_setup_secretsdb(self):
58         path = os.path.join(self.tempdir, "secrets.ldb")
59         paths = ProvisionPaths()
60         secrets_tdb_path = os.path.join(self.tempdir, "secrets.tdb")
61         paths.secrets = path
62         paths.private_dir = os.path.dirname(path)
63         paths.binddns_dir = os.path.dirname(path)
64         paths.keytab = "no.keytab"
65         paths.dns_keytab = "no.dns.keytab"
66         ldb = setup_secretsdb(paths, None, None, lp=env_loadparm())
67         try:
68             self.assertEquals("LSA Secrets",
69                  ldb.searchone(basedn="CN=LSA Secrets", attribute="CN"))
70         finally:
71             del ldb
72             os.unlink(path)
73             if os.path.exists(secrets_tdb_path):
74                 os.unlink(secrets_tdb_path)
75
76 class FindNssTests(TestCase):
77     """Test findnss() function."""
78
79     def test_nothing(self):
80         def x(y):
81             raise KeyError
82         self.assertRaises(KeyError, findnss, x, [])
83
84     def test_first(self):
85         self.assertEquals("bla", findnss(lambda x: "bla", ["bla"]))
86
87     def test_skip_first(self):
88         def x(y):
89             if y != "bla":
90                 raise KeyError
91             return "ha"
92         self.assertEquals("ha", findnss(x, ["bloe", "bla"]))
93
94
95 class Disabled(object):
96
97     def test_setup_templatesdb(self):
98         raise NotImplementedError(self.test_setup_templatesdb)
99
100     def test_setup_registry(self):
101         raise NotImplementedError(self.test_setup_registry)
102
103     def test_setup_samdb_rootdse(self):
104         raise NotImplementedError(self.test_setup_samdb_rootdse)
105
106     def test_setup_samdb_partitions(self):
107         raise NotImplementedError(self.test_setup_samdb_partitions)
108
109     def test_provision_dns(self):
110         raise NotImplementedError(self.test_provision_dns)
111
112     def test_provision_ldapbase(self):
113         raise NotImplementedError(self.test_provision_ldapbase)
114
115     def test_provision_guess(self):
116         raise NotImplementedError(self.test_provision_guess)
117
118     def test_join_domain(self):
119         raise NotImplementedError(self.test_join_domain)
120
121
122 class SanitizeServerRoleTests(TestCase):
123
124     def test_same(self):
125         self.assertEquals("standalone server",
126             sanitize_server_role("standalone server"))
127         self.assertEquals("member server",
128             sanitize_server_role("member server"))
129
130     def test_invalid(self):
131         self.assertRaises(ValueError, sanitize_server_role, "foo")
132
133     def test_valid(self):
134         self.assertEquals(
135             "standalone server",
136             sanitize_server_role("ROLE_STANDALONE"))
137         self.assertEquals(
138             "standalone server",
139             sanitize_server_role("standalone"))
140         self.assertEquals(
141             "active directory domain controller",
142             sanitize_server_role("domain controller"))
143
144
145 class DummyLogger(object):
146
147     def __init__(self):
148         self.entries = []
149
150     def info(self, text, *args):
151         self.entries.append(("INFO", text % args))
152
153
154 class ProvisionResultTests(TestCase):
155
156     def report_logger(self, result):
157         logger = DummyLogger()
158         result.report_logger(logger)
159         return logger.entries
160
161     def base_result(self):
162         result = ProvisionResult()
163         result.server_role = "domain controller"
164         result.names = ProvisionNames()
165         result.names.hostname = "hostnaam"
166         result.names.domain = "DOMEIN"
167         result.names.dnsdomain = "dnsdomein"
168         result.domainsid = "S1-1-1"
169         result.paths = ProvisionPaths()
170         return result
171
172     def test_basic_report_logger(self):
173         result = self.base_result()
174         entries = self.report_logger(result)
175         self.assertEquals(entries, [
176             ('INFO', 'Once the above files are installed, your Samba AD server '
177                 'will be ready to use'),
178             ('INFO', 'Server Role:           domain controller'),
179             ('INFO', 'Hostname:              hostnaam'),
180             ('INFO', 'NetBIOS Domain:        DOMEIN'),
181             ('INFO', 'DNS Domain:            dnsdomein'),
182             ('INFO', 'DOMAIN SID:            S1-1-1')])
183
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'))
191
192
193 class DetermineNetbiosNameTests(TestCase):
194
195     def test_limits_to_15(self):
196         self.assertEquals("A" * 15, determine_netbios_name("a" * 30))
197
198     def test_strips_invalid(self):
199         self.assertEquals("BLABLA", determine_netbios_name("bla/bla"))