| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2016 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 |
from collections import Counter |
| 10 |
|
| 11 |
import pytest |
| 12 |
from lib389.tasks import * |
| 13 |
from lib389.utils import * |
| 14 |
from test389.topologies import topology_m2 |
| 15 |
|
| 16 |
from lib389._constants import SUFFIX, DEFAULT_SUFFIX, ErrorLog |
| 17 |
|
| 18 |
from lib389.agreement import Agreements |
| 19 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 20 |
|
| 21 |
pytestmark = pytest.mark.tier1 |
| 22 |
|
| 23 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 24 |
log = logging.getLogger(__name__) |
| 25 |
|
| 26 |
installation1_prefix = None |
| 27 |
|
| 28 |
# Expected minimum and maximum number of async result in usual cases |
| 29 |
USUAL_MIN_AP = 2 |
| 30 |
USUAL_MAX_AP = 11 |
| 31 |
|
| 32 |
@pytest.fixture(params=[(None, (USUAL_MIN_AP, USUAL_MAX_AP)), |
| 33 |
('2000', (0, 2)), |
| 34 |
('0', (USUAL_MIN_AP, USUAL_MAX_AP)), |
| 35 |
('-5', (USUAL_MIN_AP, USUAL_MAX_AP))]) |
| 36 |
def waitfor_async_attr(topology_m2, request): |
| 37 |
"""Sets attribute on all replicas""" |
| 38 |
|
| 39 |
attr_value = request.param[0] |
| 40 |
expected_result = request.param[1] |
| 41 |
|
| 42 |
# Run through all suppliers |
| 43 |
|
| 44 |
for supplier in topology_m2.ms.values(): |
| 45 |
agmt = Agreements(supplier).list()[0] |
| 46 |
|
| 47 |
if attr_value: |
| 48 |
agmt.set_wait_for_async_results(attr_value) |
| 49 |
else: |
| 50 |
try: |
| 51 |
# Sometimes we can double remove this. |
| 52 |
agmt.remove_wait_for_async_results() |
| 53 |
except ldap.NO_SUCH_ATTRIBUTE: |
| 54 |
pass |
| 55 |
|
| 56 |
return (attr_value, expected_result) |
| 57 |
|
| 58 |
|
| 59 |
@pytest.fixture |
| 60 |
def entries(topology_m2, request): |
| 61 |
"""Adds entries to the supplier1""" |
| 62 |
|
| 63 |
supplier1 = topology_m2.ms["supplier1"] |
| 64 |
|
| 65 |
test_list = [] |
| 66 |
|
| 67 |
log.info("Add 100 nested entries under replicated suffix on %s" % supplier1.serverid) |
| 68 |
ous = OrganizationalUnits(supplier1, DEFAULT_SUFFIX) |
| 69 |
for i in range(100): |
| 70 |
ou = ous.create(properties={ |
| 71 |
'ou' : 'test_ou_%s' % i, |
| 72 |
}) |
| 73 |
test_list.append(ou) |
| 74 |
|
| 75 |
log.info("Delete created entries") |
| 76 |
for test_ou in test_list: |
| 77 |
test_ou.delete() |
| 78 |
|
| 79 |
def fin(): |
| 80 |
log.info("Clear the errors log in the end of the test case") |
| 81 |
with open(supplier1.errlog, 'w') as errlog: |
| 82 |
errlog.writelines("") |
| 83 |
|
| 84 |
request.addfinalizer(fin) |
| 85 |
|
| 86 |
|
| 87 |
def test_not_int_value(topology_m2): |
| 88 |
"""Tests not integer value |
| 89 |
|
| 90 |
:id: 67c9994f-9251-425a-8197-8d12ad9beafc |
| 91 |
:setup: Replication with two suppliers |
| 92 |
:steps: |
| 93 |
1. Try to set some string value |
| 94 |
to nsDS5ReplicaWaitForAsyncResults |
| 95 |
:expectedresults: |
| 96 |
1. Invalid syntax error should be raised |
| 97 |
""" |
| 98 |
supplier1 = topology_m2.ms["supplier1"] |
| 99 |
agmt = Agreements(supplier1).list()[0] |
| 100 |
|
| 101 |
with pytest.raises(ldap.INVALID_SYNTAX): |
| 102 |
agmt.set_wait_for_async_results("ws2") |
| 103 |
|
| 104 |
def test_multi_value(topology_m2): |
| 105 |
"""Tests multi value |
| 106 |
|
| 107 |
:id: 1932301a-db29-407e-b27e-4466a876d1d3 |
| 108 |
:setup: Replication with two suppliers |
| 109 |
:steps: |
| 110 |
1. Set nsDS5ReplicaWaitForAsyncResults to some int |
| 111 |
2. Try to add one more int value |
| 112 |
to nsDS5ReplicaWaitForAsyncResults |
| 113 |
:expectedresults: |
| 114 |
1. nsDS5ReplicaWaitForAsyncResults should be set |
| 115 |
2. Object class violation error should be raised |
| 116 |
""" |
| 117 |
|
| 118 |
supplier1 = topology_m2.ms["supplier1"] |
| 119 |
agmt = Agreements(supplier1).list()[0] |
| 120 |
|
| 121 |
agmt.set_wait_for_async_results('100') |
| 122 |
with pytest.raises(ldap.OBJECT_CLASS_VIOLATION): |
| 123 |
agmt.add('nsDS5ReplicaWaitForAsyncResults', '101') |
| 124 |
|
| 125 |
def test_value_check(topology_m2, waitfor_async_attr): |
| 126 |
"""Checks that value has been set correctly |
| 127 |
|
| 128 |
:id: 3e81afe9-5130-410d-a1bb-d798d8ab8519 |
| 129 |
:parametrized: yes |
| 130 |
:setup: Replication with two suppliers, |
| 131 |
wait for async set on all suppliers, try: |
| 132 |
None, '2000', '0', '-5' |
| 133 |
:steps: |
| 134 |
1. Search for nsDS5ReplicaWaitForAsyncResults on supplier 1 |
| 135 |
2. Search for nsDS5ReplicaWaitForAsyncResults on supplier 2 |
| 136 |
:expectedresults: |
| 137 |
1. nsDS5ReplicaWaitForAsyncResults should be set correctly |
| 138 |
2. nsDS5ReplicaWaitForAsyncResults should be set correctly |
| 139 |
""" |
| 140 |
|
| 141 |
attr_value = waitfor_async_attr[0] |
| 142 |
|
| 143 |
for supplier in topology_m2.ms.values(): |
| 144 |
agmt = Agreements(supplier).list()[0] |
| 145 |
|
| 146 |
server_value = agmt.get_wait_for_async_results_utf8() |
| 147 |
assert server_value == attr_value |
| 148 |
|
| 149 |
def test_behavior_with_value(topology_m2, waitfor_async_attr, entries): |
| 150 |
"""Tests replication behavior with valid |
| 151 |
nsDS5ReplicaWaitForAsyncResults attribute values |
| 152 |
|
| 153 |
:id: 117b6be2-cdab-422e-b0c7-3b88bbeec036 |
| 154 |
:parametrized: yes |
| 155 |
:setup: Replication with two suppliers, |
| 156 |
wait for async set on all suppliers, try: |
| 157 |
None, '2000', '0', '-5' |
| 158 |
:steps: |
| 159 |
1. Set Replication Debugging loglevel for the errorlog |
| 160 |
2. Gather all sync attempts, group by timestamp |
| 161 |
3. Take the most common timestamp and assert it has appeared |
| 162 |
in the set range |
| 163 |
:expectedresults: |
| 164 |
1. Replication Debugging loglevel should be set |
| 165 |
2. Operation should be successful |
| 166 |
3. Errors log should have all timestamp appear |
| 167 |
""" |
| 168 |
|
| 169 |
supplier1 = topology_m2.ms["supplier1"] |
| 170 |
supplier2 = topology_m2.ms["supplier2"] |
| 171 |
|
| 172 |
log.info("Set Replication Debugging loglevel for the errorlog") |
| 173 |
supplier1.config.loglevel((ErrorLog.REPLICA,)) |
| 174 |
supplier2.config.loglevel((ErrorLog.REPLICA,)) |
| 175 |
|
| 176 |
sync_dict = Counter() |
| 177 |
min_ap = waitfor_async_attr[1][0] |
| 178 |
max_ap = waitfor_async_attr[1][1] |
| 179 |
|
| 180 |
time.sleep(20) |
| 181 |
|
| 182 |
log.info("Gather all sync attempts within Counter dict, group by timestamp") |
| 183 |
with open(supplier1.errlog, 'r') as errlog: |
| 184 |
errlog_filtered = filter(lambda x: "waitfor_async_results" in x, errlog) |
| 185 |
|
| 186 |
# Watch only over unsuccessful sync attempts |
| 187 |
for line in errlog_filtered: |
| 188 |
if line.split()[3] != line.split()[4]: |
| 189 |
# A timestamp looks like: |
| 190 |
# [03/Jan/2018:14:35:15.806396035 +1000] LOGMESSAGE HERE |
| 191 |
# We want to assert a range of "seconds", so we need to reduce |
| 192 |
# this to a reasonable amount. IE: |
| 193 |
# [03/Jan/2018:14:35:15 |
| 194 |
# So to achieve this we split on ] and . IE. |
| 195 |
# [03/Jan/2018:14:35:15.806396035 +1000] LOGMESSAGE HERE |
| 196 |
# ^ split here first |
| 197 |
# ^ now split here |
| 198 |
# [03/Jan/2018:14:35:15 |
| 199 |
# ^ final result |
| 200 |
timestamp = line.split(']')[0].split('.')[0] |
| 201 |
sync_dict[timestamp] += 1 |
| 202 |
|
| 203 |
log.info("Take the most common timestamp and assert it has appeared " \ |
| 204 |
"in the range from %s to %s times" % (min_ap, max_ap)) |
| 205 |
most_common_val = sync_dict.most_common(1)[0][1] |
| 206 |
log.debug("%s <= %s <= %s" % (min_ap, most_common_val, max_ap)) |
| 207 |
assert min_ap <= most_common_val <= max_ap |
| 208 |
|
| 209 |
|
| 210 |
if __name__ == '__main__': |
| 211 |
# Run isolated |
| 212 |
# -s for DEBUG mode |
| 213 |
CURRENT_FILE = os.path.realpath(__file__) |
| 214 |
pytest.main("-s %s" % CURRENT_FILE) |