From 32bb7ab68e124af71d8cd16ce2d9037452f45b3d Mon Sep 17 00:00:00 2001 From: Tan Han Chong Date: Tue, 1 Oct 2024 15:28:59 +0800 Subject: [PATCH 1/3] check connection string in StorageAccountDetails since its no longer assigned any value in StorageConnectionString --- .../AzureStorageDurabilityProviderFactory.cs | 2 +- .../CustomTestStorageAccountProvider.cs | 25 +++++++++++ ...reStorageDurabilityProviderFactoryTests.cs | 41 +++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 test/Common/CustomTestStorageAccountProvider.cs diff --git a/src/WebJobs.Extensions.DurableTask/AzureStorageDurabilityProviderFactory.cs b/src/WebJobs.Extensions.DurableTask/AzureStorageDurabilityProviderFactory.cs index 242bef777..51015f79e 100644 --- a/src/WebJobs.Extensions.DurableTask/AzureStorageDurabilityProviderFactory.cs +++ b/src/WebJobs.Extensions.DurableTask/AzureStorageDurabilityProviderFactory.cs @@ -159,7 +159,7 @@ private AzureStorageDurabilityProvider GetAzureStorageStorageProvider(DurableCli // Need to check this.defaultStorageProvider != null for external clients that call GetDurabilityProvider(attribute) // which never initializes the defaultStorageProvider. if (string.Equals(this.defaultSettings?.TaskHubName, settings.TaskHubName, StringComparison.OrdinalIgnoreCase) && - string.Equals(this.defaultSettings?.StorageConnectionString, settings.StorageConnectionString, StringComparison.OrdinalIgnoreCase) && + string.Equals(this.defaultSettings?.StorageAccountDetails?.ConnectionString, settings.StorageAccountDetails?.ConnectionString, StringComparison.OrdinalIgnoreCase) && this.defaultStorageProvider != null) { // It's important that clients use the same AzureStorageOrchestrationService instance diff --git a/test/Common/CustomTestStorageAccountProvider.cs b/test/Common/CustomTestStorageAccountProvider.cs new file mode 100644 index 000000000..3f6d3e45d --- /dev/null +++ b/test/Common/CustomTestStorageAccountProvider.cs @@ -0,0 +1,25 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using DurableTask.AzureStorage; +using Microsoft.WindowsAzure.Storage; + +namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests +{ + internal class CustomTestStorageAccountProvider : IStorageAccountProvider + { + private const string CustomConnectionString = "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=dGVzdA==;EndpointSuffix=core.windows.net"; + private readonly string customConnectionName; + + public CustomTestStorageAccountProvider(string connectionName) + { + this.customConnectionName = connectionName; + } + + public CloudStorageAccount GetCloudStorageAccount(string name) => + CloudStorageAccount.Parse(name != this.customConnectionName ? TestHelpers.GetStorageConnectionString() : CustomConnectionString); + + public StorageAccountDetails GetStorageAccountDetails(string name) => + new StorageAccountDetails { ConnectionString = name != this.customConnectionName ? TestHelpers.GetStorageConnectionString() : CustomConnectionString }; + } +} diff --git a/test/FunctionsV2/AzureStorageDurabilityProviderFactoryTests.cs b/test/FunctionsV2/AzureStorageDurabilityProviderFactoryTests.cs index 30363c325..a55933384 100644 --- a/test/FunctionsV2/AzureStorageDurabilityProviderFactoryTests.cs +++ b/test/FunctionsV2/AzureStorageDurabilityProviderFactoryTests.cs @@ -154,5 +154,46 @@ public void EnvironmentIsVMSS_WorkerIdFromEnvironmentVariables() Assert.Equal("waws-prod-euapbn1-003:dw0SmallDedicatedWebWorkerRole_hr0HostRole-3-VM-13", settings.WorkerId); } + + [Fact] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public void CustomConnectionNameIsResolved() + { + var storageAccountProvider = new CustomTestStorageAccountProvider("CustomConnection"); + var mockOptions = new OptionsWrapper(new DurableTaskOptions()); + var nameResolver = new Mock().Object; + + var factory = new AzureStorageDurabilityProviderFactory( + mockOptions, + storageAccountProvider, + nameResolver, + NullLoggerFactory.Instance, + TestHelpers.GetMockPlatformInformationService()); + + factory.GetDurabilityProvider(); // This will initialize the default connection string + var provider = factory.GetDurabilityProvider(new DurableClientAttribute() { ConnectionName = "CustomConnection", TaskHub = "TestHubName" }); + + Assert.Equal("CustomConnection", provider.ConnectionName); + } + + [Fact] + [Trait("Category", PlatformSpecificHelpers.TestCategory)] + public void DefaultConnectionNameIsResolved() + { + var storageAccountProvider = new CustomTestStorageAccountProvider("CustomConnection"); + var mockOptions = new OptionsWrapper(new DurableTaskOptions()); + var nameResolver = new Mock().Object; + + var factory = new AzureStorageDurabilityProviderFactory( + mockOptions, + storageAccountProvider, + nameResolver, + NullLoggerFactory.Instance, + TestHelpers.GetMockPlatformInformationService()); + + var provider = factory.GetDurabilityProvider(); + + Assert.Equal("Storage", provider.ConnectionName); + } } } From 7dbed477d993ac7a823e54ce0a2a00d7ac40da3f Mon Sep 17 00:00:00 2001 From: Han Chong Date: Wed, 2 Oct 2024 09:08:40 +0800 Subject: [PATCH 2/3] Use random key for storage account in unit test --- test/Common/CustomTestStorageAccountProvider.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test/Common/CustomTestStorageAccountProvider.cs b/test/Common/CustomTestStorageAccountProvider.cs index 3f6d3e45d..41337555a 100644 --- a/test/Common/CustomTestStorageAccountProvider.cs +++ b/test/Common/CustomTestStorageAccountProvider.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. +using System; using DurableTask.AzureStorage; using Microsoft.WindowsAzure.Storage; @@ -8,18 +9,25 @@ namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests { internal class CustomTestStorageAccountProvider : IStorageAccountProvider { - private const string CustomConnectionString = "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=dGVzdA==;EndpointSuffix=core.windows.net"; + private readonly string customConnectionString; private readonly string customConnectionName; public CustomTestStorageAccountProvider(string connectionName) { this.customConnectionName = connectionName; + this.customConnectionString = $"DefaultEndpointsProtocol=https;AccountName=test;AccountKey={GenerateRandomKey()};EndpointSuffix=core.windows.net"; } public CloudStorageAccount GetCloudStorageAccount(string name) => - CloudStorageAccount.Parse(name != this.customConnectionName ? TestHelpers.GetStorageConnectionString() : CustomConnectionString); + CloudStorageAccount.Parse(name != this.customConnectionName ? TestHelpers.GetStorageConnectionString() : this.customConnectionString); public StorageAccountDetails GetStorageAccountDetails(string name) => - new StorageAccountDetails { ConnectionString = name != this.customConnectionName ? TestHelpers.GetStorageConnectionString() : CustomConnectionString }; + new StorageAccountDetails { ConnectionString = name != this.customConnectionName ? TestHelpers.GetStorageConnectionString() : this.customConnectionString }; + + private static string GenerateRandomKey() + { + string key = Guid.NewGuid().ToString(); + return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(key)); + } } } From 7de358fd8a972ad74a5e5e7a11235a033f5dea7a Mon Sep 17 00:00:00 2001 From: Chris Gillum Date: Thu, 17 Oct 2024 10:32:52 -0700 Subject: [PATCH 3/3] Updating release notes --- release_notes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/release_notes.md b/release_notes.md index 4d172b565..aa232c219 100644 --- a/release_notes.md +++ b/release_notes.md @@ -6,6 +6,8 @@ ### Bug Fixes +- Fix custom connection name not working when using IDurableClientFactory.CreateClient() - contributed by [@hctan](https://github.com/hctan) + ### Breaking Changes ### Dependency Updates