2 # -*- coding: utf-8 -*-
4 # Tests various schema replication scenarios
6 # Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # export DC1=dc1_dns_name
25 # export DC2=dc2_dns_name
26 # export SUBUNITRUN=$samba4srcdir/scripting/bin/subunitrun
27 # PYTHONPATH="$PYTHONPATH:$samba4srcdir/torture/drs/python" $SUBUNITRUN repl_schema -U"$DOMAIN/$DC_USERNAME"%"$DC_PASSWORD"
35 sys
.path
.append("bin/python")
37 samba
.ensure_external_module("testtools", "testtools")
38 samba
.ensure_external_module("subunit", "subunit/python")
51 class DrsReplSchemaTestCase(samba
.tests
.TestCase
):
53 # prefix for all objects created
57 super(DrsReplSchemaTestCase
, self
).setUp()
60 url_dc
= samba
.tests
.env_get_var_value("DC1")
61 (self
.ldb_dc1
, self
.info_dc1
) = samba
.tests
.connect_samdb_ex(url_dc
,
63 url_dc
= samba
.tests
.env_get_var_value("DC2")
64 (self
.ldb_dc2
, self
.info_dc2
) = samba
.tests
.connect_samdb_ex(url_dc
,
67 # initialize objects prefix if not done yet
68 if self
.obj_prefix
is None:
69 t
= time
.strftime("%s", time
.gmtime())
70 DrsReplSchemaTestCase
.obj_prefix
= "DrsReplSchema-%s-" % t
72 # cache some of RootDSE props
73 self
.schema_dn
= self
.info_dc1
["schemaNamingContext"][0]
74 self
.domain_dn
= self
.info_dc1
["defaultNamingContext"][0]
75 self
.config_dn
= self
.info_dc1
["configurationNamingContext"][0]
76 self
.forest_level
= int(self
.info_dc1
["forestFunctionality"][0])
78 # we will need DCs DNS names for 'samba-tool drs' command
79 self
.dnsname_dc1
= self
.info_dc1
["dnsHostName"][0]
80 self
.dnsname_dc2
= self
.info_dc2
["dnsHostName"][0]
83 super(DrsReplSchemaTestCase
, self
).tearDown()
85 def _net_drs_replicate(self
, DC
, fromDC
, nc_dn
):
86 """Triggers replication cycle on 'DC' to
87 replicate from 'fromDC'. Naming context to
88 be replicated is 'nc_dn' dn"""
89 # find out where is net command
90 samba_tool_cmd
= os
.path
.abspath("./bin/samba-tool")
91 # make command line credentials string
92 creds
= samba
.tests
.cmdline_credentials
93 cmd_line_auth
= "-U%s/%s%%%s" % (creds
.get_domain(),
94 creds
.get_username(), creds
.get_password())
95 # bin/samba-tool drs replicate <Dest_DC_NAME> <Src_DC_NAME> <Naming Context>
96 cmd_line
= "%s drs replicate %s %s %s %s" % (samba_tool_cmd
, DC
, fromDC
,
98 ret
= os
.system(cmd_line
)
99 self
.assertEquals(ret
, 0, "Replicating %s from %s has failed!" % (DC
, fromDC
))
101 def _GUID_string(self
, guid
):
102 return self
.ldb_dc1
.schema_format_value("objectGUID", guid
)
104 def _ldap_schemaUpdateNow(self
, sam_db
):
106 "schemaUpdateNow": "1"}
107 m
= Message
.from_dict(sam_db
, rec
, FLAG_MOD_REPLACE
)
110 def _make_obj_names(self
, base_name
):
111 '''Try to create a unique name for an object
112 that is to be added to schema'''
113 obj_name
= self
.obj_prefix
+ base_name
114 obj_ldn
= obj_name
.replace("-", "")
115 obj_dn
= "CN=%s,%s" % (obj_name
, self
.schema_dn
)
116 return (obj_dn
, obj_name
, obj_ldn
)
118 def _schema_new_class(self
, ldb_ctx
, base_name
, attrs
=None):
119 (class_dn
, class_name
, class_ldn
) = self
._make
_obj
_names
(base_name
)
120 rec
= {"dn": class_dn
,
121 "objectClass": ["top", "classSchema"],
123 "lDAPDisplayName": class_ldn
,
124 "governsId": "1.2.840." + str(random
.randint(1,100000)) + ".1.5.13",
126 "objectClassCategory": "1",
127 "subClassOf": "organizationalPerson",
128 "systemOnly": "FALSE"}
129 # allow overriding/adding attributes
130 if not attrs
is None:
132 # add it to the Schema
134 self
._ldap
_schemaUpdateNow
(ldb_ctx
)
135 return (rec
["lDAPDisplayName"], rec
["dn"])
137 def _schema_new_attr(self
, ldb_ctx
, base_name
, attrs
=None):
138 (attr_dn
, attr_name
, attr_ldn
) = self
._make
_obj
_names
(base_name
)
139 rec
= {"dn": attr_dn
,
140 "objectClass": ["top", "attributeSchema"],
142 "lDAPDisplayName": attr_ldn
,
143 "attributeId": "1.2.841." + str(random
.randint(1,100000)) + ".1.5.13",
144 "attributeSyntax": "2.5.5.12",
147 "isSingleValued": "TRUE",
148 "systemOnly": "FALSE"}
149 # allow overriding/adding attributes
150 if not attrs
is None:
152 # add it to the Schema
154 self
._ldap
_schemaUpdateNow
(ldb_ctx
)
155 return (rec
["lDAPDisplayName"], rec
["dn"])
157 def _check_object(self
, obj_dn
):
158 '''Check if object obj_dn exists on both DCs'''
159 res_dc1
= self
.ldb_dc1
.search(base
=obj_dn
,
162 self
.assertEquals(len(res_dc1
), 1,
163 "%s doesn't exists on %s" % (obj_dn
, self
.dnsname_dc1
))
165 res_dc2
= self
.ldb_dc2
.search(base
=obj_dn
,
168 except LdbError
, (enum
, estr
):
169 if enum
== ERR_NO_SUCH_OBJECT
:
170 self
.fail("%s doesn't exists on %s" % (obj_dn
, self
.dnsname_dc2
))
172 self
.assertEquals(len(res_dc2
), 1,
173 "%s doesn't exists on %s" % (obj_dn
, self
.dnsname_dc2
))
176 """Basic plan is to create bunch of classSchema
177 and attributeSchema objects, replicate Schema NC
178 and then check all objects are replicated correctly"""
180 # add new classSchema object
181 (c_ldn
, c_dn
) = self
._schema
_new
_class
(self
.ldb_dc1
, "cls-A")
182 # add new attributeSchema object
183 (a_ldn
, a_dn
) = self
._schema
_new
_attr
(self
.ldb_dc1
, "attr-A")
185 # add attribute to the class we have
186 m
= Message
.from_dict(self
.ldb_dc1
,
188 "mayContain": a_ldn
},
190 self
.ldb_dc1
.modify(m
)
192 # force replication from DC1 to DC2
193 self
._net
_drs
_replicate
(DC
=self
.dnsname_dc2
, fromDC
=self
.dnsname_dc1
, nc_dn
=self
.schema_dn
)
195 # check objects are replicated
196 self
._check
_object
(c_dn
)
197 self
._check
_object
(a_dn
)