Debian Codegraph @

dirsrvtests/tests/suites/monitor/monitor_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2026 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 ldap
11 import pytest
12 import os
13 import threading
14 import time
15 from lib389.monitor import *
16 from lib389.backend import Backends, DatabaseConfig
17 from lib389._constants import *
18 from test389.topologies import topology_st as topo
19 from lib389._mapped_object import DSLdapObjects
20 from lib389.utils import get_default_db_lib
21 from lib389.plugins import MemberOfPlugin
22 from lib389.idm.user import UserAccounts
23 from lib389.idm.group import Groups
24
25 pytestmark = pytest.mark.tier1
26
27 DEBUGGING = os.getenv("DEBUGGING", default=False)
28 if DEBUGGING:
29 logging.getLogger(__name__).setLevel(logging.DEBUG)
30 else:
31 logging.getLogger(__name__).setLevel(logging.INFO)
32 log = logging.getLogger(__name__)
33
34
35 def test_monitor(topo):
36 """This test is to display monitor attributes to check the performace
37
38 :id: f7c8a815-07cf-4e67-9574-d26a0937d3db
39 :setup: Single instance
40 :steps:
41 1. Get the cn=monitor connections attributes
42 2. Print connections attributes
43 3. Get the cn=monitor version
44 4. Print cn=monitor version
45 5. Get the cn=monitor threads attributes
46 6. Print cn=monitor threads attributes
47 7. Get cn=monitor backends attributes
48 8. Print cn=monitor backends attributes
49 9. Get cn=monitor operations attributes
50 10. Print cn=monitor operations attributes
51 11. Get cn=monitor statistics attributes
52 12. Print cn=monitor statistics attributes
53 :expectedresults:
54 1. cn=monitor attributes should be fetched and printed successfully.
55 """
56
57 #define the monitor object from Monitor class in lib389
58 monitor = Monitor(topo.standalone)
59
60 #get monitor connections
61 connections = monitor.get_connections()
62 log.info('connection: {0[0]}, currentconnections: {0[1]}, totalconnections: {0[2]}'.format(connections))
63
64 #get monitor version
65 version = monitor.get_version()
66 log.info('version :: %s' %version)
67
68 #get monitor threads
69 threads = monitor.get_threads()
70 log.info('threads: {0[0]},currentconnectionsatmaxthreads: {0[1]},maxthreadsperconnhits: {0[2]}'.format(threads))
71
72 #get monitor backends
73 backend = monitor.get_backends()
74 log.info('nbackends: {0[0]}, backendmonitordn: {0[1]}'.format(backend))
75
76 #get monitor operations
77 operations = monitor.get_operations()
78 log.info('opsinitiated: {0[0]}, opscompleted: {0[1]}'.format(operations))
79
80 #get monitor stats
81 stats = monitor.get_statistics()
82 log.info('dtablesize: {0[0]},readwaiters: {0[1]},entriessent: {0[2]},bytessent: {0[3]},currenttime: {0[4]},starttime: {0[5]}'.format(stats))
83
84
85 def test_monitor_ldbm(topo):
86 """This test is to check if we are getting the correct monitor entry
87
88 :id: e62ba369-32f5-4b03-8865-f597a5bb6a70
89 :setup: Single instance
90 :steps:
91 1. Get the backend library (bdb, ldbm, etc)
92 2. Get the database monitor
93 3. Check for expected attributes in output
94 4. Check for expected DB library specific attributes
95 :expectedresults:
96 1. Success
97 2. Success
98 3. Success
99 4. Success
100 """
101
102 # Are we using BDB?
103 db_config = DatabaseConfig(topo.standalone)
104 db_lib = db_config.get_db_lib()
105
106 # Get the database monitor entry
107 monitor = MonitorLDBM(topo.standalone).get_status()
108
109 # Check that known attributes exist (only NDN cache stats)
110 assert 'normalizeddncachehits' in monitor
111 # Check for library specific attributes
112 if db_lib == 'bdb':
113 assert 'dbcachehits' in monitor
114 assert 'nsslapd-db-configured-locks' in monitor
115 elif db_lib == 'mdb':
116 assert 'dbcachehits' not in monitor
117 else:
118 # Unknown - the server would probably fail to start but check it anyway
119 log.fatal(f'Unknown backend library: {db_lib}')
120 assert False
121
122
123 def test_monitor_backend(topo):
124 """This test is to check if we are getting the correct backend monitor entry
125
126 :id: 27b0534f-a18c-4c95-aa2b-936bc1886a7b
127 :setup: Single instance
128 :steps:
129 1. Get the backend library (bdb, ldbm, etc)
130 2. Get the backend monitor
131 3. Check for expected attributes in output
132 4. Check for expected DB library specific attributes
133 :expectedresults:
134 1. Success
135 2. Success
136 3. Success
137 4. Success
138 """
139
140 # Are we using BDB?
141 db_lib = topo.standalone.get_db_lib()
142
143 # Get the backend monitor
144 be = Backends(topo.standalone).list()[0]
145 monitor = be.get_monitor().get_status()
146
147 # Check for expected attributes
148 assert 'entrycachehits' in monitor
149
150 # Check for library specific attributes
151 if db_lib == 'bdb':
152 assert 'dncachehits' in monitor
153 assert 'dbfilename-0' in monitor
154 elif db_lib == 'mdb':
155 assert 'dbiname-1' in monitor
156 pass
157 else:
158 # Unknown - the server would probably fail to start but check it anyway
159 log.fatal(f'Unknown backend library: {db_lib}')
160 assert False
161
162
163 def test_num_subordinates_with_monitor_suffix(topo):
164 """This test is to compare the numSubordinates value on the root entry with the actual number of direct subordinate(s).
165
166 :id: fdcfe0ac-33c3-4252-bf38-79819ec58a51
167 :setup: Single instance
168 :steps:
169 1. Create sample entries and perform a search with basedn as cn=monitor, filter as "(objectclass=*)" and scope as base.
170 2. Extract the numSubordinates value.
171 3. Perform another search with basedn as cn=monitor, filter as "(\|(objectclass=*)(objectclass=ldapsubentry))" and scope as one.
172 4. Compare numSubordinates value with the number of sub-entries.
173 :expectedresults:
174 1. Success
175 2. Success
176 3. Success
177 4. Should be same
178 """
179
180 raw_objects = DSLdapObjects(topo.standalone, basedn='cn=monitor')
181 filter1 = raw_objects.filter("(objectclass=*)", scope=0)
182 num_subordinates_val = filter1[0].get_attr_val_int('numSubordinates')
183 filter2 = raw_objects.filter("(|(objectclass=*)(objectclass=ldapsubentry))",scope=1)
184 assert len(filter2) == num_subordinates_val
185
186
187 def test_monitor_connections(topo):
188 """Validates connection monitoring functionality.
189
190 :id: 34eb85c7-00cf-4e72-8467-a431040150a0
191 :setup: Standalone Instance
192 :steps:
193 1. Clone the DirSrv object to create a new connection handle.
194 2. Open a persistent connection to the server.
195 3. Query the 'cn=monitor' entry to fetch connection statistics.
196 4. Verify that the 'currentconnections' count is greater than 0.
197 5. Close the persistent connection.
198 :expectedresults:
199 1. The connection is established successfully.
200 2. The server's monitoring entry returns valid statistics.
201 3. The number of current connections reported is at least 1.
202 4. The connection is closed successfully.
203 """
204 inst1 = topo.standalone
205 monitor = Monitor(inst1)
206
207 conn = inst1.clone()
208 try:
209 conn.open()
210 log.info('LDAP connection established for monitoring check via conn.open()')
211
212 _, current_connections, _ = monitor.get_connections()
213 num_conns = int(current_connections[0])
214 log.info(f"Reported current connections: {num_conns}")
215
216 assert num_conns > 0
217
218 finally:
219 conn.close()
220 log.info("LDAP monitoring connection closed via conn.close()")
221
222
223 def test_monitor_work_queue(topo):
224 """Verify work queue metrics are exposed via cn=monitor
225
226 :id: 7e2f74ac-e662-4e70-b4f3-a010295d8b99
227 :setup: Standalone Instance
228 :steps:
229 1. Query cn=monitor for work queue attributes
230 2. Verify all 4 attributes are present and parseable as integers
231 3. Verify current values are non-negative
232 4. Verify max values >= current values
233 5. Generate some load and verify maxbusyworkers > 0
234 :expectedresults:
235 1. Success
236 2. Success
237 3. Success
238 4. Success
239 5. Success
240 """
241 inst = topo.standalone
242 monitor = Monitor(inst)
243
244 # Get work queue metrics
245 (currentworkqueue, maxworkqueue,
246 currentbusyworkers, maxbusyworkers) = monitor.get_work_queue()
247
248 # Verify all attributes are present and parseable as integers
249 cur_wq = int(currentworkqueue[0])
250 max_wq = int(maxworkqueue[0])
251 cur_bw = int(currentbusyworkers[0])
252 max_bw = int(maxbusyworkers[0])
253
254 log.info(f"currentworkqueue={cur_wq}, maxworkqueue={max_wq}, "
255 f"currentbusyworkers={cur_bw}, maxbusyworkers={max_bw}")
256
257 # Verify non-negative values
258 assert cur_wq >= 0, f"currentworkqueue should be >= 0, got {cur_wq}"
259 assert max_wq >= 0, f"maxworkqueue should be >= 0, got {max_wq}"
260 assert cur_bw >= 0, f"currentbusyworkers should be >= 0, got {cur_bw}"
261 assert max_bw >= 0, f"maxbusyworkers should be >= 0, got {max_bw}"
262
263 # Verify max >= current
264 assert max_wq >= cur_wq, f"maxworkqueue ({max_wq}) should be >= currentworkqueue ({cur_wq})"
265 assert max_bw >= cur_bw, f"maxbusyworkers ({max_bw}) should be >= currentbusyworkers ({cur_bw})"
266
267 # Generate some load - several searches to ensure at least one worker was busy
268 for _ in range(10):
269 inst.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(objectclass=*)')
270
271 # Re-check - maxbusyworkers should now be > 0
272 (_, _, _, maxbusyworkers) = monitor.get_work_queue()
273 max_bw = int(maxbusyworkers[0])
274 log.info(f"After load: maxbusyworkers={max_bw}")
275 assert max_bw > 0, f"maxbusyworkers should be > 0 after generating load, got {max_bw}"
276
277 # Also verify the attributes appear in get_status()
278 status = monitor.get_status()
279 assert 'currentworkqueue' in status
280 assert 'maxworkqueue' in status
281 assert 'currentbusyworkers' in status
282 assert 'maxbusyworkers' in status
283
284
285 def test_monitor_busy_workers_concurrent(topo):
286 """Verify currentbusyworkers increments under concurrent operations
287
288 :id: b86971df-3627-499a-975c-f46c47b199fc
289 :setup: Standalone Instance
290 :steps:
291 1. Record maxbusyworkers before load
292 2. Launch multiple threads performing searches concurrently
293 3. While threads are running, sample currentbusyworkers
294 4. Verify maxbusyworkers increased and currentbusyworkers
295 returns to a low value after threads finish
296 :expectedresults:
297 1. Success
298 2. Success
299 3. Success
300 4. Success
301 """
302 inst = topo.standalone
303 monitor = Monitor(inst)
304
305 NUM_THREADS = 10
306 SEARCHES_PER_THREAD = 50
307 errors = []
308 peak_busy = [0]
309 peak_lock = threading.Lock()
310
311 def search_worker():
312 """Perform repeated searches to keep a worker thread busy"""
313 try:
314 conn = ldap.initialize(inst.ldapuri)
315 conn.simple_bind_s(DN_DM, PW_DM)
316 for _ in range(SEARCHES_PER_THREAD):
317 conn.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE,
318 '(objectclass=*)')
319 conn.unbind_s()
320 except Exception as e:
321 errors.append(str(e))
322
323 def monitor_sampler(stop_event):
324 """Sample currentbusyworkers while load is running"""
325 while not stop_event.is_set():
326 try:
327 (_, _, currentbusyworkers, _) = monitor.get_work_queue()
328 val = int(currentbusyworkers[0])
329 with peak_lock:
330 if val > peak_busy[0]:
331 peak_busy[0] = val
332 except Exception:
333 pass
334
335 # Record baseline
336 (_, _, _, maxbusyworkers_before) = monitor.get_work_queue()
337 max_bw_before = int(maxbusyworkers_before[0])
338 log.info(f"Before concurrent load: maxbusyworkers={max_bw_before}")
339
340 # Start the monitor sampler
341 stop_event = threading.Event()
342 sampler = threading.Thread(target=monitor_sampler, args=(stop_event,))
343 sampler.start()
344
345 # Launch concurrent search threads
346 threads = []
347 for _ in range(NUM_THREADS):
348 t = threading.Thread(target=search_worker)
349 threads.append(t)
350 t.start()
351
352 for t in threads:
353 t.join()
354
355 # Stop the sampler
356 stop_event.set()
357 sampler.join()
358
359 assert not errors, f"Search threads reported errors: {errors}"
360
361 # Check that we observed concurrent busy workers
362 log.info(f"Peak sampled currentbusyworkers during load: {peak_busy[0]}")
363 assert peak_busy[0] > 0, \
364 "currentbusyworkers was never > 0 during concurrent load"
365
366 # Verify maxbusyworkers reflects the concurrent activity
367 (_, _, currentbusyworkers, maxbusyworkers) = monitor.get_work_queue()
368 max_bw_after = int(maxbusyworkers[0])
369 cur_bw_after = int(currentbusyworkers[0])
370 log.info(f"After concurrent load: maxbusyworkers={max_bw_after}, "
371 f"currentbusyworkers={cur_bw_after}")
372
373 assert max_bw_after > 0, \
374 f"maxbusyworkers should be > 0 after concurrent load, got {max_bw_after}"
375 assert max_bw_after >= max_bw_before, \
376 f"maxbusyworkers should not decrease: before={max_bw_before}, after={max_bw_after}"
377 assert cur_bw_after < NUM_THREADS, \
378 f"currentbusyworkers should drop back after load completes, got {cur_bw_after}"
379
380
381 @pytest.mark.skipif(get_default_db_lib() == "mdb", reason="Not supported over mdb")
382 def test_monitor_memberof(topo):
383 """Test MemberOf plugin deferred processing monitoring with real activity
384
385 :id: 1c4b382a-ebaf-4d80-9232-67e1b3ab58d4
386 :setup: Single instance
387 :steps:
388 1. Enable MemberOf plugin and verify default state
389 2. Verify monitoring unavailable when deferred disabled
390 3. Enable deferred processing and verify monitor creation
391 4. Generate memberof activity and verify stats update
392 5. Disable deferred processing and verify monitor cleanup
393 :expectedresults:
394 1. Plugin enabled, deferred setting is None by default
395 2. ValueError raised for unavailable monitoring
396 3. Monitor accessible with expected attributes
397 4. TotalAdded counter increases with activity
398 5. Monitoring becomes unavailable after disable
399 """
400 inst = topo.standalone
401 memberof = MemberOfPlugin(inst)
402 memberof.enable()
403
404 # Verify plugin is enabled
405 enabled = memberof.get_attr_val_utf8('nsslapd-pluginEnabled')
406 assert enabled
407
408 # Verify deferred processing is disabled by default (None = unset)
409 deferred = memberof.get_memberofdeferredupdate()
410 assert deferred is None
411
412 # Verify monitoring not available when deferred disabled
413 with pytest.raises(ValueError, match="not available"):
414 MonitorMemberOf(inst).get_status()
415
416 # Enable deferred processing
417 memberof.set_memberofdeferredupdate('on')
418 inst.restart()
419
420 # Verify deferred processing is now enabled
421 deferred = memberof.get_memberofdeferredupdate()
422 assert deferred and deferred.lower() == "on"
423
424 # Verify monitor is now accessible
425 monitor = MonitorMemberOf(inst)
426 stats = monitor.get_status()
427 assert 'CurrentTasks' in stats
428 assert 'TotalAdded' in stats
429 assert 'TotalRemoved' in stats
430 assert 'ThreadStatus' in stats
431
432 # Get baseline stats before generating activity
433 added_before = int(stats.get('TotalAdded', ['0'])[0])
434
435 # Generate memberof activity to trigger deferred processing
436 users = UserAccounts(inst, DEFAULT_SUFFIX)
437 groups = Groups(inst, DEFAULT_SUFFIX)
438 user1 = users.create_test_user(uid=1000)
439 user2 = users.create_test_user(uid=1001)
440
441 # Creating group with members triggers deferred memberof processing
442 group = groups.create(properties={
443 'cn': 'testgroup',
444 'member': [user1.dn, user2.dn]
445 })
446
447 # Wait for deferred processing to complete
448 time.sleep(2)
449
450 # Verify stats were updated by the activity
451 updated_stats = monitor.get_status()
452 added_after = int(updated_stats.get('TotalAdded', ['0'])[0])
453
454 # Stats should definitely increase with activity
455 assert added_after > added_before, f"Expected TotalAdded to increase from {added_before}, got {added_after}"
456
457 # Clean up test data
458 group.delete()
459 user1.delete()
460 user2.delete()
461
462 # Disable deferred processing
463 memberof.set_memberofdeferredupdate('off')
464 inst.restart()
465
466 # Verify deferred processing is now disabled
467 deferred = memberof.get_memberofdeferredupdate()
468 assert deferred and deferred.lower() == "off"
469
470 # Verify monitoring is no longer available
471 with pytest.raises(ValueError, match="not available"):
472 monitor.get_status()
473
474
475 if __name__ == '__main__':
476 # Run isolated
477 # -s for DEBUG mode
478 CURRENT_FILE = os.path.realpath(__file__)
479 pytest.main("-s %s" % CURRENT_FILE)