mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 17:23:27 -05:00
Porting of the vast majority of Azure-related code from SSDT. This is very large, so I want to put this out as one large "lift and shift" PR before I do the tools-service specific JSON-RPC service handlers, connect a new account handler (as the code to get necessary info from accounts and subscriptions isn't fully complete) and add tests over these **What's in this PR**: - Created 3 new projects: - Microsoft.SqlTools.ResourceProvider will host the executable that accepts requests for Azure-related actions over the JSON-RPC protocol. This must be separate from other DLLs since a direct dependency on the Azure SDK DLLs fails (they're NetStandard 1.4 and you can't reference them if you have RuntimeIdentifiers in your .csproj file) - Microsoft.SqlTools.ResourceProvider.Core is where all the main business logic is, including definitions and logic on how to navigate over resources and create firewall rules, etc. - Microsoft.SqlTools.ResourceProvider.DefaultImpl is the actual Azure implementation of the resource provider APIs. The reason for separating this is to support eventual integration back into other tools (since their Azure and Identity services will be different). - Implemented the AzureResourceManager that connects to Azure via ARM APIs and handles creating firewall rule and querying databases. The dependent DLLs have had major breaking changes, so will need additional verification to ensure this works as expected - Ported the unit tests for all code that was not a viewmodel. Viewmodel test code will be ported in a future update as we plumb through a service-equivalent to these. Also, the DependencyManager code which has overlap with our service provider code is commented out. Will work to uncomment in a future update as it has value to test some scenarios **What's not in this PR**: - Identity Services. We currently just have a stub for the interface, and even that will likely change a little - anything JSON-RPC or registered service related. These will be adapted from the viewmodels and added in a separate PR
169 lines
7.0 KiB
C#
169 lines
7.0 KiB
C#
//
|
|
// Copyright (c) Microsoft. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using Microsoft.SqlTools.Extensibility;
|
|
using Microsoft.SqlTools.ResourceProvider.Core;
|
|
using Microsoft.SqlTools.ResourceProvider.Core.Authentication;
|
|
using Microsoft.SqlTools.ResourceProvider.Core.Extensibility;
|
|
using Microsoft.SqlTools.ServiceLayer.UnitTests.ResourceProvider.Azure;
|
|
using Microsoft.SqlTools.ServiceLayer.UnitTests.ResourceProvider.Fakes;
|
|
using Moq;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ResourceProvider
|
|
{
|
|
/// <summary>
|
|
/// Tests for ServiceManager to verify finding services and providers for specific type correctly
|
|
/// </summary>
|
|
public class ServiceManagerTest
|
|
{
|
|
private IList<IServerDiscoveryProvider> _providers;
|
|
private IList<IAccountManager> _accountManagers;
|
|
|
|
public ServiceManagerTest()
|
|
{
|
|
_providers = new List<IServerDiscoveryProvider>()
|
|
{
|
|
new FakeServerDiscoveryProvider2(new ExportableMetadata("SqlServer", "Local", Guid.NewGuid().ToString())),
|
|
new FakeSecureServerDiscoveryProvider(new ExportableMetadata("SqlServer", "Azure", Guid.NewGuid().ToString())),
|
|
new FakeServerDiscoveryProvider(new ExportableMetadata("SqlServer", "Network", Guid.NewGuid().ToString()))
|
|
};
|
|
|
|
_accountManagers = new List<IAccountManager>()
|
|
{
|
|
new FakeAccountManager(new ExportableMetadata("SqlServer", "Azure", Guid.NewGuid().ToString())),
|
|
new FakeAccountManager2(new ExportableMetadata("SqlServer", "Network", Guid.NewGuid().ToString()))
|
|
};
|
|
}
|
|
|
|
[Fact]
|
|
public void GetServiceShouldReturnTheServiceThatHasGivenMetadataCorrectly()
|
|
{
|
|
//given
|
|
var serverDefinition = new ServerDefinition("SqlServer", "Azure");
|
|
IMultiServiceProvider provider = CreateServiceProvider();
|
|
|
|
//when
|
|
IServerDiscoveryProvider service = ExtensionUtils.GetService<IServerDiscoveryProvider>(provider, serverDefinition);
|
|
|
|
//then
|
|
Assert.NotNull(service);
|
|
Assert.True(service.GetType() == typeof(FakeSecureServerDiscoveryProvider));
|
|
}
|
|
|
|
[Fact]
|
|
public void GetServiceShouldReturnNullGivenInvalidMetadata()
|
|
{
|
|
//given
|
|
var serverDefinition = new ServerDefinition(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
IMultiServiceProvider provider = CreateServiceProvider();
|
|
|
|
//when
|
|
IServerDiscoveryProvider service = ExtensionUtils.GetService<IServerDiscoveryProvider>(provider, serverDefinition);
|
|
|
|
//then
|
|
Assert.Null(service);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetServiceShouldReturnNullGivenUnSupportedMetadata()
|
|
{
|
|
//given
|
|
var serverDefinition = new ServerDefinition("SqlServer", "Local");
|
|
IMultiServiceProvider provider = CreateServiceProvider();
|
|
|
|
//when
|
|
IAccountManager service = ExtensionUtils.GetService<IAccountManager>(provider, serverDefinition);
|
|
|
|
//then
|
|
Assert.Null(service);
|
|
}
|
|
|
|
[Fact]
|
|
public void RequiresUserAccountShouldReturnFalseGivenNotSecuredService()
|
|
{
|
|
//given
|
|
var serverDefinition = new ServerDefinition("SqlServer", "Local");
|
|
IMultiServiceProvider provider = CreateServiceProvider();
|
|
|
|
//when
|
|
IServerDiscoveryProvider service = ExtensionUtils.GetService<IServerDiscoveryProvider>(provider, serverDefinition);
|
|
|
|
//then
|
|
Assert.NotNull(service);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetShouldReturnDefaultAzureServiceGivenDefaultCatalog()
|
|
{
|
|
// given
|
|
ExtensionServiceProvider provider = ExtensionServiceProvider.Create(new Assembly[]
|
|
{
|
|
typeof(IAccountManager).Assembly,
|
|
typeof(IAzureResourceManager).Assembly
|
|
});
|
|
var serverDefinition = new ServerDefinition("sqlserver", "azure");
|
|
|
|
// when I query each provider
|
|
IServerDiscoveryProvider serverDiscoveryProvider = ExtensionUtils.GetService<IServerDiscoveryProvider>(provider, serverDefinition);
|
|
// Then I get a valid provider back
|
|
Assert.NotNull(serverDiscoveryProvider);
|
|
Assert.True(serverDiscoveryProvider is AzureSqlServerDiscoveryProvider);
|
|
|
|
IDatabaseDiscoveryProvider databaseDiscoveryProvider =
|
|
ExtensionUtils.GetService<IDatabaseDiscoveryProvider>(provider, serverDefinition);
|
|
|
|
// TODO Verify account manager is detected as soon as the account manager has a real implementation
|
|
//IAccountManager accountManager = ((AzureSqlServerDiscoveryProvider)serverDiscoveryProvider).AccountManager;
|
|
//Assert.NotNull(accountManager);
|
|
//Assert.True(accountManager is IAzureAuthenticationManager);
|
|
|
|
Assert.NotNull(databaseDiscoveryProvider);
|
|
Assert.True(databaseDiscoveryProvider is AzureDatabaseDiscoveryProvider);
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void GetShouldReturnImplementedAzureServiceIfFoundInCatalog()
|
|
{
|
|
//given
|
|
ExtensionServiceProvider provider = ExtensionServiceProvider.Create(typeof(FakeAzureServerDiscoveryProvider).SingleItemAsEnumerable());
|
|
|
|
//when
|
|
IServerDiscoveryProvider serverDiscoveryProvider = ExtensionUtils.GetService<IServerDiscoveryProvider>(provider, new ServerDefinition("sqlserver", "azure"));
|
|
|
|
Assert.NotNull(serverDiscoveryProvider);
|
|
Assert.True(serverDiscoveryProvider is FakeAzureServerDiscoveryProvider);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetGetServiceOfExportableShouldReturnNullGivenSameTypeAsExportable()
|
|
{
|
|
//given
|
|
ExtensionServiceProvider provider = ExtensionServiceProvider.Create(typeof(FakeAzureServerDiscoveryProvider).SingleItemAsEnumerable());
|
|
|
|
//when
|
|
IServerDiscoveryProvider serverDiscoveryProvider = ExtensionUtils.GetService<IServerDiscoveryProvider>(provider, new ServerDefinition("sqlserver", "azure"));
|
|
|
|
Assert.NotNull(serverDiscoveryProvider);
|
|
FakeAzureServerDiscoveryProvider fakeAzureServerDiscovery = serverDiscoveryProvider as FakeAzureServerDiscoveryProvider;
|
|
Assert.NotNull(fakeAzureServerDiscovery);
|
|
Assert.Null(fakeAzureServerDiscovery.ServerDiscoveryProvider);
|
|
}
|
|
|
|
private IMultiServiceProvider CreateServiceProvider()
|
|
{
|
|
var providerMock = new Mock<IMultiServiceProvider>();
|
|
|
|
providerMock.Setup(x => x.GetServices<IServerDiscoveryProvider>()).Returns(_providers);
|
|
providerMock.Setup(x => x.GetServices<IAccountManager>()).Returns(_accountManagers);
|
|
|
|
return providerMock.Object;
|
|
}
|
|
}
|
|
}
|