Debian Codegraph @

dirsrvtests/tests/suites/setup_ds/db_home_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2022 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 pytest
11
12 import os
13 from lib389.utils import *
14 from test389.topologies import topology_st as topo
15 from lib389._constants import *
16 from lib389.dseldif import *
17 from lib389.cli_conf.backend import *
18 from lib389.config import BDB_LDBMConfig
19 from .... conftest import get_rpm_version
20 from lib389.paths import DEFAULTS_PATH
21
22 # Check if we are in a container
23 container_result = subprocess.run(["systemd-detect-virt", "-c"], stdout=subprocess.PIPE)
24
25 pytestmark = [pytest.mark.tier1,
26 pytest.mark.skipif(get_default_db_lib() == "mdb", reason='dbhome is meaningless on lmdb'),
27 pytest.mark.skipif(get_rpm_version("selinux-policy") <= "3.14.3-79" or
28 get_rpm_version("selinux-policy") <= "34.1.19-1",
29 reason="Will fail because of incorrect selinux labels"),
30 pytest.mark.skipif(ds_is_older('1.4.3.28'), reason='Not implemented'),
31 pytest.mark.skipif(container_result.returncode == 0, reason='db_home_dir is in old location in container')]
32
33 DEBUGGING = os.getenv("DEBUGGING", default=False)
34 if DEBUGGING:
35 logging.getLogger(__name__).setLevel(logging.DEBUG)
36 else:
37 logging.getLogger(__name__).setLevel(logging.INFO)
38 log = logging.getLogger(__name__)
39
40 PREFIX = os.getenv('PREFIX')
41
42 def expected_dbhome_value(inst):
43 if os.getuid() != 0:
44 log.info('expected_dbhome_value: Non root install')
45 return inst.dbdir
46 if inst.is_in_container():
47 log.info('expected_dbhome_value: Container install')
48 return inst.db_dir
49 if PREFIX is not None:
50 log.info('expected_dbhome_value: Prefixed install')
51 return inst.db_dir
52 log.info('expected_dbhome_value: Standard install')
53 return '/dev/shm/slapd-{}'.format(inst.serverid)
54
55
56 def test_check_db_home_dir_in_config(topo):
57 """Test to check nsslapd-db-home-directory is set to /dev/shm/slapd-instance in cn=config
58
59 :id: 9a1d0fcf-ca31-4f60-8b31-4de495b0b3ce
60 :customerscenario: True
61 :setup: Standalone Instance
62 :steps:
63 1. Create instance
64 2. Check nsslapd-db-home-directory is set to /dev/shm/slapd-instance in cn=config
65 :expectedresults:
66 1. Success
67 2. Success
68 """
69
70 standalone = topo.standalone
71 bdb_ldbmconfig = BDB_LDBMConfig(standalone)
72 dbhome_value = expected_dbhome_value(standalone)
73
74 log.info('Check the config value of nsslapd-db-home-directory')
75 assert bdb_ldbmconfig.get_attr_val_utf8('nsslapd-db-home-directory') == dbhome_value
76
77
78 def test_check_db_home_dir_contents(topo):
79 """Test to check contents of /dev/shm/slapd-instance
80
81 :id: a2d36990-2bb6-46af-99ca-f0cb30e68460
82 :customerscenario: True
83 :setup: Standalone Instance
84 :steps:
85 1. Create instance
86 2. Check the directory /dev/shm/slapd-instance exists
87 3. Check the contents of /dev/shm/slapd-instance/
88 4. Check the contents of /dev/shm/slapd-instance/ are not present in var/lib/dirsrv/slapd-instance/db
89 :expectedresults:
90 1. Success
91 2. Success
92 3. Success
93 4. Success
94 """
95
96 standalone = topo.standalone
97 file_list = ['__db.001', '__db.002', '__db.003', 'DBVERSION']
98 dbhome_value = expected_dbhome_value(standalone)
99 old_dbhome = '/var/lib/dirsrv/slapd-{}/db'.format(standalone.serverid)
100 existing_files = list(next(os.walk(dbhome_value))[2])
101 try:
102 old_location_files = list(next(os.walk(old_dbhome))[2])
103 except StopIteration:
104 old_location_files = []
105 log.info(f'existing_files = {existing_files}')
106 log.info(f'old_location_files = {old_location_files}')
107
108 log.info('Check the directory exists')
109 assert os.path.exists(dbhome_value)
110
111 log.info('Check the files are present in /dev/shm/slapd-instance/')
112 for item in file_list:
113 assert item in existing_files
114
115 if dbhome_value != old_dbhome:
116 log.info('Check these files are not present in old location')
117 for item in file_list:
118 assert item not in old_location_files
119
120
121 def test_check_db_home_dir_in_dse(topo):
122 """Test to check nsslapd-db-home-directory is set to /dev/shm/slapd-instance in dse.ldif
123
124 :id: f25befd2-a57c-4365-8eaf-70ea5fb987ea
125 :customerscenario: True
126 :setup: Standalone Instance
127 :steps:
128 1. Create instance
129 2. Check nsslapd-db-home-directory is set to /dev/shm/slapd-instance in dse.ldif
130 :expectedresults:
131 1. Success
132 2. Success
133 """
134
135 standalone = topo.standalone
136 bdb_ldbmconfig = BDB_LDBMConfig(standalone)
137 dbhome_value = expected_dbhome_value(standalone)
138 dse_ldif = DSEldif(standalone)
139
140 log.info('Check value of nsslapd-db-home-directory in dse.ldif')
141 dse_value = dse_ldif.get(bdb_ldbmconfig.dn, 'nsslapd-db-home-directory', True)
142 assert dse_value == dbhome_value
143
144
145 def test_check_db_home_dir_in_defaults(topo):
146 """Test to check nsslapd-db-home-directory is set to /dev/shm/slapd-instance in defaults.inf file
147
148 :id: 995ef963-acb1-4210-887e-803fc63e716c
149 :customerscenario: True
150 :setup: Standalone Instance
151 :steps:
152 1. Create instance
153 2. Check nsslapd-db-home-directory is set to /dev/shm/slapd-instance in defaults.inf file
154 :expectedresults:
155 1. Success
156 2. Success
157 """
158
159 standalone = topo.standalone
160 def_val = expected_dbhome_value(standalone)
161 def_val = def_val.replace(f'slapd-{standalone.serverid}', 'slapd-{instance_name}')
162 dbhome_value = f'db_home_dir = {def_val}'
163
164 log.info('Get defaults.inf path')
165 def_loc = standalone.ds_paths._get_defaults_loc(DEFAULTS_PATH)
166
167 log.info('Check db_home value is /dev/shm/slapd-{instance_name} in defaults.inf')
168 with open(def_loc) as f:
169 assert dbhome_value in f.read()
170
171
172 def test_delete_db_home_dir(topo):
173 """Test to check behaviour when deleting contents of /dev/shm/slapd-instance/ and restarting the instance
174
175 :id: 07764487-4cb1-438f-a327-bba7d762fea3
176 :customerscenario: True
177 :setup: Standalone Instance
178 :steps:
179 1. Create instance
180 2. Delete contents of /dev/shm/slapd-instance
181 3. Restart instance
182 4. Check the contents of /dev/shm/slapd-instance are recreated
183 :expectedresults:
184 1. Success
185 2. Success
186 3. Success
187 4. Success
188 """
189
190 standalone = topo.standalone
191 file_list = ['__db.001', '__db.002', '__db.003', 'DBVERSION']
192 dbhome_value = expected_dbhome_value(standalone)
193 existing_files = list(next(os.walk(dbhome_value))[2])
194
195 log.info('Stop the instance')
196 standalone.stop()
197
198 log.info('Remove contents of /dev/shm/slapd-instance/')
199 for f in os.listdir(dbhome_value):
200 if f in file_list:
201 os.remove(os.path.join(dbhome_value, f))
202
203 if dbhome_value != standalone.dbdir:
204 expected_len = 0
205 expected_len_when_started = 0
206 else:
207 # Should have: 'log.0000000001', 'guardian', 'userRoot'
208 expected_len = 3
209 expected_len_when_started = expected_len -1 # No guardian when started
210
211 log.info('Check there are no unexpected files')
212 assert len(os.listdir(dbhome_value)) == expected_len
213
214 log.info('Restart the instance')
215 standalone.restart()
216
217 log.info('Check number of files')
218 assert len(os.listdir(dbhome_value)) == 4 + expected_len_when_started
219
220 log.info('Check the filenames')
221 for item in file_list:
222 assert item in existing_files
223
224
225 if __name__ == '__main__':
226 # Run isolated
227 # -s for DEBUG mode
228 CURRENT_FILE = os.path.realpath(__file__)
229 pytest.main("-s %s" % CURRENT_FILE)