Skip to content

test(security): record the scanner runs again inside pytest #5083

Done

Description

@dan-petty

Update 2026-11-26 (infra move): tools.lock and devops install-tools are gone.

The tools come from the devtools image (#6555), and no contract test compares a recording with a lock. So the lock updates and the darwin semgrep question no longer block anything. The work left is the pytest record route: the scanner recordings have none, and docker_engine_recording.py, valkey_recording.py and git_recording.py in tests/support/ still say uv run python -c.


Problem or Motivation

Four tools-lock updates cannot go in until their recorded runs are made again with the real tool:

Tool Pin Newest Recordings that change
semgrep 1.179.0 1.185.0 tests/fixtures/scanner_runs/recorded_runs.json, tests/fixtures/review_canary/recorded_outputs.json (semgrep), src/devops_cli/review/rules/rules.lock semgrep_version
osv-scanner 2.6.0 2.7.1 tests/fixtures/scanner_runs/recorded_runs.json; src/devops_cli/security/osv_scanner.py names 2.6.0 three times (docstring, offline cache salt, CycloneDX metadata version)
pluto 5.24.4 5.24.5 tests/fixtures/scanner_runs/runs_with_findings.json, tests/fixtures/review_canary/recorded_outputs.json (pluto)
tflint 0.65.0 0.66.0 tests/fixtures/scanner_runs/runs_with_findings.json; openspec/specs/tflint-release-pin/spec.md names 0.65.0 (change it through a spec delta)

With the four pins set, tests/security/test_scanner_contract.py::test_each_recording_is_of_the_version_the_tools_lock_pins and ::test_each_run_with_findings_is_of_the_version_the_tools_lock_pins fail (research run of 2026-11-12). That is the design: a recording must be of the pinned tool.

But the project has no route to record these runs inside pytest. tests/support/fakes/scanners.py says only "start the adapter as the contract suite does, with the real tool at its locked version on PATH". The OpenSpec and Docker engine recorders document uv run python -c "from tests.support... import record_scenarios; ...", which runs a tests/support helper outside pytest, where no isolation of HOME applies (on 2026-11-09 such a run wrote stubs over 18 real tools, #4739). The baseline (#5065) recorded the OpenSpec runs with a scratch live test that called record_scenarios() inside pytest; it left these four tools at their pins.

So each Renovate update of these tools fails CI with no documented safe fix.

Research of 2026-11-12 (research-ci, records tool:semgrep, tool:osv-scanner, tool:pluto, tool:tflint).

Proposed Solution & Architectural Design

  • Add a pytest record route, for example a test module marked @pytest.mark.live(<tool>) that runs only with an opt-in option or variable and writes the recordings: the clean run of each scanner, the runs with findings (with their input trees kept as fixtures), and the canary outputs. Point the docstrings of tests/support/fakes/scanners.py, tests/support/openspec_recording.py and tests/support/docker_engine_recording.py and the tools.lock runbook at it, in place of uv run python -c.
  • osv_scanner.py reads its version from the tools lock, so the next update needs no code edit.
  • Then take the four updates and record again. The sha256 of each build is in the research (all equal the release asset digests).
  • semgrep 1.183.0 and later publish no Intel-Mac wheel, so the compiled semgrep.txt has 7 hashes in place of 8 and devops install-tools --tool semgrep fails on darwin-amd64. Decide whether darwin-amd64 keeps semgrep (hold at 1.182.0) or drops it.

Done when the four tools are at their newest pins and their recordings were made inside pytest.

Target Subsystem

Security & Vulnerability Scanners

Existing Tool Considered

pytest markers (live) and the existing recorders. The new code is a small opt-in test module.

Source: #4893

Alternatives check of 2026-11-26: not-applicable.
Class: test, no model call.
Tool: none found.

Proposed design

Problem statement

The scanner recordings under tests/fixtures/ have no record route inside pytest. The only documented route is uv run python -c, which runs outside the session HOME isolation. On 2026-11-09 that route wrote stubs over 18 real tools (#4739). The repository no longer holds tools.lock or devops install-tools, so no lock holds the scanner versions. This change adds a record-marked pytest module that writes the recordings inside the session isolation, and points the recorder docstrings at it.

Acceptance criteria

Requirement: Opt-in record module

The test suite MUST hold a pytest module that writes the scanner recordings, and the default run MUST deselect its tests.

Scenario: The record module is off by default

  • WHEN uv run pytest tests/security/test_scanner_record.py runs with no -m record on its command line.
  • THEN pytest reports each test of the module as deselected, and no file under tests/fixtures/ changes.

Requirement: Record route runs in the pytest process

The record module MUST call the tests/support recorder functions in the pytest process, and MUST NOT start a uv run python -c subprocess.

Scenario: The record module starts no second interpreter

  • WHEN A test reads the source of the record module and of the tests/support recorders that it calls.
  • THEN The record module imports the recorder functions, and no source that it calls holds python -c.

Requirement: The recorders name the pytest route

The docstrings of tests/support/fakes/scanners.py, tests/support/docker_engine_recording.py, tests/support/valkey_recording.py and tests/support/git_recording.py SHALL name the record-marked pytest route in place of the uv run python -c command.

Scenario: No recorder documents the out-of-pytest command

  • WHEN A test searches those four modules for uv run python -c.
  • THEN No module holds that command, and each module names uv run pytest -m record.

Requirement: One record test for each recorder

The record module SHALL hold one record test for each recorder that the issue names, and each test SHALL carry the record marker and the live mark of its tool or service.

Scenario: Each named recorder has a record test

  • WHEN A test reads the record module and the four recorder modules.
  • THEN The module holds a record test for the scanner runs, the Docker engine runs, the Valkey runs and the git runs, and each test carries pytest.mark.record and a pytest.mark.live.

Requirement: The record route writes the scanner fixtures

The scanner recorder SHALL write recorded_runs.json, runs_with_findings.json and recorded_outputs.json to the output directory that its caller gives.

Scenario: A record run writes the three fixture files

  • WHEN A test calls the scanner recorder with a stub semgrep on PATH and an output directory under tmp_path.
  • THEN The output directory holds the three JSON files, and the semgrep record holds the version and the argv of the stub run.

Requirement: A recording holds no path of the recording machine

The recorder MUST replace each path of the recording machine with the {tree} or {workdir} placeholder in the recorded argv, cwd, stdout and stderr.

Scenario: The bandit metrics key uses the placeholder

  • WHEN A test reads the bandit record of tests/fixtures/scanner_runs/recorded_runs.json.
  • THEN The metrics key starts with {tree}/, and no record holds /tmp/scanner-recording.

Requirement: The input tree of a run with findings is a fixture

Each run with findings that the scanner recorder writes SHALL read its input from a committed tree under tests/fixtures/scanner_runs/trees/<tool>/.

Scenario: The pluto record reads a committed tree

  • WHEN A test reads the pluto record of tests/fixtures/scanner_runs/runs_with_findings.json and the tree directory.
  • THEN The record's cwd is {tree}/k8s, and tests/fixtures/scanner_runs/trees/pluto/k8s/deploy.yaml exists.

Existing Tool Considered

The alternatives check of 2026-11-26 in the issue body gives not-applicable for this test-class item and names no tool. pytest covers the need: the project runs it already, and _deselect_by_marker in tests/conftest.py:1324 deselects a marker in the default run. pytest lacks only an entry point that calls the tests/support recorders inside the session. The proposal adds one small record-marked module and one recorder function. Cost of that code: about 30,000 tokens to build, about 10,000 tokens a year to keep and fix, and about 2 hours of elapsed time.

Key questions

  • Which gate runs the record module only on purpose?: Register a record marker. _deselect_by_marker deselects its tests in the default run. An operator selects them with uv run pytest -m record. Each record test also carries @pytest.mark.live(<tool>), so it skips where the tool is absent. (best practice) [sources: tests/conftest.py:1324, pyproject.toml, docs/TESTING.md]
    • Chosen: A record marker that the default run deselects. The project uses this pattern for the bwrap suite. _deselect_by_marker already implements it, and -m record selects the tests.
    • Rejected: A marker plus an environment variable. It adds a second switch for one module, and no opt-in suite uses an environment variable.
    • Rejected: A new pytest --record option. The project has no pytest_addoption, and the marker hook already selects and deselects a suite.
    • Rejected: A new devops CLI subcommand. It runs outside pytest, so the session HOME isolation does not apply. That is the fix(tests): test helpers write to the real home outside pytest #4739 failure.
  • Which files still document the out-of-pytest command?: tests/support/docker_engine_recording.py, tests/support/valkey_recording.py and tests/support/git_recording.py hold uv run python -c. tests/support/fakes/scanners.py gives no command; it says to start the adapter as the contract suite does. [sources: tests/support/docker_engine_recording.py, tests/support/valkey_recording.py, tests/support/git_recording.py, tests/support/fakes/scanners.py]
  • Which files hold the recordings that the route writes?: tests/fixtures/scanner_runs/recorded_runs.json holds the semgrep, gitleaks, bandit and osv-scanner clean runs. tests/fixtures/scanner_runs/runs_with_findings.json holds the gitleaks, pluto, popeye and uv runs with findings. tests/fixtures/review_canary/recorded_outputs.json holds the semgrep, ruff, bandit, mypy, kube-linter and pluto canary outputs. [sources: tests/fixtures/scanner_runs/recorded_runs.json, tests/fixtures/scanner_runs/runs_with_findings.json, tests/fixtures/review_canary/recorded_outputs.json]
  • How does a test prove the record route with no real scanner?: The scanner recorder takes the output directory as a parameter. A test gives it tmp_path and a stub semgrep script on PATH. The stub prints a fixed JSON, and the test reads the written file. (best practice) [sources: tests/support/fakes/scanners.py, tests/fixtures/scanner_runs/recorded_runs.json]
    • Chosen: A stub tool on PATH. The scanner process is the edge, and a stub script gives a fixed output for the test.
    • Rejected: Skip the test without a real tool. No test then proves that the recorder writes a file.
    • Rejected: A fake recorder module. It tests the fake, not the recorder.
  • How does the route keep the input tree of a run with findings?: Commit the tree under tests/fixtures/scanner_runs/trees/<tool>/. The record test copies it to tmp_path, runs the tool there, and writes {tree} in place of the copy path. (best practice) [sources: tests/fixtures/scanner_runs/runs_with_findings.json]
    • Chosen: A committed tree, copied to tmp_path. The input that produced the record stays reviewable, and {tree} already names the copy.
    • Rejected: Generate the tree in the record test. The input would live only in code, so a later reader cannot reproduce it.
    • Rejected: Keep the tree outside the repository. A clean checkout could not record or check the run.
  • How does a record hold no path of the recording machine?: Replace each occurrence of the scratch tree path with {tree} and each work directory with {workdir}. Do it in argv, cwd, stdout and stderr. The bandit record still holds /tmp/scanner-recording/tree/app.py as a metrics key. (best practice) [sources: tests/fixtures/scanner_runs/recorded_runs.json, tests/fixtures/scanner_runs/runs_with_findings.json]
    • Chosen: Replace the path in argv, cwd, stdout and stderr. The bandit metrics key shows that a path hides in stdout, not only in argv.
    • Rejected: Replace the path in argv only. The bandit record would keep /tmp/scanner-recording/tree/app.py.
  • Do tools.lock, the version contract tests and semgrep_version still exist?: No. src/devops_cli/security/osv_scanner.py imports no tools lock and holds no osv-scanner version literal. src/devops_cli/review/rules/rules.lock holds no semgrep_version key. The recordings hold the versions as record data. The 2026-11-26 update removes tools.lock and devops install-tools. [sources: src/devops_cli/security/osv_scanner.py, src/devops_cli/review/rules/rules.lock, tests/fixtures/scanner_runs/recorded_runs.json]
  • Does darwin-amd64 keep semgrep?: The question is out of this issue. The repository holds no semgrep pin, and the tools come from the devtools image. Issue chore(tools): update semgrep past 1.182.0 when an Intel-Mac wheel returns #6100 tracks the semgrep update past 1.182.0 when an Intel-Mac wheel returns. [sources: src/devops_cli/review/rules/rules.lock, tests/fixtures/scanner_runs/recorded_runs.json]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Moved to new items

#6100

Activity

  1. added
    source/agent
    on Nov 12, 2026
  2. added
    type/test
    scope/security
    status/new
    value/medium
    effort/medium
    on Nov 12, 2026
  3. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor
  4. added
    priority/p2-medium
    on Nov 12, 2026
  5. devops-cli-service commented on Nov 15, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Class: test. The check makes no search and no model call for an item of this class.

    Tool: none found

    Checked on 2026-11-15. A person decides what this means for the item. The check changes no label and closes no issue.

  6. added
    status/ready
    and removed
    status/new
    on Nov 17, 2026
  7. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 19bfec3 (see proposed design in issue body).

    Moved to new items: #6100.

  8. devops-cli-service commented on Nov 27, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See Moved to new items in the section Proposed design of the body.

  9. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 28, 2026
  10. removed
    status/in-review
    owner/agent
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/securitysource/agenttype/testvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions