Skip to content

Commit

Permalink
Add CosmosDB trigger test for Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsafia committed Jan 28, 2025
1 parent 59ba067 commit 0efa174
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using Azure.Messaging.EventHubs.Producer;
#if !SKIP_UNSTABLE_EMULATORS
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Cosmos;
#endif
using Microsoft.Azure.Cosmos;
using Azure.Storage.Blobs;
using Azure.Storage.Queues;
using Newtonsoft.Json;
Expand All @@ -17,9 +17,9 @@
builder.AddAzureQueueClient("queue");
builder.AddAzureBlobClient("blob");
builder.AddAzureEventHubProducerClient("eventhubs", static settings => settings.EventHubName = "myhub");
builder.AddAzureCosmosClient("cosmosdb");
#if !SKIP_UNSTABLE_EMULATORS
builder.AddAzureServiceBusClient("messaging");
builder.AddAzureCosmosClient("cosmosdb");
#endif

var app = builder.Build();
Expand Down Expand Up @@ -67,6 +67,7 @@ static string RandomString(int length)
await sender.SendMessageAsync(message, cancellationToken);
return Results.Ok("Message sent to Azure Service Bus.");
});
#endif

app.MapGet("/publish/cosmosdb", async (CosmosClient cosmosClient) =>
{
Expand All @@ -78,7 +79,6 @@ static string RandomString(int length)

return Results.Ok("Document created in Azure Cosmos DB.");
});
#endif

app.MapGet("/", async (HttpClient client) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
var eventHubs = builder.AddAzureEventHubs("eventhubs").RunAsEmulator().WithHub("myhub");
#if !SKIP_UNSTABLE_EMULATORS
var serviceBus = builder.AddAzureServiceBus("messaging").RunAsEmulator().WithQueue("myqueue");
#endif
var cosmosDb = builder.AddAzureCosmosDB("cosmosdb")

Check failure on line 10 in playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire (Build Linux)

playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs#L10

playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs(10,24): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'IDistributedApplicationBuilder' does not contain a definition for 'AddAzureCosmosDB' and no accessible extension method 'AddAzureCosmosDB' accepting a first argument of type 'IDistributedApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 10 in playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire (Build Linux_helix_tests)

playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs#L10

playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs(10,24): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'IDistributedApplicationBuilder' does not contain a definition for 'AddAzureCosmosDB' and no accessible extension method 'AddAzureCosmosDB' accepting a first argument of type 'IDistributedApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 10 in playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire

playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs#L10

playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs(10,24): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'IDistributedApplicationBuilder' does not contain a definition for 'AddAzureCosmosDB' and no accessible extension method 'AddAzureCosmosDB' accepting a first argument of type 'IDistributedApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 10 in playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire

playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs#L10

playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs(10,24): error CS1061: (NETCORE_ENGINEERING_TELEMETRY=Build) 'IDistributedApplicationBuilder' does not contain a definition for 'AddAzureCosmosDB' and no accessible extension method 'AddAzureCosmosDB' accepting a first argument of type 'IDistributedApplicationBuilder' could be found (are you missing a using directive or an assembly reference?)
.RunAsEmulator()
.WithDatabase("mydatabase", (database)
=> database.Containers.AddRange([new("mycontainer", "/id")]));
#endif

var funcApp = builder.AddAzureFunctionsProject<Projects.AzureFunctionsEndToEnd_Functions>("funcapp")
.WithExternalHttpEndpoints()
.WithReference(eventHubs).WaitFor(eventHubs)
#if !SKIP_UNSTABLE_EMULATORS
.WithReference(serviceBus).WaitFor(serviceBus)
.WithReference(cosmosDb).WaitFor(cosmosDb)
#endif
.WithReference(cosmosDb).WaitFor(cosmosDb)
.WithReference(blob)
.WithReference(queue);

builder.AddProject<Projects.AzureFunctionsEndToEnd_ApiService>("apiservice")
.WithReference(eventHubs).WaitFor(eventHubs)
#if !SKIP_UNSTABLE_EMULATORS
.WithReference(serviceBus).WaitFor(serviceBus)
.WithReference(cosmosDb).WaitFor(cosmosDb)
#endif
.WithReference(cosmosDb).WaitFor(cosmosDb)
.WithReference(queue)
.WithReference(blob)
.WithReference(funcApp);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if !SKIP_UNSTABLE_EMULATORS
using System.Reflection.Metadata;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
Expand All @@ -17,4 +16,3 @@ public void Run([CosmosDBTrigger(
logger.LogInformation("C# cosmosdb trigger function processed: {Count} messages", input.Count);
}
}
#endif
9 changes: 9 additions & 0 deletions tests/Aspire.Playground.Tests/ProjectSpecificTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ await WaitForAllTextAsync(app,
timeoutSecs: 160);
#endif

// Assert that CosmosDB triggers work correctly
await apiServiceClient.GetAsync("/publish/cosmosdb");
await WaitForAllTextAsync(app,
[
"Executed 'Functions.MyCosmosDbTrigger'"
],
resourceName: "funcapp",
timeoutSecs: 160);

// TODO: The following line is commented out because the test fails due to an erroneous log in the Functions App
// resource that happens after the Functions host has been built. The error log shows up after the Functions
// worker extension has been built and before the host has launched.
Expand Down

0 comments on commit 0efa174

Please sign in to comment.