Skip to content

Commit c87ed5b

Browse files
authored
Improve test reliability (dotnet#3321)
1 parent ae68751 commit c87ed5b

16 files changed

+418
-282
lines changed

src/Aspire.Hosting/Dashboard/DashboardLifecycleHook.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private async Task WatchDashboardLogsAsync(CancellationToken cancellationToken)
215215

216216
try
217217
{
218-
await foreach (var notification in resourceNotificationService.WatchAsync(cancellationToken))
218+
await foreach (var notification in resourceNotificationService.WatchAsync(cancellationToken).ConfigureAwait(false))
219219
{
220220
// Track all dashboard resources and start watching their logs.
221221
// TODO: In the future when resources can restart, we should handle purging the taskCache.

tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<ProjectReference Include="..\..\src\Aspire.Hosting.Nats\Aspire.Hosting.Nats.csproj" IsAspireProjectResource="false" />
3434
<ProjectReference Include="..\..\src\Aspire.Hosting.PostgreSQL\Aspire.Hosting.PostgreSQL.csproj" IsAspireProjectResource="false" />
3535
<ProjectReference Include="..\..\src\Aspire.Hosting.Qdrant\Aspire.Hosting.Qdrant.csproj" IsAspireProjectResource="false" />
36+
<ProjectReference Include="..\..\src\Aspire.Hosting.Testing\Aspire.Hosting.Testing.csproj" />
3637
<ProjectReference Include="..\testproject\TestProject.AppHost\TestProject.AppHost.csproj" IsAspireProjectResource="false" />
3738
<ProjectReference Include="..\Aspire.Hosting.Tests.SharedShim\Aspire.Hosting.Tests.SharedShim.csproj" IsAspireProjectResource="false" Aliases="AspireHostingShared" />
3839
<ProjectReference Include="..\..\src\Components\Aspire.Microsoft.Data.SqlClient\Aspire.Microsoft.Data.SqlClient.csproj" IsAspireProjectResource="false" />

tests/Aspire.Hosting.Tests/Azure/AzureBicepResourceTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void CreateConstruct(ResourceModuleConstruct construct)
7373
[MemberData(nameof(AzureExtensions))]
7474
public void AzureExtensionsAutomaticallyAddAzureProvisioning(Func<IDistributedApplicationBuilder, IResourceBuilder<IResource>> addAzureResource)
7575
{
76-
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Run);
76+
using var builder = TestDistributedApplicationBuilder.Create();
7777
addAzureResource(builder);
7878

7979
var app = builder.Build();
@@ -85,7 +85,7 @@ public void AzureExtensionsAutomaticallyAddAzureProvisioning(Func<IDistributedAp
8585
[MemberData(nameof(AzureExtensions))]
8686
public void BicepResourcesAreIdempotent(Func<IDistributedApplicationBuilder, IResourceBuilder<IResource>> addAzureResource)
8787
{
88-
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Run);
88+
using var builder = TestDistributedApplicationBuilder.Create();
8989
var azureResourceBuilder = addAzureResource(builder);
9090

9191
if (azureResourceBuilder.Resource is not AzureConstructResource bicepResource)
@@ -541,7 +541,7 @@ param logAnalyticsWorkspaceId string
541541
[Fact]
542542
public async Task AddApplicationInsightsWithoutExplicitLawGetsDefaultLawParameterInRunMode()
543543
{
544-
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Run);
544+
using var builder = TestDistributedApplicationBuilder.Create();
545545

546546
var appInsights = builder.AddAzureApplicationInsights("appInsights");
547547

tests/Aspire.Hosting.Tests/Azure/AzureResourceExtensionsTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void AzureStorageUseEmulatorCallbackWithWithDataBindMountResultsInBindMou
2828
});
2929

3030
var volumeAnnotation = storage.Resource.Annotations.OfType<ContainerMountAnnotation>().Single();
31-
Assert.Equal(Path.GetFullPath(".azurite/storage"), volumeAnnotation.Source);
31+
Assert.Equal(Path.Combine(builder.AppHostDirectory, ".azurite", "storage"), volumeAnnotation.Source);
3232
Assert.Equal("/data", volumeAnnotation.Target);
3333
Assert.Equal(ContainerMountType.BindMount, volumeAnnotation.Type);
3434
Assert.Equal(isReadOnly ?? false, volumeAnnotation.IsReadOnly);
@@ -54,7 +54,7 @@ public void AzureStorageUseEmulatorCallbackWithWithDataBindMountResultsInBindMou
5454
});
5555

