|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import subprocess |
| 4 | +import time |
| 5 | + |
| 6 | +import requests |
| 7 | + |
| 8 | +from mysql_ch_replicator import clickhouse_api |
| 9 | +from mysql_ch_replicator import config |
| 10 | +from mysql_ch_replicator import mysql_api |
| 11 | +from mysql_ch_replicator.binlog_replicator import State as BinlogState |
| 12 | +from mysql_ch_replicator.db_replicator import State as DbReplicatorState |
| 13 | +from mysql_ch_replicator.runner import ProcessRunner |
| 14 | + |
| 15 | + |
| 16 | +CONFIG_FILE = 'tests/tests_config.yaml' |
| 17 | +CONFIG_FILE_MARIADB = 'tests/tests_config_mariadb.yaml' |
| 18 | +TEST_DB_NAME = 'replication-test_db' |
| 19 | +TEST_DB_NAME_2 = 'replication-test_db_2' |
| 20 | +TEST_DB_NAME_2_DESTINATION = 'replication-destination' |
| 21 | + |
| 22 | +TEST_TABLE_NAME = 'test_table' |
| 23 | +TEST_TABLE_NAME_2 = 'test_table_2' |
| 24 | +TEST_TABLE_NAME_3 = 'test_table_3' |
| 25 | + |
| 26 | + |
| 27 | +class BinlogReplicatorRunner(ProcessRunner): |
| 28 | + def __init__(self, cfg_file=CONFIG_FILE): |
| 29 | + super().__init__(f'./main.py --config {cfg_file} binlog_replicator') |
| 30 | + |
| 31 | + |
| 32 | +class DbReplicatorRunner(ProcessRunner): |
| 33 | + def __init__(self, db_name, additional_arguments=None, cfg_file=CONFIG_FILE): |
| 34 | + additional_arguments = additional_arguments or '' |
| 35 | + if not additional_arguments.startswith(' '): |
| 36 | + additional_arguments = ' ' + additional_arguments |
| 37 | + super().__init__(f'./main.py --config {cfg_file} --db {db_name} db_replicator{additional_arguments}') |
| 38 | + |
| 39 | + |
| 40 | +class RunAllRunner(ProcessRunner): |
| 41 | + def __init__(self, cfg_file=CONFIG_FILE): |
| 42 | + super().__init__(f'./main.py --config {cfg_file} run_all') |
| 43 | + |
| 44 | + |
| 45 | +def kill_process(pid, force=False): |
| 46 | + command = f'kill {pid}' |
| 47 | + if force: |
| 48 | + command = f'kill -9 {pid}' |
| 49 | + subprocess.run(command, shell=True) |
| 50 | + |
| 51 | + |
| 52 | +def assert_wait(condition, max_wait_time=20.0, retry_interval=0.05): |
| 53 | + max_time = time.time() + max_wait_time |
| 54 | + while time.time() < max_time: |
| 55 | + if condition(): |
| 56 | + return |
| 57 | + time.sleep(retry_interval) |
| 58 | + assert condition() |
| 59 | + |
| 60 | + |
| 61 | +def prepare_env( |
| 62 | + cfg: config.Settings, |
| 63 | + mysql: mysql_api.MySQLApi, |
| 64 | + ch: clickhouse_api.ClickhouseApi, |
| 65 | + db_name: str = TEST_DB_NAME, |
| 66 | + set_mysql_db: bool = True |
| 67 | +): |
| 68 | + if os.path.exists(cfg.binlog_replicator.data_dir): |
| 69 | + shutil.rmtree(cfg.binlog_replicator.data_dir) |
| 70 | + os.mkdir(cfg.binlog_replicator.data_dir) |
| 71 | + mysql.drop_database(db_name) |
| 72 | + mysql.create_database(db_name) |
| 73 | + if set_mysql_db: |
| 74 | + mysql.set_database(db_name) |
| 75 | + ch.drop_database(db_name) |
| 76 | + assert_wait(lambda: db_name not in ch.get_databases()) |
| 77 | + |
| 78 | + |
| 79 | +def get_binlog_replicator_pid(cfg: config.Settings): |
| 80 | + path = os.path.join( |
| 81 | + cfg.binlog_replicator.data_dir, |
| 82 | + 'state.json', |
| 83 | + ) |
| 84 | + state = BinlogState(path) |
| 85 | + return state.pid |
| 86 | + |
| 87 | + |
| 88 | +def get_db_replicator_pid(cfg: config.Settings, db_name: str): |
| 89 | + path = os.path.join( |
| 90 | + cfg.binlog_replicator.data_dir, |
| 91 | + db_name, |
| 92 | + 'state.pckl', |
| 93 | + ) |
| 94 | + state = DbReplicatorState(path) |
| 95 | + return state.pid |
| 96 | + |
| 97 | + |
| 98 | +def read_logs(db_name): |
| 99 | + return open(os.path.join('binlog', db_name, 'db_replicator.log')).read() |
0 commit comments