#!/usr/bin/env python3 """BNC regression test runner. Each test case is a directory tests/cases// containing case.json - how to run BNC and which outputs to compare expected/ - gzipped, normalised reference outputs ("golden files") case.json keys: description free text config BNC config file, relative to the BNC root (or "/dev/null") raw optional raw capture, relative to tests/ -> passed as --file args optional list of extra command line arguments (e.g. -key pairs) exit_code expected exit code (default 0; replay via --file exits with 5) timeout seconds (default 600) outputs list of output specs, each either a glob string or an object glob path relative to the work directory; stdout.txt and stderr.txt hold BNC's console output name reference name; if given the glob must match exactly one file, otherwise every match is compared under its basename and missing/extra files are reported abs_tol compare numbers with this absolute tolerance instead of requiring identical text debug_trace true for BNC_DEBUG_SSR stderr traces: reported as SKIP instead of FAIL if the build has no trace output fakecaster optional local Ntrip caster for an upload during the run: {"port", "out" (raw file, relative to the work dir), "sta", "time" (epoch stamped on the raw file), "timeout"} post optional list of further BNC runs after the main run, each {"dir" (subdirectory of the work dir), "config", "raw" (relative to the work dir), "args", "exit_code", "timeout"}; outputs may refer to their files as "/Output/..."; instead of a BNC run, {"dir", "tool": [script in tools/, args]} runs a Python tool in the work directory, stdout to /stdout.txt BNC runs in tests/work//, which contains an "Input" symlink to Example_Configs/Input and an empty "Output" directory, so the example configurations work unchanged and never write into Example_Configs/. Usage: tests/run_tests.py run all cases tests/run_tests.py ssr_ rinex run cases whose name contains a pattern tests/run_tests.py --update NAME (re)generate the reference outputs; references of outputs no longer produced are removed tests/run_tests.py --list """ import argparse import concurrent.futures import difflib import glob import gzip import json import os import re import shutil import subprocess import sys import time TESTS = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(TESTS) CASES = os.path.join(TESTS, "cases") WORK = os.path.join(TESTS, "work") # Content that changes from run to run or with the BNC version DEFAULT_FILTERS = [ # wall-clock prefix of BNC log lines "yy-mm-dd hh:mm:ss " (re.compile(r"^\d\d-\d\d-\d\d \d\d:\d\d:\d\d "), ""), (re.compile(r"BNC v?\d+\.\d+(\.\d+)?"), "BNC "), (re.compile(r"^.*(PGM / RUN BY / DATE)\s*$"), r" \1"), (re.compile(r"^(Run by\s*:).*$"), r"\1 "), (re.compile(r"^(Date\s*:).*$"), r"\1 "), ] # Lines dropped completely. The start line carries the wall-clock date and, # in --file replay, lands in a log file named after today's date, whereas all # other log lines go to a file named after the data date. DROP_LINES = re.compile(r"========== Start BNC") NUMBER = re.compile(r"[-+]?(?:\d+\.\d*|\.\d+|\d+)(?:[eEdD][-+]?\d+)?") def normalise(text): out = [] for line in text.splitlines(): if DROP_LINES.search(line): continue for rx, repl in DEFAULT_FILTERS: line = rx.sub(repl, line) out.append(line.rstrip()) return "\n".join(out) + "\n" if out else "" def read_text(path): with open(path, "rb") as f: return f.read().decode("latin-1") def read_expected(path): with gzip.open(path, "rb") as f: return f.read().decode("latin-1") def write_expected(path, text): os.makedirs(os.path.dirname(path), exist_ok=True) # mtime=0 keeps the .gz byte-identical when the content is unchanged with open(path, "wb") as raw: with gzip.GzipFile(fileobj=raw, mode="wb", mtime=0) as f: f.write(text.encode("latin-1")) def numeric_equal(exp, act, tol): """Line-by-line compare; numbers may differ by tol, other text must match.""" el, al = exp.splitlines(), act.splitlines() if len(el) != len(al): return False for e, a in zip(el, al): if NUMBER.sub("#", e) != NUMBER.sub("#", a): return False for ne, na in zip(NUMBER.findall(e), NUMBER.findall(a)): try: if abs(float(ne.replace("D", "e").replace("d", "e")) - float(na.replace("D", "e").replace("d", "e"))) > tol: return False except ValueError: if ne != na: return False return True class Result: def __init__(self, name): self.name = name self.status = "PASS" self.messages = [] self.seconds = 0.0 def fail(self, msg): self.status = "FAIL" self.messages.append(msg) def skip(self, msg): if self.status == "PASS": self.status = "SKIP" self.messages.append(msg) def load_case(name): with open(os.path.join(CASES, name, "case.json")) as f: return json.load(f) def resolve(base, path): return path if os.path.isabs(path) else os.path.normpath(os.path.join(base, path)) def run_bnc(bnc, workdir, spec, rawBase): """Run BNC once in workdir (created with Input link and Output dir); spec holds config, optional raw (relative to tests/, or to rawBase if given), args and timeout. Returns the exit code or None on timeout.""" os.makedirs(os.path.join(workdir, "Output"), exist_ok=True) if not os.path.exists(os.path.join(workdir, "Input")): os.symlink(os.path.join(ROOT, "Example_Configs", "Input"), os.path.join(workdir, "Input")) cmd = [bnc, "--nw", "--platform", "offscreen", "--conf", resolve(ROOT, spec["config"])] if spec.get("raw"): cmd += ["--file", resolve(rawBase or TESTS, spec["raw"])] cmd += spec.get("args", []) env = dict(os.environ, QT_QPA_PLATFORM="offscreen", LC_ALL="C", TZ="UTC") with open(os.path.join(workdir, "stdout.txt"), "wb") as out, \ open(os.path.join(workdir, "stderr.txt"), "wb") as err: try: code = subprocess.run(cmd, cwd=workdir, stdout=out, stderr=err, env=env, timeout=spec.get("timeout", 600)).returncode except subprocess.TimeoutExpired: code = None with open(os.path.join(workdir, "command.txt"), "w") as f: f.write(" ".join(cmd) + "\n") return code def run_tool(work, spec): """Run tools/