Skip to content

Commit c7b2365

Browse files
committed
feat: 增加一键启动测试集群后再运行集群测试用例脚本;#240
1 parent 2e323bc commit c7b2365

3 files changed

Lines changed: 139 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ exclude = [
3131
members = [
3232
".",
3333
"loadtest",
34+
"integration_tests/rust-client-test",
3435
]
3536

3637
[[bin]]

integration_tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/test_data
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/usr/bin/env python3
2+
"""
3+
run_test.py —— 跨平台一键启动 rnacos 三节点集群并跑测试
4+
数据目录 & 日志目录均集中到同一个临时目录,脚本会打印该目录路径
5+
"""
6+
7+
import os
8+
import platform
9+
import shutil
10+
import signal
11+
import subprocess
12+
import sys
13+
import tempfile
14+
import time
15+
from pathlib import Path
16+
17+
18+
SCRIPT_DIR = Path(__file__).parent.absolute()
19+
SYSTEM = platform.system().lower()
20+
def rnacos_exe_name() -> str:
21+
return "rnacos.exe" if SYSTEM == "windows" else "rnacos"
22+
23+
# ========== 可调整参数 ==========
24+
INTEGRATION_PROJECT_ROOT_DIR = SCRIPT_DIR.parent.joinpath("rust-client-test")
25+
RNACOS_ROOT_DIR = SCRIPT_DIR.parent.parent;
26+
RNACOS_BIN = RNACOS_ROOT_DIR.joinpath("target").joinpath("debug").joinpath(rnacos_exe_name())
27+
BUILD_CMD= "cargo build"
28+
TEST_CMD = "cargo test" # 测试命令
29+
NODE_CNT = 3 # 节点数
30+
BASE_HTTP_PORT = 8848
31+
BASE_RAFT_PORT = 9848
32+
# =================================
33+
34+
def get_test_data_dir():
35+
"""获取test_data目录路径,确保其存在"""
36+
test_data_dir = SCRIPT_DIR.parent / "test_data"
37+
test_data_dir.mkdir(exist_ok=True)
38+
return test_data_dir
39+
40+
def kill_rnacos():
41+
exe = rnacos_exe_name()
42+
if SYSTEM == "windows":
43+
subprocess.run(["taskkill", "/F", "/IM", exe],
44+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
45+
if SYSTEM == "darwin":
46+
subprocess.run(["pkill", "-f", exe],
47+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
48+
else:
49+
subprocess.run(["killall", exe],
50+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
51+
def cleanup(work_dir: Path):
52+
#"""杀掉进程"""
53+
kill_rnacos()
54+
# 清理数据库目录
55+
for node_id in range(1, NODE_CNT + 1):
56+
dir_path = work_dir / f"db{node_id:02d}"
57+
try:
58+
if dir_path.exists():
59+
if SYSTEM == 'windows':
60+
os.system(f'rmdir /s /q "{dir_path}"')
61+
else:
62+
subprocess.run(['rm', '-rf', str(dir_path)], check=True)
63+
except subprocess.CalledProcessError as e:
64+
print(f"Error cleaning up {dir_path}: {e}")
65+
66+
def gen_env(node_id: int, work_dir: Path) -> Path:
67+
http_port = BASE_HTTP_PORT + node_id - 1
68+
raft_port = BASE_RAFT_PORT + node_id - 1
69+
lines = [
70+
f"RNACOS_HTTP_PORT={http_port}",
71+
f"RNACOS_RAFT_NODE_ADDR=127.0.0.1:{raft_port}",
72+
f"RNACOS_CONFIG_DB_DIR={work_dir}/db{node_id:02d}",
73+
f"RNACOS_RAFT_NODE_ID={node_id}",
74+
"RNACOS_ENABLE_NO_AUTH_CONSOLE=true",
75+
]
76+
if node_id == 1:
77+
lines.append("RNACOS_RAFT_AUTO_INIT=true")
78+
else:
79+
lines.append(f"RNACOS_RAFT_JOIN_ADDR=127.0.0.1:{BASE_RAFT_PORT}")
80+
81+
env_path = work_dir / f"env{node_id:02d}"
82+
env_path.write_text("\n".join(lines) + "\n", encoding="utf-8")
83+
return env_path
84+
85+
def start_node(env_path: Path, log_path: Path) -> subprocess.Popen:
86+
log_fp = log_path.open("w", encoding="utf-8")
87+
return subprocess.Popen([RNACOS_BIN,"-e",str(env_path)],stdout=log_fp, stderr=subprocess.STDOUT)
88+
89+
def main():
90+
exe = Path(RNACOS_BIN)
91+
if not exe.exists():
92+
subprocess.run(BUILD_CMD, shell=True, cwd=RNACOS_ROOT_DIR)
93+
if not exe.exists():
94+
print(f"rnacos executable not found: {exe.absolute()}", file=sys.stderr)
95+
sys.exit(1)
96+
97+
work_dir = get_test_data_dir()
98+
print(f"Working directory (data & logs): {work_dir}")
99+
# 保险起见,先清理
100+
cleanup(work_dir)
101+
procs = []
102+
103+
try:
104+
for i in range(1, NODE_CNT + 1):
105+
env_path = gen_env(i, work_dir)
106+
log_path = work_dir / f"n{i:02d}.log"
107+
print(f"Starting node {i} ...")
108+
procs.append(start_node(env_path, log_path))
109+
time.sleep(3)
110+
111+
print("Running test command:", TEST_CMD)
112+
test_ret = subprocess.run(TEST_CMD, shell=True, cwd=INTEGRATION_PROJECT_ROOT_DIR).returncode
113+
finally:
114+
print("Stopping rnacos processes...")
115+
kill_rnacos()
116+
for p in procs:
117+
try:
118+
p.wait(timeout=5)
119+
except subprocess.TimeoutExpired:
120+
if SYSTEM == "windows":
121+
p.kill()
122+
else:
123+
p.send_signal(signal.SIGKILL)
124+
p.wait()
125+
if p.stdout and not p.stdout.closed:
126+
p.stdout.close()
127+
try:
128+
cleanup(work_dir)
129+
except:
130+
pass
131+
if test_ret != 0:
132+
print(f"running command error,error_code: {test_ret}")
133+
sys.exit(test_ret)
134+
135+
if __name__ == "__main__":
136+
main()
137+

0 commit comments

Comments
 (0)