Skip to content
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

Fix endpoint suffix (Azure China) not being used #10112

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Ng/CommandHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ private static CatalogStorageFactory CreateStorageFactoryImpl(
var storageAccountName = arguments.GetOrThrow<string>(argumentNameMap[Arguments.StorageAccountName]);
var storageContainer = arguments.GetOrThrow<string>(argumentNameMap[Arguments.StorageContainer]);
var storagePath = arguments.GetOrDefault<string>(argumentNameMap[Arguments.StoragePath]);
var storageSuffix = arguments.GetOrDefault<string>(argumentNameMap[Arguments.StorageSuffix]);
var storageSuffix = arguments.GetOrDefault(argumentNameMap[Arguments.StorageSuffix], "core.windows.net");
var storageOperationMaxExecutionTime = MaxExecutionTime(arguments.GetOrDefault<int>(argumentNameMap[Arguments.StorageOperationMaxExecutionTimeInSeconds]));
var storageServerTimeout = MaxExecutionTime(arguments.GetOrDefault<int>(argumentNameMap[Arguments.StorageServerTimeoutInSeconds]));
var storageUseServerSideCopy = arguments.GetOrDefault<bool>(argumentNameMap[Arguments.StorageUseServerSideCopy]);
var storageInitializeContainer = arguments.GetOrDefault<bool>(argumentNameMap[Arguments.StorageInitializeContainer], defaultValue: true);
var storageInitializeContainer = arguments.GetOrDefault(argumentNameMap[Arguments.StorageInitializeContainer], defaultValue: true);

BlobServiceClient account = GetBlobServiceClient(storageAccountName, storageSuffix, arguments, argumentNameMap);

Expand Down Expand Up @@ -382,7 +382,7 @@ private static BlobServiceClient GetBlobServiceClient(string storageAccountName,
storageSasValue = storageSasValue.Substring(1);
}

connectionString = $"BlobEndpoint=https://{storageAccountName}.blob.core.windows.net/;SharedAccessSignature={storageSasValue}";
connectionString = $"BlobEndpoint=https://{storageAccountName}.blob.{endpointSuffix}/;SharedAccessSignature={storageSasValue}";
}
else
{
Expand All @@ -401,7 +401,7 @@ private static QueueServiceClient GetQueueServiceClient(string storageAccountNam
if (string.IsNullOrEmpty(storageKeyValue))
{
var storageSasValue = arguments.GetOrThrow<string>(argumentNameMap[Arguments.StorageSasValue]);
connectionString = $"BlobEndpoint=https://{storageAccountName}.blob.core.windows.net/;SharedAccessSignature={storageSasValue}";
connectionString = $"BlobEndpoint=https://{storageAccountName}.blob.{endpointSuffix}/;SharedAccessSignature={storageSasValue}";
}
else
{
Expand Down
59 changes: 39 additions & 20 deletions tests/NgTests/StorageFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
Expand All @@ -11,33 +11,52 @@ namespace NgTests
{
public class StorageFactoryTests
{
[Theory]
[Description("The regular azure factory should not compress the content if.")]
[InlineData("http://localhost/reg", "testAccount", "DummyDUMMYpZxLeDumMyyN52gJj+ZlGE0ipRi9PaTcn9AU4epwvsngE5rLSMk9TwpazxUtzeyBnFeWFAdummyw==", "testContainer", "testStoragePath", "azure", "core.windows.net")]
public void AzureFactory(string storageBaseAddress,
string storageAccountName,
string storageKeyValue,
string storageContainer,
string storagePath,
string storageType,
string storageSuffix)
private const string DummyKey = "DummyDUMMYpZxLeDumMyyN52gJj+ZlGE0ipRi9PaTcn9AU4epwvsngE5rLSMk9TwpazxUtzeyBnFeWFAdummyw==";

[Fact]
public void AzureFactory_DefaultsToAzurePublicWithNoCompression()
{
// Arrange
Dictionary<string, string> arguments = new Dictionary<string, string>()
{
{ Arguments.StorageBaseAddress, storageBaseAddress },
{ Arguments.StorageAccountName, storageAccountName },
{ Arguments.StorageKeyValue, storageKeyValue },
{ Arguments.StorageContainer, storageContainer },
{ Arguments.StoragePath, storagePath },
{ Arguments.StorageType, storageType},
{ Arguments.StorageSuffix, storageSuffix} // Without BlobServiceClient couldn't create new instance.
{ Arguments.StorageBaseAddress, "http://localhost/reg" },
{ Arguments.StorageAccountName, "testAccount" },
{ Arguments.StorageKeyValue, DummyKey },
{ Arguments.StorageContainer, "testContainer" },
{ Arguments.StoragePath, "testStoragePath" },
{ Arguments.StorageType, "azure" },
};

// Act
StorageFactory factory = CommandHelpers.CreateStorageFactory(arguments, true);
AzureStorageFactory azureFactory = factory as AzureStorageFactory;

// Assert
Assert.True(azureFactory != null, "The CreateCompressedStorageFactory should return an AzureStorageFactory type.");
var azureFactory = Assert.IsType<AzureStorageFactory>(factory);
Assert.False(azureFactory.CompressContent, "The azure storage factory should not compress the content.");
Assert.Equal("https://testaccount.blob.core.windows.net/testContainer/testStoragePath/", azureFactory.DestinationAddress.AbsoluteUri);
}

[Fact]
public void AzureFactory_AllowsCustomStorageSuffix()
{
// Arrange
Dictionary<string, string> arguments = new Dictionary<string, string>()
{
{ Arguments.StorageBaseAddress, "http://localhost/reg" },
{ Arguments.StorageAccountName, "testAccount" },
{ Arguments.StorageKeyValue, DummyKey },
{ Arguments.StorageContainer, "testContainer" },
{ Arguments.StoragePath, "testStoragePath" },
{ Arguments.StorageType, "azure" },
{ Arguments.StorageSuffix, "core.chinacloudapi.cn" },
};

// Act
StorageFactory factory = CommandHelpers.CreateStorageFactory(arguments, true);

// Assert
var azureFactory = Assert.IsType<AzureStorageFactory>(factory);
Assert.Equal("https://testaccount.blob.core.chinacloudapi.cn/testContainer/testStoragePath/", azureFactory.DestinationAddress.AbsoluteUri);
}
}
}