5656
var volumeAnnotation = storage.Resource.Annotations.OfType<ContainerMountAnnotation>().Single();
57-
Assert.Equal(Path.GetFullPath("mydata"), volumeAnnotation.Source);
57+
Assert.Equal(Path.Combine(builder.AppHostDirectory, "mydata"), volumeAnnotation.Source);
5858
Assert.Equal("/data", volumeAnnotation.Target);
5959
Assert.Equal(ContainerMountType.BindMount, volumeAnnotation.Type);
6060
Assert.Equal(isReadOnly ?? false, volumeAnnotation.IsReadOnly);
@@ -80,7 +80,7 @@ public void AzureStorageUseEmulatorCallbackWithWithDataVolumeResultsInVolumeAnno
8080
});
8181

8282
var volumeAnnotation = storage.Resource.Annotations.OfType<ContainerMountAnnotation>().Single();
83-
Assert.Equal("testhost-storage-data", volumeAnnotation.Source);
83+
Assert.Equal("Aspire.Hosting.Tests-storage-data", volumeAnnotation.Source);
8484
Assert.Equal("/data", volumeAnnotation.Target);
8585
Assert.Equal(ContainerMountType.Volume, volumeAnnotation.Type);
8686
Assert.Equal(isReadOnly ?? false, volumeAnnotation.IsReadOnly);

