s4:python tests __init__.py - do not depend on "subprocess.check_call()"
[samba.git] / source4 / scripting / python / samba / tests / samdb.py
1 #!/usr/bin/env python
2
3 # Unix SMB/CIFS implementation. Tests for SamDB
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
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.samdb."""
21
22 import logging
23 import os
24 import uuid
25
26 from samba.auth import system_session
27 from samba.provision import (setup_samdb, guess_names, make_smbconf,
28     provision_paths_from_lp)
29 from samba.provision import DEFAULT_POLICY_GUID, DEFAULT_DC_POLICY_GUID
30 from samba.provision.backend import ProvisionBackend
31 from samba.tests import TestCaseInTempDir
32 from samba.dcerpc import security
33 from samba.schema import Schema
34 from samba import param
35
36
37 class SamDBTestCase(TestCaseInTempDir):
38     """Base-class for tests with a Sam Database.
39
40     This is used by the Samba SamDB-tests, but e.g. also by the OpenChange
41     provisioning tests (which need a Sam).
42     """
43
44     def setUp(self):
45         super(SamDBTestCase, self).setUp()
46         invocationid = str(uuid.uuid4())
47         domaindn = "DC=COM,DC=EXAMPLE"
48         self.domaindn = domaindn
49         configdn = "CN=Configuration," + domaindn
50         schemadn = "CN=Schema," + configdn
51         domainguid = str(uuid.uuid4())
52         policyguid = DEFAULT_POLICY_GUID
53         domainsid = security.random_sid()
54         path = os.path.join(self.tempdir, "samdb.ldb")
55         session_info = system_session()
56         
57         hostname="foo"
58         domain="EXAMPLE"
59         dnsdomain="example.com" 
60         serverrole="domain controller"
61         policyguid_dc = DEFAULT_DC_POLICY_GUID
62
63         smbconf = os.path.join(self.tempdir, "smb.conf")
64         make_smbconf(smbconf, hostname, domain, dnsdomain,
65                      serverrole, self.tempdir)
66
67         self.lp = param.LoadParm()
68         self.lp.load(smbconf)
69
70         names = guess_names(lp=self.lp, hostname=hostname, 
71                             domain=domain, dnsdomain=dnsdomain, 
72                             serverrole=serverrole, 
73                             domaindn=self.domaindn, configdn=configdn, 
74                             schemadn=schemadn)
75
76         paths = provision_paths_from_lp(self.lp, names.dnsdomain)
77
78         logger = logging.getLogger("provision")
79
80         provision_backend = ProvisionBackend("ldb", paths=paths,
81                 lp=self.lp, credentials=None,
82                 names=names, logger=logger)
83
84         schema = Schema(domainsid, invocationid=invocationid,
85                 schemadn=names.schemadn, serverdn=names.serverdn,
86                 am_rodc=False)
87
88         self.samdb = setup_samdb(path, session_info,
89                 provision_backend, self.lp, names, logger,
90                 domainsid, domainguid, policyguid, policyguid_dc, False,
91                 "secret", "secret", "secret", invocationid, "secret",
92                 None, "domain controller", schema=schema)
93
94     def tearDown(self):
95         for f in ['schema.ldb', 'configuration.ldb',
96                   'users.ldb', 'samdb.ldb', 'smb.conf']:
97             os.remove(os.path.join(self.tempdir, f))
98         super(SamDBTestCase, self).tearDown()