s4-sites: Document, fix under optimal coding, use exceptions
[Samba/gebeck_regimport.git] / source4 / dsdb / tests / python / sites.py
blob8b984b2fe8b71e3ce02654f58af4ceb5c71e605c
1 #!/usr/bin/env python
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/>.
21 import optparse
22 import sys
23 sys.path.insert(0, "bin/python")
24 import samba
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
32 import samba.tests
33 from samba.dcerpc import security
34 from subunit.run import SubunitTestRunner
35 import unittest
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()
47 if len(args) < 1:
48 parser.print_usage()
49 sys.exit(1)
51 host = args[0]
52 if not "://" in host:
53 ldaphost = "ldap://%s" % host
54 ldapshost = "ldaps://%s" % host
55 else:
56 ldaphost = host
57 start = host.rindex("://")
58 host = host.lstrip(start+3)
60 lp = sambaopts.get_loadparm()
61 creds = credopts.get_credentials(lp)
64 # Tests start here
67 class SitesBaseTests(samba.tests.TestCase):
69 def setUp(self):
70 super(SitesBaseTests, self).setUp()
71 self.ldb_admin = ldb
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)
80 #tests on sites
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(),
88 "testsamba")
89 self.ldb_admin.transaction_commit()
91 self.assertRaises(sites.SiteAlreadyExistsException,
92 sites.create_site, self.ldb_admin, self.ldb_admin.get_config_basedn(),
93 "testsamba")
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(),
100 "testsamba")
102 self.ldb_admin.transaction_commit()
104 self.assertRaises(sites.SiteNotFoundException,
105 sites.delete_site, self.ldb_admin, self.ldb_admin.get_config_basedn(),
106 "testsamba")
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()
120 rc = 0
122 if not runner.run(unittest.makeSuite(SimpleSitesTests)).wasSuccessful():
123 rc = 1
125 sys.exit(rc)