Debian Codegraph @

dirsrvtests/tests/suites/monitor/db_locks_monitor_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2021 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 datetime
12 import subprocess
13 from multiprocessing import Process, Queue
14 from lib389 import pid_from_file
15 from lib389.utils import ldap, os, ds_is_older, get_default_db_lib
16 from lib389._constants import DEFAULT_SUFFIX, ReplicaRole
17 from lib389.cli_base import LogCapture
18 from lib389.idm.user import UserAccounts
19 from lib389.idm.organizationalunit import OrganizationalUnits
20 from lib389.tasks import AccessLog
21 from lib389.backend import Backends
22 from lib389.ldclt import Ldclt
23 from lib389.dbgen import dbgen_users
24 from lib389.tasks import ImportTask
25 from lib389.index import Indexes
26 from lib389.plugins import AttributeUniquenessPlugin
27 from lib389.config import BDB_LDBMConfig
28 from lib389.monitor import MonitorLDBM
29 from test389.topologies import create_topology, _remove_ssca_db
30
31 pytestmark = pytest.mark.tier2
32 db_locks_monitoring_ack = pytest.mark.skipif(not os.environ.get('DB_LOCKS_MONITORING_ACK', False),
33 reason="DB locks monitoring tests may take hours if the feature is not present or another failure exists. "
34 "Also, the feature requires a big amount of space as we set nsslapd-db-locks to 1300000.")
35
36 DEBUGGING = os.getenv('DEBUGGING', default=False)
37 if DEBUGGING:
38 logging.getLogger(__name__).setLevel(logging.DEBUG)
39 else:
40 logging.getLogger(__name__).setLevel(logging.INFO)
41 log = logging.getLogger(__name__)
42
43
44 def _kill_ns_slapd(inst):
45 pid = str(pid_from_file(inst.ds_paths.pid_file))
46 cmd = ['kill', '-9', pid]
47 subprocess.Popen(cmd, stdout=subprocess.PIPE)
48
49
50 @pytest.fixture(scope="function")
51 def topology_st_fn(request):
52 """Create DS standalone instance for each test case"""
53
54 topology = create_topology({ReplicaRole.STANDALONE: 1})
55
56 def fin():
57 # Kill the hanging process at the end of test to prevent failures in the following tests
58 if DEBUGGING:
59 [_kill_ns_slapd(inst) for inst in topology]
60 else:
61 [_kill_ns_slapd(inst) for inst in topology]
62 assert _remove_ssca_db(topology)
63 [inst.stop() for inst in topology if inst.exists()]
64 [inst.delete() for inst in topology if inst.exists()]
65 request.addfinalizer(fin)
66
67 topology.logcap = LogCapture()
68 return topology
69
70
71 @pytest.fixture(scope="function")
72 def setup_attruniq_index_be_import(topology_st_fn):
73 """Enable Attribute Uniqueness, disable indexes and
74 import 120000 entries to the default backend
75 """
76 inst = topology_st_fn.standalone
77
78 inst.config.loglevel([AccessLog.DEFAULT, AccessLog.INTERNAL], service='access')
79 inst.config.set('nsslapd-plugin-logging', 'on')
80 inst.restart()
81
82 attruniq = AttributeUniquenessPlugin(inst, dn="cn=attruniq,cn=plugins,cn=config")
83 attruniq.create(properties={'cn': 'attruniq'})
84 for cn in ['uid', 'cn', 'sn', 'uidNumber', 'gidNumber', 'homeDirectory', 'givenName', 'description']:
85 attruniq.add_unique_attribute(cn)
86 attruniq.add_unique_subtree(DEFAULT_SUFFIX)
87 attruniq.enable_all_subtrees()
88 attruniq.enable()
89
90 indexes = Indexes(inst)
91 for cn in ['uid', 'cn', 'sn', 'uidNumber', 'gidNumber', 'homeDirectory', 'givenName', 'description']:
92 indexes.ensure_state(properties={
93 'cn': cn,
94 'nsSystemIndex': 'false',
95 'nsIndexType': 'none'})
96
97 bdb_config = BDB_LDBMConfig(inst)
98 bdb_config.replace("nsslapd-db-locks", "130000")
99 inst.restart()
100
101 ldif_dir = inst.get_ldif_dir()
102 import_ldif = ldif_dir + '/perf_import.ldif'
103
104 # Valid online import
105 import_task = ImportTask(inst)
106 dbgen_users(inst, 120000, import_ldif, DEFAULT_SUFFIX, entry_name="userNew")
107 import_task.import_suffix_from_ldif(ldiffile=import_ldif, suffix=DEFAULT_SUFFIX)
108 import_task.wait()
109 assert import_task.is_complete()
110
111
112 def create_user_wrapper(q, users):
113 try:
114 users.create_test_user()
115 except Exception as ex:
116 q.put(ex)
117
118
119 def spawn_worker_thread(function, users, log, timeout, info):
120 log.info(f"Starting the thread - {info}")
121 q = Queue()
122 p = Process(target=function, args=(q,users,))
123 p.start()
124
125 log.info(f"Waiting for {timeout} seconds for the thread to finish")
126 p.join(timeout)
127
128 if p.is_alive():
129 log.info("Killing the thread as it's still running")
130 p.terminate()
131 p.join()
132 raise RuntimeError(f"Function call was aborted: {info}")
133 result = q.get()
134 if isinstance(result, Exception):
135 raise result
136 else:
137 return result
138
139
140 @db_locks_monitoring_ack
141 @pytest.mark.parametrize("lock_threshold", [("70"), ("80"), ("95")])
142 def test_exhaust_db_locks_basic(topology_st_fn, setup_attruniq_index_be_import, lock_threshold):
143 """Test that when all of the locks are exhausted the instance still working
144 and database is not corrupted
145
146 :id: 299108cc-04d8-4ddc-b58e-99157fccd643
147 :customerscenario: True
148 :parametrized: yes
149 :setup: Standalone instance with Attr Uniq plugin and user indexes disabled
150 :steps: 1. Set nsslapd-db-locks to 11000
151 2. Check that we stop acquiring new locks when the threshold is reached
152 3. Check that we can regulate a pause interval for DB locks monitoring thread
153 4. Make sure the feature works for different backends on the same suffix
154 :expectedresults:
155 1. Success
156 2. Success
157 3. Success
158 4. Success
159 """
160
161 inst = topology_st_fn.standalone
162 ADDITIONAL_SUFFIX = 'ou=newpeople,dc=example,dc=com'
163
164 backends = Backends(inst)
165 backends.create(properties={'nsslapd-suffix': ADDITIONAL_SUFFIX,
166 'name': ADDITIONAL_SUFFIX[-3:]})
167 ous = OrganizationalUnits(inst, DEFAULT_SUFFIX)
168 ous.create(properties={'ou': 'newpeople'})
169
170 bdb_config = BDB_LDBMConfig(inst)
171 bdb_config.replace("nsslapd-db-locks", "11000")
172
173 # Restart server
174 inst.restart()
175
176 for lock_enabled in ["on", "off"]:
177 for lock_pause in ["100", "500", "1000"]:
178 bdb_config.replace("nsslapd-db-locks-monitoring-enabled", lock_enabled)
179 bdb_config.replace("nsslapd-db-locks-monitoring-threshold", lock_threshold)
180 bdb_config.replace("nsslapd-db-locks-monitoring-pause", lock_pause)
181 inst.restart()
182
183 if lock_enabled == "off":
184 raised_exception = (RuntimeError, ldap.SERVER_DOWN)
185 else:
186 raised_exception = ldap.OPERATIONS_ERROR
187
188 users = UserAccounts(inst, DEFAULT_SUFFIX)
189 with pytest.raises(raised_exception):
190 spawn_worker_thread(create_user_wrapper, users, log, 30,
191 f"Adding user with monitoring enabled='{lock_enabled}'; "
192 f"threshold='{lock_threshold}'; pause='{lock_pause}'.")
193 # Restart because we already run out of locks and the next unindexed searches will fail eventually
194 if lock_enabled == "off":
195 _kill_ns_slapd(inst)
196 inst.restart()
197
198 users = UserAccounts(inst, ADDITIONAL_SUFFIX, rdn=None)
199 with pytest.raises(raised_exception):
200 spawn_worker_thread(create_user_wrapper, users, log, 30,
201 f"Adding user with monitoring enabled='{lock_enabled}'; "
202 f"threshold='{lock_threshold}'; pause='{lock_pause}'.")
203 # In case feature is disabled - restart for the clean up
204 if lock_enabled == "off":
205 _kill_ns_slapd(inst)
206 inst.restart()
207
208
209 @db_locks_monitoring_ack
210 @pytest.mark.skipif(get_default_db_lib() == "mdb", reason="Not supported over mdb")
211 def test_exhaust_db_locks_big_pause(topology_st_fn, setup_attruniq_index_be_import):
212 """Test that DB lock pause setting increases the wait interval value for the monitoring thread
213
214 :id: 7d5bf838-5d4e-4ad5-8c03-5716afb84ea6
215 :customerscenario: True
216 :setup: Standalone instance with Attr Uniq plugin and user indexes disabled
217 :steps: 1. Set nsslapd-db-locks to 20000 while using the default threshold value (95%)
218 2. Set nsslapd-db-locks-monitoring-pause to 10000 (10 seconds)
219 3. Make sure that the pause is successfully increased a few times in a row
220 :expectedresults:
221 1. Success
222 2. Success
223 3. Success
224 """
225
226 inst = topology_st_fn.standalone
227
228 bdb_config = BDB_LDBMConfig(inst)
229 bdb_config.replace("nsslapd-db-locks", "20000")
230 lock_pause = bdb_config.get_attr_val_int("nsslapd-db-locks-monitoring-pause")
231 assert lock_pause == 500
232 lock_pause = "10000"
233 bdb_config.replace("nsslapd-db-locks-monitoring-pause", lock_pause)
234
235 # Restart server
236 inst.restart()
237
238 lock_enabled = bdb_config.get_attr_val_utf8_l("nsslapd-db-locks-monitoring-enabled")
239 lock_threshold = bdb_config.get_attr_val_int("nsslapd-db-locks-monitoring-threshold")
240 assert lock_enabled == "on"
241 assert lock_threshold == 90
242
243 users = UserAccounts(inst, DEFAULT_SUFFIX)
244 start = datetime.datetime.now()
245 with pytest.raises(ldap.OPERATIONS_ERROR):
246 spawn_worker_thread(create_user_wrapper, users, log, 30,
247 f"Adding user with monitoring enabled='{lock_enabled}'; "
248 f"threshold='{lock_threshold}'; pause='{lock_pause}'. Expect it to 'Work'")
249 end = datetime.datetime.now()
250 time_delta = end - start
251 if time_delta.seconds < 9:
252 raise RuntimeError("nsslapd-db-locks-monitoring-pause attribute doesn't function correctly. "
253 f"Finished the execution in {time_delta.seconds} seconds")
254 # In case something has failed - restart for the clean up
255 inst.restart()
256
257
258 @pytest.mark.skipif(ds_is_older("1.4.3.23"), reason="Not implemented")
259 @pytest.mark.skipif(get_default_db_lib() == "mdb", reason="Not supported over mdb")
260 @pytest.mark.parametrize("invalid_value", [("0"), ("1"), ("42"), ("68"), ("69"), ("96"), ("120")])
261 def test_invalid_threshold_range(topology_st_fn, invalid_value):
262 """Test that setting nsslapd-db-locks-monitoring-threshold to 60 % is rejected
263
264 :id: e4551de1-8582-4c13-b59d-3d5ec4701457
265 :customerscenario: True
266 :parametrized: yes
267 :setup: Standalone instance
268 :steps: 1. Set nsslapd-db-locks-monitoring-threshold to 60 %
269 2. Check if exception message contains info about invalid value range
270 :expectedresults:
271 1. Exception is raised
272 2. Success
273 """
274
275 inst = topology_st_fn.standalone
276 bdb_config = BDB_LDBMConfig(inst)
277 msg = 'threshold is indicated as a percentage and it must lie in range of 70 and 95'
278
279 try:
280 bdb_config.replace("nsslapd-db-locks-monitoring-threshold", invalid_value)
281 except ldap.OPERATIONS_ERROR as e:
282 log.info('Got expected error: {}'.format(str(e)))
283 assert msg in str(e)
284
285
286 @pytest.mark.skipif(ds_is_older("1.4.3.23"), reason="Not implemented")
287 @pytest.mark.skipif(get_default_db_lib() == "mdb", reason="Not supported over mdb")
288 @pytest.mark.parametrize("locks_invalid", [("0"), ("1"), ("9999"), ("10000")])
289 def test_invalid_db_locks_value(topology_st_fn, locks_invalid):
290 """Test that setting nsslapd-db-locks to 0 is rejected
291
292 :id: bbb40279-d622-4f36-a129-c54f963f494a
293 :customerscenario: True
294 :parametrized: yes
295 :setup: Standalone instance
296 :steps: 1. Set nsslapd-db-locks to 0
297 2. Check if exception message contains info about invalid value
298 :expectedresults:
299 1. Exception is raised
300 2. Success
301 """
302
303 inst = topology_st_fn.standalone
304 bdb_config = BDB_LDBMConfig(inst)
305 msg = 'Invalid value for nsslapd-db-locks ({}). Must be greater than 10000'.format(locks_invalid)
306
307 try:
308 bdb_config.replace("nsslapd-db-locks", locks_invalid)
309 except ldap.UNWILLING_TO_PERFORM as e:
310 log.info('Got expected error: {}'.format(str(e)))
311 assert msg in str(e)
312
313
314 if __name__ == '__main__':
315 # Run isolated
316 # -s for DEBUG mode
317 CURRENT_FILE = os.path.realpath(__file__)
318 pytest.main(["-s", CURRENT_FILE])