From 69bb2231cbd8d24d2a49aebcf8bcbcba602376f3 Mon Sep 17 00:00:00 2001 From: papertager <2567587994@qq.com> Date: Wed, 10 Jun 2026 16:12:04 +0800 Subject: [PATCH 1/2] Add mx-exporter port alignment audit --- tests/test_port_alignment.py | 29 +++++++++++++++++++++ tools/port_alignment.py | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 tests/test_port_alignment.py create mode 100644 tools/port_alignment.py diff --git a/tests/test_port_alignment.py b/tests/test_port_alignment.py new file mode 100644 index 0000000..7e6ae6e --- /dev/null +++ b/tests/test_port_alignment.py @@ -0,0 +1,29 @@ +import sys +import tempfile +import unittest +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools")) + +from port_alignment import build + + +class PortAlignmentTest(unittest.TestCase): + def test_reports_alignment(self): + with tempfile.TemporaryDirectory() as tmpdir: + root = Path(tmpdir) + (root / "mx_exporter").mkdir() + (root / "deployment" / "mx-exporter").mkdir(parents=True) + (root / "mx_exporter" / "__init__.py").write_text('parser.add_argument("-p", "--port", default=8000)\n', encoding="utf-8") + (root / "start_mxexporter.sh").write_text("HOST_PORT=8000\n", encoding="utf-8") + (root / "deployment" / "mx-exporter" / "mx-exporter-daemonset.yaml").write_text( + "port: 8000\ncontainerPort: 8000\n", encoding="utf-8" + ) + + report = build(root) + + self.assertTrue(report["aligned"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/port_alignment.py b/tools/port_alignment.py new file mode 100644 index 0000000..b690825 --- /dev/null +++ b/tools/port_alignment.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +"""Audit mx-exporter port defaults across code and deployment files.""" + +from __future__ import annotations + +import argparse +import json +import re +from pathlib import Path + + +CLI_RE = re.compile(r'parser\.add_argument\("-p".*default=(\d+)') +SCRIPT_RE = re.compile(r"HOST_PORT=(\d+)") +YAML_PORT_RE = re.compile(r"(?:port|containerPort):\s*(\d+)") + + +def build(repo_root: Path) -> dict[str, object]: + cli_text = (repo_root / "mx_exporter" / "__init__.py").read_text(encoding="utf-8") + script_text = (repo_root / "start_mxexporter.sh").read_text(encoding="utf-8") + static_text = (repo_root / "deployment" / "mx-exporter" / "mx-exporter-daemonset.yaml").read_text(encoding="utf-8") + + cli_default = int(CLI_RE.search(cli_text).group(1)) + script_default = int(SCRIPT_RE.search(script_text).group(1)) + static_ports = sorted({int(value) for value in YAML_PORT_RE.findall(static_text)}) + aligned = script_default == cli_default and all(port == cli_default for port in static_ports) + return { + "cli_default_port": cli_default, + "script_default_port": script_default, + "static_ports": static_ports, + "aligned": aligned, + } + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--repo-root", type=Path, default=Path(".")) + parser.add_argument("--output", type=Path) + args = parser.parse_args() + + text = json.dumps(build(args.repo_root), indent=2, ensure_ascii=False) + if args.output: + args.output.write_text(text + "\n", encoding="utf-8") + else: + print(text) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) From 68a583e5fc8fa413d48be40597fbb51d7010fd1f Mon Sep 17 00:00:00 2001 From: papertager <2567587994@qq.com> Date: Thu, 11 Jun 2026 00:22:46 +0800 Subject: [PATCH 2/2] Validate mx-exporter port sources --- tests/test_port_alignment.py | 18 ++++++++++++++++++ tools/port_alignment.py | 13 +++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/tests/test_port_alignment.py b/tests/test_port_alignment.py index 7e6ae6e..2b982bf 100644 --- a/tests/test_port_alignment.py +++ b/tests/test_port_alignment.py @@ -24,6 +24,24 @@ def test_reports_alignment(self): self.assertTrue(report["aligned"]) + def test_errors_when_yaml_ports_are_missing(self): + with tempfile.TemporaryDirectory() as tmpdir: + root = Path(tmpdir) + (root / "mx_exporter").mkdir() + (root / "deployment" / "mx-exporter").mkdir(parents=True) + (root / "mx_exporter" / "__init__.py").write_text( + 'parser.add_argument("-p", "--port", default=8000)\n', + encoding="utf-8", + ) + (root / "start_mxexporter.sh").write_text("HOST_PORT=8000\n", encoding="utf-8") + (root / "deployment" / "mx-exporter" / "mx-exporter-daemonset.yaml").write_text( + "kind: DaemonSet\n", + encoding="utf-8", + ) + + with self.assertRaises(ValueError): + build(root) + if __name__ == "__main__": unittest.main() diff --git a/tools/port_alignment.py b/tools/port_alignment.py index b690825..4166079 100644 --- a/tools/port_alignment.py +++ b/tools/port_alignment.py @@ -19,9 +19,18 @@ def build(repo_root: Path) -> dict[str, object]: script_text = (repo_root / "start_mxexporter.sh").read_text(encoding="utf-8") static_text = (repo_root / "deployment" / "mx-exporter" / "mx-exporter-daemonset.yaml").read_text(encoding="utf-8") - cli_default = int(CLI_RE.search(cli_text).group(1)) - script_default = int(SCRIPT_RE.search(script_text).group(1)) + cli_match = CLI_RE.search(cli_text) + script_match = SCRIPT_RE.search(script_text) + if not cli_match: + raise ValueError("Failed to find CLI default port in mx_exporter/__init__.py") + if not script_match: + raise ValueError("Failed to find default HOST_PORT in start_mxexporter.sh") + + cli_default = int(cli_match.group(1)) + script_default = int(script_match.group(1)) static_ports = sorted({int(value) for value in YAML_PORT_RE.findall(static_text)}) + if not static_ports: + raise ValueError("Failed to find any ports in mx-exporter-daemonset.yaml") aligned = script_default == cli_default and all(port == cli_default for port in static_ports) return { "cli_default_port": cli_default,