Debian Codegraph @

dirsrvtests/tests/suites/clu/task_shutdown_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
10 import glob
11 import logging
12 import os
13
14 import ldap
15 import pytest
16
17 from lib389._constants import DEFAULT_SUFFIX
18 from lib389.dbgen import dbgen_users
19 from lib389.idm.account import Account
20 from lib389.plugins import (
21 AutoMembershipDefinitions,
22 MemberOfPlugin,
23 ReferentialIntegrityPlugin,
24 AutoMembershipPlugin,
25 USNPlugin,
26 )
27 from lib389.properties import TASK_WAIT
28 from lib389.tasks import Tasks
29 from test389.topologies import topology_st as topo
30
31 pytestmark = pytest.mark.tier0
32
33 log = logging.getLogger(__name__)
34
35 # Number of entries for import
36 NUM_IMPORT_ENTRIES = 10000
37
38
39 def _allow_core_dumps(inst):
40 """Ensure init config allows core dumps (ulimit / LimitCORE)."""
41 initconfig_dir = inst.get_initconfig_dir()
42 sysconfig_dirsrv = os.path.join(initconfig_dir, 'dirsrv')
43 sysconfig_systemd = sysconfig_dirsrv + '.systemd'
44
45 for path, pattern, line in [
46 (sysconfig_dirsrv, 'ulimit -c unlimited', 'ulimit -c unlimited\n'),
47 (sysconfig_systemd, 'LimitCORE=infinity', 'LimitCORE=infinity\n'),
48 ]:
49 if not os.path.isfile(path):
50 continue
51 with open(path, 'r') as f:
52 content = f.read()
53 if pattern not in content:
54 log.info(f'Adding {line.strip()} to {path}')
55 with open(path, 'a') as f:
56 f.write(line)
57
58 inst.restart(timeout=10)
59
60
61 def _assert_no_core(inst, label=''):
62 """Assert no core file in the instance log directory; move to /tmp if found."""
63 logdir = os.path.dirname(inst.errlog)
64 core_glob = os.path.join(logdir, 'core*')
65 cores = glob.glob(core_glob)
66 if cores:
67 mytmp = '/tmp'
68 for c in cores:
69 try:
70 os.rename(c, os.path.join(mytmp, os.path.basename(c) + '.' + (label or 'task_shutdown')))
71 except OSError:
72 pass
73 log.error(f'Core file(s) found in {logdir}; moved to {mytmp}. Test failed.')
74 pytest.fail(f'Core file generated (label: {label or "task_shutdown"})')
75 log.info(f'No core files found {(label or "ok")}')
76
77
78 @pytest.fixture(scope="function")
79 def task_shutdown_setup(topo):
80 """Allow core dumps, generate LDIF with dbgen, import, and return the instance."""
81 inst = topo.standalone
82 _allow_core_dumps(inst)
83
84 ldif_file = os.path.join(inst.get_ldif_dir(), 'task_shutdown.ldif')
85 if os.path.exists(ldif_file):
86 os.remove(ldif_file)
87
88 dbgen_users(inst, NUM_IMPORT_ENTRIES, ldif_file, DEFAULT_SUFFIX, generic=True)
89 assert os.path.isfile(ldif_file), 'dbgen should create LDIF'
90
91 tasks = Tasks(inst)
92 tasks.importLDIF(DEFAULT_SUFFIX, None, ldif_file, {TASK_WAIT: True})
93 log.info('Import complete: %s', ldif_file)
94
95 return inst
96
97
98 def test_task_shutdown_memberof(task_shutdown_setup):
99 """Stop server during fixupMemberOf (no wait); assert no core.
100
101 :id: f47ac10b-58cc-4372-a567-0e02b2c3d4e5
102 :setup: Standalone instance
103 :steps:
104 1. Enable MemberOf and Referential Integrity plugins, restart
105 2. Start fixupMemberOf task with TASK_WAIT=False
106 3. Stop server, assert no core file, start server
107 4. Disable plugins, restart
108 :expectedresults:
109 1. Success
110 2. Success
111 3. No core file; server starts
112 4. Success
113 """
114 inst = task_shutdown_setup
115 MemberOfPlugin(inst).enable()
116 ReferentialIntegrityPlugin(inst).enable()
117 inst.restart(timeout=10)
118
119 Tasks(inst).fixupMemberOf(suffix=DEFAULT_SUFFIX, args={TASK_WAIT: False})
120 inst.stop(timeout=10)
121 _assert_no_core(inst, 'memberof')
122 inst.start(timeout=10)
123
124 ReferentialIntegrityPlugin(inst).disable()
125 MemberOfPlugin(inst).disable()
126 inst.restart(timeout=10)
127
128
129 def test_task_shutdown_automember(task_shutdown_setup):
130 """Stop server during automember rebuild/export/map (no wait); assert no core.
131
132 :id: a0b1c2d3-e4f5-46a7-b8c9-d0e1f2a3b4c5
133 :setup: Standalone instance
134 :steps:
135 1. Enable AutoMembership and Referential Integrity, add automember definition
136 2. automemberRebuild(TASK_WAIT=False), stop, assert no core, start
137 3. automemberExport(TASK_WAIT=False), stop, assert no core, start
138 4. automemberMap(TASK_WAIT=False), stop, assert no core, start
139 5. Disable referint and automember, restart
140 :expectedresults:
141 1. Success
142 2. No core file; server starts
143 3. No core file; server starts
144 4. No core file; server starts
145 5. Success
146 """
147 inst = task_shutdown_setup
148 AutoMembershipPlugin(inst).enable()
149 ReferentialIntegrityPlugin(inst).enable()
150
151 definitions = AutoMembershipDefinitions(inst)
152 definitions.ensure_state(
153 rdn='cn=group cfg',
154 properties={
155 'cn': 'group cfg',
156 'autoMemberScope': DEFAULT_SUFFIX,
157 'autoMemberFilter': 'objectclass=inetorgperson',
158 'autoMemberDefaultGroup': 'cn=group0,' + DEFAULT_SUFFIX,
159 'autoMemberGroupingAttr': 'uniquemember:dn',
160 },
161 )
162 inst.restart(timeout=10)
163
164 tasks = Tasks(inst)
165
166 tasks.automemberRebuild(suffix=DEFAULT_SUFFIX, args={TASK_WAIT: False})
167 inst.stop(timeout=10)
168 _assert_no_core(inst, 'automember_rebuild')
169 inst.start(timeout=10)
170
171 export_ldif = '/tmp/task_shutdown_automember_exported.ldif'
172 tasks.automemberExport(suffix=DEFAULT_SUFFIX, ldif_out=export_ldif, args={TASK_WAIT: False})
173 inst.stop(timeout=10)
174 _assert_no_core(inst, 'automember_export')
175 inst.start(timeout=10)
176
177 ldif_in = os.path.join(inst.get_ldif_dir(), 'task_shutdown.ldif')
178 map_ldif = '/tmp/task_shutdown_automember_map.ldif'
179 tasks.automemberMap(ldif_in=ldif_in, ldif_out=map_ldif, args={TASK_WAIT: False})
180 inst.stop(timeout=10)
181 _assert_no_core(inst, 'automember_map')
182 inst.start(timeout=10)
183
184 ReferentialIntegrityPlugin(inst).disable()
185 AutoMembershipPlugin(inst).disable()
186 inst.restart(timeout=10)
187
188
189 def test_task_shutdown_syntaxvalidate(task_shutdown_setup):
190 """Stop server during syntaxValidate (no wait); assert no core.
191
192 :id: b1c2d3e4-f5a6-47b8-c9d0-e1f2a3b4c5d6
193 :setup: Standalone instance
194 :steps:
195 1. Start syntaxValidate task with TASK_WAIT=False
196 2. Stop server, assert no core file, start server
197 :expectedresults:
198 1. Success
199 2. No core file; server starts
200 """
201 inst = task_shutdown_setup
202 Tasks(inst).syntaxValidate(suffix=DEFAULT_SUFFIX, args={TASK_WAIT: False})
203 inst.stop(timeout=10)
204 _assert_no_core(inst, 'syntaxvalidate')
205 inst.start(timeout=10)
206
207
208 def test_task_shutdown_usn(task_shutdown_setup):
209 """Stop server during USN tombstone cleanup (no wait); assert no core.
210
211 :id: c2d3e4f5-a6b7-48c9-d0e1-f2a3b4c5d6e7
212 :setup: Standalone instance
213 :steps:
214 1. Enable USN plugin, restart
215 2. Delete all inetOrgPerson entries
216 3. Start usnTombstoneCleanup with TASK_WAIT=False
217 4. Stop server, assert no core file, start server
218 5. Disable USN, restart
219 :expectedresults:
220 1. Success
221 2. Success
222 3. Success
223 4. No core file; server starts
224 5. Success
225 """
226 inst = task_shutdown_setup
227 USNPlugin(inst).enable()
228 inst.restart(timeout=10)
229
230 entries = inst.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(objectclass=inetorgperson)')
231 for ent in entries:
232 Account(inst, ent.dn).delete()
233
234 Tasks(inst).usnTombstoneCleanup(suffix=DEFAULT_SUFFIX, bename='userRoot', args={TASK_WAIT: False})
235 inst.stop(timeout=10)
236 _assert_no_core(inst, 'usn')
237 inst.start(timeout=10)
238
239 USNPlugin(inst).disable()
240 inst.restart(timeout=10)
241
242
243 def test_task_shutdown_schemareload(topo):
244 """Stop server during schemaReload (no wait); assert no core.
245
246 :id: d3e4f5a6-b7c8-49d0-e1f2-a3b4c5d6e7f8
247 :setup: Standalone instance
248 :steps:
249 1. Start schemaReload task with TASK_WAIT=False
250 2. Stop server, assert no core file, start server
251 :expectedresults:
252 1. Success
253 2. No core file; server starts
254 """
255 inst = topo.standalone
256 _allow_core_dumps(inst)
257
258 Tasks(inst).schemaReload(args={TASK_WAIT: False})
259 inst.stop(timeout=10)
260 _assert_no_core(inst, 'schema_reload')
261 inst.start(timeout=10)
262
263
264 if __name__ == '__main__':
265 import sys
266 pytest.main([__file__, '-s'] + sys.argv[1:])