Skip to content

Fix subscription ID tests when logged in with Azure CLI#2391

Merged
alzimmermsft merged 3 commits intomicrosoft:mainfrom
alzimmermsft:FixTestFailuresWhenLoggedIn
Apr 14, 2026
Merged

Fix subscription ID tests when logged in with Azure CLI#2391
alzimmermsft merged 3 commits intomicrosoft:mainfrom
alzimmermsft:FixTestFailuresWhenLoggedIn

Conversation

@alzimmermsft
Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes testing pain point where some tests fail if you're signed into Azure CLI due to subscription falling back to the logged in Azure CLI subscription if it's available.

Tests targeting subscription testing now use the Azure CLI subscription if it's set, otherwise fallback to previous handling with using the injected / omitted AZURE_SUBSCRIPTION_ID environment variable.

GitHub issue number?

[Link to the GitHub issue this PR addresses]

Pre-merge Checklist

  • Required for All PRs
    • Read contribution guidelines
    • PR title clearly describes the change
    • Commit history is clean with descriptive messages (cleanup guide)
    • Added comprehensive tests for new/modified functionality
    • Created a changelog entry if the change falls among the following: new feature, bug fix, UI/UX update, breaking change, or updated dependencies. Follow the changelog entry guide
  • For MCP tool changes:
    • One tool per PR: This PR adds or modifies only one MCP tool for faster review cycles
    • Updated servers/Azure.Mcp.Server/README.md and/or servers/Fabric.Mcp.Server/README.md documentation
    • Validate README.md changes running the script ./eng/scripts/Process-PackageReadMe.ps1. See Package README
    • For new or modified tool descriptions, ran ToolDescriptionEvaluator and obtained a score of 0.4 or more and a top 3 ranking for all related test prompts
    • For tools with new names, including new tools or renamed tools, update consolidated-tools.json
    • For renamed tools, follow the Tool Rename Checklist and tag the PR with the breaking-change label
    • For new tools associated with Azure services or publicly available tools/APIs/products, add URL to documentation in the PR description
  • Extra steps for Azure MCP Server tool changes:
    • Updated command list in servers/Azure.Mcp.Server/docs/azmcp-commands.md
    • Ran ./eng/scripts/Update-AzCommandsMetadata.ps1 to update tool metadata in azmcp-commands.md (required for CI)
    • Updated test prompts in servers/Azure.Mcp.Server/docs/e2eTestPrompts.md
    • 👉 For Community (non-Microsoft team member) PRs:
      • Security review: Reviewed code for security vulnerabilities, malicious code, or suspicious activities before running tests (crypto mining, spam, data exfiltration, etc.)
      • Manual tests run: added comment /azp run mcp - pullrequest - live to run Live Test Pipeline

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates subscription-related unit tests to be resilient when a developer is signed into Azure CLI by allowing tests to use the Azure CLI default subscription when present, otherwise falling back to AZURE_SUBSCRIPTION_ID.

Changes:

  • Adjusts EnvironmentHelpers subscription helpers (adds ClearAzureSubscriptionId, changes SetAzureSubscriptionId behavior/signature).
  • Exposes CommandHelper.GetProfileSubscription() and uses it in default-subscription resolution.
  • Updates subscription-focused unit tests to assert against “CLI subscription if available, else env var”.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tools/Azure.Mcp.Tools.ContainerApps/tests/Azure.Mcp.Tools.ContainerApps.UnitTests/ContainerApp/ContainerAppListCommandTests.cs Clears AZURE_SUBSCRIPTION_ID for validation tests and partially restores it afterward.
core/Microsoft.Mcp.Core/src/Helpers/EnvironmentVariableHelpers.cs Adds ClearAzureSubscriptionId; changes SetAzureSubscriptionId signature/behavior to prefer Azure CLI subscription.
core/Microsoft.Mcp.Core/src/Helpers/CommandHelper.cs Uses GetProfileSubscription() and exposes it internally for reuse.
core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Helpers/CommandHelperTests.cs Updates expectations to allow Azure CLI subscription to win; adds tests that reference profile subscription.
core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Subscription/SubscriptionCommandTests.cs Updates expectations to allow Azure CLI subscription to win when env var is “set” for tests.

Comment thread core/Microsoft.Mcp.Core/src/Helpers/EnvironmentVariableHelpers.cs Outdated
Comment thread core/Microsoft.Mcp.Core/src/Helpers/EnvironmentVariableHelpers.cs Outdated
Comment thread core/Microsoft.Mcp.Core/src/Helpers/EnvironmentVariableHelpers.cs Outdated
Copy link
Copy Markdown
Contributor

@jongio jongio left a comment

Choose a reason for hiding this comment

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

CI is red - the signature change on SetAzureSubscriptionId breaks RegistryListCommandTests.cs:57 which passes null. Beyond that compile fix, I think the approach here has a deeper issue worth discussing.

The core problem: SetAzureSubscriptionId now conditionally doesn't set anything and returns a value unrelated to its input. On a CLI-logged-in machine, calling Set("env-subs") silently no-ops and returns the CLI profile subscription. Set/Get isn't round-trippable, and callers can't tell whether their value was actually written. That's a correctness hazard for any future caller.

The test assertions also become tautological when CLI is logged in - both sides of Assert.Equal(subscription, actual) resolve through the same s_profileDefault.Value cache, so the tests prove cache consistency rather than subscription resolution behavior.

A simpler alternative: keep SetAzureSubscriptionId as a pure setter (always writes the env var), and have tests derive the expected value via CommandHelper.GetDefaultSubscription(). That way the env var IS written (no hidden no-op), the resolution precedence is tested for real, and the assertions are meaningful regardless of whether CLI is logged in.

Comment thread core/Microsoft.Mcp.Core/src/Helpers/EnvironmentVariableHelpers.cs Outdated
Comment thread core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Helpers/CommandHelperTests.cs Outdated
@github-project-automation github-project-automation Bot moved this from Untriaged to In Progress in Azure MCP Server Apr 13, 2026
Copy link
Copy Markdown
Contributor

@jongio jongio left a comment

Choose a reason for hiding this comment

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

The iteration looks good - all my previous concerns are addressed. Moving SetAzureSubscriptionId to TestEnvironment as a pure setter and using GetDefaultSubscription() / GetProfileSubscription() to derive expected values is the right approach. Clean separation between production and test code.

One small nit for a future pass: the ContainerAppListCommandTests.cs finally block could add an else { TestEnvironment.ClearAzureSubscriptionId(); } for defensive cleanup when the original value was null. Not blocking - the env var is already cleared from the try block.

@alzimmermsft alzimmermsft merged commit 57c413d into microsoft:main Apr 14, 2026
20 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Azure MCP Server Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants