Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.UnitTests/ResourceProvider/DependencyManagerTest.cs
Kevin Cunnane ac64ac063b Port Azure code from SSDT to the tools service (#477)
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
2017-10-04 12:37:20 -07:00

296 lines
17 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// TODO Ideally would reenable these but using ExtensionServiceProvider
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Threading.Tasks;
//using Microsoft.SqlTools.ResourceProvider.Core;
//using Microsoft.SqlTools.ResourceProvider.Core.Extensibility;
//using Microsoft.SqlTools.ServiceLayer.UnitTests.ResourceProvider.Fakes;
//using Moq;
//using Xunit;
//namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ResourceProvider
//{
// /// <summary>
// /// Tests for DependencyManager to verify the services and providers can be created given different types of catalogs
// /// </summary>
// public class DependencyManagerTest
// {
// private ExtensionProperties _serviceProperties;
// private DependencyManager _dependencyManager;
// private IList<Lazy<IServerDiscoveryProvider, IExportableMetadata>> _providers;
// private readonly List<ServerInstanceInfo> _localSqlServers = new List<ServerInstanceInfo>()
// {
// new ServerInstanceInfo(),
// new ServerInstanceInfo(),
// };
// public DependencyManagerTest()
// {
// var provider1 = new Mock<IServerDiscoveryProvider>();
// var provider2 = new Mock<IServerDiscoveryProvider>();
// provider1.Setup(x => x.GetServerInstancesAsync()).Returns(Task.FromResult(new ServiceResponse<ServerInstanceInfo>(_localSqlServers.AsEnumerable())));
// _providers = new List<Lazy<IServerDiscoveryProvider, IExportableMetadata>>()
// {
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => provider1.Object,
// new ExportableAttribute("SqlServer", "Local", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString())),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => provider2.Object,
// new ExportableAttribute("SqlServer", "Network", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString())),
// };
// _serviceProperties = FakeDataFactory.CreateServiceProperties(_providers);
// _dependencyManager = new DependencyManager(_serviceProperties);
// }
// [Fact]
// public void GetShouldReturnProvidersFromTheCatalog()
// {
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> providers =
// _dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>();
// Assert.NotNull(providers);
// }
// [Fact]
// public void GetShouldReturnEmptyListGivenInvalidCategory()
// {
// Assert.False(_dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>(new ServerDefinition(null, "invalid category")).Any());
// }
// [Fact]
// public void GetShouldReturnEmptyListGivenInvalidServerType()
// {
// Assert.False(_dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>(new ServerDefinition("invalid server type", null)).Any());
// }
// [Fact]
// public void GetShouldReturnAllProvidersGivenNoParameter()
// {
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> providers =
// _dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>();
// Assert.NotNull(providers);
// Assert.True(providers.Count() == _providers.Count());
// }
// [Fact]
// public void GetShouldReturnProvidersGivenServerType()
// {
// var serverType = "sqlServer";
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> providers =
// _dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>(new ServerDefinition(serverType, null));
// Assert.NotNull(providers);
// Assert.True(providers.Any());
// Assert.True(providers.Count() == _providers.Count(x => x.Metadata.ServerType.Equals(serverType, StringComparison.OrdinalIgnoreCase)));
// }
// [Fact]
// public void GetShouldReturnProvidersGivenCategory()
// {
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> providers =
// _dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>(new ServerDefinition(null, "local"));
// Assert.NotNull(providers);
// Assert.True(providers.Count() == 1);
// }
// [Fact]
// public void GetShouldReturnProviderForEmptyCategoryGivenEmptyCategory()
// {
// // Given choice of 2 providers, one with empty category and other with specified one
// IServerDiscoveryProvider provider1 = new Mock<IServerDiscoveryProvider>();
// IServerDiscoveryProvider provider2 = new Mock<IServerDiscoveryProvider>();
// provider1.Setup(x => x.GetServerInstancesAsync()).Returns(Task.FromResult(new ServiceResponse<ServerInstanceInfo>(_localSqlServers.AsEnumerable())));
// var providers = new List<Lazy<IServerDiscoveryProvider, IExportableMetadata>>()
// {
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => provider1,
// new ExportableAttribute("SqlServer", "Azure", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString())),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => provider2,
// new ExportableAttribute("SqlServer", "", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString())),
// };
// var serviceProperties = FakeDataFactory.CreateServiceProperties(providers);
// var dependencyManager = new DependencyManager(serviceProperties);
// // When getting the correct descriptor
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> foundProviders =
// dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>(new ServerDefinition("SqlServer", ""));
// // Then expect only the provider with the empty categorty to be returned
// Assert.NotNull(foundProviders);
// Assert.True(foundProviders.Count() == 1);
// }
// [Fact]
// public void GetShouldReturnProviderGivenServerTypeAndLocationWithValidProvider()
// {
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> providers =
// _dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>(new ServerDefinition("SqlServer", "local"));
// Assert.NotNull(providers);
// Assert.True(providers.Count() == 1);
// }
// [Fact]
// public void GetShouldReturnTheServiceWithTheHighestPriorityIdMultipleFound()
// {
// IServerDiscoveryProvider expectedProvider = new Mock<IServerDiscoveryProvider>();
// List<Lazy<IServerDiscoveryProvider, IExportableMetadata>> providers = new List<Lazy<IServerDiscoveryProvider, IExportableMetadata>>()
// {
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => new Mock<IServerDiscoveryProvider>(),
// new ExportableAttribute("SqlServer", "Local", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString())),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => new Mock<IServerDiscoveryProvider>(),
// new ExportableAttribute("SqlServer", "Network", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString(), 1)),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => expectedProvider,
// new ExportableAttribute("SqlServer", "Network", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString(), 2))
// };
// ExtensionProperties serviceProperties = FakeDataFactory.CreateServiceProperties(providers);
// DependencyManager dependencyManager = new DependencyManager(serviceProperties);
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> descriptors =
// dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>();
// Assert.NotNull(descriptors);
// ExportableDescriptor<IServerDiscoveryProvider> descriptor = descriptors.FindMatchedDescriptor(new ServerDefinition("SqlServer", "network"));
// Assert.NotNull(descriptor);
// Assert.True(descriptor.Exportable == expectedProvider);
// }
// [Fact]
// public void GetShouldReturnTheServiceEvenIfTheServerTypeNotSet()
// {
// IServerDiscoveryProvider expectedProvider = new Mock<IServerDiscoveryProvider>();
// List<Lazy<IServerDiscoveryProvider, IExportableMetadata>> providers = new List<Lazy<IServerDiscoveryProvider, IExportableMetadata>>()
// {
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => new Mock<IServerDiscoveryProvider>(),
// new ExportableAttribute("SqlServer", "", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString())),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => new Mock<IServerDiscoveryProvider>(),
// new ExportableAttribute("SqlServer", "Network", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString(), 1)),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => expectedProvider,
// new ExportableAttribute("", "Network", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString(), 2))
// };
// ExtensionProperties serviceProperties = FakeDataFactory.CreateServiceProperties(providers);
// DependencyManager dependencyManager = new DependencyManager(serviceProperties);
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> descriptors =
// dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>();
// Assert.NotNull(descriptors);
// ExportableDescriptor<IServerDiscoveryProvider> descriptor = descriptors.FindMatchedDescriptor(new ServerDefinition("", "network"));
// Assert.NotNull(descriptor);
// Assert.True(descriptor.Exportable == expectedProvider);
// }
// [Fact]
// public void GetShouldReturnTheServiceThatMatchedExactlyIfServerTypeSpecified()
// {
// IServerDiscoveryProvider expectedProvider = new Mock<IServerDiscoveryProvider>();
// List<Lazy<IServerDiscoveryProvider, IExportableMetadata>> providers = new List<Lazy<IServerDiscoveryProvider, IExportableMetadata>>()
// {
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => new Mock<IServerDiscoveryProvider>(),
// new ExportableAttribute("SqlServer", "", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString())),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => expectedProvider,
// new ExportableAttribute("SqlServer", "Network", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString(), 1)),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => new Mock<IServerDiscoveryProvider>(),
// new ExportableAttribute("", "Network", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString(), 2))
// };
// ExtensionProperties serviceProperties = FakeDataFactory.CreateServiceProperties(providers);
// DependencyManager dependencyManager = new DependencyManager(serviceProperties);
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> descriptors =
// dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>();
// Assert.NotNull(descriptors);
// ExportableDescriptor<IServerDiscoveryProvider> descriptor = descriptors.FindMatchedDescriptor(new ServerDefinition("SqlServer", "network"));
// Assert.NotNull(descriptor);
// Assert.True(descriptor.Exportable == expectedProvider);
// }
// [Fact]
// public void GetShouldReturnTheServiceThatMatchedExactlyIfCategorySpecified()
// {
// IServerDiscoveryProvider expectedProvider = new Mock<IServerDiscoveryProvider>();
// List<Lazy<IServerDiscoveryProvider, IExportableMetadata>> providers = new List<Lazy<IServerDiscoveryProvider, IExportableMetadata>>()
// {
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => new Mock<IServerDiscoveryProvider>(),
// new ExportableAttribute("SqlServer", "", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString())),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => expectedProvider,
// new ExportableAttribute("SqlServer", "Network", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString(), 1)),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => new Mock<IServerDiscoveryProvider>(),
// new ExportableAttribute("", "Network", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString(), 2))
// };
// ExtensionProperties serviceProperties = FakeDataFactory.CreateServiceProperties(providers);
// DependencyManager dependencyManager = new DependencyManager(serviceProperties);
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> descriptors =
// dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>();
// Assert.NotNull(descriptors);
// ExportableDescriptor<IServerDiscoveryProvider> descriptor = descriptors.FindMatchedDescriptor(new ServerDefinition("SqlServer", "network"));
// Assert.NotNull(descriptor);
// Assert.True(descriptor.Exportable == expectedProvider);
// }
// [Fact]
// public void GetShouldReturnTheServiceEvenIfTheCategoryNotSet()
// {
// IServerDiscoveryProvider expectedProvider = new Mock<IServerDiscoveryProvider>();
// List<Lazy<IServerDiscoveryProvider, IExportableMetadata>> providers = new List<Lazy<IServerDiscoveryProvider, IExportableMetadata>>()
// {
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => new Mock<IServerDiscoveryProvider>(),
// new ExportableAttribute("SqlServer", "Local", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString())),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => new Mock<IServerDiscoveryProvider>(),
// new ExportableAttribute("SqlServer", "Network", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString(), 1)),
// new Lazy<IServerDiscoveryProvider, IExportableMetadata>(() => expectedProvider,
// new ExportableAttribute("SqlServer", "", typeof(IServerDiscoveryProvider), Guid.NewGuid().ToString(), 2))
// };
// ExtensionProperties serviceProperties = FakeDataFactory.CreateServiceProperties(providers);
// DependencyManager dependencyManager = new DependencyManager(serviceProperties);
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> descriptors =
// dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>();
// Assert.NotNull(descriptors);
// ExportableDescriptor<IServerDiscoveryProvider> descriptor = descriptors.FindMatchedDescriptor(new ServerDefinition("SqlServer", ""));
// Assert.NotNull(descriptor);
// Assert.True(descriptor.Exportable == expectedProvider);
// }
// [Fact]
// public void GetShouldReturnProvidersGivenServerTypeAndMoreThanOneLocation()
// {
// var serverType = "sqlServer";
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> providers =
// _dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>(new ServerDefinition(serverType, null));
// Assert.NotNull(providers);
// Assert.True(providers.Count() == _providers.Count(x => x.Metadata.ServerType.Equals(serverType, StringComparison.OrdinalIgnoreCase)));
// }
// [Fact]
// public async Task ProviderCreatedByFactoryShouldReturnServersSuccessfully()
// {
// List<ServerInstanceInfo> expectedServers = _localSqlServers;
// IEnumerable<ExportableDescriptor<IServerDiscoveryProvider>> providers =
// _dependencyManager.GetServiceDescriptors<IServerDiscoveryProvider>(new ServerDefinition("SqlServer",
// "local"));
// ExportableDescriptor<IServerDiscoveryProvider> provider = providers.First();
// Assert.NotNull(provider);
// ServiceResponse<ServerInstanceInfo> result = await provider.Exportable.GetServerInstancesAsync();
// var servers = result.Data;
// Assert.NotNull(servers);
// Assert.Equal(expectedServers, servers);
// }
// }
//}