Skip to content
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
10 changes: 10 additions & 0 deletions src/Aspire.Hosting.Azure/AzureResourcePreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

#pragma warning disable ASPIREAZURE003
#pragma warning disable ASPIREAZURE001
#pragma warning disable ASPIREPIPELINES002

using Aspire.Dashboard.Model;
Expand Down Expand Up @@ -32,6 +33,15 @@ internal async Task PrepareResourcesAsync(DistributedApplicationModel model, Can
var azureResources = GetAzureResourcesFromAppModel(model);
if (azureResources.Count == 0)
{
// AddAzureProvisioning creates the environment before resources are configured, so wait until
// preparation to hide it when the final run-mode model contains only local emulators.
if (executionContext.IsRunMode &&
model.Resources.OfType<AzureEnvironmentResource>().SingleOrDefault() is { } environmentResource &&
!environmentResource.HasAnnotationOfType<HiddenAnnotation>())
{
environmentResource.Annotations.Add(new HiddenAnnotation(HiddenBehavior.Always));
}
Comment thread
eerhardt marked this conversation as resolved.

return;
}

Expand Down
38 changes: 38 additions & 0 deletions tests/Aspire.Hosting.Azure.Tests/AzureRunAsEmulatorModeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.

#pragma warning disable ASPIRECOSMOSDB001
#pragma warning disable ASPIREAZURE001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Utils;
using Microsoft.Extensions.DependencyInjection;

namespace Aspire.Hosting.Azure.Tests;

Expand Down Expand Up @@ -50,4 +52,40 @@ public void RunAsEmulator_InPublishMode_DoesNotConfigureLocalContainer(string re
Assert.False(resource.IsContainer(), $"{resourceType} should not be configured as a local container in publish mode.");
Assert.DoesNotContain(builder.Resources, resource => resource.Annotations.OfType<ContainerImageAnnotation>().Any());
}

[Fact]
public async Task RunAsEmulator_WhenAllAzureResourcesUseEmulators_HidesAzureEnvironment()
{
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Run);
builder.AddAzureCosmosDB("cosmos").RunAsEmulator();
builder.AddAzureStorage("storage").RunAsEmulator();
builder.AddAzureEventHubs("eventhubs").RunAsEmulator();

using var app = builder.Build();
var model = app.Services.GetRequiredService<DistributedApplicationModel>();
var preparer = app.Services.GetRequiredService<AzureResourcePreparer>();

await preparer.OnBeforeStartAsync(new BeforeStartEvent(app.Services, model), CancellationToken.None);

var environment = Assert.Single(model.Resources.OfType<AzureEnvironmentResource>());
var hidden = Assert.Single(environment.Annotations.OfType<HiddenAnnotation>());
Assert.Equal(HiddenBehavior.Always, hidden.Behavior);
}

[Fact]
public async Task RunAsEmulator_WhenAzureResourceRequiresProvisioning_ShowsAzureEnvironment()
{
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Run);
builder.AddAzureStorage("emulated-storage").RunAsEmulator();
builder.AddAzureStorage("provisioned-storage");

using var app = builder.Build();
var model = app.Services.GetRequiredService<DistributedApplicationModel>();
var preparer = app.Services.GetRequiredService<AzureResourcePreparer>();

await preparer.OnBeforeStartAsync(new BeforeStartEvent(app.Services, model), CancellationToken.None);

var environment = Assert.Single(model.Resources.OfType<AzureEnvironmentResource>());
Assert.Empty(environment.Annotations.OfType<HiddenAnnotation>());
}
}
Loading