Debian Codegraph @

dirsrvtests/tests/suites/export/export_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2023 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 pytest
11 import subprocess
12 import ldap
13 from lib389.idm.organizationalunit import OrganizationalUnit
14 from test389.topologies import topology_st as topo
15 from lib389._constants import DEFAULT_SUFFIX, DEFAULT_BENAME
16 from lib389.utils import *
17 from lib389.paths import Paths
18 from lib389.cli_base import FakeArgs
19 from lib389.cli_ctl.dbtasks import dbtasks_db2ldif
20
21 pytestmark = pytest.mark.tier1
22
23
24 def run_db2ldif_and_clear_logs(topology, instance, backend, ldif, output_msg, encrypt=False, repl=False):
25 args = FakeArgs()
26 args.instance = instance.serverid
27 args.backend = backend
28 args.encrypted = encrypt
29 args.replication = repl
30 args.ldif = ldif
31
32 dbtasks_db2ldif(instance, topology.logcap.log, args)
33
34 log.info('checking output msg')
35 if not topology.logcap.contains(output_msg):
36 log.error('The output message is not the expected one')
37 assert False
38
39 log.info('Clear the log')
40 topology.logcap.flush()
41
42
43 @pytest.mark.skipif(ds_is_older("1.3.10", "1.4.2"), reason="Not implemented")
44 def test_dbtasks_db2ldif_with_non_accessible_ldif_file_path(topo):
45 """Export with dsctl db2ldif, giving a ldif file path which can't be accessed by the user (dirsrv by default)
46
47 :id: 511e7702-7685-4951-9966-38f402d6214b
48 :setup: Standalone Instance - entries imported in the db
49 :steps:
50 1. Stop the server
51 2. Launch db2ldif with an non accessible ldif file path
52 3. Catch the reported error code
53 4. Check that an appropriate error was returned
54 :expectedresults:
55 1. Operation successful
56 2. Operation properly fails, without crashing
57 3. An error code different from 139 (segmentation fault) should be reported
58 4. "location does not exist" is returned
59 """
60 export_ldif = '/tmp/nonexistent/export.ldif'
61
62 log.info("Stopping the instance...")
63 topo.standalone.stop()
64
65 log.info("Performing an offline export to a non accessible ldif file path - should fail properly")
66 expected_output="location does not exist"
67 with pytest.raises(ValueError) as e:
68 run_db2ldif_and_clear_logs(topo, topo.standalone, DEFAULT_BENAME, export_ldif, expected_output)
69 assert "location does not exist" in str(e.value)
70
71 log.info("Restarting the instance...")
72 topo.standalone.start()
73
74
75 @pytest.mark.skipif(ds_is_older("1.4.3.8"), reason="bz1806978 not fixed")
76 def test_db2ldif_cli_with_non_accessible_ldif_file_path(topo):
77 """Export with ns-slapd db2ldif, giving a ldif file path which can't be accessed by the user (dirsrv by default)
78
79 :id: ca91eda7-27b1-4750-a013-531a63d3f5b0
80 :setup: Standalone Instance - entries imported in the db
81 :steps:
82 1. Stop the server
83 2. Launch db2ldif with an non accessible ldif file path
84 3. Catch the reported error code
85 4. Check that an appropriate error was returned
86 :expectedresults:
87 1. Operation successful
88 2. Operation properly fails, without crashing
89 3. An error code different from 139 (segmentation fault) should be reported
90 4. "The LDIF file location does not exist" is returned
91 """
92 export_ldif = '/tmp/nonexistent/export.ldif'
93 db2ldif_cmd = os.path.join(topo.standalone.ds_paths.sbin_dir, 'dsctl')
94
95 log.info("Stopping the instance...")
96 topo.standalone.stop()
97
98 log.info("Performing an offline export to a non accessible ldif file path - should fail properly")
99 try:
100 subprocess.check_output([db2ldif_cmd, topo.standalone.serverid, 'db2ldif', 'userroot', export_ldif])
101 except subprocess.CalledProcessError as e:
102 if format(e.returncode) == '139':
103 log.error('db2ldif had a Segmentation fault (core dumped)')
104 assert False
105
106 log.info("Restarting the instance...")
107 topo.standalone.start()
108
109
110 @pytest.mark.xfail(reason="bug 1860291")
111 @pytest.mark.skipif(ds_is_older("1.3.10", "1.4.2"), reason="Not implemented")
112 def test_dbtasks_db2ldif_with_non_accessible_ldif_file_path_output(topo):
113 """Export with db2ldif, giving a ldif file path which can't be accessed by the user (dirsrv by default)
114
115 :id: fcc63387-e650-40a7-b643-baa68c190037
116 :setup: Standalone Instance - entries imported in the db
117 :steps:
118 1. Stop the server
119 2. Launch db2ldif with a non accessible ldif file path
120 3. check the error reported in the command output
121 :expectedresults:
122 1. Operation successful
123 2. Operation properly fails
124 3. An clear error message is reported as output of the cli
125 """
126 export_ldif = '/tmp/nonexistent/export.ldif'
127
128 log.info("Stopping the instance...")
129 topo.standalone.stop()
130
131 log.info("Performing an offline export to a non accessible ldif file path - should fail and output a clear error message")
132 expected_output="No such file or directory"
133 run_db2ldif_and_clear_logs(topo, topo.standalone, DEFAULT_BENAME, export_ldif, expected_output)
134 # This test will possibly have to be updated with the error message reported after bz1860291 fix
135
136 log.info("Restarting the instance...")
137 topo.standalone.start()
138
139
140 def test_db2ldif_preserves_dn_case_in_exported_ldif(topo):
141 """Ensure db2ldif keeps the original DN case in exported LDIF.
142
143 :id: a3af2131-1134-43d2-9cf5-838bf53ec23f
144 :setup: Standalone Instance
145 :steps:
146 1. Add an organizational unit with DN "ou=test_dn_case,dc=EXAMPLE,dc=COM"
147 2. Stop the server
148 3. Perform offline db2ldif export
149 4. Verify the exported LDIF contains the DN with unchanged case
150 :expectedresults:
151 1. Entry is added successfully
152 2. Server stops cleanly
153 3. Offline export succeeds
154 4. DN case is preserved in the exported LDIF file
155 """
156 topo.standalone.start()
157
158 test_dn = "ou=test_dn_case,dc=EXAMPLE,dc=COM"
159 test_rdn_value = "test_dn_case"
160 export_ldif = os.path.join(topo.standalone.get_ldif_dir(), "export_dn_case.ldif")
161 test_ou = OrganizationalUnit(topo.standalone, test_dn)
162
163 log.info("Adding test entry with mixed-case DN suffix")
164 test_ou.create(properties={'ou': test_rdn_value})
165
166 log.info("Stopping the instance...")
167 topo.standalone.stop()
168 try:
169 log.info("Running offline db2ldif export")
170 assert topo.standalone.db2ldif(DEFAULT_BENAME, (DEFAULT_SUFFIX,), None, None, None, export_ldif)
171 finally:
172 log.info("Restarting the instance...")
173 topo.standalone.start()
174
175 log.info("Checking exported LDIF preserves the exact DN case")
176 with open(export_ldif, "r", encoding="utf-8") as export_file:
177 content = export_file.read()
178 assert f"dn: {test_dn}" in content
179
180 # Cleanup test artifacts
181 test_ou.delete()
182 if os.path.exists(export_ldif):
183 os.remove(export_ldif)