tests/Aspire.Hosting.Tests/Dapr/DaprTests.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ public class DaprTests
1414
[Fact]
1515
public async Task WithDaprSideCarAddsAnnotationAndSidecarResource()
1616
{
17-
using var builder = TestDistributedApplicationBuilder.Create(new DistributedApplicationOptions
18-
{
19-
DisableDashboard = true
20-
});
17+
using var builder = TestDistributedApplicationBuilder.Create();
2118

2219
builder.AddDapr(o =>
2320
{
@@ -95,10 +92,7 @@ public async Task WithDaprSideCarAddsAnnotationAndSidecarResource()
9592
[InlineData("https", "http", null, "https", "localhost", 8000)]
9693
public async Task WithDaprSideCarAddsAnnotationBasedOnTheSidecarAppOptions(string? schema, string? endPoint, int? port, string expectedSchema, string expectedChannelAddress, int expectedPort)
9794
{
98-
using var builder = TestDistributedApplicationBuilder.Create(new DistributedApplicationOptions
99-
{
100-
DisableDashboard = true
101-
});
95+
using var builder = TestDistributedApplicationBuilder.Create();
10296

10397
builder.AddDapr(o =>
10498
{

tests/Aspire.Hosting.Tests/Dashboard/DashboardResourceTests.cs

+27-24
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class DashboardResourceTests
1919
[Fact]
2020
public async Task DashboardIsAutomaticallyAddedAsHiddenResource()
2121
{
22-
using var builder = TestDistributedApplicationBuilder.Create();
22+
using var builder = TestDistributedApplicationBuilder.Create(options => options.DisableDashboard = false);
2323

2424
// Ensure any ambient configuration doesn't impact this test.
2525
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>
@@ -34,7 +34,7 @@ public async Task DashboardIsAutomaticallyAddedAsHiddenResource()
3434
o.DashboardPath = dashboardPath;
3535
});
3636

37-
var app = builder.Build();
37+
using var app = builder.Build();
3838

3939
await app.ExecuteBeforeStartHooksAsync(default);
4040

@@ -52,11 +52,11 @@ public async Task DashboardIsAutomaticallyAddedAsHiddenResource()
5252
[Fact]
5353
public async Task DashboardIsAddedFirst()
5454
{
55-
using var builder = TestDistributedApplicationBuilder.Create();
55+
using var builder = TestDistributedApplicationBuilder.Create(options => options.DisableDashboard = false);
5656

5757
builder.AddContainer("my-container", "my-image");
5858

59-
var app = builder.Build();
59+
using var app = builder.Build();
6060

6161
await app.ExecuteBeforeStartHooksAsync(default);
6262

@@ -71,7 +71,7 @@ public async Task DashboardIsAddedFirst()
7171
[Fact]
7272
public async Task DashboardDoesNotAddResource_ConfiguresExistingDashboard()
7373
{
74-
using var builder = TestDistributedApplicationBuilder.Create();
74+
using var builder = TestDistributedApplicationBuilder.Create(options => options.DisableDashboard = false);
7575

7676
builder.Services.AddSingleton<IDashboardEndpointProvider, MockDashboardEndpointProvider>();
7777

@@ -85,7 +85,7 @@ public async Task DashboardDoesNotAddResource_ConfiguresExistingDashboard()
8585

8686
var container = builder.AddContainer(KnownResourceNames.AspireDashboard, "my-image");
8787

88-
var app = builder.Build();
88+
using var app = builder.Build();
8989

9090
await app.ExecuteBeforeStartHooksAsync(default);
9191

@@ -144,7 +144,7 @@ public async Task DashboardDoesNotAddResource_ConfiguresExistingDashboard()
144144
[Fact]
145145
public async Task DashboardWithDllPathLaunchesDotnet()
146146
{
147-
using var builder = TestDistributedApplicationBuilder.Create();
147+
using var builder = TestDistributedApplicationBuilder.Create(options => options.DisableDashboard = false);
148148

149149
var dashboardPath = Path.GetFullPath("dashboard.dll");
150150

@@ -173,7 +173,7 @@ public async Task DashboardWithDllPathLaunchesDotnet()
173173
public async Task DashboardAuthConfigured_EnvVarsPresent()
174174
{
175175
// Arrange
176-
using var builder = TestDistributedApplicationBuilder.Create();
176+
using var builder = TestDistributedApplicationBuilder.Create(options => options.DisableDashboard = false);
177177

178178
builder.Services.AddSingleton<IDashboardEndpointProvider, MockDashboardEndpointProvider>();
179179

@@ -187,7 +187,7 @@ public async Task DashboardAuthConfigured_EnvVarsPresent()
187187
["AppHost:OtlpApiKey"] = "TestOtlpApiKey!"
188188
});
189189

190-
var app = builder.Build();
190+
using var app = builder.Build();
191191

192192
await app.ExecuteBeforeStartHooksAsync(default);
193193

@@ -208,7 +208,7 @@ public async Task DashboardAuthConfigured_EnvVarsPresent()
208208
public async Task DashboardAuthRemoved_EnvVarsUnsecured()
209209
{
210210
// Arrange
211-
using var builder = TestDistributedApplicationBuilder.Create();
211+
using var builder = TestDistributedApplicationBuilder.Create(options => options.DisableDashboard = false);
212212

213213
builder.Services.AddSingleton<IDashboardEndpointProvider, MockDashboardEndpointProvider>();
214214

@@ -220,7 +220,7 @@ public async Task DashboardAuthRemoved_EnvVarsUnsecured()
220220
["DOTNET_DASHBOARD_OTLP_ENDPOINT_URL"] = "http://localhost"
221221
});
222222

223-
var app = builder.Build();
223+
using var app = builder.Build();
224224

225225
await app.ExecuteBeforeStartHooksAsync(default);
226226

@@ -238,7 +238,7 @@ public async Task DashboardAuthRemoved_EnvVarsUnsecured()
238238
public async Task DashboardResourceServiceUriIsSet()
239239
{
240240
// Arrange
241-
using var builder = TestDistributedApplicationBuilder.Create();
241+
using var builder = TestDistributedApplicationBuilder.Create(options => options.DisableDashboard = false);
242242

243243
builder.Services.AddSingleton<IDashboardEndpointProvider, MockDashboardEndpointProvider>();
244244

@@ -250,7 +250,7 @@ public async Task DashboardResourceServiceUriIsSet()
250250
["DOTNET_DASHBOARD_OTLP_ENDPOINT_URL"] = "http://localhost"
251251
});
252252

253-
var app = builder.Build();
253+
using var app = builder.Build();
254254

255255
await app.ExecuteBeforeStartHooksAsync(default);
256256

@@ -266,9 +266,13 @@ public async Task DashboardResourceServiceUriIsSet()
266266
[Fact]
267267
public async Task DashboardIsNotAddedInPublishMode()
268268
{
269-
using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);
269+
using var builder = TestDistributedApplicationBuilder.Create(options =>
270+
{
271+
options.DisableDashboard = false;
272+
options.Args = ["--publisher", "manifest"];
273+
});
270274

271-
var app = builder.Build();
275+
using var app = builder.Build();
272276

273277
await app.ExecuteBeforeStartHooksAsync(default);
274278

@@ -280,7 +284,7 @@ public async Task DashboardIsNotAddedInPublishMode()
280284
[Fact]
281285
public async Task DashboardIsNotAddedIfDisabled()
282286
{
283-
using var builder = TestDistributedApplicationBuilder.Create(new DistributedApplicationOptions { DisableDashboard = true });
287+
using var builder = TestDistributedApplicationBuilder.Create(options => options.DisableDashboard = true);
284288

285289
var app = builder.Build();
286290

@@ -295,12 +299,11 @@ public async Task DashboardIsNotAddedIfDisabled()
295299
public void ContainerIsValidWithDashboardIsDisabled()
296300
{
297301
// Set the host environment to "Development" so that the container validates services.
298-
using var builder = TestDistributedApplicationBuilder.Create(new DistributedApplicationOptions
302+
using var builder = TestDistributedApplicationBuilder.Create(options =>
299303
{
300-
DisableDashboard = true,
301-
Args = ["--environment", "Development"]
302-
}
303-
);
304+
options.DisableDashboard = true;
305+
options.Args = ["--environment", "Development"];
306+
});
304307

305308
// Container validation logic runs when the service provider is built.
306309
using var app = builder.Build();
@@ -315,7 +318,7 @@ public void ContainerIsValidWithDashboardIsDisabled()
315318
[InlineData(LogLevel.Trace)]
316319
public async Task DashboardLifecycleHookWatchesLogs(LogLevel logLevel)
317320
{
318-
using var builder = TestDistributedApplicationBuilder.Create();
321+
using var builder = TestDistributedApplicationBuilder.Create(o => o.DisableDashboard = false);
319322

320323
var loggerProvider = new TestLoggerProvider();
321324

@@ -337,7 +340,7 @@ public async Task DashboardLifecycleHookWatchesLogs(LogLevel logLevel)
337340
await app.ExecuteBeforeStartHooksAsync(default);
338341

339342
var model = app.Services.GetRequiredService<DistributedApplicationModel>();
340-
var resourceNotifcationService = app.Services.GetRequiredService<ResourceNotificationService>();
343+
var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();
341344
var resourceLoggerService = app.Services.GetRequiredService<ResourceLoggerService>();
342345

343346
var dashboard = Assert.Single(model.Resources.OfType<ExecutableResource>());
@@ -346,7 +349,7 @@ public async Task DashboardLifecycleHookWatchesLogs(LogLevel logLevel)
346349
Assert.Equal("aspire-dashboard", dashboard.Name);
347350

348351
// Push a notification through to the dashboard resource.
349-
await resourceNotifcationService.PublishUpdateAsync(dashboard, "aspire-dashboard-0", s => s with { State = "Running" });
352+
await resourceNotificationService.PublishUpdateAsync(dashboard, "aspire-dashboard-0", s => s with { State = "Running" });
350353

351354
// Push some logs through to the dashboard resource.
352355
var logger = resourceLoggerService.GetLogger("aspire-dashboard-0");

0 commit comments

Comments
 (0)