| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2026 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 pytest |
| 12 |
import re |
| 13 |
import os |
| 14 |
from lib389.utils import * |
| 15 |
from lib389.cli_base import FakeArgs |
| 16 |
from test389.topologies import topology_st |
| 17 |
from lib389.cli_ctl.health import health_check_run |
| 18 |
from lib389.paths import Paths |
| 19 |
|
| 20 |
CMD_OUTPUT = 'No issues found.' |
| 21 |
JSON_OUTPUT = '[]' |
| 22 |
RET_CODE = 'DSTHPLE0001' |
| 23 |
|
| 24 |
log = logging.getLogger(__name__) |
| 25 |
p = Paths() |
| 26 |
|
| 27 |
|
| 28 |
def run_healthcheck_and_flush_log(topology, instance, searched_code=None, json=False, searched_code2=None, |
| 29 |
list_checks=False, list_errors=False, check=None, searched_list=None): |
| 30 |
# If we are using BDB as a backend, we will get error DSBLE0006 on new versions |
| 31 |
if ds_is_newer("3.0.0") and instance.get_db_lib() == 'bdb' and \ |
| 32 |
(searched_code is CMD_OUTPUT or searched_code is JSON_OUTPUT): |
| 33 |
searched_code = 'DSBLE0006' |
| 34 |
|
| 35 |
args = FakeArgs() |
| 36 |
args.instance = instance.serverid |
| 37 |
args.verbose = instance.verbose |
| 38 |
args.list_errors = list_errors |
| 39 |
args.list_checks = list_checks |
| 40 |
args.exclude_check = [] |
| 41 |
args.check = check |
| 42 |
args.dry_run = False |
| 43 |
args.json = json |
| 44 |
|
| 45 |
log.info('Use healthcheck with --json == {} option'.format(json)) |
| 46 |
health_check_run(instance, topology.logcap.log, args) |
| 47 |
|
| 48 |
if searched_list is not None: |
| 49 |
for item in searched_list: |
| 50 |
assert topology.logcap.contains(item) |
| 51 |
log.info('Healthcheck returned searched item: %s' % item) |
| 52 |
else: |
| 53 |
assert topology.logcap.contains(searched_code) |
| 54 |
log.info('Healthcheck returned searched code: %s' % searched_code) |
| 55 |
|
| 56 |
if searched_code2 is not None: |
| 57 |
assert topology.logcap.contains(searched_code2) |
| 58 |
log.info('Healthcheck returned searched code: %s' % searched_code2) |
| 59 |
|
| 60 |
log.info('Clear the log') |
| 61 |
topology.logcap.flush() |
| 62 |
|
| 63 |
|
| 64 |
def _set_thp_system_mode(mode): |
| 65 |
thp_path = '/sys/kernel/mm/transparent_hugepage/enabled' |
| 66 |
with open(thp_path, 'w') as f: |
| 67 |
log.info(f"Setting THP mode to {mode}") |
| 68 |
f.write(mode) |
| 69 |
|
| 70 |
|
| 71 |
def _set_thp_instance_mode(inst, disable: bool): |
| 72 |
service_config = f"[Service]\nEnvironment=THP_DISABLE={int(disable)}" |
| 73 |
drop_in_path = f"/etc/systemd/system/dirsrv@{inst.serverid}.service.d/" |
| 74 |
os.makedirs(drop_in_path, exist_ok=True) |
| 75 |
with open(os.path.join(drop_in_path, "thp.conf"), 'w') as f: |
| 76 |
f.write(service_config) |
| 77 |
subprocess.run(['systemctl', 'daemon-reload'], check=True) |
| 78 |
inst.restart() |
| 79 |
|
| 80 |
|
| 81 |
def _get_thp_system_mode(): |
| 82 |
thp_path = '/sys/kernel/mm/transparent_hugepage/enabled' |
| 83 |
enabled_value_pattern = r'\[([^\]]+)\]' |
| 84 |
with open(thp_path, 'r') as f: |
| 85 |
text = f.read().strip() |
| 86 |
mode = re.search(enabled_value_pattern, text)[1] |
| 87 |
log.info(f"Current THP mode is {mode}") |
| 88 |
return mode |
| 89 |
|
| 90 |
|
| 91 |
@pytest.fixture(scope="function") |
| 92 |
def thp_reset(request): |
| 93 |
mode = _get_thp_system_mode() |
| 94 |
|
| 95 |
def fin(): |
| 96 |
_set_thp_system_mode(mode) |
| 97 |
|
| 98 |
request.addfinalizer(fin) |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.skipif(get_user_is_root() is False, |
| 102 |
reason="This test requires root permissions to change kernel tunables") |
| 103 |
@pytest.mark.skipif(p.with_systemd is False, reason='Needs systemd to run') |
| 104 |
@pytest.mark.skipif(ds_is_older("2.5.0"), reason="Not implemented") |
| 105 |
@pytest.mark.parametrize("system_thp_mode,instance_thp_mode,expected_output", |
| 106 |
[("always", False, (RET_CODE, RET_CODE)), |
| 107 |
("always", True, (CMD_OUTPUT, JSON_OUTPUT)), |
| 108 |
("never", False, (CMD_OUTPUT, JSON_OUTPUT)), |
| 109 |
("never", True, (CMD_OUTPUT, JSON_OUTPUT))], |
| 110 |
ids=["System and Instance THP ON", |
| 111 |
"System THP ON, Instance THP OFF", |
| 112 |
"System THP OFF, Instance THP ON", |
| 113 |
"System THP OFF, Instance THP OFF"]) |
| 114 |
@pytest.mark.usefixtures("thp_reset") |
| 115 |
def test_healthcheck_transparent_huge_pages(topology_st, system_thp_mode, instance_thp_mode, expected_output): |
| 116 |
"""Check if HealthCheck returns DSTHPLE0001 code |
| 117 |
|
| 118 |
:id: 1f195e10-6403-4c92-8ac9-724b669e8cf2 |
| 119 |
:setup: Standalone instance |
| 120 |
:parametrized: yes |
| 121 |
:steps: |
| 122 |
1. Enable THP system wide and for the instance |
| 123 |
2. Use HealthCheck without --json option |
| 124 |
3. Use HealthCheck with --json option |
| 125 |
4. Enable THP system wide, disable THP for the instance |
| 126 |
5. Use HealthCheck without --json option |
| 127 |
6. Use HealthCheck with --json option |
| 128 |
7. Disable THP system wide, enable THP for the instance |
| 129 |
8. Use HealthCheck without --json option |
| 130 |
9. Use HealthCheck with --json option |
| 131 |
10. Disable THP system wide, disable THP for the instance |
| 132 |
11. Use HealthCheck without --json option |
| 133 |
12. Use HealthCheck with --json option |
| 134 |
:expectedresults: |
| 135 |
1. Success |
| 136 |
2. HealthCheck should return code DSTHPLE0001 |
| 137 |
3. HealthCheck should return code DSTHPLE0001 |
| 138 |
4. Success |
| 139 |
5. HealthCheck reports no issue found |
| 140 |
6. HealthCheck reports no issue found |
| 141 |
7. Success |
| 142 |
8. HealthCheck reports no issue found |
| 143 |
9. HealthCheck reports no issue found |
| 144 |
10. Success |
| 145 |
11. HealthCheck reports no issue found |
| 146 |
12. HealthCheck reports no issue found |
| 147 |
""" |
| 148 |
standalone = topology_st.standalone |
| 149 |
standalone.config.set("nsslapd-accesslog-logbuffering", "on") |
| 150 |
|
| 151 |
_set_thp_system_mode(system_thp_mode) |
| 152 |
_set_thp_instance_mode(standalone, instance_thp_mode) |
| 153 |
|
| 154 |
run_healthcheck_and_flush_log(topology_st, standalone, expected_output[0], json=False) |
| 155 |
run_healthcheck_and_flush_log(topology_st, standalone, expected_output[1], json=True) |
| 156 |
|
| 157 |
standalone.stop() |
| 158 |
|
| 159 |
try: |
| 160 |
run_healthcheck_and_flush_log(topology_st, standalone, expected_output[0], json=False) |
| 161 |
run_healthcheck_and_flush_log(topology_st, standalone, expected_output[1], json=True) |
| 162 |
finally: |
| 163 |
if not standalone.status(): |
| 164 |
standalone.start() |
| 165 |
|