Debian Codegraph @

dirsrvtests/tests/suites/replication/cleanallruv_multiple_force_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2022 Red Hat, Inc.
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 import ldap
10 import logging
11 import os
12 import pytest
13 import random
14 import time
15 import threading
16 from lib389._constants import DEFAULT_SUFFIX
17 from test389.topologies import topology_m4
18 from lib389.tasks import CleanAllRUVTask
19 from lib389.idm.directorymanager import DirectoryManager
20 from lib389.idm.user import UserAccounts
21 from lib389.replica import ReplicationManager, Replicas
22
23 log = logging.getLogger(__name__)
24
25
26 class AddUsers(threading.Thread):
27 def __init__(self, inst, num_users):
28 threading.Thread.__init__(self)
29 self.daemon = True
30 self.inst = inst
31 self.num_users = num_users
32
33 def run(self):
34 """Start adding users"""
35
36 dm = DirectoryManager(self.inst)
37 conn = dm.bind()
38
39 users = UserAccounts(conn, DEFAULT_SUFFIX)
40
41 u_range = list(range(self.num_users))
42 random.shuffle(u_range)
43
44 for idx in u_range:
45 try:
46 users.create(properties={
47 'uid': 'testuser%s' % idx,
48 'cn' : 'testuser%s' % idx,
49 'sn' : 'user%s' % idx,
50 'uidNumber' : '%s' % (1000 + idx),
51 'gidNumber' : '%s' % (1000 + idx),
52 'homeDirectory' : '/home/testuser%s' % idx
53 })
54 # One of the suppliers was probably put into read only mode - just break out
55 except ldap.UNWILLING_TO_PERFORM:
56 break
57 except ldap.ALREADY_EXISTS:
58 pass
59 conn.close()
60
61 def remove_some_supplier4_agmts(msg, topology_m4):
62 """Remove all the repl agmts to supplier4 except from supplier3. Used by
63 the force tests."""
64
65 log.info('%s: remove the agreements to supplier 4...' % msg)
66 repl = ReplicationManager(DEFAULT_SUFFIX)
67 # This will delete m4 from the topo *and* remove all incoming agreements
68 # to m4.
69 repl.remove_supplier(topology_m4.ms["supplier4"],
70 [topology_m4.ms["supplier1"], topology_m4.ms["supplier2"]])
71
72 def task_done(topology_m4, task_dn, timeout=60):
73 """Check if the task is complete"""
74
75 attrlist = ['nsTaskLog', 'nsTaskStatus', 'nsTaskExitCode',
76 'nsTaskCurrentItem', 'nsTaskTotalItems']
77 done = False
78 count = 0
79
80 while not done and count < timeout:
81 try:
82 entry = topology_m4.ms["supplier1"].getEntry(task_dn, attrlist=attrlist)
83 if entry is not None:
84 if entry.hasAttr('nsTaskExitCode'):
85 done = True
86 break
87 else:
88 done = True
89 break
90 except ldap.NO_SUCH_OBJECT:
91 done = True
92 break
93 except ldap.LDAPError:
94 break
95 time.sleep(1)
96 count += 1
97
98 return done
99
100 def check_ruvs(msg, topology_m4, m4rid):
101 """Check suppliers 1-3 for supplier 4's rid."""
102 for inst in (topology_m4.ms["supplier1"], topology_m4.ms["supplier2"], topology_m4.ms["supplier3"]):
103 clean = False
104 replicas = Replicas(inst)
105 replica = replicas.get(DEFAULT_SUFFIX)
106 log.info('check_ruvs for replica %s:%s (suffix:rid)' % (replica.get_suffix(), replica.get_rid()))
107
108 count = 0
109 while not clean and count < 20:
110 ruv = replica.get_ruv()
111 if m4rid in ruv._rids:
112 time.sleep(5)
113 count = count + 1
114 else:
115 clean = True
116 if not clean:
117 raise Exception("Supplier %s was not cleaned in time." % inst.serverid)
118 return True
119
120
121 def test_multiple_tasks_with_force(topology_m4):
122 """Check that multiple tasks with a 'force' option work properly
123
124 :id: eb76a93d-8d1c-405e-9f25-6e8d5a781098
125 :setup: Replication setup with four suppliers
126 :steps:
127 1. Stop supplier 3
128 2. Add a bunch of updates to supplier 4
129 3. Disable replication on supplier 4
130 4. Start supplier 3
131 5. Remove agreements to supplier 4 from other suppliers
132 6. Run a cleanallruv task on supplier 1 with a 'force' option 'on'
133 7. Run one more cleanallruv task on supplier 1 with a 'force' option 'off'
134 8. Check that everything was cleaned
135 :expectedresults:
136 1. Supplier 3 should be stopped
137 2. Operation should be successful
138 3. Replication on supplier 4 should be disabled
139 4. Supplier 3 should be started
140 5. Agreements to supplier 4 should be removed
141 6. Operation should be successful
142 7. Operation should be successful
143 8. Everything should be cleaned
144 """
145
146 log.info('Running test_multiple_tasks_with_force...')
147
148 # Stop supplier 3, while we update supplier 4, so that 3 is behind the other suppliers
149 topology_m4.ms["supplier3"].stop()
150 repl = ReplicationManager(DEFAULT_SUFFIX)
151 m4rid = repl.get_rid(topology_m4.ms["supplier4"])
152
153 # Add a bunch of updates to supplier 4
154 m4_add_users = AddUsers(topology_m4.ms["supplier4"], 10)
155 m4_add_users.start()
156 m4_add_users.join()
157
158 # Disable supplier 4
159 # Remove the agreements from the other suppliers that point to supplier 4
160 remove_some_supplier4_agmts("test_multiple_tasks_with_force", topology_m4)
161
162 # Start supplier 3, it should be out of sync with the other replicas...
163 topology_m4.ms["supplier3"].start()
164
165 # Remove the agreement to replica 4
166 replica = Replicas(topology_m4.ms["supplier3"]).get(DEFAULT_SUFFIX)
167 replica.get_agreements().get("004").delete()
168
169 # Run the task, use "force" because supplier 3 is not in sync with the other replicas
170 # in regards to the replica 4 RUV
171 log.info('test_multiple_tasks_with_force: run the cleanAllRUV task with "force" on...')
172 cruv_task = CleanAllRUVTask(topology_m4.ms["supplier1"])
173 cruv_task.create(properties={
174 'replica-id': m4rid,
175 'replica-base-dn': DEFAULT_SUFFIX,
176 'replica-force-cleaning': 'yes',
177 'replica-certify-all': 'no'
178 })
179
180 log.info('test_multiple_tasks_with_force: run the cleanAllRUV task with "force" off...')
181
182 # NOTE: This must be try not py.test raises, because the above may or may
183 # not have completed yet ....
184 try:
185 cruv_task_fail = CleanAllRUVTask(topology_m4.ms["supplier1"])
186 cruv_task_fail.create(properties={
187 'replica-id': m4rid,
188 'replica-base-dn': DEFAULT_SUFFIX,
189 'replica-force-cleaning': 'no',
190 'replica-certify-all': 'no'
191 })
192 cruv_task_fail.wait()
193 except ldap.UNWILLING_TO_PERFORM:
194 pass
195 # Wait for the force task ....
196 cruv_task.wait()
197
198 # Check the other supplier's RUV for 'replica 4'
199 log.info('test_multiple_tasks_with_force: check all the suppliers have been cleaned...')
200 clean = check_ruvs("test_clean_force", topology_m4, m4rid)
201 assert clean
202 # Check supplier 1 does not have the clean task running
203 log.info('test_abort: check supplier 1 no longer has a cleanAllRUV task...')
204 if not task_done(topology_m4, cruv_task.dn):
205 log.fatal('test_abort: CleanAllRUV task was not aborted')
206 assert False
207
208
209 if __name__ == '__main__':
210 # Run isolated
211 # -s for DEBUG mode
212 CURRENT_FILE = os.path.realpath(__file__)
213 pytest.main(["-s", CURRENT_FILE])
214