provision: Start splitting out provision result reporting from actual provisioning.
[obnox/samba/samba-obnox.git] / source4 / scripting / python / samba / tests / provision.py
1 #!/usr/bin/env python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2012
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 """Tests for samba.provision."""
21
22 import os
23 from samba.provision import (
24     ProvisionNames,
25     ProvisionPaths,
26     ProvisionResult,
27     sanitize_server_role,
28     setup_secretsdb,
29     findnss,
30     )
31 import samba.tests
32 from samba.tests import env_loadparm, TestCase
33
34 def create_dummy_secretsdb(path, lp=None):
35     """Create a dummy secrets database for use in tests.
36
37     :param path: Path to store the secrets db
38     :param lp: Optional loadparm context. A simple one will
39         be generated if not specified.
40     """
41     if lp is None:
42         lp = env_loadparm()
43     paths = ProvisionPaths()
44     paths.secrets = path
45     paths.private_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         paths.secrets = path
61         paths.private_dir = os.path.dirname(path)
62         paths.keytab = "no.keytab"
63         paths.dns_keytab = "no.dns.keytab"
64         ldb = setup_secretsdb(paths, None, None, lp=env_loadparm())
65         try:
66             self.assertEquals("LSA Secrets",
67                  ldb.searchone(basedn="CN=LSA Secrets", attribute="CN"))
68         finally:
69             del ldb
70             os.unlink(path)
71
72
73 class FindNssTests(TestCase):
74     """Test findnss() function."""
75
76     def test_nothing(self):
77         def x(y):
78             raise KeyError
79         self.assertRaises(KeyError, findnss, x, [])
80
81     def test_first(self):
82         self.assertEquals("bla", findnss(lambda x: "bla", ["bla"]))
83
84     def test_skip_first(self):
85         def x(y):
86             if y != "bla":
87                 raise KeyError
88             return "ha"
89         self.assertEquals("ha", findnss(x, ["bloe", "bla"]))
90
91
92 class Disabled(object):
93
94     def test_setup_templatesdb(self):
95         raise NotImplementedError(self.test_setup_templatesdb)
96
97     def test_setup_registry(self):
98         raise NotImplementedError(self.test_setup_registry)
99
100     def test_setup_samdb_rootdse(self):
101         raise NotImplementedError(self.test_setup_samdb_rootdse)
102
103     def test_setup_samdb_partitions(self):
104         raise NotImplementedError(self.test_setup_samdb_partitions)
105
106     def test_create_phpldapadmin_config(self):
107         raise NotImplementedError(self.test_create_phpldapadmin_config)
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     def test_vampire(self):
122         raise NotImplementedError(self.test_vampire)
123
124
125 class SanitizeServerRoleTests(TestCase):
126
127     def test_same(self):
128         self.assertEquals("standalone", sanitize_server_role("standalone"))
129         self.assertEquals("member server",
130             sanitize_server_role("member server"))
131
132     def test_invalid(self):
133         self.assertRaises(ValueError, sanitize_server_role, "foo")
134
135     def test_valid(self):
136         self.assertEquals("standalone", sanitize_server_role("ROLE_STANDALONE"))
137
138
139 class DummyLogger(object):
140
141     def __init__(self):
142         self.entries = []
143
144     def info(self, text):
145         self.entries.append(("INFO", text))
146
147
148 class ProvisionResultTests(TestCase):
149
150     def test_report_logger(self):
151         logger = DummyLogger()
152         result = ProvisionResult()
153         result.server_role = "domain controller"
154         result.names = ProvisionNames()
155         result.names.hostname = "hostnaam"
156         result.names.domain = "DOMEIN"
157         result.names.dnsdomain = "dnsdomein"
158         result.domainsid = "S1-1-1"
159         result.report_logger(logger)
160         self.assertEquals(logger.entries, [
161             ('INFO', 'Server Role:           domain controller'),
162             ('INFO', 'Hostname:              hostnaam'),
163             ('INFO', 'NetBIOS Domain:        DOMEIN'),
164             ('INFO', 'DNS Domain:            dnsdomein'),
165             ('INFO', 'DOMAIN SID:            S1-1-1')])