Debian Codegraph @

dirsrvtests/tests/suites/clu/dsctl_tasks_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 os
10 import subprocess
11 import uuid
12
13 import pytest
14
15 from lib389._constants import DEFAULT_BENAME, DEFAULT_SUFFIX, DN_DM
16 from test389.topologies import topology_st as topo
17 from lib389.dbgen import dbgen_users
18 from tempfile import TemporaryDirectory
19
20 DSCONF = '/usr/sbin/dsconf'
21 DSCTL = '/usr/sbin/dsctl'
22 WATCH_TEST_NUM_USERS = 20000
23
24
25 def _dsconf_base_cmd(inst):
26 return [DSCONF, inst.serverid, '-D', DN_DM, '-w', 'password']
27
28
29 def _run_dsconf(inst, argv, timeout=7200):
30 """Run dsconf; return (returncode, combined stdout+stderr)."""
31 cmd = _dsconf_base_cmd(inst) + argv
32 proc = subprocess.run(
33 cmd,
34 capture_output=True,
35 text=True,
36 timeout=timeout,
37 )
38 out = (proc.stdout or '') + (proc.stderr or '')
39 return proc.returncode, out
40
41
42 def _non_empty_lines(text):
43 return [ln for ln in text.splitlines() if ln.strip()]
44
45
46 def _dsctl_base_cmd(inst):
47 return [DSCTL, inst.serverid]
48
49
50 def _run_dsctl(inst, argv, timeout=7200):
51 """Run dsctl; return (returncode, combined stdout+stderr)."""
52 cmd = _dsctl_base_cmd(inst) + argv
53 proc = subprocess.run(
54 cmd,
55 capture_output=True,
56 text=True,
57 timeout=timeout,
58 )
59 out = (proc.stdout or '') + (proc.stderr or '')
60 return proc.returncode, out
61
62
63 def test_dsctl_task_watch_output(topo):
64 """dsctl db2ldif, ldif2db, db2bak, bak2db: ``--watch`` yields more output than default
65
66 Offline ns-slapd tools are run with ``-V`` when ``--watch`` is set; that verbose output is
67 logged at INFO so it is visible without ``dsctl -v``.
68
69 :id: 3f8a2c10-9e4b-4d7a-8f2e-1c0d5b6a7e9f
70 :setup: Standalone Instance (stopped for offline db utilities)
71 :steps:
72 1. Online-import a large LDIF via dsconf to populate the database
73 2. Stop the server; run ``db2ldif`` without then with ``--watch``
74 3. Run ``ldif2db`` twice with the same LDIF (without / with ``--watch``)
75 4. Run ``db2bak`` to two new archive paths (without / with ``--watch``)
76 5. Run ``bak2db`` from each archive (without / with ``--watch``)
77 6. Start the server
78 :expectedresults:
79 1. Each dsctl invocation succeeds (rc 0)
80 2. For each pair, the ``--watch`` run has strictly more non-empty output lines
81 """
82 inst = topo.standalone
83 tag = uuid.uuid4().hex[:12]
84 be = DEFAULT_BENAME
85
86 ldif_populate = os.path.join(inst.ldifdir, f'dsctl_watch_populate_{tag}.ldif')
87 dbgen_users(
88 inst,
89 WATCH_TEST_NUM_USERS,
90 ldif_populate,
91 DEFAULT_SUFFIX,
92 parent='ou=people,' + DEFAULT_SUFFIX,
93 generic=True,
94 )
95
96 rc, out = _run_dsconf(inst, ['backend', 'import', be, ldif_populate])
97 assert rc == 0, out
98
99 inst.stop()
100
101 try:
102 export_a = os.path.join(inst.ldifdir, f'dsctl_watch_db2ldif_a_{tag}.ldif')
103 export_b = os.path.join(inst.ldifdir, f'dsctl_watch_db2ldif_b_{tag}.ldif')
104
105 # db2ldif
106 rc, out_db2ldif_nowatch = _run_dsctl(inst, ['db2ldif', be, export_a])
107 assert rc == 0, out_db2ldif_nowatch
108 lines_db2ldif_nowatch = len(_non_empty_lines(out_db2ldif_nowatch))
109 assert "export userRoot: " not in out_db2ldif_nowatch, "'export userRoot: ' should not be in the output"
110
111 rc, out_db2ldif_watch = _run_dsctl(inst, ['db2ldif', be, export_b, '--watch'])
112 assert rc == 0, out_db2ldif_watch
113 lines_db2ldif_watch = len(_non_empty_lines(out_db2ldif_watch))
114 assert lines_db2ldif_watch > lines_db2ldif_nowatch, (
115 f'db2ldif --watch should log more lines (got {lines_db2ldif_watch} vs {lines_db2ldif_nowatch})'
116 )
117 assert "export userRoot: " in out_db2ldif_watch, "'export userRoot: ' should be in the output"
118
119 # ldif2db
120 rc, out_ldif2db_nowatch = _run_dsctl(inst, ['ldif2db', be, ldif_populate])
121 assert rc == 0, out_ldif2db_nowatch
122 lines_ldif2db_nowatch = len(_non_empty_lines(out_ldif2db_nowatch))
123 assert "import userRoot: Import complete" not in out_ldif2db_nowatch, "'import userRoot: Import complete' should not be in the output"
124
125 rc, out_ldif2db_watch = _run_dsctl(inst, ['ldif2db', be, ldif_populate, '--watch'])
126 assert rc == 0, out_ldif2db_watch
127 lines_ldif2db_watch = len(_non_empty_lines(out_ldif2db_watch))
128 assert lines_ldif2db_watch > lines_ldif2db_nowatch, (
129 f'ldif2db --watch should log more lines (got {lines_ldif2db_watch} vs {lines_ldif2db_nowatch})'
130 )
131 assert "import userRoot: Import complete" in out_ldif2db_watch, "'import userRoot: Import complete' should be in the output"
132
133 bak_a = os.path.join(inst.ds_paths.backup_dir, f'dsctl_watch_bak_a_{tag}')
134 bak_b = os.path.join(inst.ds_paths.backup_dir, f'dsctl_watch_bak_b_{tag}')
135
136 # db2bak
137 rc, out_db2bak_nowatch = _run_dsctl(inst, ['db2bak', bak_a])
138 assert rc == 0, out_db2bak_nowatch
139 lines_db2bak_nowatch = len(_non_empty_lines(out_db2bak_nowatch))
140 assert "archive_copyfile - Copying" not in out_db2bak_nowatch, "'archive_copyfile - Copying' should not be in the output"
141
142 rc, out_db2bak_watch = _run_dsctl(inst, ['db2bak', bak_b, '--watch'])
143 assert rc == 0, out_db2bak_watch
144 lines_db2bak_watch = len(_non_empty_lines(out_db2bak_watch))
145 assert lines_db2bak_watch > lines_db2bak_nowatch, (
146 f'db2bak --watch should log more lines (got {lines_db2bak_watch} vs {lines_db2bak_nowatch})'
147 )
148 assert "archive_copyfile - Copying" in out_db2bak_watch, "'archive_copyfile - Copying' should be in the output"
149
150 # bak2db
151 rc, out_bak2db_nowatch = _run_dsctl(inst, ['bak2db', bak_a])
152 assert rc == 0, out_bak2db_nowatch
153 lines_bak2db_nowatch = len(_non_empty_lines(out_bak2db_nowatch))
154 assert "- Copying" not in out_bak2db_nowatch, "'- Copying' should not be in the output"
155
156 rc, out_bak2db_watch = _run_dsctl(inst, ['bak2db', bak_b, '--watch'])
157 assert rc == 0, out_bak2db_watch
158 lines_bak2db_watch = len(_non_empty_lines(out_bak2db_watch))
159 assert lines_bak2db_watch > lines_bak2db_nowatch, (
160 f'bak2db --watch should log more lines (got {lines_bak2db_watch} vs {lines_bak2db_nowatch})'
161 )
162 assert "- Copying" in out_bak2db_watch, "'- Copying' should be in the output"
163
164 finally:
165 inst.start()
166
167
168 def generate_ldif_file(inst, ldif, perm):
169 dbgen_users(
170 inst,
171 WATCH_TEST_NUM_USERS/100,
172 ldif,
173 DEFAULT_SUFFIX,
174 parent='ou=people,' + DEFAULT_SUFFIX,
175 generic=True,
176 )
177 os.chown(ldif, 0, 0)
178 os.chmod(ldif, perm)
179
180
181 def check_import(inst, ldif, expected_error):
182 inst.log.info(f'Try importing {ldif} Expecting {expected_error}')
183 rc, out = _run_dsctl(inst, ['ldif2db', DEFAULT_BENAME, ldif])
184 inst.log.info(f'Get: {out}')
185 assert expected_error in out
186
187
188 @pytest.mark.skipif(os.getuid() != 0, reason="Test not run by root")
189 def test_dsctl_ldif2db_file_access(topo):
190 """Check that import file is readable
191
192 :id: 642c331e-4252-11f1-9fad-c85309d5c3e3
193 :setup: Standalone Instance (stopped for offline db utilities)
194 :steps:
195 1. Stop the server
196 2. Run ``ldif2db`` on a file with 0644 permission
197 3. Run ``ldif2db`` on a file with 0600 permission
198 4. Run ``ldif2db`` on a directory with 0711 permission
199 5. Run ``ldif2db`` on a directory with 0700 permission
200 6. Run ``ldif2db`` on a file within directory with 0700 permission
201 7. Start the server
202 :expectedresults:
203 1. Success
204 2. Success
205 3. dsctl should fail with 'User dirsrv cannot read file' error
206 4. dsctl should fail with "Can't find file" error
207 5. dsctl should fail with "Can't find file" error
208 6. dsctl should fail with 'User dirsrv cannot read file' error
209 7. Success
210 """
211 inst = topo.standalone
212
213 inst.stop()
214
215 try:
216 with TemporaryDirectory() as dir:
217 os.chmod(dir, 0o711)
218 d1 = f'{dir}/d1'
219 os.mkdir(d1)
220 os.chmod(d1, 0o700)
221 ldif1 = f'{dir}/db1.ldif'
222 ldif2 = f'{dir}/db2.ldif'
223 ldif3 = f'{d1}/db3.ldif'
224 generate_ldif_file(inst, ldif1, 0o644)
225 generate_ldif_file(inst, ldif2, 0o600)
226 generate_ldif_file(inst, ldif3, 0o644)
227 check_import(inst, ldif1, 'ldif2db successful')
228 check_import(inst, ldif2, 'ldif2db: User dirsrv cannot read')
229 check_import(inst, dir, "ldif2db: Can't find file")
230 check_import(inst, d1, "ldif2db: Can't find file")
231 check_import(inst, ldif3, 'ldif2db: User dirsrv cannot read')
232 finally:
233 inst.start()
234
235
236 if __name__ == '__main__':
237 CURRENT_FILE = os.path.realpath(__file__)
238 pytest.main(['-s', CURRENT_FILE])