| 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 |
import logging |
| 10 |
import pytest |
| 11 |
import os |
| 12 |
import time |
| 13 |
import datetime |
| 14 |
from lib389.utils import get_default_db_lib |
| 15 |
from lib389.tasks import DBCompactTask |
| 16 |
from lib389.backend import DatabaseConfig |
| 17 |
from test389.topologies import topology_m1 as topo |
| 18 |
from lib389.utils import ldap, ds_is_older |
| 19 |
from lib389.idm.user import UserAccounts |
| 20 |
from lib389._constants import DEFAULT_SUFFIX |
| 21 |
|
| 22 |
|
| 23 |
pytestmark = pytest.mark.tier2 |
| 24 |
log = logging.getLogger(__name__) |
| 25 |
|
| 26 |
|
| 27 |
def test_compact_db_task(topo): |
| 28 |
"""Test creation of dbcompact task is successful |
| 29 |
|
| 30 |
:id: 1b3222ef-a336-4259-be21-6a52f76e1859 |
| 31 |
:customerscenario: True |
| 32 |
:setup: Standalone Instance |
| 33 |
:steps: |
| 34 |
1. Create task |
| 35 |
2. Check task was successful |
| 36 |
3. Check errors log to show task was run |
| 37 |
4. Create task just for changelog |
| 38 |
:expectedresults: |
| 39 |
1. Success |
| 40 |
2. Success |
| 41 |
3. Success |
| 42 |
4. Success |
| 43 |
""" |
| 44 |
inst = topo.ms["supplier1"] |
| 45 |
|
| 46 |
task = DBCompactTask(inst) |
| 47 |
task.create() |
| 48 |
task.wait() |
| 49 |
assert task.get_exit_code() == 0 |
| 50 |
|
| 51 |
# Check errors log to make sure task actually compacted db |
| 52 |
assert inst.searchErrorsLog("Compacting databases") |
| 53 |
inst.deleteErrorLogs() |
| 54 |
|
| 55 |
# Create new task that only compacts changelog |
| 56 |
task = DBCompactTask(inst) |
| 57 |
task_properties = {'justChangelog': 'yes'} |
| 58 |
task.create(properties=task_properties) |
| 59 |
task.wait() |
| 60 |
assert task.get_exit_code() == 0 |
| 61 |
|
| 62 |
# On bdb, check errors log to make sure task only performed changelog compaction |
| 63 |
# Note: as mdb contains a single map file (the justChangelog flags has |
| 64 |
# no impact (and whole db is compacted)) |
| 65 |
if get_default_db_lib() == "bdb": |
| 66 |
assert inst.searchErrorsLog("Compacting DB") == False |
| 67 |
assert inst.searchErrorsLog("Compacting Replication Changelog") |
| 68 |
inst.deleteErrorLogs(restart=False) |
| 69 |
|
| 70 |
|
| 71 |
@pytest.mark.skipif(get_default_db_lib() == "mdb", reason="Not supported over mdb") |
| 72 |
def test_compaction_interval_and_time(topo): |
| 73 |
"""Test dbcompact is successful when nsslapd-db-compactdb-interval and nsslapd-db-compactdb-time is set |
| 74 |
|
| 75 |
:id: f361bee9-d7e7-4569-9255-d7b60dd9d92e |
| 76 |
:customerscenario: True |
| 77 |
:setup: Supplier Instance |
| 78 |
:steps: |
| 79 |
1. Configure compact interval and time |
| 80 |
2. Check compaction occurs as expected |
| 81 |
:expectedresults: |
| 82 |
1. Success |
| 83 |
2. Success |
| 84 |
""" |
| 85 |
|
| 86 |
inst = topo.ms["supplier1"] |
| 87 |
|
| 88 |
# Add and delete some entries so compaction has something to do |
| 89 |
log.info("Adding and deleting 100 users ...") |
| 90 |
users = UserAccounts(inst, DEFAULT_SUFFIX, rdn=None) |
| 91 |
for num in range(100): |
| 92 |
USER_NAME = f'test_{num}' |
| 93 |
user = users.create(properties={ |
| 94 |
'uid': USER_NAME, |
| 95 |
'sn': USER_NAME, |
| 96 |
'cn': USER_NAME, |
| 97 |
'uidNumber': f'{num}', |
| 98 |
'gidNumber': f'{num}', |
| 99 |
'description': f'Description for {USER_NAME}', |
| 100 |
'homeDirectory': f'/home/{USER_NAME}' |
| 101 |
}) |
| 102 |
user.delete() |
| 103 |
|
| 104 |
# Calculate the compaction time (1 minute from now) |
| 105 |
now = datetime.datetime.now() |
| 106 |
current_hour = now.hour |
| 107 |
current_minute = now.minute + 2 |
| 108 |
|
| 109 |
if current_minute >= 60: |
| 110 |
# handle time wrapping/rollover |
| 111 |
current_minute = current_minute - 60 |
| 112 |
# Bump to the next hour |
| 113 |
current_hour += 1 |
| 114 |
|
| 115 |
if current_hour < 10: |
| 116 |
hour = "0" + str(current_hour) |
| 117 |
else: |
| 118 |
hour = str(current_hour) |
| 119 |
if current_minute < 10: |
| 120 |
minute = "0" + str(current_minute) |
| 121 |
else: |
| 122 |
minute = str(current_minute) |
| 123 |
|
| 124 |
compact_time = hour + ":" + minute |
| 125 |
|
| 126 |
# Get number of seconds to wait before compaction should happen |
| 127 |
wait_seconds = 120 - now.second |
| 128 |
|
| 129 |
# Set compaction TOD |
| 130 |
log.debug("compact time: %s", compact_time) |
| 131 |
log.debug("now: %s", str(now)) |
| 132 |
config = DatabaseConfig(inst) |
| 133 |
config.set([('nsslapd-db-compactdb-interval', '45'), |
| 134 |
('nsslapd-db-compactdb-time', compact_time)]) |
| 135 |
inst.deleteErrorLogs(restart=True) |
| 136 |
|
| 137 |
# Check compaction occurred as expected |
| 138 |
time.sleep(25) |
| 139 |
assert not inst.searchErrorsLog("Compacting databases") |
| 140 |
|
| 141 |
# Make sure we can handle a restart correctly |
| 142 |
inst.stop() |
| 143 |
log.debug("sleeping for: %d", wait_seconds - 45) |
| 144 |
time.sleep(wait_seconds - 45) |
| 145 |
inst.start() |
| 146 |
time.sleep(17) |
| 147 |
|
| 148 |
now = datetime.datetime.now() |
| 149 |
log.debug("checking now: %s", str(now)) |
| 150 |
assert inst.searchErrorsLog("Compacting databases") |
| 151 |
inst.deleteErrorLogs(restart=False) |
| 152 |
|
| 153 |
|
| 154 |
@pytest.mark.skipif(ds_is_older("1.4.3.23"), reason="Not implemented") |
| 155 |
def test_no_compaction(topo): |
| 156 |
"""Test there is no compaction when nsslapd-db-compactdb-interval is set to 0 |
| 157 |
|
| 158 |
:id: 80fdb0e3-a70c-42ad-9841-eebb74287b19 |
| 159 |
:customerscenario: True |
| 160 |
:setup: Supplier Instance |
| 161 |
:steps: |
| 162 |
1. Configure nsslapd-db-compactdb-interval to 0 |
| 163 |
2. Check there is no compaction |
| 164 |
:expectedresults: |
| 165 |
1. Success |
| 166 |
2. Success |
| 167 |
""" |
| 168 |
|
| 169 |
inst = topo.ms["supplier1"] |
| 170 |
config = DatabaseConfig(inst) |
| 171 |
config.set([('nsslapd-db-compactdb-interval', '0'), ('nsslapd-db-compactdb-time', '00:01')]) |
| 172 |
inst.deleteErrorLogs() |
| 173 |
|
| 174 |
time.sleep(3) |
| 175 |
assert not inst.searchErrorsLog("Compacting databases") |
| 176 |
inst.deleteErrorLogs(restart=False) |
| 177 |
|
| 178 |
|
| 179 |
@pytest.mark.skipif(ds_is_older("1.4.3.23"), reason="Not implemented") |
| 180 |
def test_compaction_interval_invalid(topo): |
| 181 |
"""Test that invalid value is rejected for nsslapd-db-compactdb-interval |
| 182 |
|
| 183 |
:id: 408ee3ee-727c-4565-8b08-2e07d0c6f7d7 |
| 184 |
:customerscenario: True |
| 185 |
:setup: Supplier Instance |
| 186 |
:steps: |
| 187 |
1. Set nsslapd-db-compactdb-interval to 2147483650 |
| 188 |
2. Check exception message contains invalid value and no compaction occurred |
| 189 |
:expectedresults: |
| 190 |
1. Exception is raised |
| 191 |
2. Success |
| 192 |
""" |
| 193 |
|
| 194 |
inst = topo.ms["supplier1"] |
| 195 |
msg = 'value 2147483650 for attr nsslapd-db-compactdb-interval is greater than the maximum 2147483647' |
| 196 |
config = DatabaseConfig(inst) |
| 197 |
|
| 198 |
try: |
| 199 |
config.set([('nsslapd-db-compactdb-interval', '2147483650'), ('nsslapd-db-compactdb-time', '00:01')]) |
| 200 |
except ldap.UNWILLING_TO_PERFORM as e: |
| 201 |
log.info('Got expected error: {}'.format(str(e))) |
| 202 |
assert msg in str(e) |
| 203 |
time.sleep(3) |
| 204 |
assert not inst.searchErrorsLog("Compacting databases") |
| 205 |
inst.deleteErrorLogs(restart=False) |
| 206 |
|
| 207 |
|
| 208 |
if __name__ == '__main__': |
| 209 |
# Run isolated |
| 210 |
# -s for DEBUG mode |
| 211 |
CURRENT_FILE = os.path.realpath(__file__) |
| 212 |
pytest.main(["-s", CURRENT_FILE]) |
| 213 |
|