Debian Codegraph @

dirsrvtests/tests/suites/syncrepl_plugin/__init__.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2020 William Brown <[email protected]>
3 # All rights reserved.
4 #
5 # License: GPL (version 3 or any later version).
6 # See LICENSE for details.
7 # --- END COPYRIGHT BLOCK ---
8 """
9 :Requirement: 389-ds-base: Sync Replication Plugin
10 """
11 import logging
12 import ldap
13 import time
14 from ldap.syncrepl import SyncreplConsumer
15 from lib389 import DirSrv
16 from lib389.idm.user import nsUserAccounts
17 from test389.topologies import topology_st as topology
18 from lib389._constants import DEFAULT_SUFFIX
19
20 log = logging.getLogger(__name__)
21
22 OU_PEOPLE = "ou=people,%s" % DEFAULT_SUFFIX
23
24 class ISyncRepl(DirSrv, SyncreplConsumer):
25 """
26 This implements a test harness for checking syncrepl, and allowing us to check various actions or
27 behaviours. During a "run" it stores the results in it's instance, so that they can be inspected
28 later to ensure that syncrepl worked as expected.
29 """
30 def __init__(self, inst, openldap=False):
31 ### 🚧 WARNING 🚧
32 # There are bugs with python ldap sync repl in ALL VERSIONS below 3.3.1.
33 # These tests WILL FAIL unless you have version 3.3.1 or higher!
34 assert ldap.__version__ >= '3.3.1'
35
36 self.inst = inst
37 self.msgid = None
38
39 self.last_cookie = None
40 self.next_cookie = None
41 self.cookie = None
42 self.openldap = openldap
43 if self.openldap:
44 # In openldap mode, our initial cookie needs to be a rid.
45 self.cookie = "rid=123"
46 self.delete = []
47 self.present = []
48 self.entries = {}
49
50 super().__init__()
51
52 def result4(self, *args, **kwargs):
53 return self.inst.result4(*args, **kwargs, escapehatch='i am sure')
54
55 def search_ext(self, *args, **kwargs):
56 return self.inst.search_ext(*args, **kwargs, escapehatch='i am sure')
57
58 def syncrepl_search(self, base=DEFAULT_SUFFIX, scope=ldap.SCOPE_SUBTREE, mode='refreshOnly', cookie=None, **search_args):
59 # Wipe the last result set.
60 self.delete = []
61 self.present = []
62 self.entries = {}
63 self.refdel = False
64 self.next_cookie = None
65 # Start the sync
66 # If cookie is none, will call "get_cookie" we have.
67 self.msgid = super().syncrepl_search(base, scope, mode, cookie, **search_args)
68 log.debug(f'syncrepl_search -> {self.msgid}')
69 assert self.msgid is not None
70
71 def syncrepl_complete(self):
72 log.debug(f'syncrepl_complete -> {self.msgid}')
73 assert self.msgid is not None
74 # Loop until the operation is complete.
75 time.sleep(1)
76 while super().syncrepl_poll(msgid=self.msgid) is True:
77 pass
78 assert self.next_cookie is not None
79 self.last_cookie = self.cookie
80 self.cookie = self.next_cookie
81
82 def check_cookie(self):
83 assert self.last_cookie != self.cookie
84
85 def syncrepl_set_cookie(self, cookie):
86 log.debug(f'set_cookie -> {cookie}')
87 if self.openldap:
88 assert self.cookie.startswith("rid=123")
89 self.next_cookie = cookie
90
91 def syncrepl_get_cookie(self):
92 log.debug('get_cookie -> %s' % self.cookie)
93 if self.openldap:
94 assert self.cookie.startswith("rid=123")
95 return self.cookie
96
97 def syncrepl_present(self, uuids, refreshDeletes=False):
98 log.debug(f'=====> refdel -> {refreshDeletes} uuids -> {uuids}')
99 if refreshDeletes:
100 # Indicate we recieved a refdel in the process.
101 self.refdel = True
102 if uuids is not None:
103 self.present = self.present + uuids
104
105 def syncrepl_delete(self, uuids):
106 log.debug(f'delete -> {uuids}')
107 self.delete = uuids
108
109 def syncrepl_entry(self, dn, attrs, uuid):
110 log.debug(f'entry -> {dn}')
111 self.entries[dn] = (uuid, attrs)
112
113 def syncrepl_refreshdone(self):
114 log.debug('refreshdone')
115
116 def syncstate_assert(st, sync):
117 # How many entries do we have?
118 # We setup sync under ou=people so we can modrdn out of the scope.
119 r = st.search_ext_s(
120 base=OU_PEOPLE,
121 scope=ldap.SCOPE_SUBTREE,
122 filterstr='(objectClass=*)',
123 attrsonly=1,
124 escapehatch='i am sure'
125 )
126
127 # Initial sync
128 log.debug("*test* initial")
129 sync.syncrepl_search(base=OU_PEOPLE)
130 sync.syncrepl_complete()
131 # check we caught them all
132 assert len(r) == len(sync.entries.keys())
133 assert len(r) == len(sync.present)
134 assert 0 == len(sync.delete)
135 if sync.openldap:
136 assert True == sync.refdel
137 else:
138 assert False == sync.refdel
139
140 # Add a new entry
141 account = nsUserAccounts(st, DEFAULT_SUFFIX).create_test_user()
142
143 # Find the primary uuid we expect to see in syncrepl.
144 # This will be None if not present.
145 acc_uuid = account.get_attr_val_utf8('entryuuid')
146 if not sync.openldap:
147 nsid = account.get_attr_val_utf8('nsuniqueid')
148 # nsunique has a diff format, so we change it up.
149 # 431cf081-b44311ea-83fdb082-f24d490e
150 # Add a hyphen V
151 # 431cf081-b443-11ea-83fdb082-f24d490e
152 nsid_a = nsid[:13] + '-' + nsid[13:]
153 # Add a hyphen V
154 # 431cf081-b443-11ea-83fd-b082-f24d490e
155 nsid_b = nsid_a[:23] + '-' + nsid_a[23:]
156 # Remove a hyphen V
157 # 431cf081-b443-11ea-83fd-b082-f24d490e
158 acc_uuid = nsid_b[:28] + nsid_b[29:]
159 # Tada!
160 # 431cf081-b443-11ea-83fd-b082f24d490e
161 log.debug(f"--> expected sync uuid (from nsuniqueid): {acc_uuid}")
162 else:
163 log.debug(f"--> expected sync uuid (from entryuuid): {acc_uuid}")
164
165 # Check
166 log.debug("*test* add")
167 sync.syncrepl_search(base=OU_PEOPLE)
168 sync.syncrepl_complete()
169 sync.check_cookie()
170 log.debug(f"sd: {sync.delete}, sp: {sync.present} sek: {sync.entries.keys()}")
171
172 assert 1 == len(sync.entries.keys())
173 assert 1 == len(sync.present)
174 ####################################
175 assert sync.present == [acc_uuid]
176 assert 0 == len(sync.delete)
177 if sync.openldap:
178 assert True == sync.refdel
179 else:
180 assert False == sync.refdel
181
182 # Mod
183 account.replace('description', 'change')
184 # Check
185 log.debug("*test* mod")
186 sync.syncrepl_search(base=OU_PEOPLE)
187 sync.syncrepl_complete()
188 sync.check_cookie()
189 log.debug(f"sd: {sync.delete}, sp: {sync.present} sek: {sync.entries.keys()}")
190 assert 1 == len(sync.entries.keys())
191 assert 1 == len(sync.present)
192 ####################################
193 assert sync.present == [acc_uuid]
194 assert 0 == len(sync.delete)
195 if sync.openldap:
196 assert True == sync.refdel
197 else:
198 assert False == sync.refdel
199
200 ## ModRdn (remain in scope)
201 account.rename('uid=test1_modrdn')
202 # newsuperior=None
203 # Check
204 log.debug("*test* modrdn (in scope)")
205 sync.syncrepl_search(base=OU_PEOPLE)
206 sync.syncrepl_complete()
207 sync.check_cookie()
208 log.debug(f"sd: {sync.delete}, sp: {sync.present} sek: {sync.entries.keys()}")
209 assert 1 == len(sync.entries.keys())
210 assert 1 == len(sync.present)
211 ####################################
212 assert sync.present == [acc_uuid]
213 assert 0 == len(sync.delete)
214 if sync.openldap:
215 assert True == sync.refdel
216 else:
217 assert False == sync.refdel
218
219 # import time
220 # print("attach now ....")
221 # time.sleep(45)
222
223 ## Modrdn (out of scope, then back into scope)
224 account.rename('uid=test1_modrdn', newsuperior=DEFAULT_SUFFIX)
225
226 # Check it's gone.
227 log.debug("*test* modrdn (move out of scope)")
228 sync.syncrepl_search(base=OU_PEOPLE)
229 sync.syncrepl_complete()
230 sync.check_cookie()
231 log.debug(f"sd: {sync.delete}, sp: {sync.present} sek: {sync.entries.keys()}")
232 assert 0 == len(sync.entries.keys())
233 assert 0 == len(sync.present)
234 ## WARNING: This test MAY FAIL here if you do not have a new enough python-ldap
235 # due to an ASN.1 parsing bug. You require at least python-ldap 3.3.1
236 assert 1 == len(sync.delete)
237 assert sync.delete == [acc_uuid]
238 if sync.openldap:
239 assert True == sync.refdel
240 else:
241 assert False == sync.refdel
242
243 # Put it back
244 account.rename('uid=test1_modrdn', newsuperior=OU_PEOPLE)
245 log.debug("*test* modrdn (move in to scope)")
246 sync.syncrepl_search(base=OU_PEOPLE)
247 sync.syncrepl_complete()
248 sync.check_cookie()
249 log.debug(f"sd: {sync.delete}, sp: {sync.present} sek: {sync.entries.keys()}")
250 assert 1 == len(sync.entries.keys())
251 assert 1 == len(sync.present)
252 ####################################
253 assert sync.present == [acc_uuid]
254 assert 0 == len(sync.delete)
255 if sync.openldap:
256 assert True == sync.refdel
257 else:
258 assert False == sync.refdel
259
260 ## Delete
261 account.delete()
262
263 # Check
264 log.debug("*test* del")
265 sync.syncrepl_search(base=OU_PEOPLE)
266 sync.syncrepl_complete()
267 # In a delete, the cookie isn't updated (?)
268 sync.check_cookie()
269 log.debug(f'{sync.entries.keys()}')
270 log.debug(f'{sync.present}')
271 log.debug(f'{sync.delete}')
272 log.debug(f"sd: {sync.delete}, sp: {sync.present} sek: {sync.entries.keys()}")
273 assert 0 == len(sync.entries.keys())
274 assert 0 == len(sync.present)
275 assert 1 == len(sync.delete)
276 assert sync.delete == [acc_uuid]
277 ####################################
278 if sync.openldap:
279 assert True == sync.refdel
280 else:
281 assert False == sync.refdel
282