From 3d3aba3d10a404626193e89df19d7aab310ddbac Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Fri, 3 Sep 2021 16:30:09 +0800 Subject: [PATCH 1/2] Move identity-based service endpoint parsing methods here The code won't exist in SDK anymore. Also move the tests here --- build/dependencies.props | 3 +- .../Config/IConfigurationExtensions.cs | 5 - ...e.WebJobs.Extensions.SignalRService.csproj | 1 + .../Config/IConfigurationExtensionsFacts.cs | 106 ++++++++++++++++++ .../SignalRServiceExtension.Tests.csproj | 1 + .../SingleAzureComponentFactory.cs | 0 6 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 test/SignalRServiceExtension.Tests/Config/IConfigurationExtensionsFacts.cs rename test/SignalRServiceExtension.Tests/{Config => Utils}/SingleAzureComponentFactory.cs (100%) diff --git a/build/dependencies.props b/build/dependencies.props index acd008f..73f07be 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -5,7 +5,7 @@ 0.3.0 - 1.11.0-preview1-10835 + 1.11.0-preview1-10841 1.0.0 15.8.0 4.9.0 @@ -20,6 +20,7 @@ 3.0.0 5.0.3 5.0.0 + 1.1.0 1.0.30 diff --git a/src/SignalRServiceExtension/Config/IConfigurationExtensions.cs b/src/SignalRServiceExtension/Config/IConfigurationExtensions.cs index 499b2d2..ffaf0e5 100644 --- a/src/SignalRServiceExtension/Config/IConfigurationExtensions.cs +++ b/src/SignalRServiceExtension/Config/IConfigurationExtensions.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using Azure.Core; using Microsoft.Azure.SignalR; using Microsoft.Extensions.Azure; @@ -11,10 +10,6 @@ namespace Microsoft.Azure.WebJobs.Extensions.SignalRService /// /// A helper class to parse from configuration. /// - /// - /// Copyied from https://github.com/Azure/azure-signalr/blob/e5a69b20daf7f7aa2786ff59df61433c4050c6ab/src/Microsoft.Azure.SignalR.Common/Endpoints/IConfigurationExtension.cs. - /// Although we have friend access to that internal class, referencing internal methods are not recommended as changes of internal methods might break this package. - /// internal static class IConfigurationExtensions { public static IEnumerable GetEndpoints(this IConfiguration config, AzureComponentFactory azureComponentFactory) diff --git a/src/SignalRServiceExtension/Microsoft.Azure.WebJobs.Extensions.SignalRService.csproj b/src/SignalRServiceExtension/Microsoft.Azure.WebJobs.Extensions.SignalRService.csproj index 5af9d13..acdb48c 100644 --- a/src/SignalRServiceExtension/Microsoft.Azure.WebJobs.Extensions.SignalRService.csproj +++ b/src/SignalRServiceExtension/Microsoft.Azure.WebJobs.Extensions.SignalRService.csproj @@ -11,6 +11,7 @@ + diff --git a/test/SignalRServiceExtension.Tests/Config/IConfigurationExtensionsFacts.cs b/test/SignalRServiceExtension.Tests/Config/IConfigurationExtensionsFacts.cs new file mode 100644 index 0000000..cbbc0dd --- /dev/null +++ b/test/SignalRServiceExtension.Tests/Config/IConfigurationExtensionsFacts.cs @@ -0,0 +1,106 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Linq; +using Azure.Identity; +using Microsoft.Azure.SignalR; +using Microsoft.Azure.SignalR.Tests.Common; +using Microsoft.Azure.WebJobs.Extensions.SignalRService; +using Microsoft.Extensions.Azure; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace SignalRServiceExtension.Tests.Config +{ + public class IConfigurationExtensionsFacts + { + [Fact] + public void TestGetNamedEndpointFromIdentityWithNoUri() + { + var services = new ServiceCollection(); + services.AddAzureClientsCore(); + var factory = services.BuildServiceProvider().GetRequiredService(); + var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); + Assert.False(config.GetSection("eastus").TryGetNamedEndpointFromIdentity(factory, out _)); + } + + [Fact] + public void TestGetNamedEndpointFromIdentityWithOnlyUri() + { + var services = new ServiceCollection(); + services.AddAzureClientsCore(); + var factory = services.BuildServiceProvider().GetRequiredService(); + + var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); + var uri = "http://signalr.service.uri.com:441"; + config["eastus:serviceUri"] = uri; + + Assert.True(config.GetSection("eastus").TryGetNamedEndpointFromIdentity(factory, out var endpoint)); + Assert.Equal("eastus", endpoint.Name); + Assert.Equal(uri, endpoint.Endpoint); + Assert.IsType((endpoint.AccessKey as AadAccessKey).TokenCredential); + Assert.Equal(EndpointType.Primary, endpoint.EndpointType); + } + + [Fact] + public void TestGetNamedEndpointFromIdentityWithAllEndpointField() + { + var services = new ServiceCollection(); + services.AddAzureClientsCore(); + var factory = services.BuildServiceProvider().GetRequiredService(); + + var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); + var uri = "http://signalr.service.uri.com:441"; + config["eastus:serviceUri"] = uri; + config["eastus:credential"] = "managedidentity"; + config["eastus:type"] = "secondary"; + + Assert.True(config.GetSection("eastus").TryGetNamedEndpointFromIdentity(factory, out var endpoint)); + Assert.Equal("eastus", endpoint.Name); + Assert.Equal(uri, endpoint.Endpoint); + Assert.IsType((endpoint.AccessKey as AadAccessKey).TokenCredential); + Assert.Equal(EndpointType.Secondary, endpoint.EndpointType); + } + + [Fact] + public void TestGetNamedEndpointsFromConnectionString() + { + var connectionString = FakeEndpointUtils.GetFakeConnectionString(1).Single(); + var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); + config["eastus"] = connectionString; + config["eastus:primary"] = connectionString; + config["eastus:secondary"] = connectionString; + var endpoints = config.GetSection("eastus").GetNamedEndpointsFromConnectionString().ToArray(); + Assert.Equal(3, endpoints.Length); + Assert.All(endpoints, e => Assert.Equal("eastus", e.Name)); + Assert.Equal(EndpointType.Primary, endpoints[0].EndpointType); + Assert.Equal(EndpointType.Primary, endpoints[1].EndpointType); + Assert.Equal(EndpointType.Secondary, endpoints[2].EndpointType); + } + + [Fact] + public void TestGetEndpointsFromIdentityAndConnectionString() + { + var services = new ServiceCollection(); + services.AddAzureClientsCore(); + var factory = services.BuildServiceProvider().GetRequiredService(); + var config = new ConfigurationBuilder().AddInMemoryCollection().Build(); + + var serviceUri = "http://signalr.service.uri.com:441"; + config["endpoints:eastus:serviceUri"] = serviceUri; + config["endpoints:westus:secondary"] = FakeEndpointUtils.GetFakeConnectionString(1).Single(); + + var endpoints = config.GetSection("endpoints").GetEndpoints(factory).ToArray(); + Assert.Collection(endpoints, e => + { + Assert.Equal("eastus", e.Name); + Assert.Equal(serviceUri, e.Endpoint); + }, e => + { + Assert.Equal("westus", e.Name); + Assert.Equal(EndpointType.Secondary, e.EndpointType); + }); + } + } +} diff --git a/test/SignalRServiceExtension.Tests/SignalRServiceExtension.Tests.csproj b/test/SignalRServiceExtension.Tests/SignalRServiceExtension.Tests.csproj index 898409a..8b73f0a 100644 --- a/test/SignalRServiceExtension.Tests/SignalRServiceExtension.Tests.csproj +++ b/test/SignalRServiceExtension.Tests/SignalRServiceExtension.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/test/SignalRServiceExtension.Tests/Config/SingleAzureComponentFactory.cs b/test/SignalRServiceExtension.Tests/Utils/SingleAzureComponentFactory.cs similarity index 100% rename from test/SignalRServiceExtension.Tests/Config/SingleAzureComponentFactory.cs rename to test/SignalRServiceExtension.Tests/Utils/SingleAzureComponentFactory.cs From c26741cc01d416a6af14931645009ec7a24fa81a Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Fri, 3 Sep 2021 16:31:47 +0800 Subject: [PATCH 2/2] Fix broken tests after SDK behaviour changes SDK won't check endpoint health with single endpoint anymore. --- .../Config/ServiceManagerStoreTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/SignalRServiceExtension.Tests/Config/ServiceManagerStoreTests.cs b/test/SignalRServiceExtension.Tests/Config/ServiceManagerStoreTests.cs index b34cd6d..f4b85cd 100644 --- a/test/SignalRServiceExtension.Tests/Config/ServiceManagerStoreTests.cs +++ b/test/SignalRServiceExtension.Tests/Config/ServiceManagerStoreTests.cs @@ -63,7 +63,8 @@ namespace SignalRServiceExtension.Tests var host = builder .ConfigureAppConfiguration(b => b.AddInMemoryCollection( new Dictionary { - { "key", FakeEndpointUtils.GetFakeConnectionString(1).Single() }, + { "Azure:SignalR:Endpoints:A", FakeEndpointUtils.GetFakeConnectionString(1).Single() }, + { "Azure:SignalR:Endpoints:B", FakeEndpointUtils.GetFakeConnectionString(1).Single() }, { Constants.ServiceTransportTypeName, ServiceTransportType.Persistent.ToString() } })) .ConfigureWebJobs(b => b.AddSignalR().Services.AddAzureClientsCore()).Build();