Files
Open_Ultra_SAST/tests/test_push_background.py
T
Marius Ciepluch 739688730f fix(push): the background child is started with -P, as the wrapper is (item 5b)
The maintainer's answer of 2026-11-27 to question 3 of the ninth session's menu, "Add -P there
too", as offered: one line, a test first, a reviewer.

src/openultrasast/push/background.py starts the detached replay of `ousast pre-push --engine
background` with the interpreter, -m and openultrasast.cli, from the pushed repository. With -m
alone the working directory is first on the interpreter's path, so a file of the pushed repository
could stand in for a module the CLI imports. The wrapper's native path had the same fault and was
fixed with -P in a6e8e26; the reviewer of that commit found this one and it was left alone.

The child is now started with -P, which keeps that directory off the path. The flag needs Python
3.11, the project's floor (pyproject.toml).

Measured by the new test, with a real child process: tests/test_push_background.py puts a json.py
that exits into the pushed repository and starts the child. On a git archive of HEAD the child's
log is "SHADOW-EXECUTED" and its artifact is never written (s10_c3_red2.out). With the change the
artifact is written and the log does not hold that line, on this host's default PATH, where Joern is
not found (the child ends in about 1 s), and with Joern 4.0.625 and its JavaScript
frontend on PATH, where the child uses its whole deadline (s10_c3_green2.out, s10_c3_green3.out).
The five push test files give 106 passed. ruff, mypy, the three gates and compileall pass, and the
gates print what they printed before (s10_c3b_exits.txt). `git diff --check` exits 0. No
whole-suite run: that is step 9.

An independent reviewer blocked the first round on the test: its wait equalled the child's
deadline, so it was red on a host where the engine can build a JavaScript graph (1 failed,
4 passed). The child's deadline is now 5 s. The second round approved the final diff.

Found and not fixed, for the maintainer:
- The container path has the fault too, by one probe. docker-compose.yml:37 sets the working
  directory to /work, and the wrapper's own comment (ops/shell/ousast.sh:13-16 and :34-35) says
  the container runs in the caller's directory. On the local openultrasast:dev image
  3f959b31f2f2, network none, a tree that holds such a json.py mounted at /work: the image's own
  entrypoint (Dockerfile:46, python -m openultrasast.cli) prints SHADOW-EXECUTED for --help and
  exits 1; the same tree with -P prints the usage (s10_c3_container_probe.out). The wrapper and
  compose were not run, and the published image was not probed.
- ops/ax/smoke-run.sh:9 and :12, ops/ax/up.sh:14 and ops/ax/probe-placement.sh:16 run this
  checkout's code with -m and without -P, in the caller's directory (by reading): the CLI at
  smoke-run.sh:12, openultrasast.plane.profile at the other three.
- src/openultrasast/plane/runner.py:678 starts a task's module with -m from the task's first
  workspace (by reading). Whether a file of that workspace can stand in for a module the task
  imports was not measured.
- The two launches in src/openultrasast/search/demo.py use -I, which keeps the directory off the
  path.
2026-11-27 17:49:08 +01:00

105 lines
5.1 KiB
Python

