2 # -*- coding: utf-8 -*-
4 # Unix SMB/CIFS implementation.
5 # Copyright (C) Anatoliy Atanasov <anatoliy.atanasov@postpath.com> 2010
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/>.
22 # export DC1=dc1_dns_name
23 # export DC2=dc2_dns_name
24 # export SUBUNITRUN=$samba4srcdir/scripting/bin/subunitrun
25 # PYTHONPATH="$PYTHONPATH:$samba4srcdir/torture/drs/python" $SUBUNITRUN fsmo -U"$DOMAIN/$DC_USERNAME"%"$DC_PASSWORD"
32 sys.path.append("bin/python")
34 from samba.auth import system_session
35 from ldb import SCOPE_BASE
36 from samba.samdb import SamDB
40 class DrsFsmoTestCase(samba.tests.TestCase):
50 super(DrsFsmoTestCase, self).setUp()
52 # we have to wait for the replication before we make the check
53 self.fsmo_wait_max_time = 20
54 self.fsmo_wait_sleep_time = 0.2
55 # connect to DCs singleton
56 if self.ldb_dc1 is None:
57 DrsFsmoTestCase.dc1 = samba.tests.env_get_var_value("DC1")
58 DrsFsmoTestCase.ldb_dc1 = samba.tests.connect_samdb(self.dc1, ldap_only=True)
59 if self.ldb_dc2 is None:
60 DrsFsmoTestCase.dc2 = samba.tests.env_get_var_value("DC2")
61 DrsFsmoTestCase.ldb_dc2 = samba.tests.connect_samdb(self.dc2, ldap_only=True)
64 if self.info_dc1 is None:
66 res = ldb.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
67 self.assertEquals(len(res), 1)
68 DrsFsmoTestCase.info_dc1 = res[0]
69 if self.info_dc2 is None:
71 res = ldb.search(base="", expression="", scope=SCOPE_BASE, attrs=["*"])
72 self.assertEquals(len(res), 1)
73 DrsFsmoTestCase.info_dc2 = res[0]
75 # cache some of RootDSE props
76 self.schema_dn = self.info_dc1["schemaNamingContext"][0]
77 self.domain_dn = self.info_dc1["defaultNamingContext"][0]
78 self.config_dn = self.info_dc1["configurationNamingContext"][0]
79 self.dsServiceName_dc1 = self.info_dc1["dsServiceName"][0]
80 self.dsServiceName_dc2 = self.info_dc2["dsServiceName"][0]
81 self.infrastructure_dn = "CN=Infrastructure," + self.domain_dn
82 self.naming_dn = "CN=Partitions," + self.config_dn
83 self.rid_dn = "CN=RID Manager$,CN=System," + self.domain_dn
85 # we will need DCs DNS names for 'net fsmo' command
86 self.dnsname_dc1 = self.info_dc1["dnsHostName"][0]
87 self.dnsname_dc2 = self.info_dc2["dnsHostName"][0]
91 super(DrsFsmoTestCase, self).tearDown()
93 def _net_fsmo_role_transfer(self, DC, role):
94 # find out where is samba-tool command
95 net_cmd = os.path.abspath("./bin/samba-tool")
96 # make command line credentials string
97 creds = samba.tests.cmdline_credentials
98 cmd_line_auth = "-U%s/%s%%%s" % (creds.get_domain(),
99 creds.get_username(), creds.get_password())
100 # bin/samba-tool fsmo transfer --role=role --host=ldap://DC:389
101 cmd_line = "%s fsmo transfer --role=%s --host=ldap://%s:389 %s" % (net_cmd, role, DC,
103 ret = os.system(cmd_line)
104 self.assertEquals(ret, 0, "Transferring schema to %s has failed!" % (DC))
107 def _wait_for_role_transfer(self, ldb_dc, role_dn, master):
108 """Wait for role transfer for certain amount of time
110 :return: (Result=True|False, CurrentMasterDnsName) tuple
113 retries = int(self.fsmo_wait_max_time / self.fsmo_wait_sleep_time) + 1
114 for i in range(0, retries):
115 # check if master has been transfered
116 res = ldb_dc.search(role_dn,
117 scope=SCOPE_BASE, attrs=["fSMORoleOwner"])
118 assert len(res) == 1, "Only one fSMORoleOwner value expected!"
119 cur_master = res[0]["fSMORoleOwner"][0]
120 if master == cur_master:
121 return (True, cur_master)
122 # skip last sleep, if no need to wait anymore
123 if i != (retries - 1):
124 # wait a little bit before next retry
125 time.sleep(self.fsmo_wait_sleep_time)
126 return (False, cur_master)
128 def _role_transfer(self, role, role_dn):
129 """Triggers transfer of role from DC1 to DC2
130 and vice versa so the role goes back to the original dc"""
131 # dc2 gets the schema master role from dc1
132 print "Testing for %s role transfer from %s to %s" % (role, self.dnsname_dc1, self.dnsname_dc2)
134 self._net_fsmo_role_transfer(DC=self.dnsname_dc2, role=role)
135 # check if the role is transfered
136 (res, master) = self._wait_for_role_transfer(ldb_dc=self.ldb_dc2,
138 master=self.dsServiceName_dc2)
140 "Transferring %s role to %s has failed, master is: %s!"%(role, self.dsServiceName_dc2, master))
142 # dc1 gets back the schema master role from dc2
143 print "Testing for %s role transfer from %s to %s" % (role, self.dnsname_dc2, self.dnsname_dc1)
144 self._net_fsmo_role_transfer(DC=self.dnsname_dc1, role=role)
145 # check if the role is transfered
146 (res, master) = self._wait_for_role_transfer(ldb_dc=self.ldb_dc1,
148 master=self.dsServiceName_dc1)
150 "Transferring %s role to %s has failed, master is: %s!"%(role, self.dsServiceName_dc1, master))
153 def test_SchemaMasterTransfer(self):
154 self._role_transfer(role="schema", role_dn=self.schema_dn)
157 def test_InfrastructureMasterTransfer(self):
158 self._role_transfer(role="infrastructure", role_dn=self.infrastructure_dn)
161 def test_PDCMasterTransfer(self):
162 self._role_transfer(role="pdc", role_dn=self.domain_dn)
165 def test_RIDMasterTransfer(self):
166 self._role_transfer(role="rid", role_dn=self.rid_dn)
169 def test_NamingMasterTransfer(self):
170 self._role_transfer(role="naming", role_dn=self.naming_dn)