Files
NTrace-core/scripts/test_mtr_columns_pty.py
T
tsosunchia aeb6ee8c89 feat(mtr): 支持自定义统计列与 TUI 列编辑 (#231)
- 为 MTR 文字输出添加八项既有统计列的选择与排序
- 支持 TUI 列编辑、暂停和窗口缩放时串行重绘及终端恢复
- 在初始化前校验模式冲突并保持默认布局和 RAW/JSON schema
- 补充跨平台 PTY/ConPTY、排版、输入和并发回归及中英文说明
2026-09-07 16:42:13 +08:00

146 lines
4.8 KiB
Python

"""Exercise the real MTR TUI on a PTY using loopback probes only.
Usage: python3 scripts/test_mtr_columns_pty.py /path/to/nexttrace
Windows requires pywinpty==3.0.5 for ConPTY.
Linux needs permission to open ICMP sockets (CI runs this under sudo).
"""
import json
import os
import platform
import re
import select
import struct
import subprocess
import sys
import tempfile
import time
if os.name != "nt":
import fcntl
import pty
import termios
def main(binary):
log = bytearray()
windows = os.name == "nt"
if not windows:
master, slave = pty.openpty()
original = termios.tcgetattr(slave)
def resize(width):
if windows:
process.setwinsize(32, width)
else:
fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack("HHHH", 32, width, 0, 0))
def read(seconds=0.3):
end = time.monotonic() + seconds
data = bytearray()
while time.monotonic() < end:
source = process.fileobj if windows else master
if select.select([source], [], [], max(0, end - time.monotonic()))[0]:
try:
chunk = process.read(65536).encode() if windows else os.read(master, 65536)
if not chunk:
break
data.extend(chunk)
except (OSError, EOFError):
break
log.extend(data)
return data.decode(errors="replace")
def expect(*needles):
text = ""
deadline = time.monotonic() + 5
while time.monotonic() < deadline:
text += read()
if all(needle in text for needle in needles):
return text
raise AssertionError(f"Missing {needles!r}: {text!r}")
def send(keys):
if windows:
process.write(keys)
else:
os.write(master, keys.encode())
def counters(text):
rows = re.findall(r"^ 1\. 127\.0\.0\.1\s+(.+)$", text, re.M)
assert rows, text
return rows[-1].split()
command = [
os.path.abspath(binary), "-t", "--mtr-columns", "loss,snt,received,avg",
"-d", "disable-geoip", "-n", "-s", "127.0.0.1", "-m", "1",
"-i", "100", "--timeout", "100", "--no-color", "127.0.0.1",
]
if windows:
from winpty import PtyProcess
process = PtyProcess.spawn(command, dimensions=(32, 120))
else:
resize(120)
process = subprocess.Popen(command, stdin=slave, stdout=slave, stderr=slave)
try:
expect("Rcv", " 1. 127.0.0.1")
send("p")
paused = expect("Paused") + read(0.6) # Drain any in-flight probes.
send("o")
expect("Columns: LSRA_", "\x1b[?2004h")
send("\x15\x1b[200~r\nl s n a b w v\x1b[201~")
expect("Columns: r l s n a b w v_")
send("\r")
applied = expect("Rcv", "Last", "Paused", "\x1b[?2004l")
old, new = counters(paused), counters(applied)
assert old[1:3] == [new[2], new[0]], "Editing changed paused counters"
resize(24)
expect("Select fewer columns")
send("o")
expect("Columns: RLSNABWV_", "R=Received", "N=Last")
send("\x15l\x1b")
expect("Select fewer columns") # Bare Esc needs no subsequent read.
resize(120)
expect("Rcv", "Paused")
send("d")
expect("History")
send("o\x15ar\r")
applied = expect("Avg", "Rcv", "Paused")
assert "History" not in applied.split("\x1b[H\x1b[2J")[-1], applied
send("q")
expect("\x1b[?1049l", "\x1b[?25h")
if windows:
deadline = time.monotonic() + 3
while process.isalive() and time.monotonic() < deadline:
read(0.1)
assert not process.isalive(), "Process did not exit"
assert process.exitstatus == 0, process.exitstatus
else:
process.wait(timeout=3)
assert process.returncode == 0, process.returncode
assert termios.tcgetattr(slave) == original, "Terminal attributes not restored"
print(json.dumps({
"platform": platform.system(), "pty": "PASS",
"checks": ["loopback probes", "pause", "prefill", "paste newline",
"apply", "unchanged paused counters", "narrow notice",
"narrow editor", "bare Esc", "resize while paused",
"history apply", "terminal restore"],
}, indent=2))
finally:
if windows:
process.close(force=True)
elif process.poll() is None:
process.kill()
process.wait()
with open(os.path.join(tempfile.gettempdir(), "mtr-columns-pty.log"), "wb") as output:
output.write(log)
if not windows:
os.close(master)
os.close(slave)
if __name__ == "__main__":
main(sys.argv[1])