"""Engine honesty within the deadline and the background engine (task 17.5, Requirement 9.5)."""
from __future__ import annotations
import json
import os
import time
from pathlib import Path
from test_push_runner import history
from openultrasast.cpg.backend import NullBackend
from openultrasast.push import background
from openultrasast.push.runner import replay
def test_engine_off_runs_the_quick_tier_and_says_the_engine_was_skipped(tmp_path):
root, base, head, _ = history(tmp_path)
delivery = replay(root, base=base, head=head, artifact=tmp_path / "r.json", backend=NullBackend(), engine="off")
assert delivery.exit_code == 0
assert "Skipped: engine checks were switched off for this run (--engine off)" in delivery.text
data = json.loads((tmp_path / "r.json").read_text())
assert data["scans"] == [] and data["quick_tier"][0]["status"] == "completed"
def test_background_engine_result_is_shown_once_on_the_next_run(tmp_path):
root, base, head, _ = history(tmp_path)
artifacts = tmp_path / "artifacts"
first = replay(root, base=base, head=head, artifact=artifacts / "push-1.json", engine="background", background_deadline=60)
assert first.exit_code == 0
assert "Engine: running in the background for " + head[:12] in first.text
assert "Skipped: engine checks run in the background after this push" in first.text
started = json.loads((artifacts / "push-1.json").read_text())["engine_background"]
assert started["status"] == "started" and started["head"] == head
result = background.results_dir(artifacts / "push-1.json", root) / f"{head}.json"
deadline = time.monotonic() + 90
while not result.exists() and time.monotonic() < deadline:
time.sleep(0.2)
assert result.exists(), (background.results_dir(artifacts / "push-1.json", root) / f"{head}.log").read_text()
second = replay(root, base=base, head=head, artifact=artifacts / "push-2.json", backend=NullBackend(), engine="off")
assert f"Engine result for {head[:12]} from the previous push:" in second.text
assert str(result) in second.text
third = replay(root, base=base, head=head, artifact=artifacts / "push-3.json", backend=NullBackend(), engine="off")
assert "from the previous push" not in third.text
def test_one_background_engine_at_a_time(tmp_path):
root, base, head, _ = history(tmp_path)
artifact = tmp_path / "a" / "push.json"
directory = background.results_dir(artifact, root)
directory.mkdir(parents=True)
(directory / "running.json").write_text(json.dumps({"pid": os.getpid(), "head": "f" * 40}))
state = background.start(root, base=base, head=head, artifact=artifact, deadline_seconds=5)
assert state["status"] == "busy" and not (directory / f"{head}.log").exists()
delivery = replay(root, base=base, head=head, artifact=artifact, backend=NullBackend(), engine="background")
assert "is still busy, so none was started" in delivery.text
def test_the_background_child_does_not_import_a_module_of_the_pushed_repository(tmp_path):
"""`-m` alone puts the working directory first on the interpreter's path, and the child runs in the pushed
repository: a file there could stand in for a module the CLI imports. That is the fault fixed for the
wrapper in a6e8e26; the child is started with `-P`, which keeps that directory off the path.
"""
root, base, head, _ = history(tmp_path)
(root / "json.py").write_text('import sys\nsys.exit("SHADOW-EXECUTED")\n')
artifact = tmp_path / "a" / "push.json"
# A short deadline for the child, and a longer wait: where the engine is installed the child uses its
# whole deadline and then writes the artifact.
state = background.start(root, base=base, head=head, artifact=artifact, deadline_seconds=5)
assert state["status"] == "started"
target = Path(state["artifact"])
deadline = time.monotonic() + 60
while not target.exists() and time.monotonic() < deadline:
time.sleep(0.2)
log = Path(state["log"]).read_text()
assert "SHADOW-EXECUTED" not in log
assert target.exists(), log
def test_previous_summarises_advisory_engine_findings(tmp_path):
root = tmp_path / "repo"
root.mkdir()
artifact = tmp_path / "out" / "push.json"
directory = background.results_dir(artifact, root)
directory.mkdir(parents=True)
delta = {
"family": "injection",
"novelty": "new",
"witness": "req.query.code -> eval",
"head_operation": {"path": "debug.js", "line": 2},
}
payload = {
"result": {"coverage_status": "incomplete"},
"admission": {
"defects": [],
"coverage_reasons": ["capability_unavailable"],
"dispositions": [{"admitted": False, "reasons": ["capability_unavailable"], "candidate": {"delta": delta}}],
},
}
(directory / ("a" * 40 + ".json")).write_text(json.dumps(payload))
(item,) = background.previous(artifact, root)
assert item["advisory"] == [{"family": "injection", "path": "debug.js", "line": 2, "novelty": "new"}]
assert item["finished"] and item["engine_ran"] and item["alerts"] == 0
assert background.previous(artifact, root) == []