s4 upgradeprovision: Move functions to helpers and improve code
[kamenim/samba.git] / source4 / scripting / python / samba / tests / upgradeprovisionneeddc.py
1 #!/usr/bin/python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-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 import os
21 import re
22 import shutil
23 from samba.credentials import Credentials
24 from samba.auth import system_session
25 from samba.provision import getpolicypath
26 from samba.upgradehelpers import (get_paths, get_ldbs,
27                                  find_provision_key_parameters, identic_rename,
28                                  updateOEMInfo, getOEMInfo, update_gpo,
29                                  delta_update_basesamdb)
30
31 from samba.tests.provision import create_dummy_secretsdb
32 from samba import param
33 from samba.tests import env_loadparm, TestCaseInTempDir
34 import ldb
35
36
37 def dummymessage(a=None, b=None):
38     if 0:
39         print "none"
40
41 lp = env_loadparm()
42 smbConfPath = "%s/%s/%s" % (os.environ["SELFTEST_PREFIX"], "dc", "etc/smb.conf")
43
44 class UpgradeProvisionBasicLdbHelpersTestCase(TestCaseInTempDir):
45     """Some simple tests for individual functions in the provisioning code.
46     """
47
48     def test_get_ldbs(self):
49         paths = get_paths(param, None, smbConfPath)
50         creds = Credentials()
51         creds.guess(lp)
52         get_ldbs(paths, creds, system_session(), lp)
53
54     def test_find_key_param(self):
55         paths = get_paths(param, None, smbConfPath)
56         creds = Credentials()
57         creds.guess(lp)
58         rootdn = "dc=samba,dc=example,dc=com"
59         ldbs = get_ldbs(paths, creds, system_session(), lp)
60         names = find_provision_key_parameters(ldbs.sam, ldbs.secrets, ldbs.idmap,
61                                                 paths, smbConfPath, lp)
62         self.assertEquals(names.realm, "SAMBA.EXAMPLE.COM")
63         self.assertTrue(str(names.rootdn).lower() == rootdn.lower())
64         self.assertTrue(names.policyid_dc != None)
65         self.assertTrue(names.ntdsguid != "")
66
67
68 class UpgradeProvisionWithLdbTestCase(TestCaseInTempDir):
69     def _getEmptyDbName(self):
70         return os.path.join(self.tempdir, "sam.ldb")
71
72     def setUp(self):
73         super(UpgradeProvisionWithLdbTestCase, self).setUp()
74         paths = get_paths(param, None, smbConfPath)
75         self.creds = Credentials()
76         self.creds.guess(lp)
77         self.paths = paths
78         self.ldbs = get_ldbs(paths, self.creds, system_session(), lp)
79         self.lp = lp
80         self.names = find_provision_key_parameters(self.ldbs.sam, self.ldbs.secrets,
81                                                        self.ldbs.idmap, paths, smbConfPath, lp)
82         self.referencedb = create_dummy_secretsdb(
83             os.path.join(self.tempdir, "ref.ldb"))
84
85     def test_identic_rename(self):
86         rootdn = "DC=samba,DC=example,DC=com"
87
88         guestDN = ldb.Dn(self.ldbs.sam, "CN=Guest,CN=Users,%s" % rootdn)
89         identic_rename(self.ldbs.sam, guestDN)
90         res = self.ldbs.sam.search(expression="(name=Guest)", base=rootdn,
91                                 scope=ldb.SCOPE_SUBTREE, attrs=["dn"])
92         self.assertEquals(len(res), 1)
93         self.assertEquals(str(res[0]["dn"]), "CN=Guest,CN=Users,%s" % rootdn)
94
95     def test_delta_update_basesamdb(self):
96         dummysampath = self._getEmptyDbName()
97         delta_update_basesamdb(self.paths.samdb, dummysampath,
98                                 self.creds, system_session(), self.lp, dummymessage)
99
100     def test_update_gpo_simple(self):
101         dir = getpolicypath(self.paths.sysvol, self.names.dnsdomain, self.names.policyid)
102         shutil.rmtree(dir)
103         self.assertFalse(os.path.isdir(dir))
104         update_gpo(self.paths, self.ldbs.sam, self.names, self.lp, dummymessage)
105         self.assertTrue(os.path.isdir(dir))
106
107     def test_update_gpo_acl(self):
108         path = os.path.join(self.tempdir, "testupdategpo")
109         save = self.paths.sysvol
110         self.paths.sysvol = path
111         os.mkdir(path)
112         os.mkdir(os.path.join(path, self.names.dnsdomain))
113         os.mkdir(os.path.join(os.path.join(path, self.names.dnsdomain), "Policies"))
114         update_gpo(self.paths, self.ldbs.sam, self.names, self.lp, dummymessage)
115         shutil.rmtree(path)
116         self.paths.sysvol = save
117
118     def test_getOEMInfo(self):
119         realm = self.lp.get("realm")
120         basedn = "DC=%s" % realm.replace(".", ", DC=")
121         oem = getOEMInfo(self.ldbs.sam, basedn)
122         self.assertTrue(oem != "")
123
124     def test_updateOEMInfo(self):
125         realm = self.lp.get("realm")
126         basedn = "DC=%s" % realm.replace(".", ", DC=")
127         oem = getOEMInfo(self.ldbs.sam, basedn)
128         updateOEMInfo(self.ldbs.sam, basedn)
129         oem2 = getOEMInfo(self.ldbs.sam, basedn)
130         self.assertTrue(str(oem) != str(oem2))
131         self.assertTrue(re.match(".*upgrade to.*", str(oem2)))
132
133     def tearDown(self):
134         for name in ["ref.ldb", "secrets.ldb", "sam.ldb"]:
135             path = os.path.join(self.tempdir, name)
136             if os.path.exists(path):
137                 os.unlink(path)
138         super(UpgradeProvisionWithLdbTestCase, self).tearDown()