selftest: Allow sites test to run against a remote ldap:// host
[ambi/samba.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
26 from samba.tests.subunitrun import TestProgram, SubunitOptions
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
35 parser = optparse.OptionParser(__file__ + " [options] <host>")
36 sambaopts = options.SambaOptions(parser)
37 parser.add_option_group(sambaopts)
38 parser.add_option_group(options.VersionOptions(parser))
39
40 # use command line creds if available
41 credopts = options.CredentialsOptions(parser)
42 parser.add_option_group(credopts)
43 subunitopts = SubunitOptions(parser)
44 parser.add_option_group(subunitopts)
45
46 opts, args = parser.parse_args()
47
48 if len(args) < 1:
49     parser.print_usage()
50     sys.exit(1)
51
52 host = args[0]
53 if not "://" in host:
54     ldaphost = "ldap://%s" % host
55 else:
56     ldaphost = host
57
58 lp = sambaopts.get_loadparm()
59 creds = credopts.get_credentials(lp)
60
61 #
62 # Tests start here
63 #
64
65 class SitesBaseTests(samba.tests.TestCase):
66
67     def setUp(self):
68         super(SitesBaseTests, self).setUp()
69         self.ldb = SamDB(ldaphost, credentials=creds,
70                          session_info=system_session(lp), lp=lp)
71         self.base_dn = self.ldb.domain_dn()
72         self.domain_sid = security.dom_sid(self.ldb.get_domain_sid())
73         self.configuration_dn = self.ldb.get_config_basedn().get_linearized()
74
75     def get_user_dn(self, name):
76         return "CN=%s,CN=Users,%s" % (name, self.base_dn)
77
78
79 #tests on sites
80 class SimpleSitesTests(SitesBaseTests):
81
82     def test_create_and_delete(self):
83         """test creation and deletion of 1 site"""
84
85         self.ldb.transaction_start()
86         sites.create_site(self.ldb, self.ldb.get_config_basedn(),
87                           "testsamba")
88         self.ldb.transaction_commit()
89
90         self.assertRaises(sites.SiteAlreadyExistsException,
91                           sites.create_site, self.ldb,
92                           self.ldb.get_config_basedn(),
93                           "testsamba")
94
95         self.ldb.transaction_start()
96         sites.delete_site(self.ldb, self.ldb.get_config_basedn(),
97                           "testsamba")
98         self.ldb.transaction_commit()
99
100         self.assertRaises(sites.SiteNotFoundException,
101                           sites.delete_site, self.ldb,
102                           self.ldb.get_config_basedn(),
103                           "testsamba")
104
105     def test_delete_not_empty(self):
106         """test removal of 1 site with servers"""
107
108         self.assertRaises(sites.SiteServerNotEmptyException,
109                           sites.delete_site, self.ldb,
110                           self.ldb.get_config_basedn(),
111                           "Default-First-Site-Name")
112
113
114
115 TestProgram(module=__name__, opts=subunitopts)