| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2023 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 os |
| 11 |
import itertools |
| 12 |
import pytest |
| 13 |
import ldap |
| 14 |
from lib389._constants import SUFFIX |
| 15 |
from test389.topologies import topology_st as topo |
| 16 |
from lib389.replica import Replicas |
| 17 |
|
| 18 |
|
| 19 |
log = logging.getLogger(__name__) |
| 20 |
|
| 21 |
|
| 22 |
ROLE_TO_CONFIG = { |
| 23 |
"None" : {}, |
| 24 |
"supplier" : { |
| 25 |
"nsDS5Flags": 1, |
| 26 |
"nsDS5ReplicaType": 3, |
| 27 |
"nsDS5ReplicaId": 1, |
| 28 |
}, |
| 29 |
"hub" : { |
| 30 |
"nsDS5Flags": 1, |
| 31 |
"nsDS5ReplicaType": 2, |
| 32 |
"nsDS5ReplicaId": 65535, |
| 33 |
}, |
| 34 |
"consumer" : { |
| 35 |
"nsDS5Flags": 0, |
| 36 |
"nsDS5ReplicaType": 2, |
| 37 |
"nsDS5ReplicaId": 65535, |
| 38 |
}, |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
REPLICA_PROPERTIES = { |
| 43 |
'cn': 'replica', |
| 44 |
'nsDS5ReplicaRoot': SUFFIX, |
| 45 |
'nsDS5ReplicaBindDN': 'cn=replmgr,cn=config', |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
def verify_role(replicas, role): |
| 50 |
"""Verify that instance has the right replica attrbutes.""" |
| 51 |
log.info("Verify role '%s'", role) |
| 52 |
expected = ROLE_TO_CONFIG[role] |
| 53 |
rep = {} |
| 54 |
try: |
| 55 |
replica = replicas.get(SUFFIX) |
| 56 |
rep["nsDS5Flags"] = replica.get_attr_val_int("nsDS5Flags") |
| 57 |
rep["nsDS5ReplicaType"] = replica.get_attr_val_int("nsDS5ReplicaType") |
| 58 |
rep["nsDS5ReplicaId"] = replica.get_attr_val_int("nsDS5ReplicaId") |
| 59 |
except ldap.NO_SUCH_OBJECT: |
| 60 |
pass |
| 61 |
log.info('verify_role: role: %s expected: %s found: %s', role, expected, rep) |
| 62 |
assert rep == expected |
| 63 |
|
| 64 |
|
| 65 |
def config_role(replicas, role): |
| 66 |
"""Configure replica role.""" |
| 67 |
log.info("Set role to: '%s'", role) |
| 68 |
try: |
| 69 |
replica = replicas.get(SUFFIX) |
| 70 |
except ldap.NO_SUCH_OBJECT: |
| 71 |
replica = None |
| 72 |
properties = { key:str(val) for dct in (REPLICA_PROPERTIES, |
| 73 |
ROLE_TO_CONFIG[role]) for key,val in dct.items() } |
| 74 |
if replica: |
| 75 |
if role == "None": |
| 76 |
replica.delete() |
| 77 |
else: |
| 78 |
# Cannot use replica.ensure_state here because: |
| 79 |
# lib389 complains if nsDS5ReplicaRoot is not set |
| 80 |
# 389ds complains if nsDS5ReplicaRoot it is set |
| 81 |
# replica.ensure_state(rdn='cn=replica', properties=properties) |
| 82 |
mods = [ (key, str(val)) |
| 83 |
for key,val in ROLE_TO_CONFIG[role].items() |
| 84 |
if str(val).lower() != replica.get_attr_val_utf8_l(key) ] |
| 85 |
log.debug(f'replica.replace_many({mods})') |
| 86 |
replica.replace_many(*mods) |
| 87 |
elif role != "None": |
| 88 |
replicas.create(properties=properties) |
| 89 |
|
| 90 |
|
| 91 |
@pytest.mark.parametrize( |
| 92 |
"from_role,to_role", |
| 93 |
itertools.permutations( ("None", "supplier", "hub", "consumer" ) , 2 ) |
| 94 |
) |
| 95 |
def test_switching_roles(topo, from_role, to_role): |
| 96 |
"""Test all transitions between roles/ CONSUMER/HUB/SUPPLIER/NONE |
| 97 |
|
| 98 |
:id: 6e9a697b-d5a0-45ff-b9c7-5fa14ea0c102 |
| 99 |
:setup: Standalone Instance |
| 100 |
:steps: |
| 101 |
1. Set initial replica role |
| 102 |
2. Verify initial replica role |
| 103 |
3. Set final replica role |
| 104 |
4. Verify final replica role |
| 105 |
:expectedresults: |
| 106 |
1. No error |
| 107 |
2. No error |
| 108 |
3. No error |
| 109 |
4. No error |
| 110 |
""" |
| 111 |
|
| 112 |
inst = topo.standalone |
| 113 |
replicas = Replicas(inst) |
| 114 |
inst.start() |
| 115 |
config_role(replicas, from_role) |
| 116 |
verify_role(replicas, from_role) |
| 117 |
config_role(replicas, to_role) |
| 118 |
verify_role(replicas, to_role) |
| 119 |
|
| 120 |
|
| 121 |
if __name__ == '__main__': |
| 122 |
# Run isolated |
| 123 |
# -s for DEBUG mode |
| 124 |
CURRENT_FILE = os.path.realpath(__file__) |
| 125 |
pytest.main(["-s", CURRENT_FILE]) |