exporter: name the report after the target, not always the local egress asn

target mode's filename fell back to the local machine's egress asn
unconditionally, so 'netcheck --target AS15169' wrote report_<local-asn>_*
instead of report_AS15169_*. meta.target already carries the right
subject when one was given; prefer it and fall back to the egress asn
only in auto mode.
This commit is contained in:
Andrew Pimenov 2026-08-09 03:47:55 +03:00
parent 3425e87dbf
commit bfe9529667
2 changed files with 10 additions and 2 deletions

View file

@ -89,8 +89,9 @@ def egress_asn(report: dict[str, Any]) -> str | None:
def write_report(report: dict[str, Any], markdown: str, logs_dir: Path) -> tuple[Path, Path]:
started = (report.get("meta") or {}).get("started_at", "")
asn = egress_asn(report)
meta = report.get("meta") or {}
started = meta.get("started_at", "")
asn = meta.get("target") or egress_asn(report)
base = Path(logs_dir)
json_path = atomic_write(base / report_filename(asn, started, "json"), dump_json(report))
md_path = atomic_write(base / report_filename(asn, started, "md"), markdown)

View file

@ -193,6 +193,13 @@ def test_egress_asn_is_read_out_of_the_assembled_report():
assert egress_asn({}) is None
def test_write_report_names_the_file_after_the_target_in_target_mode(tmp_path: Path):
report = build_report({**meta(), "mode": "target", "target": "AS15169"}, modules(), [], {})
json_path, md_path = write_report(report, "# netcheck report\n", tmp_path)
assert json_path.name == "report_AS15169_20260808T191200Z.json"
assert md_path.name == "report_AS15169_20260808T191200Z.md"
def test_atomic_write_creates_the_directory_and_leaves_no_temp_file(tmp_path: Path):
target = tmp_path / "logs" / "report.json"
atomic_write(target, '{"a": 1}')