From 02898b1a02caf2202694a8629f27c5f430e5bb87 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Mon, 28 Oct 2024 18:14:22 -0500 Subject: [PATCH 1/2] Extract EventHubs functional test to Azure tests. --- .../IntegrationServicesFixture.cs | 2 - .../IntegrationServicesTests.cs | 5 -- .../Aspire.Hosting.Azure.Tests.csproj | 4 +- .../AzureEventHubsExtensionsTests.cs | 49 +++++++++++++++++-- tests/cosmos.module.bicep | 37 -------------- tests/eventhubns.module.bicep | 36 -------------- tests/testproject/Common/TestResourceNames.cs | 3 +- .../TestProject.AppHost/TestProgram.cs | 5 -- .../TestProject.AppHost.csproj | 2 - .../EventHubs/EventHubsExtensions.cs | 36 -------------- .../Program.cs | 23 --------- .../TestProject.IntegrationServiceA.csproj | 1 - 12 files changed, 47 insertions(+), 156 deletions(-) delete mode 100644 tests/cosmos.module.bicep delete mode 100644 tests/eventhubns.module.bicep delete mode 100644 tests/testproject/TestProject.IntegrationServiceA/EventHubs/EventHubsExtensions.cs diff --git a/tests/Aspire.EndToEnd.Tests/IntegrationServicesFixture.cs b/tests/Aspire.EndToEnd.Tests/IntegrationServicesFixture.cs index 416e09c528c..6c8685d4cbb 100644 --- a/tests/Aspire.EndToEnd.Tests/IntegrationServicesFixture.cs +++ b/tests/Aspire.EndToEnd.Tests/IntegrationServicesFixture.cs @@ -101,7 +101,6 @@ public Task DumpComponentLogsAsync(TestResourceNames resource, ITestOutputHelper string component = resource switch { TestResourceNames.cosmos or TestResourceNames.efcosmos => "cosmos", - TestResourceNames.eventhubs => "eventhubs", TestResourceNames.postgres or TestResourceNames.efnpgsql => "postgres", TestResourceNames.redis => "redis", _ => throw new ArgumentException($"Unknown resource: {resource}") @@ -134,7 +133,6 @@ private static TestResourceNames GetResourcesToSkip() TestResourceNames resourcesToInclude = TestScenario switch { "cosmos" => TestResourceNames.cosmos | TestResourceNames.efcosmos, - "eventhubs" => TestResourceNames.eventhubs, "basicservices" => TestResourceNames.redis | TestResourceNames.postgres | TestResourceNames.efnpgsql, diff --git a/tests/Aspire.EndToEnd.Tests/IntegrationServicesTests.cs b/tests/Aspire.EndToEnd.Tests/IntegrationServicesTests.cs index 208d1da67ad..5bebfad1b19 100644 --- a/tests/Aspire.EndToEnd.Tests/IntegrationServicesTests.cs +++ b/tests/Aspire.EndToEnd.Tests/IntegrationServicesTests.cs @@ -44,11 +44,6 @@ public Task VerifyComponentWorks(TestResourceNames resourceName) } }); - [Fact] - [Trait("scenario", "eventhubs")] - public Task VerifyAzureEventHubsComponentWorks() - => VerifyComponentWorks(TestResourceNames.eventhubs); - [ActiveIssue("https://github.com/dotnet/aspire/issues/5820")] [ConditionalTheory] [Trait("scenario", "cosmos")] diff --git a/tests/Aspire.Hosting.Azure.Tests/Aspire.Hosting.Azure.Tests.csproj b/tests/Aspire.Hosting.Azure.Tests/Aspire.Hosting.Azure.Tests.csproj index 04321282d39..77c47d23db0 100644 --- a/tests/Aspire.Hosting.Azure.Tests/Aspire.Hosting.Azure.Tests.csproj +++ b/tests/Aspire.Hosting.Azure.Tests/Aspire.Hosting.Azure.Tests.csproj @@ -23,11 +23,11 @@ + + - - diff --git a/tests/Aspire.Hosting.Azure.Tests/AzureEventHubsExtensionsTests.cs b/tests/Aspire.Hosting.Azure.Tests/AzureEventHubsExtensionsTests.cs index dd1e9876672..1e98ec6bfdf 100644 --- a/tests/Aspire.Hosting.Azure.Tests/AzureEventHubsExtensionsTests.cs +++ b/tests/Aspire.Hosting.Azure.Tests/AzureEventHubsExtensionsTests.cs @@ -1,13 +1,18 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Aspire.Hosting.Utils; -using Aspire.Hosting.Azure.EventHubs; -using Xunit; +using System.Text; +using Aspire.Components.Common.Tests; using Aspire.Hosting.ApplicationModel; +using Aspire.Hosting.Azure.EventHubs; +using Aspire.Hosting.Utils; +using Azure.Messaging.EventHubs; +using Azure.Messaging.EventHubs.Consumer; +using Azure.Messaging.EventHubs.Producer; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; -using Aspire.Components.Common.Tests; +using Microsoft.Extensions.Hosting; +using Xunit; using Xunit.Abstractions; namespace Aspire.Hosting.Azure.Tests; @@ -56,6 +61,40 @@ public async Task VerifyWaitForOnEventHubsEmulatorBlocksDependentResources() await app.StopAsync(); } + [Fact] + [RequiresDocker] + public async Task VerifyAzureEventHubsEmulatorResource() + { + using var builder = TestDistributedApplicationBuilder.Create().WithTestAndResourceLogging(testOutputHelper); + var eventHub = builder.AddAzureEventHubs("eventhubns") + .RunAsEmulator() + .AddEventHub("hub"); + + using var app = builder.Build(); + await app.StartAsync(); + + var hb = Host.CreateApplicationBuilder(); + hb.Configuration["ConnectionStrings:eventhubns"] = await eventHub.Resource.ConnectionStringExpression.GetValueAsync(CancellationToken.None); + hb.AddAzureEventHubProducerClient("eventhubns", settings => settings.EventHubName = "hub"); + hb.AddAzureEventHubConsumerClient("eventhubns", settings => settings.EventHubName = "hub"); + + using var host = hb.Build(); + await host.StartAsync(); + + var producerClient = host.Services.GetRequiredService(); + var consumerClient = host.Services.GetRequiredService(); + + // If no exception is thrown when awaited, the Event Hubs service has acknowledged + // receipt and assumed responsibility for delivery of the set of events to its partition. + await producerClient.SendAsync([new EventData(Encoding.UTF8.GetBytes("hello worlds"))]); + + await foreach (var partitionEvent in consumerClient.ReadEventsAsync(new ReadEventOptions { MaximumWaitTime = TimeSpan.FromSeconds(5) })) + { + Assert.Equal("hello worlds", Encoding.UTF8.GetString(partitionEvent.Data.EventBody.ToArray())); + break; + } + } + [Fact] public void AzureEventHubsUseEmulatorCallbackWithWithDataBindMountResultsInBindMountAnnotationWithDefaultPath() { @@ -79,7 +118,7 @@ public void AzureEventHubsUseEmulatorCallbackWithWithDataBindMountResultsInBindM using var builder = TestDistributedApplicationBuilder.Create(); var eventHubs = builder.AddAzureEventHubs("eh").RunAsEmulator(configureContainer: builder => { - builder.WithDataBindMount("mydata"); + builder.WithDataBindMount("mydata"); }); // Ignoring the annotation created for the custom Config.json file diff --git a/tests/cosmos.module.bicep b/tests/cosmos.module.bicep deleted file mode 100644 index cf2940e9ef9..00000000000 --- a/tests/cosmos.module.bicep +++ /dev/null @@ -1,37 +0,0 @@ -@description('The location for the resource(s) to be deployed.') -param location string = resourceGroup().location - -param keyVaultName string - -resource keyVault 'Microsoft.KeyVault/vaults@2023-07-01' existing = { - name: keyVaultName -} - -resource cosmos 'Microsoft.DocumentDB/databaseAccounts@2024-08-15' = { - name: take('cosmos-${uniqueString(resourceGroup().id)}', 44) - location: location - properties: { - locations: [ - { - locationName: location - failoverPriority: 0 - } - ] - consistencyPolicy: { - defaultConsistencyLevel: 'Session' - } - databaseAccountOfferType: 'Standard' - } - kind: 'GlobalDocumentDB' - tags: { - 'aspire-resource-name': 'cosmos' - } -} - -resource connectionString 'Microsoft.KeyVault/vaults/secrets@2023-07-01' = { - name: 'connectionString' - properties: { - value: 'AccountEndpoint=${cosmos.properties.documentEndpoint};AccountKey=${cosmos.listKeys().primaryMasterKey}' - } - parent: keyVault -} \ No newline at end of file diff --git a/tests/eventhubns.module.bicep b/tests/eventhubns.module.bicep deleted file mode 100644 index c013957eb54..00000000000 --- a/tests/eventhubns.module.bicep +++ /dev/null @@ -1,36 +0,0 @@ -@description('The location for the resource(s) to be deployed.') -param location string = resourceGroup().location - -param sku string = 'Standard' - -param principalId string - -param principalType string - -resource eventhubns 'Microsoft.EventHub/namespaces@2024-01-01' = { - name: take('eventhubns-${uniqueString(resourceGroup().id)}', 256) - location: location - sku: { - name: sku - } - tags: { - 'aspire-resource-name': 'eventhubns' - } -} - -resource eventhubns_AzureEventHubsDataOwner 'Microsoft.Authorization/roleAssignments@2022-04-01' = { - name: guid(eventhubns.id, principalId, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'f526a384-b230-433a-b45c-95f59c4a2dec')) - properties: { - principalId: principalId - roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'f526a384-b230-433a-b45c-95f59c4a2dec') - principalType: principalType - } - scope: eventhubns -} - -resource hub 'Microsoft.EventHub/namespaces/eventhubs@2024-01-01' = { - name: 'hub' - parent: eventhubns -} - -output eventHubsEndpoint string = eventhubns.properties.serviceBusEndpoint \ No newline at end of file diff --git a/tests/testproject/Common/TestResourceNames.cs b/tests/testproject/Common/TestResourceNames.cs index 80ec042b6c2..3ad672c324c 100644 --- a/tests/testproject/Common/TestResourceNames.cs +++ b/tests/testproject/Common/TestResourceNames.cs @@ -12,9 +12,8 @@ public enum TestResourceNames postgres = 1 << 7, redis = 1 << 9, efnpgsql = 1 << 11, - eventhubs = 1 << 13, efcosmos = 1 << 17, - All = cosmos | dashboard | postgres | redis | efnpgsql | eventhubs | efcosmos + All = cosmos | dashboard | postgres | redis | efnpgsql | efcosmos } public static class TestResourceNamesExtensions diff --git a/tests/testproject/TestProject.AppHost/TestProgram.cs b/tests/testproject/TestProject.AppHost/TestProgram.cs index e6956dc2226..ab1935cd627 100644 --- a/tests/testproject/TestProject.AppHost/TestProgram.cs +++ b/tests/testproject/TestProject.AppHost/TestProgram.cs @@ -88,11 +88,6 @@ private TestProgram( var cosmos = AppBuilder.AddAzureCosmosDB("cosmos").RunAsEmulator(); IntegrationServiceABuilder = IntegrationServiceABuilder.WithReference(cosmos); } - if (!resourcesToSkip.HasFlag(TestResourceNames.eventhubs)) - { - var eventHub = AppBuilder.AddAzureEventHubs("eventhubns").RunAsEmulator().AddEventHub("hub"); - IntegrationServiceABuilder = IntegrationServiceABuilder.WithReference(eventHub); - } } AppBuilder.Services.AddHostedService(); diff --git a/tests/testproject/TestProject.AppHost/TestProject.AppHost.csproj b/tests/testproject/TestProject.AppHost/TestProject.AppHost.csproj index 52ec70b694f..b3c4761818e 100644 --- a/tests/testproject/TestProject.AppHost/TestProject.AppHost.csproj +++ b/tests/testproject/TestProject.AppHost/TestProject.AppHost.csproj @@ -9,9 +9,7 @@ - - diff --git a/tests/testproject/TestProject.IntegrationServiceA/EventHubs/EventHubsExtensions.cs b/tests/testproject/TestProject.IntegrationServiceA/EventHubs/EventHubsExtensions.cs deleted file mode 100644 index c194898ac43..00000000000 --- a/tests/testproject/TestProject.IntegrationServiceA/EventHubs/EventHubsExtensions.cs +++ /dev/null @@ -1,36 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Text; -using Azure.Messaging.EventHubs; -using Azure.Messaging.EventHubs.Producer; -using Azure.Messaging.EventHubs.Consumer; - -public static class EventHubsExtensions -{ - public static void MapEventHubsApi(this WebApplication app) - { - app.MapGet("/eventhubs/verify", VerifyEventHubsAsync); - } - - private static async Task VerifyEventHubsAsync(EventHubProducerClient producerClient, EventHubConsumerClient consumerClient) - { - try - { - // If no exception is thrown when awaited, the Event Hubs service has acknowledged - // receipt and assumed responsibility for delivery of the set of events to its partition. - await producerClient.SendAsync([new EventData(Encoding.UTF8.GetBytes("hello worlds"))]); - - await foreach (var partition in consumerClient.ReadEventsAsync(new ReadEventOptions { MaximumWaitTime = TimeSpan.FromSeconds(5) })) - { - return Results.Ok(); - } - - return Results.Problem("No events were read."); - } - catch (Exception e) - { - return Results.Problem($"Error: {e}{Environment.NewLine}**"); - } - } -} diff --git a/tests/testproject/TestProject.IntegrationServiceA/Program.cs b/tests/testproject/TestProject.IntegrationServiceA/Program.cs index 6cf3f820afe..368e8644753 100644 --- a/tests/testproject/TestProject.IntegrationServiceA/Program.cs +++ b/tests/testproject/TestProject.IntegrationServiceA/Program.cs @@ -21,11 +21,6 @@ { builder.AddNpgsqlDbContext("postgresdb"); } -if (!resourcesToSkip.HasFlag(TestResourceNames.eventhubs)) -{ - builder.AddAzureEventHubProducerClient("eventhubsns", settings => settings.EventHubName = "hub"); - builder.AddAzureEventHubConsumerClient("eventhubsns", settings => settings.EventHubName = "hub"); -} if (!resourcesToSkip.HasFlag(TestResourceNames.cosmos) || !resourcesToSkip.HasFlag(TestResourceNames.efcosmos)) { @@ -37,19 +32,6 @@ builder.AddCosmosDbContext("cosmos", "cosmos"); } -if (!resourcesToSkip.HasFlag(TestResourceNames.eventhubs)) -{ - builder.AddAzureEventHubProducerClient("eventhubns", settings => - { - settings.EventHubName = "hub"; - }); - - builder.AddAzureEventHubConsumerClient("eventhubns", settings => - { - settings.EventHubName = "hub"; - }); -} - // Ensure healthChecks are added. Some components like Cosmos // don't add this builder.Services.AddHealthChecks(); @@ -86,9 +68,4 @@ app.MapEFCoreCosmosApi(); } -if (!resourcesToSkip.HasFlag(TestResourceNames.eventhubs)) -{ - app.MapEventHubsApi(); -} - app.Run(); diff --git a/tests/testproject/TestProject.IntegrationServiceA/TestProject.IntegrationServiceA.csproj b/tests/testproject/TestProject.IntegrationServiceA/TestProject.IntegrationServiceA.csproj index bdfd67ce17f..8c691dd14b2 100644 --- a/tests/testproject/TestProject.IntegrationServiceA/TestProject.IntegrationServiceA.csproj +++ b/tests/testproject/TestProject.IntegrationServiceA/TestProject.IntegrationServiceA.csproj @@ -15,7 +15,6 @@ - From 30402f3b997e6509181abce957c7bef9dd17cd58 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Mon, 28 Oct 2024 18:39:44 -0500 Subject: [PATCH 2/2] Extract CosmosDB tests from EndToEnd tests --- .../IntegrationServicesFixture.cs | 17 ------- .../IntegrationServicesTests.cs | 20 -------- .../Aspire.Hosting.Azure.Tests.csproj | 1 + .../AzureCosmosDBEmulatorFunctionalTests.cs | 29 +++++++---- .../DistributedApplicationTests.cs | 4 +- .../ManifestGenerationTests.cs | 21 +------- tests/testproject/Common/TestResourceNames.cs | 4 +- .../TestProject.AppHost/TestProgram.cs | 5 -- .../TestProject.AppHost.csproj | 1 - .../Cosmos/CosmosExtensions.cs | 48 ------------------- .../Cosmos/EFCoreCosmosDbContext.cs | 14 ------ .../Cosmos/EFCoreCosmosExtensions.cs | 25 ---------- .../Program.cs | 20 -------- .../TestProject.IntegrationServiceA.csproj | 2 - 14 files changed, 25 insertions(+), 186 deletions(-) delete mode 100644 tests/testproject/TestProject.IntegrationServiceA/Cosmos/CosmosExtensions.cs delete mode 100644 tests/testproject/TestProject.IntegrationServiceA/Cosmos/EFCoreCosmosDbContext.cs delete mode 100644 tests/testproject/TestProject.IntegrationServiceA/Cosmos/EFCoreCosmosExtensions.cs diff --git a/tests/Aspire.EndToEnd.Tests/IntegrationServicesFixture.cs b/tests/Aspire.EndToEnd.Tests/IntegrationServicesFixture.cs index 6c8685d4cbb..2528adde178 100644 --- a/tests/Aspire.EndToEnd.Tests/IntegrationServicesFixture.cs +++ b/tests/Aspire.EndToEnd.Tests/IntegrationServicesFixture.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Runtime.InteropServices; using Xunit; using Xunit.Abstractions; using Aspire.TestProject; @@ -100,7 +99,6 @@ public Task DumpComponentLogsAsync(TestResourceNames resource, ITestOutputHelper string component = resource switch { - TestResourceNames.cosmos or TestResourceNames.efcosmos => "cosmos", TestResourceNames.postgres or TestResourceNames.efnpgsql => "postgres", TestResourceNames.redis => "redis", _ => throw new ArgumentException($"Unknown resource: {resource}") @@ -132,7 +130,6 @@ private static TestResourceNames GetResourcesToSkip() { TestResourceNames resourcesToInclude = TestScenario switch { - "cosmos" => TestResourceNames.cosmos | TestResourceNames.efcosmos, "basicservices" => TestResourceNames.redis | TestResourceNames.postgres | TestResourceNames.efnpgsql, @@ -142,20 +139,6 @@ private static TestResourceNames GetResourcesToSkip() TestResourceNames resourcesToSkip = TestResourceNames.All & ~resourcesToInclude; - // always skip cosmos on macos/arm64 - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && RuntimeInformation.ProcessArchitecture == Architecture.Arm64) - { - resourcesToSkip |= TestResourceNames.cosmos; - } - if (string.IsNullOrEmpty(TestScenario)) - { - // no scenario specified - if (BuildEnvironment.IsRunningOnCI) - { - resourcesToSkip |= TestResourceNames.cosmos; - } - } - // always skip the dashboard resourcesToSkip |= TestResourceNames.dashboard; diff --git a/tests/Aspire.EndToEnd.Tests/IntegrationServicesTests.cs b/tests/Aspire.EndToEnd.Tests/IntegrationServicesTests.cs index 5bebfad1b19..226686adffd 100644 --- a/tests/Aspire.EndToEnd.Tests/IntegrationServicesTests.cs +++ b/tests/Aspire.EndToEnd.Tests/IntegrationServicesTests.cs @@ -1,12 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Runtime.InteropServices; using Xunit; using Xunit.Abstractions; using Aspire.TestProject; using Aspire.Workload.Tests; -using Microsoft.DotNet.XUnitExtensions; namespace Aspire.EndToEnd.Tests; @@ -44,25 +42,7 @@ public Task VerifyComponentWorks(TestResourceNames resourceName) } }); - [ActiveIssue("https://github.com/dotnet/aspire/issues/5820")] - [ConditionalTheory] - [Trait("scenario", "cosmos")] - [InlineData(TestResourceNames.cosmos)] - [InlineData(TestResourceNames.efcosmos)] - public Task VerifyCosmosComponentWorks(TestResourceNames resourceName) - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && RuntimeInformation.ProcessArchitecture == Architecture.Arm64) - { - throw new SkipTestException($"Skipping 'cosmos' test because the emulator isn't supported on macOS ARM64."); - } - - return VerifyComponentWorks(resourceName); - } - [Fact] - // Include all the scenarios here so this test gets run for all of them. - // https://github.com/dotnet/aspire/issues/5820 - // [Trait("scenario", "cosmos")] [Trait("scenario", "basicservices")] public Task VerifyHealthyOnIntegrationServiceA() => RunTestAsync(async () => diff --git a/tests/Aspire.Hosting.Azure.Tests/Aspire.Hosting.Azure.Tests.csproj b/tests/Aspire.Hosting.Azure.Tests/Aspire.Hosting.Azure.Tests.csproj index 77c47d23db0..c28a6677a88 100644 --- a/tests/Aspire.Hosting.Azure.Tests/Aspire.Hosting.Azure.Tests.csproj +++ b/tests/Aspire.Hosting.Azure.Tests/Aspire.Hosting.Azure.Tests.csproj @@ -26,6 +26,7 @@ + diff --git a/tests/Aspire.Hosting.Azure.Tests/AzureCosmosDBEmulatorFunctionalTests.cs b/tests/Aspire.Hosting.Azure.Tests/AzureCosmosDBEmulatorFunctionalTests.cs index eae1d8c5b25..808ec38068f 100644 --- a/tests/Aspire.Hosting.Azure.Tests/AzureCosmosDBEmulatorFunctionalTests.cs +++ b/tests/Aspire.Hosting.Azure.Tests/AzureCosmosDBEmulatorFunctionalTests.cs @@ -5,6 +5,7 @@ using Aspire.Hosting.ApplicationModel; using Aspire.Hosting.Utils; using Microsoft.Azure.Cosmos; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; @@ -59,7 +60,7 @@ public async Task VerifyWaitForOnCosmosDBEmulatorBlocksDependentResources() await app.StopAsync(); } - [Fact(Skip = "Using CosmosDB emulator in integration tests leads to flaky tests")] + [Fact(Skip = "Using CosmosDB emulator in integration tests leads to flaky tests - https://github.com/dotnet/aspire/issues/5820")] [RequiresDocker(Reason = "CosmosDB emulator is needed for this test")] public async Task VerifyCosmosResource() { @@ -89,13 +90,9 @@ public async Task VerifyCosmosResource() await app.StartAsync(); var hb = Host.CreateApplicationBuilder(); - - hb.Configuration.AddInMemoryCollection(new Dictionary - { - [$"ConnectionStrings:{db.Resource.Name}"] = await db.Resource.ConnectionStringExpression.GetValueAsync(default) - }); - + hb.Configuration[$"ConnectionStrings:{db.Resource.Name}"] = await db.Resource.ConnectionStringExpression.GetValueAsync(default); hb.AddAzureCosmosClient(db.Resource.Name); + hb.AddCosmosDbContext(db.Resource.Name, databaseName); using var host = hb.Build(); @@ -104,6 +101,7 @@ public async Task VerifyCosmosResource() // This needs to be outside the pipeline because when the CosmosClient is disposed, // there is an exception in the pipeline using var cosmosClient = host.Services.GetRequiredService(); + using var dbContext = host.Services.GetRequiredService(); await pipeline.ExecuteAsync(async token => { @@ -115,10 +113,15 @@ await pipeline.ExecuteAsync(async token => Assert.True(results.Count == 1); Assert.True(results.First() == 1); + + await dbContext.Database.EnsureCreatedAsync(token); + dbContext.AddRange([new Entry(), new Entry()]); + var count = await dbContext.SaveChangesAsync(token); + Assert.Equal(2, count); }, cts.Token); } - [Fact(Skip = "Using CosmosDB emulator in integration tests leads to flaky tests")] + [Fact(Skip = "Using CosmosDB emulator in integration tests leads to flaky tests - https://github.com/dotnet/aspire/issues/5820")] [RequiresDocker] public async Task WithDataVolumeShouldPersistStateBetweenUsages() { @@ -246,3 +249,13 @@ await pipeline.ExecuteAsync(async token => DockerUtils.AttemptDeleteDockerVolume(volumeName); } } + +public class EFCoreCosmosDbContext(DbContextOptions options) : DbContext(options) +{ + public DbSet Entries { get; set; } +} + +public record Entry +{ + public Guid Id { get; set; } = Guid.NewGuid(); +} diff --git a/tests/Aspire.Hosting.Tests/DistributedApplicationTests.cs b/tests/Aspire.Hosting.Tests/DistributedApplicationTests.cs index 1fdf99710de..cf52f1fad6f 100644 --- a/tests/Aspire.Hosting.Tests/DistributedApplicationTests.cs +++ b/tests/Aspire.Hosting.Tests/DistributedApplicationTests.cs @@ -647,9 +647,7 @@ public async Task KubernetesHasResourceNameForContainersAndExes() var expectedContainerResources = new HashSet() { "redis", - "postgres", - "cosmos", - "eventhubns" + "postgres" }; await foreach (var resource in s.WatchAsync(cancellationToken: token)) diff --git a/tests/Aspire.Hosting.Tests/ManifestGenerationTests.cs b/tests/Aspire.Hosting.Tests/ManifestGenerationTests.cs index 43a0d1c7d29..658e04c68af 100644 --- a/tests/Aspire.Hosting.Tests/ManifestGenerationTests.cs +++ b/tests/Aspire.Hosting.Tests/ManifestGenerationTests.cs @@ -417,9 +417,7 @@ public void VerifyTestProgramFullManifest() "HTTP_PORTS": "{integrationservicea.bindings.http.targetPort}", "SKIP_RESOURCES": "None", "ConnectionStrings__redis": "{redis.connectionString}", - "ConnectionStrings__postgresdb": "{postgresdb.connectionString}", - "ConnectionStrings__cosmos": "{cosmos.connectionString}", - "ConnectionStrings__eventhubns": "{eventhubns.connectionString}" + "ConnectionStrings__postgresdb": "{postgresdb.connectionString}" }, "bindings": { "http": { @@ -471,23 +469,6 @@ public void VerifyTestProgramFullManifest() "type": "value.v0", "connectionString": "{postgres.connectionString};Database=postgresdb" }, - "cosmos": { - "type": "azure.bicep.v0", - "connectionString": "{cosmos.secretOutputs.connectionString}", - "path": "cosmos.module.bicep", - "params": { - "keyVaultName": "" - } - }, - "eventhubns": { - "type": "azure.bicep.v0", - "connectionString": "{eventhubns.outputs.eventHubsEndpoint}", - "path": "eventhubns.module.bicep", - "params": { - "principalId": "", - "principalType": "" - } - }, "postgres-password": { "type": "parameter.v0", "value": "{postgres-password.inputs.value}", diff --git a/tests/testproject/Common/TestResourceNames.cs b/tests/testproject/Common/TestResourceNames.cs index 3ad672c324c..205c47e95f0 100644 --- a/tests/testproject/Common/TestResourceNames.cs +++ b/tests/testproject/Common/TestResourceNames.cs @@ -7,13 +7,11 @@ namespace Aspire.TestProject; public enum TestResourceNames { None = 0, - cosmos = 1 << 0, dashboard = 1 << 1, postgres = 1 << 7, redis = 1 << 9, efnpgsql = 1 << 11, - efcosmos = 1 << 17, - All = cosmos | dashboard | postgres | redis | efnpgsql | efcosmos + All = dashboard | postgres | redis | efnpgsql } public static class TestResourceNamesExtensions diff --git a/tests/testproject/TestProject.AppHost/TestProgram.cs b/tests/testproject/TestProject.AppHost/TestProgram.cs index ab1935cd627..a38a3be630b 100644 --- a/tests/testproject/TestProject.AppHost/TestProgram.cs +++ b/tests/testproject/TestProject.AppHost/TestProgram.cs @@ -83,11 +83,6 @@ private TestProgram( .AddDatabase(postgresDbName); IntegrationServiceABuilder = IntegrationServiceABuilder.WithReference(postgres); } - if (!resourcesToSkip.HasFlag(TestResourceNames.cosmos) || !resourcesToSkip.HasFlag(TestResourceNames.efcosmos)) - { - var cosmos = AppBuilder.AddAzureCosmosDB("cosmos").RunAsEmulator(); - IntegrationServiceABuilder = IntegrationServiceABuilder.WithReference(cosmos); - } } AppBuilder.Services.AddHostedService(); diff --git a/tests/testproject/TestProject.AppHost/TestProject.AppHost.csproj b/tests/testproject/TestProject.AppHost/TestProject.AppHost.csproj index b3c4761818e..2995a30adf7 100644 --- a/tests/testproject/TestProject.AppHost/TestProject.AppHost.csproj +++ b/tests/testproject/TestProject.AppHost/TestProject.AppHost.csproj @@ -9,7 +9,6 @@ - diff --git a/tests/testproject/TestProject.IntegrationServiceA/Cosmos/CosmosExtensions.cs b/tests/testproject/TestProject.IntegrationServiceA/Cosmos/CosmosExtensions.cs deleted file mode 100644 index 9db35db9d26..00000000000 --- a/tests/testproject/TestProject.IntegrationServiceA/Cosmos/CosmosExtensions.cs +++ /dev/null @@ -1,48 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Text; -using Aspire.TestProject; -using Microsoft.Azure.Cosmos; -using Polly; - -public static class CosmosExtensions -{ - public static void MapCosmosApi(this WebApplication app) - { - app.MapGet("/cosmos/verify", VerifyCosmosAsync); - } - - private static async Task VerifyCosmosAsync(CosmosClient cosmosClient) - { - StringBuilder errorMessageBuilder = new(); - try - { - ResiliencePipeline pipeline = ResilienceUtils.GetDefaultResiliencePipelineBuilder(args => - { - errorMessageBuilder.AppendLine($"{Environment.NewLine}Service retry #{args.AttemptNumber} due to {args.Outcome.Exception}"); - return ValueTask.CompletedTask; - }).Build(); - - var db = await pipeline.ExecuteAsync( - async token => (await cosmosClient.CreateDatabaseIfNotExistsAsync("db", cancellationToken: token)).Database); - - var container = (await db.CreateContainerIfNotExistsAsync("todos", "/id")).Container; - - var id = Guid.NewGuid().ToString(); - var title = "Do some work."; - - var item = await container.CreateItemAsync(new - { - id, - title - }); - - return item.Resource.id == id ? Results.Ok() : Results.Problem(); - } - catch (Exception e) - { - return Results.Problem($"Error: {e}{Environment.NewLine}** Previous retries: {errorMessageBuilder}"); - } - } -} diff --git a/tests/testproject/TestProject.IntegrationServiceA/Cosmos/EFCoreCosmosDbContext.cs b/tests/testproject/TestProject.IntegrationServiceA/Cosmos/EFCoreCosmosDbContext.cs deleted file mode 100644 index 3d5b0acbc5c..00000000000 --- a/tests/testproject/TestProject.IntegrationServiceA/Cosmos/EFCoreCosmosDbContext.cs +++ /dev/null @@ -1,14 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using Microsoft.EntityFrameworkCore; - -public class EFCoreCosmosDbContext(DbContextOptions options) : DbContext(options) -{ - public DbSet Entries { get; set; } -} - -public record Entry -{ - public Guid Id { get; set; } = Guid.NewGuid(); -} diff --git a/tests/testproject/TestProject.IntegrationServiceA/Cosmos/EFCoreCosmosExtensions.cs b/tests/testproject/TestProject.IntegrationServiceA/Cosmos/EFCoreCosmosExtensions.cs deleted file mode 100644 index 8c4233ff4df..00000000000 --- a/tests/testproject/TestProject.IntegrationServiceA/Cosmos/EFCoreCosmosExtensions.cs +++ /dev/null @@ -1,25 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -public static class EFCoreCosmosExtensions -{ - public static void MapEFCoreCosmosApi(this WebApplication app) - { - app.MapGet("/efcosmos/verify", VerifyEFCoreCosmosAsync); - } - - private static async Task VerifyEFCoreCosmosAsync(EFCoreCosmosDbContext dbContext) - { - try - { - await dbContext.Database.EnsureCreatedAsync(); - dbContext.AddRange([new Entry(), new Entry()]); - var count = await dbContext.SaveChangesAsync(); - return count == 2 ? Results.Ok() : Results.Problem($"Expected 2 entries but got {count}"); - } - catch (Exception e) - { - return Results.Problem(e.ToString()); - } - } -} diff --git a/tests/testproject/TestProject.IntegrationServiceA/Program.cs b/tests/testproject/TestProject.IntegrationServiceA/Program.cs index 368e8644753..bcc60798b9c 100644 --- a/tests/testproject/TestProject.IntegrationServiceA/Program.cs +++ b/tests/testproject/TestProject.IntegrationServiceA/Program.cs @@ -22,16 +22,6 @@ builder.AddNpgsqlDbContext("postgresdb"); } -if (!resourcesToSkip.HasFlag(TestResourceNames.cosmos) || !resourcesToSkip.HasFlag(TestResourceNames.efcosmos)) -{ - builder.AddAzureCosmosClient("cosmos"); -} - -if (!resourcesToSkip.HasFlag(TestResourceNames.efcosmos)) -{ - builder.AddCosmosDbContext("cosmos", "cosmos"); -} - // Ensure healthChecks are added. Some components like Cosmos // don't add this builder.Services.AddHealthChecks(); @@ -58,14 +48,4 @@ app.MapNpgsqlEFCoreApi(); } -if (!resourcesToSkip.HasFlag(TestResourceNames.cosmos)) -{ - app.MapCosmosApi(); -} - -if (!resourcesToSkip.HasFlag(TestResourceNames.efcosmos)) -{ - app.MapEFCoreCosmosApi(); -} - app.Run(); diff --git a/tests/testproject/TestProject.IntegrationServiceA/TestProject.IntegrationServiceA.csproj b/tests/testproject/TestProject.IntegrationServiceA/TestProject.IntegrationServiceA.csproj index 8c691dd14b2..a47e0f69653 100644 --- a/tests/testproject/TestProject.IntegrationServiceA/TestProject.IntegrationServiceA.csproj +++ b/tests/testproject/TestProject.IntegrationServiceA/TestProject.IntegrationServiceA.csproj @@ -11,11 +11,9 @@ - -