Debian Codegraph @

dirsrvtests/tests/suites/clu/dsconf_config_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2025 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 subprocess
11 import logging
12 import pytest
13 from lib389._constants import DN_DM
14 from test389.topologies import topology_st
15
16 pytestmark = pytest.mark.tier1
17
18 log = logging.getLogger(__name__)
19
20 HAPROXY_IPS = {
21 'single': '192.168.1.1',
22 'multiple': ['10.0.0.1', '172.16.0.1', '192.168.2.1'],
23 'subnet_ipv4': '192.168.1.0/24',
24 'subnet_ipv6': '2001:db8::/32',
25 'mixed': ['192.168.1.0/24', '10.0.0.1', '2001:db8::/64']
26 }
27
28 REFERRALS = {
29 'single': 'ldap://primary.example.com',
30 'multiple': [
31 'ldap://server1.example.com',
32 'ldap://server2.example.com',
33 'ldap://server3.example.com'
34 ]
35 }
36
37 TEST_ATTRS = [
38 ('nsslapd-haproxy-trusted-ip', HAPROXY_IPS),
39 ('nsslapd-referral', REFERRALS)
40 ]
41
42
43 def execute_dsconf_command(dsconf_cmd, subcommands):
44 """Execute dsconf command and return output and return code"""
45
46 cmdline = dsconf_cmd + subcommands
47 proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
48 out, _ = proc.communicate()
49 return out.decode('utf-8'), proc.returncode
50
51
52 def get_dsconf_base_cmd(topology):
53 """Return base dsconf command list"""
54
55 return ['/usr/sbin/dsconf', topology.standalone.serverid,
56 '-D', DN_DM, '-w', 'password']
57
58
59 @pytest.mark.parametrize("attr_name,values_dict", TEST_ATTRS)
60 def test_single_value_add(topology_st, attr_name, values_dict):
61 """Test adding a single value to an attribute
62
63 :id: ffc912a6-c188-413d-9c35-7f4b3774d946
64 :parametrized: yes
65 :setup: Standalone DS instance
66 :steps:
67 1. Add a single value to the specified attribute
68 2. Verify the value was added correctly
69 :expectedresults:
70 1. Command should execute successfully
71 2. Added value should be present in the configuration
72 """
73 dsconf_cmd = get_dsconf_base_cmd(topology_st)
74
75 try:
76 value = values_dict['single']
77 test_attr = f"{attr_name}={value}"
78
79 # Add single value
80 output, rc = execute_dsconf_command(dsconf_cmd, ['config', 'add', test_attr])
81 assert rc == 0
82
83 # Verify
84 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
85 assert value in output
86
87 finally:
88 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
89
90
91 @pytest.mark.parametrize("attr_name,values_dict", TEST_ATTRS)
92 def test_single_value_replace(topology_st, attr_name, values_dict):
93 """Test replacing a single value in configuration attributes
94
95 :id: 112e3e5e-8db8-4974-9ea4-ed789c2d02f2
96 :parametrized: yes
97 :setup: Standalone DS instance
98 :steps:
99 1. Add initial value to the specified attribute
100 2. Replace the value with a new one
101 3. Verify the replacement was successful
102 :expectedresults:
103 1. Initial value should be added successfully
104 2. Replace command should execute successfully
105 3. New value should be present and old value should be absent
106 """
107 dsconf_cmd = get_dsconf_base_cmd(topology_st)
108
109 try:
110 # Add initial value
111 value = values_dict['single']
112 test_attr = f"{attr_name}={value}"
113 execute_dsconf_command(dsconf_cmd, ['config', 'add', test_attr])
114
115 # Replace with new value
116 new_value = values_dict['multiple'][0]
117 replace_attr = f"{attr_name}={new_value}"
118 output, rc = execute_dsconf_command(dsconf_cmd, ['config', 'replace', replace_attr])
119 assert rc == 0
120
121 # Verify
122 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
123 assert new_value in output
124 assert value not in output
125
126 finally:
127 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
128
129
130 @pytest.mark.parametrize("attr_name,values_dict", TEST_ATTRS)
131 def test_multi_value_batch_add(topology_st, attr_name, values_dict):
132 """Test adding multiple values in a single batch command
133
134 :id: 4c34c7f8-16cc-4ab6-938a-967537be5470
135 :parametrized: yes
136 :setup: Standalone DS instance
137 :steps:
138 1. Add multiple values to the attribute in a single command
139 2. Verify all values were added correctly
140 :expectedresults:
141 1. Batch add command should execute successfully
142 2. All added values should be present in the configuration
143 """
144 dsconf_cmd = get_dsconf_base_cmd(topology_st)
145
146 try:
147 # Add multiple values in one command
148 attr_values = [f"{attr_name}={val}" for val in values_dict['multiple']]
149 output, rc = execute_dsconf_command(dsconf_cmd, ['config', 'add'] + attr_values)
150 assert rc == 0
151
152 # Verify all values
153 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
154 for value in values_dict['multiple']:
155 assert value in output
156
157 finally:
158 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
159
160
161 @pytest.mark.parametrize("attr_name,values_dict", TEST_ATTRS)
162 def test_multi_value_batch_replace(topology_st, attr_name, values_dict):
163 """Test replacing with multiple values in a single batch command
164
165 :id: 05cf28b8-000e-4856-a10b-7e1df012737d
166 :parametrized: yes
167 :setup: Standalone DS instance
168 :steps:
169 1. Add initial single value
170 2. Replace with multiple values in a single command
171 3. Verify the replacement was successful
172 :expectedresults:
173 1. Initial value should be added successfully
174 2. Batch replace command should execute successfully
175 3. All new values should be present and initial value should be absent
176 """
177 dsconf_cmd = get_dsconf_base_cmd(topology_st)
178
179 try:
180 # Add initial value
181 initial_value = values_dict['single']
182 execute_dsconf_command(dsconf_cmd, ['config', 'add', f"{attr_name}={initial_value}"])
183
184 # Replace with multiple values
185 attr_values = [f"{attr_name}={val}" for val in values_dict['multiple']]
186 output, rc = execute_dsconf_command(dsconf_cmd, ['config', 'replace'] + attr_values)
187 assert rc == 0
188
189 # Verify
190 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
191 assert initial_value not in output
192 for value in values_dict['multiple']:
193 assert value in output
194
195 finally:
196 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
197
198
199 @pytest.mark.parametrize("attr_name,values_dict", TEST_ATTRS)
200 def test_multi_value_specific_delete(topology_st, attr_name, values_dict):
201 """Test deleting specific values from multi-valued attribute
202
203 :id: bb325c9a-eae8-438a-b577-bd63540b91cb
204 :parametrized: yes
205 :setup: Standalone DS instance
206 :steps:
207 1. Add multiple values to the attribute
208 2. Delete a specific value
209 3. Verify the deletion was successful
210 :expectedresults:
211 1. Multiple values should be added successfully
212 2. Specific delete command should execute successfully
213 3. Deleted value should be absent while others remain present
214 """
215 dsconf_cmd = get_dsconf_base_cmd(topology_st)
216
217 try:
218 # Add multiple values
219 attr_values = [f"{attr_name}={val}" for val in values_dict['multiple']]
220 execute_dsconf_command(dsconf_cmd, ['config', 'add'] + attr_values)
221
222 # Delete middle value
223 delete_value = values_dict['multiple'][1]
224 output, rc = execute_dsconf_command(dsconf_cmd,
225 ['config', 'delete', f"{attr_name}={delete_value}"])
226 assert rc == 0
227
228 # Verify remaining values
229 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
230 assert delete_value not in output
231 assert values_dict['multiple'][0] in output
232 assert values_dict['multiple'][2] in output
233
234 finally:
235 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
236
237
238 @pytest.mark.parametrize("attr_name,values_dict", TEST_ATTRS)
239 def test_multi_value_batch_delete(topology_st, attr_name, values_dict):
240 """Test deleting multiple values in a single batch command
241
242 :id: 4b105824-b060-4f83-97d7-001a01dba1a5
243 :parametrized: yes
244 :setup: Standalone DS instance
245 :steps:
246 1. Add multiple values to the attribute
247 2. Delete multiple values in a single command
248 3. Verify the batch deletion was successful
249 :expectedresults:
250 1. Multiple values should be added successfully
251 2. Batch delete command should execute successfully
252 3. Deleted values should be absent while others remain present
253 """
254 dsconf_cmd = get_dsconf_base_cmd(topology_st)
255
256 try:
257 # Add all values
258 attr_values = [f"{attr_name}={val}" for val in values_dict['multiple']]
259 execute_dsconf_command(dsconf_cmd, ['config', 'add'] + attr_values)
260
261 # Delete multiple values in one command
262 delete_values = [f"{attr_name}={val}" for val in values_dict['multiple'][:2]]
263 output, rc = execute_dsconf_command(dsconf_cmd, ['config', 'delete'] + delete_values)
264 assert rc == 0
265
266 # Verify
267 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
268 assert values_dict['multiple'][2] in output
269 assert values_dict['multiple'][0] not in output
270 assert values_dict['multiple'][1] not in output
271
272 finally:
273 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
274
275
276 @pytest.mark.parametrize("attr_name,values_dict", TEST_ATTRS)
277 def test_single_value_persists_after_restart(topology_st, attr_name, values_dict):
278 """Test single value persists after server restart
279
280 :id: be1a7e3d-a9ca-48a1-a3bc-062990d4f3e9
281 :parametrized: yes
282 :setup: Standalone DS instance
283 :steps:
284 1. Add single value to the attribute
285 2. Verify the value is present
286 3. Restart the server
287 4. Verify the value persists after restart
288 :expectedresults:
289 1. Value should be added successfully
290 2. Value should be present before restart
291 3. Server should restart successfully
292 4. Value should still be present after restart
293 """
294 dsconf_cmd = get_dsconf_base_cmd(topology_st)
295
296 try:
297 # Add single value
298 value = values_dict['single']
299 output, rc = execute_dsconf_command(dsconf_cmd,
300 ['config', 'add', f"{attr_name}={value}"])
301 assert rc == 0
302
303 # Verify before restart
304 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
305 assert value in output
306
307 # Restart the server
308 topology_st.standalone.restart(timeout=10)
309
310 # Verify after restart
311 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
312 assert value in output
313
314 finally:
315 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
316
317
318 @pytest.mark.parametrize("attr_name,values_dict", TEST_ATTRS)
319 def test_multi_value_batch_persists_after_restart(topology_st, attr_name, values_dict):
320 """Test multiple values added in batch persist after server restart
321
322 :id: fd0435e2-90b1-465a-8968-d3a375c8fb22
323 :parametrized: yes
324 :setup: Standalone DS instance
325 :steps:
326 1. Add multiple values in a single batch command
327 2. Verify all values are present
328 3. Restart the server
329 4. Verify all values persist after restart
330 :expectedresults:
331 1. Batch add command should execute successfully
332 2. All values should be present before restart
333 3. Server should restart successfully
334 4. All values should still be present after restart
335 """
336 dsconf_cmd = get_dsconf_base_cmd(topology_st)
337
338 try:
339 # Add multiple values
340 attr_values = [f"{attr_name}={val}" for val in values_dict['multiple']]
341 output, rc = execute_dsconf_command(dsconf_cmd, ['config', 'add'] + attr_values)
342 assert rc == 0
343
344 # Verify before restart
345 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
346 for value in values_dict['multiple']:
347 assert value in output
348
349 # Restart the server
350 topology_st.standalone.restart(timeout=10)
351
352 # Verify after restart
353 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
354 for value in values_dict['multiple']:
355 assert value in output
356
357 finally:
358 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
359
360
361 def test_haproxy_subnet_ipv4(topology_st):
362 """Test adding IPv4 subnet in CIDR notation to nsslapd-haproxy-trusted-ip
363
364 :id: 19456de2-06f6-4c6c-be00-b07f84ba1c18
365 :setup: Standalone DS instance
366 :steps:
367 1. Add an IPv4 subnet in CIDR notation
368 2. Verify the subnet was added correctly
369 :expectedresults:
370 1. Command should execute successfully
371 2. Subnet should be present in the configuration
372 """
373 dsconf_cmd = get_dsconf_base_cmd(topology_st)
374 attr_name = 'nsslapd-haproxy-trusted-ip'
375
376 try:
377 subnet = HAPROXY_IPS['subnet_ipv4']
378 test_attr = f"{attr_name}={subnet}"
379
380 # Add subnet
381 output, rc = execute_dsconf_command(dsconf_cmd, ['config', 'add', test_attr])
382 assert rc == 0
383
384 # Verify
385 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
386 assert subnet in output
387
388 finally:
389 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
390
391
392 def test_haproxy_subnet_ipv6(topology_st):
393 """Test adding IPv6 subnet in CIDR notation to nsslapd-haproxy-trusted-ip
394
395 :id: b0b8b546-979c-47a2-bc9a-a4c664be8eba
396 :setup: Standalone DS instance
397 :steps:
398 1. Add an IPv6 subnet in CIDR notation
399 2. Verify the subnet was added correctly
400 :expectedresults:
401 1. Command should execute successfully
402 2. Subnet should be present in the configuration
403 """
404 dsconf_cmd = get_dsconf_base_cmd(topology_st)
405 attr_name = 'nsslapd-haproxy-trusted-ip'
406
407 try:
408 subnet = HAPROXY_IPS['subnet_ipv6']
409 test_attr = f"{attr_name}={subnet}"
410
411 # Add subnet
412 output, rc = execute_dsconf_command(dsconf_cmd, ['config', 'add', test_attr])
413 assert rc == 0
414
415 # Verify
416 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
417 assert subnet in output
418
419 finally:
420 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
421
422
423 def test_haproxy_mixed_ips_and_subnets(topology_st):
424 """Test adding mixed individual IPs and subnets to nsslapd-haproxy-trusted-ip
425
426 :id: d4f70db4-8f32-4771-95c2-32e6c83838df
427 :setup: Standalone DS instance
428 :steps:
429 1. Add multiple values including both individual IPs and subnets
430 2. Verify all values were added correctly
431 :expectedresults:
432 1. Batch add command should execute successfully
433 2. All values should be present in the configuration
434 """
435 dsconf_cmd = get_dsconf_base_cmd(topology_st)
436 attr_name = 'nsslapd-haproxy-trusted-ip'
437
438 try:
439 # Add mixed values
440 attr_values = [f"{attr_name}={val}" for val in HAPROXY_IPS['mixed']]
441 output, rc = execute_dsconf_command(dsconf_cmd, ['config', 'add'] + attr_values)
442 assert rc == 0
443
444 # Verify all values
445 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
446 for value in HAPROXY_IPS['mixed']:
447 assert value in output
448
449 finally:
450 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
451
452
453 def test_haproxy_invalid_cidr_prefix(topology_st):
454 """Test that invalid CIDR prefix lengths are rejected
455
456 :id: cfcc9fcc-70e8-49a7-82ac-dcf5e1a0cca3
457 :setup: Standalone DS instance
458 :steps:
459 1. Try to add an IPv4 address with invalid prefix length (>32)
460 2. Try to add an IPv6 address with invalid prefix length (>128)
461 :expectedresults:
462 1. IPv4 command should fail
463 2. IPv6 command should fail
464 """
465 dsconf_cmd = get_dsconf_base_cmd(topology_st)
466 attr_name = 'nsslapd-haproxy-trusted-ip'
467
468 try:
469 # Test invalid IPv4 prefix
470 invalid_ipv4 = f"{attr_name}=192.168.1.0/33"
471 output, rc = execute_dsconf_command(dsconf_cmd, ['config', 'add', invalid_ipv4])
472 assert rc != 0 # Should fail
473
474 # Test invalid IPv6 prefix
475 invalid_ipv6 = f"{attr_name}=2001:db8::/129"
476 output, rc = execute_dsconf_command(dsconf_cmd, ['config', 'add', invalid_ipv6])
477 assert rc != 0 # Should fail
478
479 finally:
480 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])
481
482
483 def test_haproxy_subnet_persists_after_restart(topology_st):
484 """Test subnet configuration persists after server restart
485
486 :id: 7ea0d55c-5345-465e-a33b-46f2e41a90cf
487 :setup: Standalone DS instance
488 :steps:
489 1. Add subnet in CIDR notation
490 2. Verify the subnet is present
491 3. Restart the server
492 4. Verify the subnet persists after restart
493 :expectedresults:
494 1. Subnet should be added successfully
495 2. Subnet should be present before restart
496 3. Server should restart successfully
497 4. Subnet should still be present after restart
498 """
499 dsconf_cmd = get_dsconf_base_cmd(topology_st)
500 attr_name = 'nsslapd-haproxy-trusted-ip'
501
502 try:
503 # Add subnet
504 subnet = HAPROXY_IPS['subnet_ipv4']
505 output, rc = execute_dsconf_command(dsconf_cmd,
506 ['config', 'add', f"{attr_name}={subnet}"])
507 assert rc == 0
508
509 # Verify before restart
510 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
511 assert subnet in output
512
513 # Restart the server
514 topology_st.standalone.restart(timeout=10)
515
516 # Verify after restart
517 output, _ = execute_dsconf_command(dsconf_cmd, ['config', 'get', attr_name])
518 assert subnet in output
519
520 finally:
521 execute_dsconf_command(dsconf_cmd, ['config', 'delete', attr_name])