| 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 |
import logging |
| 10 |
import pytest |
| 11 |
import os |
| 12 |
import ldap |
| 13 |
from lib389.backend import Backends |
| 14 |
from lib389._constants import * |
| 15 |
from test389.topologies import topology_st |
| 16 |
from lib389.utils import ds_is_older, ensure_str, is_fips |
| 17 |
from subprocess import check_output |
| 18 |
|
| 19 |
pytestmark = pytest.mark.tier1 |
| 20 |
|
| 21 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 22 |
log = logging.getLogger(__name__) |
| 23 |
|
| 24 |
FD_ATTR = "nsslapd-maxdescriptors" |
| 25 |
RESRV_FD_ATTR = "nsslapd-reservedescriptors" |
| 26 |
SLAPD_DEFAULT_MAXDESCRIPTORS = 1048576 |
| 27 |
SYSTEMD_LIMIT = ensure_str(check_output("systemctl show -p LimitNOFILE dirsrv@standalone1".split(" ")).strip()).split('=')[1] |
| 28 |
# Server caps maxdescriptors at min(its own rlim_max, SLAPD_DEFAULT_MAXDESCRIPTORS); pytest's rlim_max is unrelated. |
| 29 |
if SYSTEMD_LIMIT == "infinity": |
| 30 |
MAX_FD_VAL = SLAPD_DEFAULT_MAXDESCRIPTORS |
| 31 |
else: |
| 32 |
MAX_FD_VAL = min(int(SYSTEMD_LIMIT), SLAPD_DEFAULT_MAXDESCRIPTORS) |
| 33 |
CUSTOM_VAL = str(MAX_FD_VAL - 10) |
| 34 |
RESRV_DESC_VAL_LOW = 10 |
| 35 |
TOO_HIGH_VAL = str(MAX_FD_VAL + 1) |
| 36 |
TOO_HIGH_VAL2 = str(MAX_FD_VAL + 1) |
| 37 |
TOO_LOW_VAL = "0" |
| 38 |
|
| 39 |
@pytest.mark.skipif(ds_is_older("1.4.1.2"), reason="Not implemented") |
| 40 |
def test_fd_limits(topology_st): |
| 41 |
"""Test the default limits, and custom limits |
| 42 |
|
| 43 |
:id: fa0a5106-612f-428f-84c0-9c85c34d0433 |
| 44 |
:setup: Standalone Instance |
| 45 |
:steps: |
| 46 |
1. Check default limit |
| 47 |
2. Change default limit |
| 48 |
3. Check invalid/too high limits are rejected |
| 49 |
4. Check invalid/too low limit is rejected |
| 50 |
:expectedresults: |
| 51 |
1. Success |
| 52 |
2. Success |
| 53 |
3. Success |
| 54 |
4. Success |
| 55 |
""" |
| 56 |
|
| 57 |
# Check systemd default |
| 58 |
max_fd = topology_st.standalone.config.get_attr_val_utf8(FD_ATTR) |
| 59 |
assert max_fd == str(MAX_FD_VAL) |
| 60 |
|
| 61 |
# Check custom value is applied |
| 62 |
topology_st.standalone.config.set(FD_ATTR, CUSTOM_VAL) |
| 63 |
max_fd = topology_st.standalone.config.get_attr_val_utf8(FD_ATTR) |
| 64 |
assert max_fd == CUSTOM_VAL |
| 65 |
|
| 66 |
# # Attempt to use value that is higher than the global system limit |
| 67 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 68 |
topology_st.standalone.config.set(FD_ATTR, TOO_HIGH_VAL) |
| 69 |
max_fd = topology_st.standalone.config.get_attr_val_utf8(FD_ATTR) |
| 70 |
assert max_fd == CUSTOM_VAL |
| 71 |
|
| 72 |
# Attempt to use value that is higher than the value defined in the systemd service |
| 73 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 74 |
topology_st.standalone.config.set(FD_ATTR, TOO_HIGH_VAL2) |
| 75 |
max_fd = topology_st.standalone.config.get_attr_val_utf8(FD_ATTR) |
| 76 |
assert max_fd == CUSTOM_VAL |
| 77 |
|
| 78 |
# Attempt to use val that is too low |
| 79 |
with pytest.raises(ldap.OPERATIONS_ERROR): |
| 80 |
topology_st.standalone.config.set(FD_ATTR, TOO_LOW_VAL) |
| 81 |
max_fd = topology_st.standalone.config.get_attr_val_utf8(FD_ATTR) |
| 82 |
assert max_fd == CUSTOM_VAL |
| 83 |
|
| 84 |
log.info("test_fd_limits PASSED") |
| 85 |
|
| 86 |
@pytest.mark.skipif(ds_is_older("1.4.1.2"), reason="Not implemented") |
| 87 |
def test_reserve_descriptor_validation(topology_st): |
| 88 |
"""Test the reserve descriptor self check |
| 89 |
|
| 90 |
:id: 9bacdbcc-7754-4955-8a56-1d8c82bce274 |
| 91 |
:setup: Standalone Instance |
| 92 |
:steps: |
| 93 |
1. Set attr nsslapd-reservedescriptors to a low value (10) |
| 94 |
2. Verify low value has been set |
| 95 |
3. Restart instance (On restart the reservedescriptor attr will be validated) |
| 96 |
4. Verify corrected value for nsslapd-reservedescriptors > low value |
| 97 |
:expectedresults: |
| 98 |
1. Success |
| 99 |
2. A value of RESRV_DESC_VAL_LOW (10) is returned |
| 100 |
3. Success |
| 101 |
4. Corrected value for nsslapd-reservedescriptors > low value |
| 102 |
""" |
| 103 |
|
| 104 |
# Set nsslapd-reservedescriptors to a low value (10) |
| 105 |
topology_st.standalone.config.set(RESRV_FD_ATTR, str(RESRV_DESC_VAL_LOW)) |
| 106 |
resrv_fd = int(topology_st.standalone.config.get_attr_val_utf8(RESRV_FD_ATTR)) |
| 107 |
assert resrv_fd == RESRV_DESC_VAL_LOW |
| 108 |
|
| 109 |
# An instance restart triggers a validation of the configured nsslapd-reservedescriptors attribute |
| 110 |
topology_st.standalone.restart() |
| 111 |
|
| 112 |
# Get the corrected value |
| 113 |
corrected_fd = int(topology_st.standalone.config.get_attr_val_utf8(RESRV_FD_ATTR)) |
| 114 |
assert corrected_fd > RESRV_DESC_VAL_LOW |
| 115 |
|
| 116 |
log.info(f"test_reserve_descriptor_validation PASSED (corrected from {RESRV_DESC_VAL_LOW} to {corrected_fd})") |
| 117 |
|
| 118 |
@pytest.mark.skipif(ds_is_older("1.4.1.2"), reason="Not implemented") |
| 119 |
def test_reserve_descriptors_high(topology_st): |
| 120 |
"""Test setting reserve descriptor value to higher than average. |
| 121 |
|
| 122 |
:id: 19c8991b-ef78-485e-bdf9-a0977fcbcd04 |
| 123 |
:setup: Standalone Instance |
| 124 |
:steps: |
| 125 |
1. Set attr nsslapd-maxdescriptors to the server's effective max |
| 126 |
2. Verify value has been set |
| 127 |
3. Set attr nsslapd-reservedescriptors to 2 less than nsslapd-maxdescriptors |
| 128 |
4. Verify value has been set |
| 129 |
5. Restart instance |
| 130 |
:expectedresults: |
| 131 |
1. Success |
| 132 |
2. Value of MAX_FD_VAL is returned |
| 133 |
3. Success |
| 134 |
4. Value of MAX_FD_VAL - 2 is returned |
| 135 |
5. Instance starts correctly |
| 136 |
""" |
| 137 |
|
| 138 |
# Set nsslapd-maxdescriptors to a custom value |
| 139 |
topology_st.standalone.config.set(FD_ATTR, str(MAX_FD_VAL)) |
| 140 |
max_fd = topology_st.standalone.config.get_attr_val_utf8(FD_ATTR) |
| 141 |
assert max_fd == str(MAX_FD_VAL) |
| 142 |
|
| 143 |
# Set nsslapd-reservedescriptors to 2 less than custom value |
| 144 |
topology_st.standalone.config.set(RESRV_FD_ATTR, str(MAX_FD_VAL - 2)) |
| 145 |
resrv_fd = topology_st.standalone.config.get_attr_val_utf8(RESRV_FD_ATTR) |
| 146 |
assert resrv_fd == str(MAX_FD_VAL - 2) |
| 147 |
|
| 148 |
# Verify instance restart |
| 149 |
topology_st.standalone.restart() |
| 150 |
assert (topology_st.standalone.status()) |
| 151 |
|
| 152 |
log.info("test_reserve_descriptors_high PASSED") |
| 153 |
|
| 154 |
if __name__ == '__main__': |
| 155 |
# Run isolated |
| 156 |
# -s for DEBUG mode |
| 157 |
CURRENT_FILE = os.path.realpath(__file__) |
| 158 |
pytest.main(["-s", CURRENT_FILE]) |