s4-test/schema: Extend the test to replicate a Class with Attribute with "mayContain...
[Samba.git] / source4 / torture / drs / python / repl_schema.py
blob02e2afeee8a9749a23e8d17da0f3f068e2f490f3
1 #!/usr/bin/env python
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/>.
23 # Usage:
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"
30 import sys
31 import time
32 import random
33 import os
35 sys.path.append("bin/python")
36 import samba
37 samba.ensure_external_module("testtools", "testtools")
38 samba.ensure_external_module("subunit", "subunit/python")
40 from ldb import (
41 LdbError,
42 SCOPE_BASE,
43 Message,
44 FLAG_MOD_ADD,
45 FLAG_MOD_REPLACE,
48 import samba.tests
51 class DrsReplSchemaTestCase(samba.tests.TestCase):
53 # prefix for all objects created
54 obj_prefix = None
56 def setUp(self):
57 super(DrsReplSchemaTestCase, self).setUp()
59 # connect to DCs
60 url_dc = samba.tests.env_get_var_value("DC1")
61 (self.ldb_dc1, self.info_dc1) = samba.tests.connect_samdb_ex(url_dc,
62 ldap_only=True)
63 url_dc = samba.tests.env_get_var_value("DC2")
64 (self.ldb_dc2, self.info_dc2) = samba.tests.connect_samdb_ex(url_dc,
65 ldap_only=True)
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]
82 def tearDown(self):
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,
97 nc_dn, cmd_line_auth)
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):
105 rec = {"dn": "",
106 "schemaUpdateNow": "1"}
107 m = Message.from_dict(sam_db, rec, FLAG_MOD_REPLACE)
108 sam_db.modify(m)
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"],
122 "cn": class_name,
123 "lDAPDisplayName": class_ldn,
124 "governsId": "1.2.840." + str(random.randint(1,100000)) + ".1.5.13",
125 "instanceType": "4",
126 "objectClassCategory": "1",
127 "subClassOf": "organizationalPerson",
128 "systemOnly": "FALSE"}
129 # allow overriding/adding attributes
130 if not attrs is None:
131 rec.update(attrs)
132 # add it to the Schema
133 ldb_ctx.add(rec)
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"],
141 "cn": attr_name,
142 "lDAPDisplayName": attr_ldn,
143 "attributeId": "1.2.841." + str(random.randint(1,100000)) + ".1.5.13",
144 "attributeSyntax": "2.5.5.12",
145 "omSyntax": "64",
146 "instanceType": "4",
147 "isSingleValued": "TRUE",
148 "systemOnly": "FALSE"}
149 # allow overriding/adding attributes
150 if not attrs is None:
151 rec.update(attrs)
152 # add it to the Schema
153 ldb_ctx.add(rec)
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,
160 scope=SCOPE_BASE,
161 attrs=["*"])
162 self.assertEquals(len(res_dc1), 1,
163 "%s doesn't exists on %s" % (obj_dn, self.dnsname_dc1))
164 try:
165 res_dc2 = self.ldb_dc2.search(base=obj_dn,
166 scope=SCOPE_BASE,
167 attrs=["*"])
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))
171 raise
172 self.assertEquals(len(res_dc2), 1,
173 "%s doesn't exists on %s" % (obj_dn, self.dnsname_dc2))
175 def test_all(self):
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,
187 {"dn": c_dn,
188 "mayContain": a_ldn},
189 FLAG_MOD_ADD)
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)