From 3cd1224eaf63f4663ef979e176c8a279298dd893 Mon Sep 17 00:00:00 2001 From: Blair L Murri Date: Mon, 11 Nov 2024 11:47:59 -0800 Subject: [PATCH] Address flaky unit tests --- .../ArmEnvironmentEndpointsTests.cs | 2 +- src/CommonUtilities/AzureCloudConfig.cs | 29 +++++++++++++++++++ .../ExpensiveObjectTestUtility.cs | 15 ---------- .../TerraSamApiClientTests.cs | 2 +- .../TerraWsmApiClientTests.cs | 2 +- .../ArmUrlTransformationStrategyTests.cs | 2 +- ...riceApiBatchSkuInformationProviderTests.cs | 2 +- .../Runner/TaskToNodeTaskConverterTests.cs | 2 +- src/TesApi.Tests/StartupTests.cs | 2 +- .../DefaultStorageAccessProviderTests.cs | 2 +- .../TerraStorageAccessProviderTests.cs | 2 +- .../TestServices/TestServiceProvider.cs | 2 +- .../KubernetesManagerTests.cs | 2 +- 13 files changed, 40 insertions(+), 26 deletions(-) delete mode 100644 src/CommonUtilities/ExpensiveObjectTestUtility.cs diff --git a/src/CommonUtilities.Tests/ArmEnvironmentEndpointsTests.cs b/src/CommonUtilities.Tests/ArmEnvironmentEndpointsTests.cs index 4db1f88c4..85033b6c7 100644 --- a/src/CommonUtilities.Tests/ArmEnvironmentEndpointsTests.cs +++ b/src/CommonUtilities.Tests/ArmEnvironmentEndpointsTests.cs @@ -6,7 +6,7 @@ namespace CommonUtilities.Tests { - [TestClass] + [TestClass, TestCategory("Integration")] public class ArmEnvironmentEndpointsTests { public enum Cloud diff --git a/src/CommonUtilities/AzureCloudConfig.cs b/src/CommonUtilities/AzureCloudConfig.cs index a801e4a7d..feff2ed46 100644 --- a/src/CommonUtilities/AzureCloudConfig.cs +++ b/src/CommonUtilities/AzureCloudConfig.cs @@ -151,6 +151,35 @@ public static Task FromKnownCloudNameAsync(string cloudName = }; } + public static AzureCloudConfig ForUnitTesting() + { + return new() + { + PortalUrl = "http://portal.test", + Authentication = new() { Tenant = "common" }, + MediaUrl = "http://media.test", + GraphAudienceUrl = "http://graph.test", + Name = "Test", + Suffixes = new() + { + AcrLoginServerSuffix = "azurecr.io", + KeyVaultDnsSuffix = "vault.azure.net", + StorageSuffix = "core.windows.net", + PostgresqlServerEndpointSuffix = "postgres.database.azure.com" + }, + BatchUrl = "https://batch.core.windows.net/", + ResourceManagerUrl = "https://management.azure.com/", + MicrosoftGraphResourceUrl = "http://graph.test", + ApplicationInsightsResourceUrl = "https://api.applicationinsights.io", + ApplicationInsightsTelemetryChannelResourceUrl = "https://dc.applicationinsights.azure.com/v2/track", + + DefaultTokenScope = "https://management.azure.com//.default", + ArmEnvironment = new(new("https://management.azure.com/"), "https://management.azure.com/"), + Domain = "Test", + AzureEnvironmentConfig = new("https://login.microsoftonline.com", "https://management.azure.com//.default", "core.windows.net") + }; + } + private static readonly Uri AzurePublicCloud = Azure.ResourceManager.ArmEnvironment.AzurePublicCloud.Endpoint; private static readonly Uri AzureUSGovernmentCloud = Azure.ResourceManager.ArmEnvironment.AzureGovernment.Endpoint; private static readonly Uri AzureChinaCloud = Azure.ResourceManager.ArmEnvironment.AzureChina.Endpoint; diff --git a/src/CommonUtilities/ExpensiveObjectTestUtility.cs b/src/CommonUtilities/ExpensiveObjectTestUtility.cs deleted file mode 100644 index 7d3e28491..000000000 --- a/src/CommonUtilities/ExpensiveObjectTestUtility.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -using CommonUtilities.AzureCloud; - -namespace CommonUtilities -{ - /// - /// A utility to help with tests that require expensive objects to create - /// - public static class ExpensiveObjectTestUtility - { - public static AzureCloudConfig AzureCloudConfig = AzureCloudConfig.FromKnownCloudNameAsync().Result; - } -} diff --git a/src/Tes.ApiClients.Tests/TerraSamApiClientTests.cs b/src/Tes.ApiClients.Tests/TerraSamApiClientTests.cs index 372ec82e1..4a24ae2fb 100644 --- a/src/Tes.ApiClients.Tests/TerraSamApiClientTests.cs +++ b/src/Tes.ApiClients.Tests/TerraSamApiClientTests.cs @@ -30,7 +30,7 @@ public void SetUp() cache.Setup(c => c.CreateEntry(It.IsAny())).Returns(new Mock().Object); cacheAndRetryBuilder.SetupGet(c => c.AppCache).Returns(cache.Object); cacheAndRetryHandler = new(TestServices.RetryHandlersHelpers.GetCachingAsyncRetryPolicyMock(cacheAndRetryBuilder, c => c.DefaultRetryHttpResponseMessagePolicyBuilder())); - azureEnvironmentConfig = ExpensiveObjectTestUtility.AzureCloudConfig.AzureEnvironmentConfig!; + azureEnvironmentConfig = CommonUtilities.AzureCloud.AzureCloudConfig.ForUnitTesting().AzureEnvironmentConfig!; terraSamApiClient = new TerraSamApiClient(TerraApiStubData.SamApiHost, tokenCredential.Object, cacheAndRetryBuilder.Object, azureEnvironmentConfig, NullLogger.Instance); diff --git a/src/Tes.ApiClients.Tests/TerraWsmApiClientTests.cs b/src/Tes.ApiClients.Tests/TerraWsmApiClientTests.cs index 3e672d8c1..71bb960d8 100644 --- a/src/Tes.ApiClients.Tests/TerraWsmApiClientTests.cs +++ b/src/Tes.ApiClients.Tests/TerraWsmApiClientTests.cs @@ -32,7 +32,7 @@ public void SetUp() cache.Setup(c => c.CreateEntry(It.IsAny())).Returns(new Mock().Object); cacheAndRetryBuilder.SetupGet(c => c.AppCache).Returns(cache.Object); cacheAndRetryHandler = new(TestServices.RetryHandlersHelpers.GetCachingAsyncRetryPolicyMock(cacheAndRetryBuilder, c => c.DefaultRetryHttpResponseMessagePolicyBuilder())); - azureEnvironmentConfig = AzureEnvironmentConfig.FromArmEnvironmentEndpoints(CommonUtilities.AzureCloud.AzureCloudConfig.FromKnownCloudNameAsync(CommonUtilities.AzureCloud.AzureCloudConfig.DefaultAzureCloudName).Result); + azureEnvironmentConfig = AzureEnvironmentConfig.FromArmEnvironmentEndpoints(CommonUtilities.AzureCloud.AzureCloudConfig.ForUnitTesting()); terraWsmApiClient = new TerraWsmApiClient(TerraApiStubData.WsmApiHost, tokenCredential.Object, cacheAndRetryBuilder.Object, azureEnvironmentConfig, NullLogger.Instance); diff --git a/src/Tes.Runner.Test/Storage/ArmUrlTransformationStrategyTests.cs b/src/Tes.Runner.Test/Storage/ArmUrlTransformationStrategyTests.cs index b711c90f8..483b771a1 100644 --- a/src/Tes.Runner.Test/Storage/ArmUrlTransformationStrategyTests.cs +++ b/src/Tes.Runner.Test/Storage/ArmUrlTransformationStrategyTests.cs @@ -27,7 +27,7 @@ public void SetUp() mockBlobServiceClient = new Mock(); RuntimeOptions options = new() { - AzureEnvironmentConfig = AzureEnvironmentConfig.FromArmEnvironmentEndpoints(CommonUtilities.AzureCloud.AzureCloudConfig.FromKnownCloudNameAsync().Result) + AzureEnvironmentConfig = AzureEnvironmentConfig.FromArmEnvironmentEndpoints(CommonUtilities.AzureCloud.AzureCloudConfig.ForUnitTesting()) }; mockBlobApiHttpUtils = new(); diff --git a/src/TesApi.Tests/PriceApiBatchSkuInformationProviderTests.cs b/src/TesApi.Tests/PriceApiBatchSkuInformationProviderTests.cs index 546afd507..ebd91713a 100644 --- a/src/TesApi.Tests/PriceApiBatchSkuInformationProviderTests.cs +++ b/src/TesApi.Tests/PriceApiBatchSkuInformationProviderTests.cs @@ -33,7 +33,7 @@ public void Initialize() cachingRetryHandler = new CachingRetryPolicyBuilder(appCache, mockRetryOptions.Object); pricingApiClient = new PriceApiClient(cachingRetryHandler, new NullLogger()); - var config = ExpensiveObjectTestUtility.AzureCloudConfig; + var config = CommonUtilities.AzureCloud.AzureCloudConfig.FromKnownCloudNameAsync().Result; provider = new PriceApiBatchSkuInformationProvider(pricingApiClient, config, new NullLogger()); //using var serviceProvider = new TestServices.TestServiceProvider(); //var provider = serviceProvider.GetT(); diff --git a/src/TesApi.Tests/Runner/TaskToNodeTaskConverterTests.cs b/src/TesApi.Tests/Runner/TaskToNodeTaskConverterTests.cs index 21e64ee92..38d51d1ef 100644 --- a/src/TesApi.Tests/Runner/TaskToNodeTaskConverterTests.cs +++ b/src/TesApi.Tests/Runner/TaskToNodeTaskConverterTests.cs @@ -78,7 +78,7 @@ public void SetUp() x.GetBlobUrlsAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult>([])); - var azureCloudIdentityConfig = AzureCloudConfig.FromKnownCloudNameAsync().Result.AzureEnvironmentConfig; + var azureCloudIdentityConfig = AzureCloudConfig.ForUnitTesting().AzureEnvironmentConfig; taskToNodeTaskConverter = new TaskToNodeTaskConverter(Options.Create(terraOptions), Options.Create(storageOptions), storageAccessProviderMock.Object, azureProxyMock.Object, azureCloudIdentityConfig, new NullLogger()); } diff --git a/src/TesApi.Tests/StartupTests.cs b/src/TesApi.Tests/StartupTests.cs index 1ba6cb562..1665988b9 100644 --- a/src/TesApi.Tests/StartupTests.cs +++ b/src/TesApi.Tests/StartupTests.cs @@ -62,7 +62,7 @@ public void SetUp() services.AddSingleton(hostEnv.Object); #pragma warning restore CS0618 - Startup.AzureCloudConfig = AzureCloudConfig.FromKnownCloudNameAsync().Result; + Startup.AzureCloudConfig = AzureCloudConfig.ForUnitTesting(); var configuration = builder.Build(); services.AddSingleton(configuration); services.AddSingleton(configuration); diff --git a/src/TesApi.Tests/Storage/DefaultStorageAccessProviderTests.cs b/src/TesApi.Tests/Storage/DefaultStorageAccessProviderTests.cs index 56340821a..5ef13f140 100644 --- a/src/TesApi.Tests/Storage/DefaultStorageAccessProviderTests.cs +++ b/src/TesApi.Tests/Storage/DefaultStorageAccessProviderTests.cs @@ -43,7 +43,7 @@ public void Setup() }; azureProxyMock.Setup(p => p.GetStorageAccountKeyAsync(It.IsAny(), It.IsAny())).ReturnsAsync(GenerateRandomTestAzureStorageKey()); azureProxyMock.Setup(p => p.GetStorageAccountInfoAsync(It.Is(s => s.Equals(DefaultStorageAccountName)), It.IsAny())).ReturnsAsync(storageAccountInfo); - var config = ExpensiveObjectTestUtility.AzureCloudConfig.AzureEnvironmentConfig; + var config = CommonUtilities.AzureCloud.AzureCloudConfig.ForUnitTesting().AzureEnvironmentConfig; defaultStorageAccessProvider = new DefaultStorageAccessProvider(NullLogger.Instance, Options.Create(storageOptions), azureProxyMock.Object, config); } diff --git a/src/TesApi.Tests/TerraStorageAccessProviderTests.cs b/src/TesApi.Tests/TerraStorageAccessProviderTests.cs index 1e16a9501..196ba3984 100644 --- a/src/TesApi.Tests/TerraStorageAccessProviderTests.cs +++ b/src/TesApi.Tests/TerraStorageAccessProviderTests.cs @@ -48,7 +48,7 @@ public void SetUp() batchSchedulingOptions = new BatchSchedulingOptions() { Prefix = BatchSchedulingPrefix }; optionsMock.Setup(o => o.Value).Returns(terraOptions); azureProxyMock = new Mock(); - var config = ExpensiveObjectTestUtility.AzureCloudConfig.AzureEnvironmentConfig; + var config = CommonUtilities.AzureCloud.AzureCloudConfig.ForUnitTesting().AzureEnvironmentConfig; terraStorageAccessProvider = new TerraStorageAccessProvider(new(wsmApiClientMock.Object), azureProxyMock.Object, optionsMock.Object, Options.Create(batchSchedulingOptions), config, NullLogger.Instance); } diff --git a/src/TesApi.Tests/TestServices/TestServiceProvider.cs b/src/TesApi.Tests/TestServices/TestServiceProvider.cs index f44014f61..195b290ef 100644 --- a/src/TesApi.Tests/TestServices/TestServiceProvider.cs +++ b/src/TesApi.Tests/TestServices/TestServiceProvider.cs @@ -48,7 +48,7 @@ internal TestServiceProvider( Action additionalActions = default) { Configuration = GetConfiguration(configuration); - var azureCloudConfig = ExpensiveObjectTestUtility.AzureCloudConfig; + var azureCloudConfig = CommonUtilities.AzureCloud.AzureCloudConfig.ForUnitTesting(); provider = new ServiceCollection() .AddSingleton(_ => new TesServiceInfo { CreatedAt = DateTimeOffset.UtcNow, Environment = "unittest", Id = "unit-test-id", Organization = new() { Name = "unit-test-org", Url = "http://localhost/" }, Storage = [], UpdatedAt = DateTimeOffset.UtcNow }) .AddSingleton(azureCloudConfig) diff --git a/src/deploy-tes-on-azure.Tests/KubernetesManagerTests.cs b/src/deploy-tes-on-azure.Tests/KubernetesManagerTests.cs index cd5a02e30..2208ac8a1 100644 --- a/src/deploy-tes-on-azure.Tests/KubernetesManagerTests.cs +++ b/src/deploy-tes-on-azure.Tests/KubernetesManagerTests.cs @@ -14,7 +14,7 @@ public class KubernetesManagerTests [TestMethod] public async Task ValuesTemplateSuccessfullyDeserializesTesdatabaseToYaml() { - var manager = new KubernetesManager(new(), ExpensiveObjectTestUtility.AzureCloudConfig, (_, _, _) => throw new System.NotImplementedException(), System.Threading.CancellationToken.None); + var manager = new KubernetesManager(new(), CommonUtilities.AzureCloud.AzureCloudConfig.ForUnitTesting(), (_, _, _) => throw new System.NotImplementedException(), System.Threading.CancellationToken.None); var helmValues = await manager.GetHelmValuesAsync(@"./cromwell-on-azure/helm/values-template.yaml"); Assert.IsNotNull(helmValues.TesDatabase); }