Skip to content

Commit 2d6bf2a

Browse files
authored
Tests separation (#201)
1 parent 39c6453 commit 2d6bf2a

File tree

8 files changed

+2230
-2184
lines changed

8 files changed

+2230
-2184
lines changed

.github/workflows/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ jobs:
1919
cd tests &&
2020
ls -la &&
2121
docker compose -f docker-compose-tests.yaml up --force-recreate --no-deps --wait -d &&
22-
sudo docker exec -w /app/ -i `docker ps | grep tests-replicator | awk '{print $1;}'` python3 -m pytest -x -v -s tests/test_mysql_ch_replicator.py
22+
sudo docker exec -w /app/ -i `docker ps | grep tests-replicator | awk '{print $1;}'` python3 -m pytest -x -v -s tests/

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,11 @@ docker compose -f docker-compose-tests.yaml up
336336
```
337337
2. Run tests with:
338338
```bash
339-
docker exec -w /app/ -it tests-replicator-1 python3 -m pytest -v -s tests/test_mysql_ch_replicator.py
339+
docker exec -w /app/ -it tests-replicator-1 python3 -m pytest -v -s tests/
340340
```
341341
3. To run a single test:
342342
```bash
343-
docker exec -w /app/ -it tests-replicator-1 python3 -m pytest -v -s tests/test_mysql_ch_replicator.py -k test_your_test_name
343+
docker exec -w /app/ -it tests-replicator-1 python3 -m pytest -v -s tests/ -k test_your_test_name
344344
```
345345

346346
## Contribution

tests/common.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)