Skip to content

test(auth): make ADO error-context tests hermetic to ambient git credentials (#4762) - #4785

Merged
Daniel Meppiel (danielmeppiel) merged 3 commits into
mainfrom
danielmeppiel-4762-ado-auth-hermetic
Nov 22, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 3 commits into
mainfrom
danielmeppiel-4762-ado-auth-hermetic

Conversation

@danielmeppiel

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

Copy link
Copy Markdown
Collaborator

Fixes #4762

Root cause

The CI leak detector on the failing shard named it: apm_cli.core.azure_cli.get_bearer_provider was left as a MagicMock. tests/utils/simulated_ado_chain.py patches that global inside try_with_fallback, and parallel downloads call that from several threads. Two overlapping patches restore each other's mock (enter A, enter B saves A's mock, exit A, exit B restores A's mock), so the rest of the xdist worker saw a foreign bearer provider. TestBuildErrorContextADO and test_ado_attempts_bearer_before_raising then failed. The ambient credential helper was not the cause: main's 8ea467f already routes git credential fills through a stand-in.

Change (tests only)

  • tests/utils/simulated_ado_chain.py: an RLock serializes the bearer patch.
  • tests/unit/core/test_simulated_ado_chain_threads.py: B calls in while A is inside the chain; the test counts concurrently active bearer patches (peak must be 1), checks both workers' results, and checks that the module global is the real function afterwards.

The issue's acceptance check (isolation from an ambient credential helper) is already met module-wide by main's autouse credential_children stand-in for git/gh children (8ea467f), so test_auth.py is unchanged. No product change, no changelog fragment.

Validation

  • Old vs new, host guard, pytest -p no:xdist tests/unit/core/test_simulated_ado_chain_threads.py: old helper 3/3 failed, fixed helper 5/5 passed.
  • scripts/check_test_assertions.py: ratchet clean. python -m tests.quality.taxonomy --changed: tm006 clean.
  • ruff 0.15.12 check and format --check: clean.

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 regression test checks only the mocked bearer failure, so it can pass despite restored credential leakage unless it also asserts that no credential-helper subprocess runs.

1 open issue
What changed in this PR

This test-only PR addresses #4762 by isolating Azure DevOps authentication tests from ambient Git credentials and resetting bearer-provider state across live module references. Product authentication behavior is unchanged.

Changes:

  • Stub Git credential resolution for the ADO error-context tests.
  • Reset bearer singletons on imported and live module objects.
  • Add a regression test configured with a hostile credential helper.
File Description
tests/​unit/​test_auth.py Adds credential-isolation fixtures, strengthens singleton cleanup, and introduces the hostile-helper regression test.

Review effort: Balanced

Comment thread tests/unit/test_auth.py Resolved
…#4762)

Parallel downloads call the simulated chain's try_with_fallback from
several threads. Each call patched azure_cli.get_bearer_provider; two
overlapping patches restore each other's MagicMock, leaving the module
global mocked for the rest of the xdist worker. TestBuildErrorContextADO
and the bearer regression trap then saw a foreign provider (CI leak
detector: apm_cli.core.azure_cli.get_bearer_provider). An RLock now
serializes the patch. Adds a thread-overlap regression test and a
hostile credential helper check for the ADO guidance.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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 new test module lacks a required promise marker and fails the taxonomy gate, while concurrency and credential-fallback tests can pass without validating the intended behavior.

5 open issues

Review effort: Balanced

Comment thread tests/unit/test_auth.py Resolved
…mise (#4762)

The hostile-helper check now uses the real bearer provider with no az on
PATH, so it adds no AQ004 owner patch; the thread test carries its
P-OWN-host-credential-resolution promise (tm006).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
… check (#4762)

Count concurrently active bearer patches instead of relying on timed
waits, surface worker results and exceptions in the main thread, and
drop the hostile-helper case: ADO build_error_context never reaches git
credential fill, and main's autouse credential_children fixture already
keeps every test_auth test off real git and gh children.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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.

🔵 Needs a closer look

The regression tests can pass despite broken behavior because they do not reliably establish lock contention, verify worker outcomes and completion, or assert that credential-helper lookups never occur.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Synchronize workers to ensure the test detects missing locking

tests/​unit/​core/​test_simulated_ado_chain_threads.py:31

This timeout allows the regression test to pass without the lock. If thread B is scheduled more than one second after A enters, A exits its patch before B enters, so the final identity assertion succeeds even with the original unsafe implementation. A standard-library reproduction passes without serialization when B is delayed by 1.1 seconds. Please coordinate at the patch/lock-entry boundary so the test proves B is competing before A is released, rather than treating a missed handshake as successful serialization.

Medium severity Propagate worker failures and verify both threads terminate

tests/​unit/​core/​test_simulated_ado_chain_threads.py:51

Thread.join() neither propagates worker assertions and exceptions nor verifies completion after a timeout. If both chain calls raise after restoring their patches, the final identity assertion can still pass. Check the call results on the pytest thread and assert that both workers have terminated.

5 resolved since last review

Review effort: Balanced

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.

🟢 Approval recommended

No active review issues are reported for the test-only changes addressing concurrent bearer-provider mock isolation.

No open issues 🎉

Review effort: Balanced

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 84913e3 into main Nov 22, 2026
50 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-4762-ado-auth-hermetic branch November 22, 2026 18:44
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.

[TEST] Flaky ADO auth unit tests in CI shard 2: ambient git credential helper leaks into test_auth.py

2 participants