| 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 pytest |
| 10 |
import ldap |
| 11 |
import logging |
| 12 |
import os |
| 13 |
from ldap.controls.vlv import VLVRequestControl |
| 14 |
from ldap.controls.sss import SSSRequestControl |
| 15 |
from test389.topologies import topology_st |
| 16 |
from lib389._constants import DEFAULT_SUFFIX |
| 17 |
from lib389.backend import Backends |
| 18 |
from lib389.index import VLVSearch, VLVIndex |
| 19 |
from lib389.idm.user import nsUserAccounts |
| 20 |
from lib389.tasks import Tasks |
| 21 |
from lib389.properties import TASK_WAIT |
| 22 |
|
| 23 |
pytestmark = pytest.mark.tier1 |
| 24 |
|
| 25 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 26 |
log = logging.getLogger(__name__) |
| 27 |
|
| 28 |
def create_vlv_search_and_index(inst, basedn=DEFAULT_SUFFIX, bename='userRoot', |
| 29 |
scope=ldap.SCOPE_SUBTREE, prefix="vlv", vlvsort="cn"): |
| 30 |
"""Create VLV search and index""" |
| 31 |
backends = Backends(inst) |
| 32 |
backend = backends.get(bename) |
| 33 |
|
| 34 |
vlv_props = { |
| 35 |
'cn': f'{prefix}Srch', |
| 36 |
'vlvbase': basedn, |
| 37 |
'vlvfilter': '(uid=*)', |
| 38 |
'vlvscope': str(scope) |
| 39 |
} |
| 40 |
backend.add_vlv_search(f'{prefix}Srch', vlv_props) |
| 41 |
|
| 42 |
vlv_search = VLVSearch(inst, dn=f'cn={prefix}Srch,cn={bename},' |
| 43 |
'cn=ldbm database,cn=plugins,cn=config') |
| 44 |
|
| 45 |
vlv_search.add_sort(f'{prefix}Idx', vlvsort) |
| 46 |
|
| 47 |
vlv_index = VLVIndex(inst, dn=f'cn={prefix}Idx,cn={prefix}Srch,' |
| 48 |
f'cn={bename},cn=ldbm database,cn=plugins,cn=config') |
| 49 |
|
| 50 |
log.info('VLV search and index created') |
| 51 |
return vlv_index |
| 52 |
|
| 53 |
def add_users(inst, users_num, suffix=DEFAULT_SUFFIX): |
| 54 |
"""Add test users for VLV testing.""" |
| 55 |
users = nsUserAccounts(inst, suffix) |
| 56 |
log.info(f'Creating {users_num} test users') |
| 57 |
starting_uid = 1000 |
| 58 |
|
| 59 |
for i in range(users_num): |
| 60 |
uid = starting_uid + i |
| 61 |
users.create_test_user(uid=uid, gid=uid) |
| 62 |
|
| 63 |
def check_vlv_search(inst): |
| 64 |
"""Test VLV search functionality.""" |
| 65 |
before_count = 1 |
| 66 |
after_count = 3 |
| 67 |
offset = 3501 |
| 68 |
|
| 69 |
vlv_control = VLVRequestControl( |
| 70 |
criticality=True, |
| 71 |
before_count=before_count, |
| 72 |
after_count=after_count, |
| 73 |
offset=offset, |
| 74 |
content_count=0 |
| 75 |
) |
| 76 |
sss_control = SSSRequestControl(criticality=True, ordering_rules=['cn']) |
| 77 |
|
| 78 |
result = inst.search_ext_s( |
| 79 |
base=DEFAULT_SUFFIX, |
| 80 |
scope=ldap.SCOPE_SUBTREE, |
| 81 |
filterstr='(uid=*)', |
| 82 |
serverctrls=[vlv_control, sss_control], |
| 83 |
escapehatch='i am sure' |
| 84 |
) |
| 85 |
|
| 86 |
log.info(f'VLV search returned {len(result)} entries') |
| 87 |
|
| 88 |
vlv_search_offset = 1000 - 2 |
| 89 |
imin = offset + vlv_search_offset - before_count |
| 90 |
|
| 91 |
for i, entry in enumerate(result, start=imin): |
| 92 |
expected_uid = f'uid=test_user_{i},' |
| 93 |
log.info(f'VLV entry: found {entry.dn}, expected to start with {expected_uid}') |
| 94 |
assert entry.dn.lower().startswith(expected_uid.lower()), \ |
| 95 |
f"VLV order incorrect: {entry.dn}" |
| 96 |
|
| 97 |
def test_vlv(topology_st): |
| 98 |
""" |
| 99 |
Testing bulk import when the backend with VLV was recreated. |
| 100 |
If the test passes without the server crash, 47966 is verified. |
| 101 |
|
| 102 |
:id: 512963fa-fe02-11e8-b1d3-8c16451d917b |
| 103 |
:setup: Standalone instance |
| 104 |
:steps: |
| 105 |
1. Add 5K users |
| 106 |
2. Generate vlvSearch entry |
| 107 |
3. Generate vlvIndex entry |
| 108 |
4. Reindex VLV with existing data |
| 109 |
5. Test VLV search functionality |
| 110 |
:expectedresults: |
| 111 |
1. Should succeed. |
| 112 |
2. Should succeed. |
| 113 |
3. Should succeed. |
| 114 |
4. Should succeed. |
| 115 |
5. Should succeed. |
| 116 |
""" |
| 117 |
inst = topology_st.standalone |
| 118 |
|
| 119 |
add_users(inst, 5000) |
| 120 |
|
| 121 |
vlv_index = create_vlv_search_and_index(inst) |
| 122 |
|
| 123 |
reindex_task = Tasks(inst) |
| 124 |
assert reindex_task.reindex(suffix=DEFAULT_SUFFIX, attrname=vlv_index.rdn, |
| 125 |
args={TASK_WAIT: True}, vlv=True) == 0 |
| 126 |
|
| 127 |
check_vlv_search(inst) |
| 128 |
|
| 129 |
|
| 130 |
if __name__ == "__main__": |
| 131 |
# Run isolated |
| 132 |
# -s for DEBUG mode |
| 133 |
CURRENT_FILE = os.path.realpath(__file__) |
| 134 |
pytest.main(["-s", CURRENT_FILE]) |