-
Notifications
You must be signed in to change notification settings - Fork 532
Fix SQL subscription name-to-GUID resolution #2861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vcolin7
wants to merge
3
commits into
main
Choose a base branch
from
vcolin7/verbose-tribble
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
servers/Azure.Mcp.Server/changelog-entries/vcolin7-sql-subscription-resolution.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| changes: | ||
| - section: "Bugs Fixed" | ||
| description: | | ||
| Fixed Azure SQL tools failing with a cryptic ARM error when a subscription display name (instead of a subscription ID) was provided. The `sql server show`, `sql server list`, `sql server create`, and `sql db rename` operations now resolve the subscription through the subscription service. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
tools/Azure.Mcp.Tools.Sql/tests/Azure.Mcp.Tools.Sql.Tests/Services/SqlServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Azure.Core; | ||
| using Azure.Mcp.Core.Services.Azure.Subscription; | ||
| using Azure.Mcp.Core.Services.Azure.Tenant; | ||
| using Azure.Mcp.Tools.Sql.Services; | ||
| using Azure.ResourceManager; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Mcp.Core.Options; | ||
| using Microsoft.Mcp.Core.Services.Azure.Authentication; | ||
| using NSubstitute; | ||
| using NSubstitute.ExceptionExtensions; | ||
| using Xunit; | ||
|
|
||
| namespace Azure.Mcp.Tools.Sql.Tests.Services; | ||
|
|
||
| public class SqlServiceTests | ||
| { | ||
| private const string SubscriptionName = "my-subscription-name"; | ||
|
|
||
| // Distinctive message thrown by the mocked subscription service so tests can prove | ||
| // the service resolves the subscription via ISubscriptionService (the #449/#453 fix) | ||
| // instead of building a SubscriptionResource directly from the raw value. | ||
| private const string SubscriptionResolvedMessage = "SqlServiceTests: subscription resolved via ISubscriptionService"; | ||
|
|
||
| private readonly ISubscriptionService _subscriptionService; | ||
| private readonly ITenantService _tenantService; | ||
| private readonly ILogger<SqlService> _logger; | ||
| private readonly SqlService _service; | ||
|
|
||
| public SqlServiceTests() | ||
| { | ||
| _subscriptionService = Substitute.For<ISubscriptionService>(); | ||
| _tenantService = Substitute.For<ITenantService>(); | ||
| _logger = Substitute.For<ILogger<SqlService>>(); | ||
|
|
||
| var cloudConfig = Substitute.For<IAzureCloudConfiguration>(); | ||
| cloudConfig.CloudType.Returns(AzureCloudConfiguration.AzureCloud.AzurePublicCloud); | ||
| cloudConfig.AuthorityHost.Returns(new Uri("https://login.microsoftonline.com")); | ||
| cloudConfig.ArmEnvironment.Returns(ArmEnvironment.AzurePublicCloud); | ||
| _tenantService.CloudConfiguration.Returns(cloudConfig); | ||
|
|
||
| var credential = Substitute.For<TokenCredential>(); | ||
| _tenantService.GetTokenCredentialAsync(Arg.Any<string?>(), Arg.Any<CancellationToken>()) | ||
| .Returns(Task.FromResult(credential)); | ||
|
|
||
| _subscriptionService.GetSubscription( | ||
| Arg.Any<string>(), | ||
| Arg.Any<string?>(), | ||
| Arg.Any<RetryPolicyOptions?>(), | ||
| Arg.Any<CancellationToken>()) | ||
| .ThrowsAsync(new InvalidOperationException(SubscriptionResolvedMessage)); | ||
|
|
||
| _service = new SqlService(_subscriptionService, _tenantService, _logger); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetServerAsync_ResolvesSubscriptionThroughSubscriptionService() | ||
| { | ||
| var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => | ||
| _service.GetServerAsync("server1", "rg", SubscriptionName, null, TestContext.Current.CancellationToken)); | ||
|
|
||
| Assert.Equal(SubscriptionResolvedMessage, ex.Message); | ||
| await AssertSubscriptionResolvedAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ListServersAsync_ResolvesSubscriptionThroughSubscriptionService() | ||
| { | ||
| var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => | ||
| _service.ListServersAsync("rg", SubscriptionName, null, TestContext.Current.CancellationToken)); | ||
|
|
||
| Assert.Equal(SubscriptionResolvedMessage, ex.Message); | ||
| await AssertSubscriptionResolvedAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CreateServerAsync_ResolvesSubscriptionThroughSubscriptionService() | ||
| { | ||
| var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => | ||
| _service.CreateServerAsync( | ||
| "server1", | ||
| "rg", | ||
| SubscriptionName, | ||
| "eastus", | ||
| "admin", | ||
| "P@ssw0rd!", | ||
| null, | ||
| null, | ||
| null, | ||
| TestContext.Current.CancellationToken)); | ||
|
|
||
| Assert.Equal(SubscriptionResolvedMessage, ex.Message); | ||
| await AssertSubscriptionResolvedAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task RenameDatabaseAsync_ResolvesSubscriptionThroughSubscriptionService() | ||
| { | ||
| var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => | ||
| _service.RenameDatabaseAsync( | ||
| "server1", | ||
| "olddb", | ||
| "newdb", | ||
| "rg", | ||
| SubscriptionName, | ||
| null, | ||
| TestContext.Current.CancellationToken)); | ||
|
|
||
| Assert.Equal(SubscriptionResolvedMessage, ex.Message); | ||
| await AssertSubscriptionResolvedAsync(); | ||
| } | ||
|
|
||
| private Task AssertSubscriptionResolvedAsync() => | ||
| _subscriptionService.Received(1).GetSubscription( | ||
| SubscriptionName, | ||
| Arg.Any<string?>(), | ||
| Arg.Any<RetryPolicyOptions?>(), | ||
| Arg.Any<CancellationToken>()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.