Skip to content

test(policy): drive install-time policy enforcement through the installed CLI - #4129

Merged
Daniel Meppiel (danielmeppiel) merged 5 commits into
mainfrom
danielmeppiel-didactic-umbrella
Nov 20, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 5 commits into
mainfrom
danielmeppiel-didactic-umbrella

Conversation

@danielmeppiel

@danielmeppiel Daniel Meppiel (danielmeppiel) commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Description

tests/integration/test_policy_install_e2e.py is named and inventoried as end-to-end. Its rows drive CliRunner in-process, though, and patch policy discovery at both entry points (_patch_both_discover, _PATCH_DISCOVER_GATE, _PATCH_DISCOVER_PREFLIGHT). They prove that a fixed discovery result routes correctly through the install pipeline. They cannot notice when the shipped CLI stops finding or applying the org policy.

This PR adds tests/integration/test_policy_install_enforcement_lifecycle.py. It holds 13 rows that run the installed apm CLI against a real org policy. The policy lives in a local contoso/.github git repository, served over the loopback GitHub HTTPS fixture. The consumer's origin points at contoso, so the CLI discovers the policy on its own. Nothing in discovery or enforcement is patched. Each row asserts:

  • the exact exit code;
  • user-visible output, with the violation on stderr;
  • ASCII output and no duplicate status lines on every command;
  • a before/after LifecycleStateSnapshot plus a whole-project byte snapshot.

A refused install must leave the project byte-identical. Successful rows also check that every lockfile deployment record exists on disk. The warn row also checks that a second install changes no bytes.

Note

Part of #4081 part (1). Renaming the old module to a component-marked routing module is separate follow-up work, after an in-flight change to its TestI8TransitiveMCPDenied row lands. This PR does not touch the old module.

Issue and approved scope

Issue: Part of #4081

Human scope-approval comment: #4081 (comment)

Does this PR complete the issue, or what remains? Partial. This PR delivers the installed-CLI rows and their red/green evidence. Remaining for part (1): rename the old module and add the component marker (separate follow-up). Parts (2) and (3), moving and classifying modules, are separate.

Approved by the test-corpus steward

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

Red-before / green-after (mutation evidence)

Each mutation was applied to src/. Both modules were then run with APM_BINARY_PATH=$PWD/.venv/bin/apm APM_E2E_TESTS=1 uv run --extra dev pytest -v -n 4 tests/integration/test_policy_install_e2e.py tests/integration/test_policy_install_enforcement_lifecycle.py, and the mutation was reverted afterwards.

Run Owner touched Old in-process rows (23) New installed-CLI rows (13)
discover_policy returns absent (fetch path) policy/discovery.py (cached-policy-shape owner module) 23 pass 8 fail
origin remote read returns None policy/_remote.py (policy-remote-origin-reader) 23 pass 8 fail
control: policy gate returns early install/phases/policy_gate.py 22 pass, 1 fail 9 fail
green (no mutation) -- 23 pass 13 pass

The two discovery-path mutations silently turn off install-time enforcement in the shipped CLI, and every old row stays green. Even the gate-level control is caught by only one old row (I20). The other rows still pass because their assertions accept any non-zero exit or any mention of a dependency name.

Mutation diffs
--- a/src/apm_cli/policy/_remote.py
+++ b/src/apm_cli/policy/_remote.py
@@ def _git_remote_origin_url(project_root: Path) -> str | None:
+    return None  # MUTATION: origin unreadable

--- a/src/apm_cli/policy/discovery.py
+++ b/src/apm_cli/policy/discovery.py
@@ def discover_policy(
+    return PolicyFetchResult(outcome="absent")  # MUTATION: fetch path

--- a/src/apm_cli/install/phases/policy_gate.py
+++ b/src/apm_cli/install/phases/policy_gate.py
@@ def run(ctx: InstallContext) -> None:
+    return  # MUTATION: gate disabled
New rows failing under the origin-read and fetch-path mutations (identical set for both)
FAILED test_policy_install_enforcement_lifecycle.py::test_allowlist_refuses_unlisted_dependency_without_writing
FAILED test_policy_install_enforcement_lifecycle.py::test_block_policy_refuses_denied_dependency_without_writing
FAILED test_policy_install_enforcement_lifecycle.py::test_dry_run_previews_blocked_dependencies_without_writing
FAILED test_policy_install_enforcement_lifecycle.py::test_non_enforcing_policy_outcomes_install[garbage-policy]
FAILED test_policy_install_enforcement_lifecycle.py::test_non_enforcing_policy_outcomes_install[malformed-policy]
FAILED test_policy_install_enforcement_lifecycle.py::test_refused_package_install_restores_manifest_bytes
FAILED test_policy_install_enforcement_lifecycle.py::test_three_level_extends_chain_enforces_root_deny
FAILED test_policy_install_enforcement_lifecycle.py::test_warn_policy_installs_and_reports_every_violation
PASSED test_policy_install_e2e.py  (all 23 nodes)

The five rows that still pass (enforcement-off, absent-policy, no-git-remote, and the two rows for the documented --no-policy / APM_POLICY_DISABLE opt-out) assert outcomes that must not block, so a disabled gate cannot fail them.

Gate early-return control
FAILED test_policy_install_e2e.py::TestI20WarnModeAllViolations::test_warn_mode_emits_all_violations
PASSED test_policy_install_e2e.py  (other 22 nodes)
FAILED test_policy_install_enforcement_lifecycle.py  9 rows (the 8 above minus dry-run, plus both opt-out rows)

Old row -> installed-CLI home

No old node is removed or renamed in this PR. The table maps each old row to the installed-CLI row that now proves it end to end.

Old row(s) in test_policy_install_e2e.py Installed-CLI home
I1 block + denied direct dep test_block_policy_refuses_denied_dependency_without_writing
I4 allowlist test_allowlist_refuses_unlisted_dependency_without_writing
I12 install <pkg> manifest rollback test_refused_package_install_restores_manifest_bytes
I10, I11 dry-run would-be-blocked + "and N more" test_dry_run_previews_blocked_dependencies_without_writing
I3, I20 warn mode, every violation, summary test_warn_policy_installs_and_reports_every_violation
I21 three-level extends: chain test_three_level_extends_chain_enforces_root_deny
I2 --no-policy, I9 APM_POLICY_DISABLE=1 test_policy_bypass_installs_denied_dependency_with_loud_warning[no-policy-flag / policy-disable-env] (executes the documented opt-out hint the refusal prints)
I13 off, I14 absent, I19 malformed, I16 garbage response, I17 no remote test_non_enforcing_policy_outcomes_install[enforcement-off / absent-policy / malformed-policy / garbage-policy / no-git-remote]
I5 MCP transport existing test_mcp_transport_policy_lifecycle.py
I6, I7 compilation target policy existing test_target_declaration_install_lifecycle.py
I8 transitive MCP unchanged; rewritten by an in-flight change
I18 direct MCP denied unchanged in this PR (in-process row stays)
I10 positional dry-run excludes manifest dep, I15 cached stale, I19 manifest rollback on refusal unchanged in this PR (in-process rows stay)

Commands run

APM_BINARY_PATH=$PWD/.venv/bin/apm APM_E2E_TESTS=1 uv run --extra dev pytest -q -n 4 tests/integration/test_policy_install_enforcement_lifecycle.py
13 passed in 14.49s

APM_E2E_TESTS=1 uv run --extra dev python -m pytest -q --strict-markers -m 'lifecycle_smoke and not lifecycle_merge_group' tests/integration/test_policy_install_enforcement_lifecycle.py
13 passed in 27.71s   (PR-time selection; console script resolved from PATH, nothing skipped)

uv run --extra dev pytest -q tests/integration/test_policy_install_e2e.py
23 passed

uv run --extra dev pytest -q tests/quality/test_ci_topology.py tests/quality/test_lifecycle_bug_ledger.py
191 passed

ruff check / ruff format --check (src/ tests/ scripts/lint_architecture_boundaries.py scripts/architecture_linter/)  -> clean
pylint R0801 (src/apm_cli/ + scripts)  -> 10.00/10
bash scripts/lint-auth-signals.sh      -> [+] auth-signal lint clean

Duration seeds

.github/ci-durations/linux-x86_64.json (the Lifecycle Smoke seed) gains the 13 node weights, measured with -p scripts.ci_sharding --shard-store-durations under -n 4 --dist loadgroup and compacted with compact_durations. The max node is 5.79s locally. On CI no node appears in any Lifecycle Smoke shard's slowest-20 list, whose 20th entries top out at 11.06s.

CI lanes

The module carries integration, e2e, lifecycle_smoke, requires_apm_binary, requires_e2e_mode and an openssl skipif, the same set as test_gitlab_subgroup_policy_lifecycle.py.

  • PR time: the required Lifecycle Smoke Shard 1-3 (Linux) jobs in ci.yml select -m 'lifecycle_smoke and not lifecycle_merge_group' with APM_E2E_TESTS=1. requires_apm_binary resolves the uv sync console script on PATH, so the rows run and are not skipped.
  • Merge queue: the Integration Tests Shard 1-4 (Linux) jobs in ci-integration.yml run the module against the frozen PyInstaller binary in dist/.

Promises

  • Architecture owners: policy-remote-origin-reader, policy-violation-subjects (.apm/architecture/owners/install-deployment.json); cached-policy-shape (.apm/architecture/owners/contracts-tooling.json).
  • Lifecycle properties: transaction.failed_command_preserves_state, outcome.status_matches_state.

Changelog

  • One line in one section under ## [Unreleased] in CHANGELOG.md
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • N/A -- this PR does not change OpenAPM-observable behaviour.

Trade-offs

  • New module instead of rewriting the old one in place. The old rows still catch in-process routing regressions, and a row there is being rewritten in parallel. Adding here and renaming in a separate change keeps every old node id stable and avoids a conflict.
  • Parametrized outcome rows. Five non-blocking outcomes share one body, which keeps CI shard time low (about 28s serial for the module). Each outcome is still its own node.
  • Mapped, not duplicated. MCP transport and compilation-target policy already have installed-CLI lifecycle tests, so this module does not repeat them.

How to test

  1. APM_BINARY_PATH="$(uv run which apm)" APM_E2E_TESTS=1 uv run --extra dev pytest -q tests/integration/test_policy_install_enforcement_lifecycle.py -> 13 passed.
  2. Add return None as the first statement of _git_remote_origin_url in src/apm_cli/policy/_remote.py, then rerun step 1 -> 8 failed.
  3. With the same mutation, run uv run --extra dev pytest -q tests/integration/test_policy_install_e2e.py -> 23 passed.
  4. Revert the mutation.

Co-authored-by: Copilot [email protected]

…lled CLI

Add test_policy_install_enforcement_lifecycle.py: 13 installed-CLI rows
against a real org policy served from a local contoso/.github repository
(policy discovered from the git remote, nothing patched). Each row checks
the exit code, output and a before/after LifecycleStateSnapshot; refused
installs must leave the project byte-identical.

Part of #4081

Co-authored-by: copilot-swe-agent[bot] <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The warn-mode test cannot detect premature stopping after the first failed check because all three packages fail the same rule; it needs a second independently failing rule and an assertion for its diagnostic.

1 open issue
What changed in this PR

Adds installed-CLI coverage for install-time organization policy enforcement, addressing the first part of #4081 while preserving the existing in-process routing tests.

Changes:

  • Adds 13 lifecycle cases using real policy discovery through a local HTTPS fixture.
  • Covers blocking, warnings, bypasses, inheritance, dry runs, and non-enforcing outcomes.
  • Checks exit codes, diagnostics, deployment state, rollback integrity, and warn-mode repeat-install stability.
File Description
tests/​integration/​test_policy_install_enforcement_lifecycle.py Adds installed-CLI policy enforcement tests with filesystem snapshots and output assertions.

Review effort: Balanced

Resolve the duration seed conflict by keeping main's seeds and adding
the new module's entries. Rename the opt-out test so its name matches
the documented --no-policy / APM_POLICY_DISABLE opt-out.

Co-authored-by: Copilot App <[email protected]>
The warn row now also requires a package that the manifest lacks, so a
regression that stops warn-mode evaluation after the first failed check
loses the required-package warning and fails the test.

Co-authored-by: Copilot App <[email protected]>
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 14057ef into main Nov 20, 2026
28 of 33 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-didactic-umbrella branch November 20, 2026 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants