Repository navigation
test(azure): mock credentials and service APIs in all Azure tests - #1467
Conversation
The azure_resolver_coverage tests called the real NewAzureResolver and made requests to test-vault.vault.azure.net. The Key Vault 401 challenge then walked the live DefaultAzureCredential chain: IMDS probe, repeated `az account get-access-token` calls, and `pwsh Get-AzAccessToken`, which reads the MSAL cache from the macOS login keychain and pops an interactive password prompt during every `go test ./...` run. The intended t.Skipf guards never fired because credential construction succeeds on any dev box. - Rewrite the live tests onto the existing fake-credential httptest harness; constructor-only tests keep exercising NewAzureResolver but make no requests and now fail loud instead of silently skipping. - Add TestMain pinning AZURE_TOKEN_CREDENTIALS=EnvironmentCredential with dummy env credentials so no test in the package can reach the az/azd/pwsh/IMDS legs, plus a regression guard test that fails if the pin is removed. Package suite now runs offline in ~4s (was minutes of network retries); verified zero az/pwsh invocations during the run.
TestRecommendationsClientAdapter_GetRecommendationsForService and TestRecommendationsClientAdapter_GetAllRecommendations built real armconsumption/armadvisor clients with a fake bearer token and issued live HTTPS requests to management.azure.com on every test run (the old comments admitted the calls were expected to fail). They discarded both return values, so they asserted nothing. Rewrite them onto the package's existing injection points (newXxxClientFn vars + getAdvisorRecsFn), the same mechanism the parallelism/ordering/error-isolation tests already use, via a shared overrideServiceClientFns helper with t.Cleanup restore. The tests now run offline and assert the returned recommendations.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAzure tests now avoid live credential and service calls by restricting the secrets credential chain, using mocked Key Vault responses, and injecting fake recommendation clients with concrete assertions. ChangesAzure test hermeticity
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/secrets/azure_resolver_coverage_test.go`:
- Around line 140-142: Strengthen the canceled-context test around
resolver.GetSecret by tracking whether notFoundHandler is invoked and asserting
that it remains uncalled after the request. Keep the existing error assertion,
but ensure the test fails if cancellation is ignored and an HTTP request reaches
the server.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1139a3a2-2550-4b33-be97-d9db9c7deaca
📒 Files selected for processing (3)
internal/secrets/azure_credential_guard_test.gointernal/secrets/azure_resolver_coverage_test.goproviders/azure/recommendations_test.go
Closes #1462
Summary
go testruns were popping macOS keychain password prompts (namingpwsh) and issuing live network calls to Azure. Traced via process ancestry of a live prompt: Azure tests were walking the realDefaultAzureCredentialfallback chain and calling real Azure endpoints.Two atomic commits:
test(secrets)—azure_resolver_coverage_test.gocalled the realNewAzureResolveragainsttest-vault.vault.azure.net; the Key Vault 401 challenge walked the live credential chain (IMDS probe,az account get-access-tokenretries,pwsh Get-AzAccessToken→ keychain prompt). Thet.Skipfguards never fired because credential construction always succeeds on a dev box. Rewrote the live tests onto the package's existing fake-credential httptest harness; constructor-only tests now fail loud instead of silently skipping. AddedTestMainpinningAZURE_TOKEN_CREDENTIALS=EnvironmentCredentialwith dummy env credentials so no test in the package can reach the az/azd/pwsh/IMDS legs, plus a regression guard test that fails if the pin is removed.test(azure)— the two recommendations-adapter tests built realarmconsumption/armadvisorclients and issued live HTTPS requests tomanagement.azure.comon every run, discarding both return values. Rewrote them onto the existingnewXxxClientFn/getAdvisorRecsFninjection points (sharedoverrideServiceClientFnshelper witht.Cleanuprestore) with real assertions on returned recommendations.Verification
internal/secrets: 245/245 pass, offline in ~4s (was ~2min of network retries).providers/azuremodule: 710/710 pass, gofmt/vet clean.~/.Azure/commands/gained zero entries across timed runs of both suites.Follow-ups filed (out of scope here)
DefaultAzureCredential/armreservationsclients with no injection seamNewAzureKeyVaultSignerno-seam construction; fully-configured factory branch untestedprovider_test.gosilentIsConfigured()guards🤖 Generated with Claude Code
Summary by CodeRabbit