]> git.samba.org - obnox/samba/samba-obnox.git/blob - source4/dsdb/tests/python/sites.py
s4:dsdb python tests - set the executable flag
[obnox/samba/samba-obnox.git] / source4 / dsdb / tests / python / sites.py
1 #!/usr/bin/env python
2 #
3 # Unit tests for sites manipulation in samba
4 # Copyright (C) Matthieu Patou <mat@matws.net> 2011
5 #
6 #
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.
11 #
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.
16 #
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/>.
19
20
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")
27
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
36
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))
41
42 # use command line creds if available
43 credopts = options.CredentialsOptions(parser)
44 parser.add_option_group(credopts)
45 opts, args = parser.parse_args()
46
47 if len(args) < 1:
48     parser.print_usage()
49     sys.exit(1)
50
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)
59
60 lp = sambaopts.get_loadparm()
61 creds = credopts.get_credentials(lp)
62
63 #
64 # Tests start here
65 #
66
67 class SitesBaseTests(samba.tests.TestCase):
68
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()
75
76     def get_user_dn(self, name):
77         return "CN=%s,CN=Users,%s" % (name, self.base_dn)
78
79
80 #tests on sites
81 class SimpleSitesTests(SitesBaseTests):
82
83     def test_create(self):
84         """test creation of 1 site"""
85
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()
90
91         self.assertRaises(sites.SiteAlreadyExistsException,
92                             sites.create_site, self.ldb_admin, self.ldb_admin.get_config_basedn(),
93                             "testsamba")
94
95     def test_delete(self):
96         """test removal of 1 site"""
97
98         self.ldb_admin.transaction_start()
99         ok = sites.delete_site(self.ldb_admin, self.ldb_admin.get_config_basedn(),
100                             "testsamba")
101
102         self.ldb_admin.transaction_commit()
103
104         self.assertRaises(sites.SiteNotFoundException,
105                             sites.delete_site, self.ldb_admin, self.ldb_admin.get_config_basedn(),
106                             "testsamba")
107
108
109     def test_delete_not_empty(self):
110         """test removal of 1 site with servers"""
111
112         self.assertRaises(sites.SiteServerNotEmptyException,
113                             sites.delete_site, self.ldb_admin, self.ldb_admin.get_config_basedn(),
114                             "Default-First-Site-Name")
115
116
117 ldb = SamDB(ldapshost, credentials=creds, session_info=system_session(lp), lp=lp)
118
119 runner = SubunitTestRunner()
120 rc = 0
121
122 if not runner.run(unittest.makeSuite(SimpleSitesTests)).wasSuccessful():
123     rc = 1
124
125 sys.exit(rc)