| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2021 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 ldap |
| 10 |
import os |
| 11 |
import pytest |
| 12 |
from lib389._constants import DEFAULT_SUFFIX |
| 13 |
from test389.topologies import topology_st |
| 14 |
from lib389.idm.group import Groups |
| 15 |
from ldap.controls.psearch import PersistentSearchControl,EntryChangeNotificationControl |
| 16 |
|
| 17 |
pytestmark = pytest.mark.tier1 |
| 18 |
|
| 19 |
def _run_psearch(inst, msg_id): |
| 20 |
"""Run a search with EntryChangeNotificationControl""" |
| 21 |
|
| 22 |
results = [] |
| 23 |
while True: |
| 24 |
try: |
| 25 |
_, data, _, _, _, _ = inst.result4(msgid=msg_id, all=0, timeout=1.0, add_ctrls=1, add_intermediates=1, |
| 26 |
resp_ctrl_classes={EntryChangeNotificationControl.controlType:EntryChangeNotificationControl}) |
| 27 |
# See if there are any entry changes |
| 28 |
for dn, entry, srv_ctrls in data: |
| 29 |
ecn_ctrls = filter(lambda c: c.controlType == EntryChangeNotificationControl.controlType, srv_ctrls) |
| 30 |
if ecn_ctrls: |
| 31 |
inst.log.info('%s has changed!' % dn) |
| 32 |
results.append(dn) |
| 33 |
except ldap.TIMEOUT: |
| 34 |
# There are no more results, so we timeout. |
| 35 |
inst.log.info('No more results') |
| 36 |
return results |
| 37 |
|
| 38 |
|
| 39 |
def test_psearch(topology_st): |
| 40 |
"""Check basic Persistent Search control functionality |
| 41 |
|
| 42 |
:id: 4b395ef4-c3ff-49d1-a680-b9fdffa633bd |
| 43 |
:setup: Standalone instance |
| 44 |
:steps: |
| 45 |
1. Run an extended search with a Persistent Search control |
| 46 |
2. Create a new group (could be any entry) |
| 47 |
3. Run an extended search with a Persistent Search control again |
| 48 |
4. Check that entry DN is in the result |
| 49 |
:expectedresults: |
| 50 |
1. Operation should be successful |
| 51 |
2. Group should be successfully created |
| 52 |
3. Operation should be successful |
| 53 |
4. Entry DN should be in the result |
| 54 |
""" |
| 55 |
|
| 56 |
# Create the search control |
| 57 |
psc = PersistentSearchControl() |
| 58 |
# do a search extended with the control |
| 59 |
msg_id = topology_st.standalone.search_ext(base=DEFAULT_SUFFIX, scope=ldap.SCOPE_SUBTREE, attrlist=['*'], serverctrls=[psc]) |
| 60 |
# Get the result for the message id with result4 |
| 61 |
_run_psearch(topology_st.standalone, msg_id) |
| 62 |
# Change an entry / add one |
| 63 |
groups = Groups(topology_st.standalone, DEFAULT_SUFFIX) |
| 64 |
group = groups.create(properties={'cn': 'group1', 'description': 'testgroup'}) |
| 65 |
# Now run the result again and see what's there. |
| 66 |
results = _run_psearch(topology_st.standalone, msg_id) |
| 67 |
# assert our group is in the changeset. |
| 68 |
assert(group.dn == results[0]) |
| 69 |
|
| 70 |
|
| 71 |
if __name__ == '__main__': |
| 72 |
# Run isolated |
| 73 |
# -s for DEBUG mode |
| 74 |
CURRENT_FILE = os.path.realpath(__file__) |
| 75 |
pytest.main("-s %s" % CURRENT_FILE) |