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