| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2020 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 time |
| 10 |
import subprocess |
| 11 |
import pytest |
| 12 |
import re |
| 13 |
|
| 14 |
from lib389.cli_conf.replication import get_repl_monitor_info |
| 15 |
from lib389.tasks import * |
| 16 |
from lib389.utils import * |
| 17 |
from test389.topologies import topology_m2 |
| 18 |
from lib389.cli_base import FakeArgs |
| 19 |
from lib389.cli_base.dsrc import dsrc_arg_concat |
| 20 |
from lib389.cli_base import connect_instance |
| 21 |
from lib389.replica import Replicas |
| 22 |
|
| 23 |
|
| 24 |
pytestmark = pytest.mark.tier0 |
| 25 |
|
| 26 |
LOG_FILE = '/tmp/monitor.log' |
| 27 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 28 |
log = logging.getLogger(__name__) |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture(scope="function") |
| 32 |
def set_log_file(request): |
| 33 |
fh = logging.FileHandler(LOG_FILE) |
| 34 |
fh.setLevel(logging.DEBUG) |
| 35 |
log.addHandler(fh) |
| 36 |
|
| 37 |
def fin(): |
| 38 |
log.info('Delete files') |
| 39 |
os.remove(LOG_FILE) |
| 40 |
|
| 41 |
config = os.path.expanduser(DSRC_HOME) |
| 42 |
if os.path.exists(config): |
| 43 |
os.remove(config) |
| 44 |
|
| 45 |
request.addfinalizer(fin) |
| 46 |
|
| 47 |
|
| 48 |
def check_value_in_log_and_reset(content_list, second_list=None, single_value=None, error_list=None): |
| 49 |
with open(LOG_FILE, 'r+') as f: |
| 50 |
file_content = f.read() |
| 51 |
|
| 52 |
for item in content_list: |
| 53 |
log.info('Check that "{}" is present'.format(item)) |
| 54 |
assert item in file_content |
| 55 |
|
| 56 |
if second_list is not None: |
| 57 |
log.info('Check for "{}"'.format(second_list)) |
| 58 |
for item in second_list: |
| 59 |
assert item in file_content |
| 60 |
|
| 61 |
if single_value is not None: |
| 62 |
log.info('Check for "{}"'.format(single_value)) |
| 63 |
assert single_value in file_content |
| 64 |
|
| 65 |
if error_list is not None: |
| 66 |
log.info('Check that "{}" is not present'.format(error_list)) |
| 67 |
for item in error_list: |
| 68 |
assert item not in file_content |
| 69 |
|
| 70 |
log.info('Reset log file') |
| 71 |
f.truncate(0) |
| 72 |
|
| 73 |
def get_hostnames_from_log(port1, port2): |
| 74 |
# Get the supplier host names as displayed in replication monitor output |
| 75 |
with open(LOG_FILE, 'r') as logfile: |
| 76 |
logtext = logfile.read() |
| 77 |
# search for Supplier :hostname:port |
| 78 |
# and use \D to insure there is no more number is after |
| 79 |
# the matched port (i.e that 10 is not matching 101) |
| 80 |
regexp = '(Supplier: )([^:]*)(:' + str(port1) + r'\D)' |
| 81 |
match=re.search(regexp, logtext) |
| 82 |
host_m1 = 'localhost.localdomain' |
| 83 |
if (match is not None): |
| 84 |
host_m1 = match.group(2) |
| 85 |
# Same for supplier 2 |
| 86 |
regexp = '(Supplier: )([^:]*)(:' + str(port2) + r'\D)' |
| 87 |
match=re.search(regexp, logtext) |
| 88 |
host_m2 = 'localhost.localdomain' |
| 89 |
if (match is not None): |
| 90 |
host_m2 = match.group(2) |
| 91 |
return (host_m1, host_m2) |
| 92 |
|
| 93 |
#unstable or unstatus tests, skipped for now |
| 94 |
@pytest.mark.flaky(max_runs=2, min_passes=1) |
| 95 |
@pytest.mark.skipif(ds_is_older("1.4.0"), reason="Not implemented") |
| 96 |
def test_dsconf_replication_monitor(topology_m2, set_log_file): |
| 97 |
"""Test replication monitor that was ported from legacy tools |
| 98 |
|
| 99 |
:id: ce48020d-7c30-41b7-8f68-144c9cd757f6 |
| 100 |
:setup: 2 MM topology |
| 101 |
:steps: |
| 102 |
1. Create DS instance |
| 103 |
2. Run replication monitor with connections option |
| 104 |
3. Run replication monitor with aliases option |
| 105 |
4. Run replication monitor with --json option |
| 106 |
5. Run replication monitor with .dsrc file created |
| 107 |
6. Run replication monitor with connections option as if using dsconf CLI |
| 108 |
:expectedresults: |
| 109 |
1. Success |
| 110 |
2. Success |
| 111 |
3. Success |
| 112 |
4. Success |
| 113 |
5. Success |
| 114 |
6. Success |
| 115 |
""" |
| 116 |
|
| 117 |
m1 = topology_m2.ms["supplier1"] |
| 118 |
m2 = topology_m2.ms["supplier2"] |
| 119 |
|
| 120 |
# Enable ldapi if not already done. |
| 121 |
for inst in [topology_m2.ms["supplier1"], topology_m2.ms["supplier2"]]: |
| 122 |
if not inst.can_autobind(): |
| 123 |
# Update ns-slapd instance |
| 124 |
inst.config.set('nsslapd-ldapilisten', 'on') |
| 125 |
inst.config.set('nsslapd-ldapiautobind', 'on') |
| 126 |
inst.restart() |
| 127 |
# Ensure that updates have been sent both ways. |
| 128 |
replicas = Replicas(m1) |
| 129 |
replica = replicas.get(DEFAULT_SUFFIX) |
| 130 |
replica.test_replication([m2]) |
| 131 |
replicas = Replicas(m2) |
| 132 |
replica = replicas.get(DEFAULT_SUFFIX) |
| 133 |
replica.test_replication([m1]) |
| 134 |
|
| 135 |
alias_content = ['Supplier: M1 (' + m1.host + ':' + str(m1.port) + ')', |
| 136 |
'Supplier: M2 (' + m2.host + ':' + str(m2.port) + ')'] |
| 137 |
|
| 138 |
connection_content = 'Supplier: '+ m1.host + ':' + str(m1.port) |
| 139 |
content_list = ['Replica Root: dc=example,dc=com', |
| 140 |
'Replica ID: 1', |
| 141 |
'Replica Status: Online', |
| 142 |
'Max CSN', |
| 143 |
'Status For Agreement: "002" ('+ m2.host + ':' + str(m2.port) + ')', |
| 144 |
'Replica Enabled: on', |
| 145 |
'Update In Progress: FALSE', |
| 146 |
'Last Update Start:', |
| 147 |
'Last Update End:', |
| 148 |
'Number Of Changes Sent:', |
| 149 |
'Number Of Changes Skipped: None', |
| 150 |
'Last Update Status: Error (0) Replica acquired successfully: Incremental update succeeded', |
| 151 |
'Last Init Start:', |
| 152 |
'Last Init End:', |
| 153 |
'Last Init Status:', |
| 154 |
'Reap Active: 0', |
| 155 |
'Replication Status: In Synchronization', |
| 156 |
'Replication Lag Time:', |
| 157 |
'Supplier: ', |
| 158 |
m2.host + ':' + str(m2.port), |
| 159 |
'Replica Root: dc=example,dc=com', |
| 160 |
'Replica ID: 2', |
| 161 |
'Status For Agreement: "001" (' + m1.host + ':' + str(m1.port)+')'] |
| 162 |
|
| 163 |
error_list = ['consumer (Unavailable)', |
| 164 |
'Failed to retrieve database RUV entry from consumer'] |
| 165 |
|
| 166 |
json_list = ['type', |
| 167 |
'list', |
| 168 |
'items', |
| 169 |
'name', |
| 170 |
m1.host + ':' + str(m1.port), |
| 171 |
'data', |
| 172 |
'"replica_id": "1"', |
| 173 |
'"replica_root": "dc=example,dc=com"', |
| 174 |
'"replica_status": "Online"', |
| 175 |
'maxcsn', |
| 176 |
'agmts_status', |
| 177 |
'agmt-name', |
| 178 |
'002', |
| 179 |
'replica', |
| 180 |
m2.host + ':' + str(m2.port), |
| 181 |
'replica-enabled', |
| 182 |
'update-in-progress', |
| 183 |
'last-update-start', |
| 184 |
'last-update-end', |
| 185 |
'number-changes-sent', |
| 186 |
'number-changes-skipped', |
| 187 |
'last-update-status', |
| 188 |
'Error (0) Replica acquired successfully: Incremental update succeeded', |
| 189 |
'last-init-start', |
| 190 |
'last-init-end', |
| 191 |
'last-init-status', |
| 192 |
'reap-active', |
| 193 |
'replication-status', |
| 194 |
'In Synchronization', |
| 195 |
'replication-lag-time', |
| 196 |
'"replica_id": "2"', |
| 197 |
'001', |
| 198 |
m1.host + ':' + str(m1.port)] |
| 199 |
|
| 200 |
connections = [m1.host + ':' + str(m1.port) + ':' + DN_DM + ':' + PW_DM, |
| 201 |
m2.host + ':' + str(m2.port) + ':' + DN_DM + ':' + PW_DM] |
| 202 |
|
| 203 |
args = FakeArgs() |
| 204 |
args.connections = connections |
| 205 |
args.aliases = None |
| 206 |
args.json = False |
| 207 |
|
| 208 |
log.info('Run replication monitor with connections option') |
| 209 |
get_repl_monitor_info(m1, DEFAULT_SUFFIX, log, args) |
| 210 |
(host_m1, host_m2) = get_hostnames_from_log(m1.port, m2.port) |
| 211 |
check_value_in_log_and_reset(content_list, connection_content, error_list=error_list) |
| 212 |
|
| 213 |
# Prepare the data for next tests |
| 214 |
aliases = ['M1=' + host_m1 + ':' + str(m1.port), |
| 215 |
'M2=' + host_m2 + ':' + str(m2.port)] |
| 216 |
|
| 217 |
alias_content = ['Supplier: M1 (' + host_m1 + ':' + str(m1.port) + ')', |
| 218 |
'Supplier: M2 (' + host_m2 + ':' + str(m2.port) + ')'] |
| 219 |
|
| 220 |
dsrc_content = '[repl-monitor-connections]\n' \ |
| 221 |
'connection1 = ' + m1.host + ':' + str(m1.port) + ':' + DN_DM + ':' + PW_DM + '\n' \ |
| 222 |
'connection2 = ' + m2.host + ':' + str(m2.port) + ':' + DN_DM + ':' + PW_DM + '\n' \ |
| 223 |
'\n' \ |
| 224 |
'[repl-monitor-aliases]\n' \ |
| 225 |
'M1 = ' + host_m1 + ':' + str(m1.port) + '\n' \ |
| 226 |
'M2 = ' + host_m2 + ':' + str(m2.port) |
| 227 |
|
| 228 |
log.info('Run replication monitor with aliases option') |
| 229 |
args.aliases = aliases |
| 230 |
get_repl_monitor_info(m1, DEFAULT_SUFFIX, log, args) |
| 231 |
check_value_in_log_and_reset(content_list, alias_content) |
| 232 |
|
| 233 |
log.info('Run replication monitor with --json option') |
| 234 |
args.aliases = None |
| 235 |
args.json = True |
| 236 |
get_repl_monitor_info(m1, DEFAULT_SUFFIX, log, args) |
| 237 |
check_value_in_log_and_reset(json_list) |
| 238 |
|
| 239 |
with open(os.path.expanduser(DSRC_HOME), 'w+') as f: |
| 240 |
f.write(dsrc_content) |
| 241 |
|
| 242 |
args.connections = None |
| 243 |
args.aliases = None |
| 244 |
args.json = False |
| 245 |
|
| 246 |
log.info('Run replication monitor when .dsrc file is present with content') |
| 247 |
get_repl_monitor_info(m1, DEFAULT_SUFFIX, log, args) |
| 248 |
check_value_in_log_and_reset(content_list, alias_content) |
| 249 |
os.remove(os.path.expanduser(DSRC_HOME)) |
| 250 |
|
| 251 |
log.info('Run replication monitor with connections option as if using dsconf CLI') |
| 252 |
# Perform same test than steps 2 test but without using directly the topology instance. |
| 253 |
# but with an instance similar to those than dsconf cli generates: |
| 254 |
# step 2 args |
| 255 |
args.connections = connections |
| 256 |
args.aliases = None |
| 257 |
args.json = False |
| 258 |
# args needed to generate an instance with dsrc_arg_concat |
| 259 |
args.instance = 'supplier1' |
| 260 |
args.basedn = None |
| 261 |
args.binddn = None |
| 262 |
args.bindpw = None |
| 263 |
args.pwdfile = None |
| 264 |
args.prompt = False |
| 265 |
args.starttls = False |
| 266 |
dsrc_inst = dsrc_arg_concat(args, None) |
| 267 |
inst = connect_instance(dsrc_inst, True, args) |
| 268 |
get_repl_monitor_info(inst, DEFAULT_SUFFIX, log, args) |
| 269 |
check_value_in_log_and_reset(content_list, connection_content, error_list=error_list) |
| 270 |
|
| 271 |
|
| 272 |
if __name__ == '__main__': |
| 273 |
# Run isolated |
| 274 |
# -s for DEBUG mode |
| 275 |
CURRENT_FILE = os.path.realpath(__file__) |
| 276 |
pytest.main("-s %s" % CURRENT_FILE) |