| 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 json |
| 10 |
import logging |
| 11 |
import re |
| 12 |
import time |
| 13 |
import ldap |
| 14 |
import pytest |
| 15 |
from lib389._constants import SUFFIX, ReplicaRole, DEFAULT_SUFFIX |
| 16 |
from test389.topologies import create_topology |
| 17 |
from lib389.replica import Agreements, ReplicationManager |
| 18 |
from lib389.schema import Schema |
| 19 |
from lib389.idm.user import UserAccounts |
| 20 |
from lib389.cli_base import LogCapture |
| 21 |
from lib389.utils import * |
| 22 |
|
| 23 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 24 |
log = logging.getLogger(__name__) |
| 25 |
|
| 26 |
def pattern_errorlog(file, log_pattern): |
| 27 |
"""Check for a pattern in the error log file.""" |
| 28 |
|
| 29 |
try: |
| 30 |
pattern_errorlog.last_pos += 1 |
| 31 |
except AttributeError: |
| 32 |
pattern_errorlog.last_pos = 0 |
| 33 |
|
| 34 |
found = None |
| 35 |
log.debug("_pattern_errorlog: start at offset %d" % pattern_errorlog.last_pos) |
| 36 |
file.seek(pattern_errorlog.last_pos) |
| 37 |
|
| 38 |
# Use a while true iteration because 'for line in file: hit a |
| 39 |
# python bug that break file.tell() |
| 40 |
while True: |
| 41 |
line = file.readline() |
| 42 |
log.debug("_pattern_errorlog: [%d] %s" % (file.tell(), line)) |
| 43 |
found = log_pattern.search(line) |
| 44 |
if ((line == '') or (found)): |
| 45 |
break |
| 46 |
|
| 47 |
log.debug("_pattern_errorlog: end at offset %d" % file.tell()) |
| 48 |
pattern_errorlog.last_pos = file.tell() |
| 49 |
return found |
| 50 |
|
| 51 |
|
| 52 |
def trigger_update(topology, user_rdn, num): |
| 53 |
"""It triggers an update on the supplier. This will start a replication |
| 54 |
session and a schema push |
| 55 |
""" |
| 56 |
|
| 57 |
users_s = UserAccounts(topology.ms["supplier1"], DEFAULT_SUFFIX) |
| 58 |
user = users_s.get(user_rdn) |
| 59 |
user.replace('telephonenumber', str(num)) |
| 60 |
|
| 61 |
# wait until the update is replicated (until up to x seconds) |
| 62 |
users_c = UserAccounts(topology.cs["consumer1"], DEFAULT_SUFFIX) |
| 63 |
for _ in range(30): |
| 64 |
try: |
| 65 |
user = users_c.get(user_rdn) |
| 66 |
val = user.get_attr_val_int('telephonenumber') |
| 67 |
if val == num: |
| 68 |
return |
| 69 |
# the expected value is not yet replicated. try again |
| 70 |
time.sleep(1) |
| 71 |
log.debug(f"trigger_update: receive {val} (expected {num})") |
| 72 |
except ldap.NO_SUCH_OBJECT: |
| 73 |
time.sleep(1) |
| 74 |
|
| 75 |
|
| 76 |
def trigger_schema_push(topology, user_rdn, num): |
| 77 |
"""Triggers a schema push from the supplier to the consumer or hub.""" |
| 78 |
|
| 79 |
supplier = topology['topology'].ms["supplier1"] |
| 80 |
if topology['type'] == "m1h1c1": |
| 81 |
consumer = topology['topology'].hs["hub1"] |
| 82 |
else: |
| 83 |
consumer = topology['topology'].cs["consumer1"] |
| 84 |
|
| 85 |
agreements = supplier.agreement.list(suffix=SUFFIX, |
| 86 |
consumer_host=consumer.host, |
| 87 |
consumer_port=consumer.port) |
| 88 |
assert (len(agreements) == 1) |
| 89 |
ra = agreements[0] |
| 90 |
trigger_update(topology['topology'], user_rdn, num) |
| 91 |
supplier.agreement.pause(ra.dn) |
| 92 |
supplier.agreement.resume(ra.dn) |
| 93 |
trigger_update(topology['topology'], user_rdn, num) |
| 94 |
|
| 95 |
|
| 96 |
def add_attributetype(inst, num, at_name, x_origin): |
| 97 |
"""Adds a new attribute type to the schema.""" |
| 98 |
|
| 99 |
schema = Schema(inst) |
| 100 |
# Add new attribute |
| 101 |
parameters = { |
| 102 |
'names': [at_name], |
| 103 |
'oid': str(9000 + num), |
| 104 |
'desc': 'Test extra parenthesis in X-ORIGIN', |
| 105 |
# 'x_origin': [x_origin], |
| 106 |
'x_origin': None, |
| 107 |
'syntax': '1.3.6.1.4.1.1466.115.121.1.15', |
| 108 |
'syntax_len': None, |
| 109 |
'x_ordered': None, |
| 110 |
'collective': None, |
| 111 |
'obsolete': None, |
| 112 |
'single_value': None, |
| 113 |
'no_user_mod': None, |
| 114 |
'equality': None, |
| 115 |
'substr': None, |
| 116 |
'ordering': None, |
| 117 |
'usage': None, |
| 118 |
'sup': None |
| 119 |
} |
| 120 |
schema.add_attributetype(parameters) |
| 121 |
|
| 122 |
|
| 123 |
@pytest.fixture(scope="function", params=["m1c1", "m1h1c1"]) |
| 124 |
def topology(request): |
| 125 |
"""Create Replication Deployment based on the params""" |
| 126 |
|
| 127 |
if request.param == "m1c1": |
| 128 |
topo_roles = {ReplicaRole.SUPPLIER: 1, ReplicaRole.CONSUMER: 1} |
| 129 |
elif request.param == "m1h1c1": |
| 130 |
topo_roles = {ReplicaRole.SUPPLIER: 1, ReplicaRole.HUB: 1, ReplicaRole.CONSUMER: 1} |
| 131 |
|
| 132 |
topology = create_topology(topo_roles, request=request) |
| 133 |
|
| 134 |
topology.logcap = LogCapture() |
| 135 |
return { |
| 136 |
'topology': topology, |
| 137 |
'type': request.param |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
@pytest.fixture(scope="function") |
| 142 |
def schema_replication_init(topology): |
| 143 |
"""Initialize the test environment """ |
| 144 |
|
| 145 |
supplier = topology['topology'].ms["supplier1"] |
| 146 |
supplier.errorlog_file = open(supplier.errlog, "r") |
| 147 |
users = UserAccounts(supplier, DEFAULT_SUFFIX) |
| 148 |
user = users.create_test_user() |
| 149 |
user.replace('telephonenumber', '0') |
| 150 |
|
| 151 |
return user |
| 152 |
|
| 153 |
|
| 154 |
@pytest.mark.parametrize("xorigin", ['user defined', 'custom xorigin']) |
| 155 |
def test_schema_xorigin_repl(topology, schema_replication_init, xorigin): |
| 156 |
"""Check consumer schema is a superset (one extra OC) of supplier schema, then |
| 157 |
schema is pushed and there is a message in the error log |
| 158 |
|
| 159 |
:id: 2b29823b-3e83-4b25-954a-8a081dbc15ee |
| 160 |
:parametrized: yes |
| 161 |
:setup: Supplier and consumer topology, with one user entry; |
| 162 |
Supplier, hub and consumer topology, with one user entry |
| 163 |
:steps: |
| 164 |
1. Push the schema from the supplier to the consumer (an error should not be generated) |
| 165 |
2. Update the schema of the consumer, so it will be a superset of the supplier's schema |
| 166 |
3. Update the schema of the supplier to make its nsSchemaCSN larger than the consumer's |
| 167 |
4. Push the schema from the supplier to the consumer (an error should be generated) |
| 168 |
5. Check if the supplier learns the missing definition |
| 169 |
6. Check the error logs for any issues |
| 170 |
7. Check the startup and final state of the schema replication process |
| 171 |
:expectedresults: |
| 172 |
1. The supplier's schema update should be successful |
| 173 |
2. The consumer's schema update should be successful |
| 174 |
3. The supplier's schema update should be successful |
| 175 |
4. The schema push operation should be successful |
| 176 |
5. The supplier should successfully learn the missing definition |
| 177 |
6. There should be no error messages in the logs |
| 178 |
7. The startup and final state of the schema replication process should be as expected |
| 179 |
""" |
| 180 |
|
| 181 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 182 |
user = schema_replication_init |
| 183 |
hub = None |
| 184 |
supplier = topology['topology'].ms["supplier1"] |
| 185 |
consumer = topology['topology'].cs["consumer1"] |
| 186 |
if topology['type'] == "m1h1c1": |
| 187 |
hub = topology['topology'].hs["hub1"] |
| 188 |
|
| 189 |
add_attributetype(supplier, 1, 'testAttribute', xorigin) |
| 190 |
|
| 191 |
# Search for attribute with JSON option |
| 192 |
schema = Schema(supplier) |
| 193 |
attr_result = schema.query_attributetype('testAttribute', json=True) |
| 194 |
# Verify the x-origin value is correct |
| 195 |
assert attr_result['at']['x_origin'][0] == "user defined" |
| 196 |
|
| 197 |
trigger_schema_push(topology, user.rdn, 1) |
| 198 |
repl.wait_for_replication(supplier, consumer) |
| 199 |
supplier_schema_csn = supplier.schema.get_schema_csn() |
| 200 |
consumer_schema_csn = consumer.schema.get_schema_csn() |
| 201 |
assert supplier_schema_csn == consumer_schema_csn |
| 202 |
|
| 203 |
# Check the error log of the supplier does not contain an error |
| 204 |
regex = re.compile(r"must not be overwritten \(set replication log for additional info\)") |
| 205 |
res = pattern_errorlog(supplier.errorlog_file, regex) |
| 206 |
if res is not None: |
| 207 |
assert False |
| 208 |
|
| 209 |
# add a new OC on the supplier so that its nsSchemaCSN is larger than the consumer (wait 2s) |
| 210 |
add_attributetype(consumer, 2, 'testAttributeCA', xorigin) |
| 211 |
time.sleep(2) |
| 212 |
add_attributetype(supplier, 3, 'testAttributeSA', xorigin) |
| 213 |
|
| 214 |
# now push the scheam |
| 215 |
trigger_schema_push(topology, user.rdn, 2) |
| 216 |
repl.wait_for_replication(supplier, consumer) |
| 217 |
supplier_schema_csn = supplier.schema.get_schema_csn() |
| 218 |
consumer_schema_csn = consumer.schema.get_schema_csn() |
| 219 |
assert supplier_schema_csn == consumer_schema_csn |
| 220 |
|
| 221 |
# Check the error log of the supplier does not contain an error |
| 222 |
# This message may happen during the learning phase |
| 223 |
regex = re.compile(r"must not be overwritten \(set replication log for additional info\)") |
| 224 |
pattern_errorlog(supplier.errorlog_file, regex) |
| 225 |
|
| 226 |
# Check that standard schema was not rewritten to be "user defined' on the consumer |
| 227 |
cn_attrs = json.loads(consumer.schema.query_attributetype("cn", json=True)) |
| 228 |
cn_attr = cn_attrs['at'] |
| 229 |
assert cn_attr['x_origin'][0].lower() != "user defined" |
| 230 |
if len(cn_attr['x_origin']) > 1: |
| 231 |
assert cn_attr['x_origin'][1].lower() != "user defined" |
| 232 |
|
| 233 |
# Check that the new OC "supplierNewOCB" was written to be "user defined' on the consumer |
| 234 |
ocs = json.loads(consumer.schema.query_attributetype("testAttributeSA", json=True)) |
| 235 |
new_oc = ocs['at'] |
| 236 |
assert new_oc['x_origin'][0].lower() == "user defined" |