3 # Unit tests for sites manipulation in samba
4 # Copyright (C) Matthieu Patou <mat@matws.net> 2011
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 sys.path.insert(0, "bin/python")
25 samba.ensure_external_module("testtools", "testtools")
26 samba.ensure_external_module("subunit", "subunit/python")
28 import samba.getopt as options
29 from samba import sites
30 from samba.auth import system_session
31 from samba.samdb import SamDB
33 from samba.dcerpc import security
34 from subunit.run import SubunitTestRunner
37 parser = optparse.OptionParser("dirsync.py [options] <host>")
38 sambaopts = options.SambaOptions(parser)
39 parser.add_option_group(sambaopts)
40 parser.add_option_group(options.VersionOptions(parser))
42 # use command line creds if available
43 credopts = options.CredentialsOptions(parser)
44 parser.add_option_group(credopts)
45 opts, args = parser.parse_args()
53 ldaphost = "ldap://%s" % host
54 ldapshost = "ldaps://%s" % host
57 start = host.rindex("://")
58 host = host.lstrip(start+3)
60 lp = sambaopts.get_loadparm()
61 creds = credopts.get_credentials(lp)
67 class SitesBaseTests(samba.tests.TestCase):
70 super(SitesBaseTests, self).setUp()
72 self.base_dn = ldb.domain_dn()
73 self.domain_sid = security.dom_sid(ldb.get_domain_sid())
74 self.configuration_dn = self.ldb_admin.get_config_basedn().get_linearized()
76 def get_user_dn(self, name):
77 return "CN=%s,CN=Users,%s" % (name, self.base_dn)
81 class SimpleSitesTests(SitesBaseTests):
83 def test_create(self):
84 """test creation of 1 site"""
86 self.ldb_admin.transaction_start()
87 ok = sites.create_site(self.ldb_admin, self.ldb_admin.get_config_basedn(),
89 self.ldb_admin.transaction_commit()
91 self.assertRaises(sites.SiteAlreadyExistsException,
92 sites.create_site, self.ldb_admin, self.ldb_admin.get_config_basedn(),
95 def test_delete(self):
96 """test removal of 1 site"""
98 self.ldb_admin.transaction_start()
99 ok = sites.delete_site(self.ldb_admin, self.ldb_admin.get_config_basedn(),
102 self.ldb_admin.transaction_commit()
104 self.assertRaises(sites.SiteNotFoundException,
105 sites.delete_site, self.ldb_admin, self.ldb_admin.get_config_basedn(),
109 def test_delete_not_empty(self):
110 """test removal of 1 site with servers"""
112 self.assertRaises(sites.SiteServerNotEmptyException,
113 sites.delete_site, self.ldb_admin, self.ldb_admin.get_config_basedn(),
114 "Default-First-Site-Name")
117 ldb = SamDB(ldapshost, credentials=creds, session_info=system_session(lp), lp=lp)
119 runner = SubunitTestRunner()
122 if not runner.run(unittest.makeSuite(SimpleSitesTests)).wasSuccessful():