| 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 time |
| 13 |
from lib389._constants import DEFAULT_SUFFIX |
| 14 |
from lib389.plugins import AutoMembershipPlugin, AutoMembershipDefinitions |
| 15 |
from lib389.idm.user import UserAccounts |
| 16 |
from lib389.idm.group import Groups |
| 17 |
from test389.topologies import topology_st as topo |
| 18 |
|
| 19 |
log = logging.getLogger(__name__) |
| 20 |
|
| 21 |
# maximum number of retries when rebuild task is too fast. |
| 22 |
MAX_TRIES = 10 |
| 23 |
|
| 24 |
@pytest.fixture(scope="module") |
| 25 |
def automember_fixture(topo, request): |
| 26 |
# Create group |
| 27 |
group_obj = Groups(topo.standalone, DEFAULT_SUFFIX) |
| 28 |
automem_group = group_obj.create(properties={'cn': 'testgroup'}) |
| 29 |
|
| 30 |
# Create users |
| 31 |
users = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn=None) |
| 32 |
NUM_USERS = 5000 |
| 33 |
for num in range(NUM_USERS): |
| 34 |
num_ran = int(round(num)) |
| 35 |
USER_NAME = 'test%05d' % num_ran |
| 36 |
users.create(properties={ |
| 37 |
'uid': USER_NAME, |
| 38 |
'sn': USER_NAME, |
| 39 |
'cn': USER_NAME, |
| 40 |
'uidNumber': '%s' % num_ran, |
| 41 |
'gidNumber': '%s' % num_ran, |
| 42 |
'homeDirectory': '/home/%s' % USER_NAME, |
| 43 |
'mail': '%[email protected]' % USER_NAME, |
| 44 |
'userpassword': 'pass%s' % num_ran, |
| 45 |
}) |
| 46 |
|
| 47 |
|
| 48 |
# Create automember definitions and regex rules |
| 49 |
automember_prop = { |
| 50 |
'cn': 'testgroup_definition', |
| 51 |
'autoMemberScope': DEFAULT_SUFFIX, |
| 52 |
'autoMemberFilter': 'objectclass=posixaccount', |
| 53 |
'autoMemberDefaultGroup': automem_group.dn, |
| 54 |
'autoMemberGroupingAttr': 'member:dn', |
| 55 |
} |
| 56 |
automembers = AutoMembershipDefinitions(topo.standalone) |
| 57 |
auto_def = automembers.create(properties=automember_prop) |
| 58 |
auto_def.add_regex_rule("regex1", automem_group.dn, include_regex=['uid=.*']) |
| 59 |
|
| 60 |
# Enable plugin |
| 61 |
automemberplugin = AutoMembershipPlugin(topo.standalone) |
| 62 |
automemberplugin.enable() |
| 63 |
topo.standalone.restart() |
| 64 |
|
| 65 |
|
| 66 |
def test_abort(automember_fixture, topo): |
| 67 |
"""Test the abort rebuild task |
| 68 |
|
| 69 |
:id: 24763279-48ec-4c34-91b3-f681679dec3a |
| 70 |
:setup: Standalone Instance |
| 71 |
:steps: |
| 72 |
1. Setup automember and create a bunch of users |
| 73 |
2. Start rebuild task |
| 74 |
3. Abort rebuild task |
| 75 |
4. Verify rebuild task was aborted |
| 76 |
:expectedresults: |
| 77 |
1. Success |
| 78 |
2. Success |
| 79 |
3. Success |
| 80 |
4. Success |
| 81 |
""" |
| 82 |
|
| 83 |
automemberplugin = AutoMembershipPlugin(topo.standalone) |
| 84 |
|
| 85 |
# Run rebuild task |
| 86 |
task_exit_code = '0' |
| 87 |
nbtries = 0 |
| 88 |
# Loops if previously run task has completed successfully |
| 89 |
while task_exit_code == '0': |
| 90 |
# ensure there is not too many loops. |
| 91 |
assert nbtries < MAX_TRIES |
| 92 |
nbtries += 1 |
| 93 |
# Start rebuild task |
| 94 |
task = automemberplugin.fixup(DEFAULT_SUFFIX, "objectclass=top") |
| 95 |
time.sleep(1) |
| 96 |
# Abort rebuild task |
| 97 |
automemberplugin.abort_fixup() |
| 98 |
task_exit_code = task.get_attr_val_utf8('nsTaskExitCode') |
| 99 |
|
| 100 |
# Wait for task completion |
| 101 |
task.wait() |
| 102 |
|
| 103 |
# Check errors log for abort message |
| 104 |
log.info('AUTOMEMBER REBUILD TASK: dn=%s %s' % (task.dn, task.get_all_attrs_utf8())) |
| 105 |
assert topo.standalone.searchErrorsLog("task was intentionally aborted") |
| 106 |
|
| 107 |
|
| 108 |
if __name__ == '__main__': |
| 109 |
# Run isolated |
| 110 |
# -s for DEBUG mode |
| 111 |
CURRENT_FILE = os.path.realpath(__file__) |
| 112 |
pytest.main(["-s", CURRENT_FILE]) |
| 113 |
|