Debian Codegraph @

dirsrvtests/tests/suites/clu/dbscan_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2024 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 os
11 import pytest
12 import re
13 import subprocess
14 import sys
15
16 from lib389 import DirSrv
17 from lib389._constants import DEFAULT_SUFFIX
18 from test389.topologies import topology_m2 as topo_m2
19 from lib389.cli_ctl.dblib import DbscanHelper
20 from lib389.idm.domain import Domain
21 from difflib import context_diff
22
23 # Metadata dbid key prefixes in replication changelog (dbscan.c ENTRY_COUNT_KEY etc.)
24 _CL_SPECIAL_DBID_PREFIXES = ('0000006f', '000000de', '0000014d')
25
26
27 def _first_real_changelog_dbid(stdout):
28 """Return the dbid key of the first non-metadata changelog record."""
29 for line in stdout.splitlines():
30 if line.startswith('dbid: '):
31 key = line[len('dbid: '):].strip()
32 if len(key) >= 8 and key[:8].lower() in _CL_SPECIAL_DBID_PREFIXES:
33 continue
34 return key
35 return None
36
37 pytestmark = pytest.mark.tier0
38
39 logging.getLogger(__name__).setLevel(logging.DEBUG)
40 log = logging.getLogger(__name__)
41
42 DEBUGGING = os.getenv("DEBUGGING", default=False)
43
44
45 def log_export_file(filename):
46 with open(filename, 'r') as file:
47 log.debug(f'=========== Dump of {filename} ================')
48 for line in file:
49 log.debug(line.rstrip('\n'))
50 log.debug(f'=========== Enf of {filename} =================')
51
52
53 @pytest.fixture(scope='module')
54 def helper(topo_m2, request):
55 inst = topo_m2.ms["supplier1"]
56 if sys.version_info < (3,5):
57 pytest.skip('requires python version >= 3.5')
58 dbsh = DbscanHelper(inst, log=log)
59 dbsh.resync()
60 if '--do-it' not in dbsh.options:
61 pytest.skip('Not supported with this dbscan version')
62 inst.stop()
63 return dbsh
64
65
66 def test_dbscan_changelog_key_lookup(helper, topo_m2):
67 """dbscan -k on the replication changelog returns exactly one record
68
69 :id: c3e8f1a2-9b4d-5e6f-a7b8-c9d0e1f2a3b4
70 :setup: Stopped supplier instance (restarted for LDAP mods)
71 :steps:
72 1. Start supplier1 and modify the suffix description five times
73 2. Stop supplier1 and run dbscan on the changelog without -k
74 3. Take the first real changelog dbid key from the dump
75 4. Run dbscan -k with that key (same value as the csn field)
76 :expectedresults:
77 1. Success
78 2. Success
79 3. A key is found
80 4. Return code 0 and exactly one dbid block is printed
81 """
82 inst = topo_m2.ms['supplier1']
83 dblib = helper.dblib
84
85 inst.start()
86 try:
87 domain = Domain(inst, DEFAULT_SUFFIX)
88 for i in range(5):
89 domain.replace('description', 'dbscan_k_test_%s' % i)
90 finally:
91 inst.stop()
92
93 helper.resync()
94 cldbi = helper.get_dbi('replication_changelog')
95 full = helper.dbscan(['-D', dblib, '-f', cldbi])
96 log.debug(full.stdout)
97 key = _first_real_changelog_dbid(full.stdout)
98 assert key is not None, 'No changelog dbid key found in full dbscan output'
99
100 one = helper.dbscan(['-D', dblib, '-f', cldbi, '-k', key])
101 dbid_lines = re.findall(r'^dbid:', one.stdout, re.MULTILINE)
102 assert len(dbid_lines) == 1, (
103 'dbscan -k should print exactly one changelog record, got %s' % len(dbid_lines)
104 )
105 assert key in one.stdout
106 assert '\tcsn: %s' % key in one.stdout
107
108
109 def test_dbscan_destructive_actions(helper, request):
110 """Test that dbscan remove/import actions
111
112 :id: f40b0c42-660a-11ef-9544-083a88554478
113 :setup: Stopped standalone instance
114 :steps:
115 1. Export cn instance with dbscan
116 2. Run dbscan --remove ...
117 3. Check the error message about missing --do-it
118 4. Check that cn instance is still present
119 5. Run dbscan -I import_file ...
120 6. Check it was properly imported
121 7. Check that cn instance is still present
122 8. Run dbscan --remove ... --doit
123 9. Check the error message about missing --do-it
124 10. Check that cn instance is still present
125 11. Run dbscan -I import_file ... --do-it
126 12. Check it was properly imported
127 13. Check that cn instance is still present
128 14. Export again the database
129 15. Check that content of export files are the same
130 :expectedresults:
131 1. Success
132 2. dbscan return code should be 1 (error)
133 3. Error message should be present
134 4. cn instance should be present
135 5. dbscan return code should be 1 (error)
136 6. Error message should be present
137 7. cn instance should be present
138 8. dbscan return code should be 0 (success)
139 9. Error message should not be present
140 10. cn instance should not be present
141 11. dbscan return code should be 0 (success)
142 12. Error message should not be present
143 13. cn instance should be present
144 14. Success
145 15. Export files content should be the same
146 """
147
148 # Export cn instance with dbscan
149 export_cn = f'{helper.ldif_dir}/dbscan_cn.data'
150 export_cn2 = f'{helper.ldif_dir}/dbscan_cn2.data'
151 cndbi = helper.get_dbi('replication_changelog')
152 inst = helper.inst
153 dblib = helper.dblib
154 exportok = False
155 def fin():
156 if exportok and os.path.exists(export_cn):
157 try:
158 helper.resync()
159 dbi = helper.get_dbi('replication_changelog')
160 except KeyError:
161 # Restore cn if it was exported successfully but does not exists any more
162 helper.dbscan(['-D', dblib, '-f', cndbi, '-I', export_cn, '--do-it'])
163 if not DEBUGGING:
164 os.remove(export_cn)
165 if os.path.exists(export_cn2) and not DEBUGGING:
166 os.remove(export_cn2)
167
168 fin()
169 request.addfinalizer(fin)
170 helper.dbscan(['-D', dblib, '-f', cndbi, '-X', export_cn])
171 exportok = True
172
173 expected_msg = "without specifying '--do-it' parameter."
174
175 # Run dbscan --remove ...
176 result = helper.dbscan(['-D', helper.dblib, '--remove', '-f', cndbi], expected_rc=1)
177
178 # Check the error message about missing --do-it
179 assert expected_msg in result.stdout
180
181 # Check that cn instance is still present
182 helper.resync()
183 assert helper.get_dbi('replication_changelog') == cndbi
184
185 # Run dbscan -I import_file ...
186 result = helper.dbscan(['-D', helper.dblib, '-f', cndbi, '-I', export_cn],
187 expected_rc=1)
188
189 # Check the error message about missing --do-it
190 assert expected_msg in result.stdout
191
192 # Check that cn instance is still present
193 helper.resync()
194 assert helper.get_dbi('replication_changelog') == cndbi
195
196 # Run dbscan --remove ... --doit
197 result = helper.dbscan(['-D', helper.dblib, '--remove', '-f', cndbi, '--do-it'],
198 expected_rc=0)
199
200 # Check the error message about missing --do-it
201 assert expected_msg not in result.stdout
202
203 # Check that cn instance is still present
204 helper.resync()
205 with pytest.raises(KeyError):
206 helper.get_dbi('replication_changelog')
207
208 # Run dbscan -I import_file ... --do-it
209 result = helper.dbscan(['-D', helper.dblib, '-f', cndbi,
210 '-I', export_cn, '--do-it'], expected_rc=0)
211
212 # Check the error message about missing --do-it
213 assert expected_msg not in result.stdout
214
215 # Check that cn instance is still present
216 helper.resync()
217 assert helper.get_dbi('replication_changelog') == cndbi
218
219 # Export again the database
220 helper.dbscan(['-D', dblib, '-f', cndbi, '-X', export_cn2])
221
222 # Check that content of export files are the same
223 with open(export_cn) as f1:
224 f1lines = f1.readlines()
225 with open(export_cn2) as f2:
226 f2lines = f2.readlines()
227 diffs = list(context_diff(f1lines, f2lines))
228 if len(diffs) > 0:
229 log.debug("Export file differences are:")
230 for d in diffs:
231 log.debug(d)
232 log_export_file(export_cn)
233 log_export_file(export_cn2)
234 assert diffs is None
235
236
237 def test_dbscan_changelog_dump(helper, request):
238 """Test that dbscan remove/import actions
239
240 :id: b6cf7922-d4c7-11ef-a028-482ae39447e5
241 :setup: Stopped standalone instance
242 :steps:
243 1. Export chanmgelog instance with dbscan
244 2. Check that replace kweyword is present in the changelog
245 :expectedresults:
246 1. Success
247 2. Success
248 """
249
250 # Export changelog with dbscan
251 cldbi = helper.get_dbi('replication_changelog')
252 inst = helper.inst
253 dblib = helper.dblib
254 exportok = False
255 result = helper.dbscan(['-D', dblib, '-f', cldbi])
256 log.info(result.stdout)
257 assert 'replace: description' in result.stdout
258
259
260 if __name__ == '__main__':
261 # Run isolated
262 # -s for DEBUG mode
263 CURRENT_FILE = os.path.realpath(__file__)
264 pytest.main("-s %s" % CURRENT_FILE)