Skip to content

Commit 7c73b81

Browse files
authored
Reenable WithDockerfile live tests.
Reenable WithDockerfile live tests.
1 parent 0f5debe commit 7c73b81

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

tests/Aspire.Hosting.Containers.Tests/WithDockerfileTests.cs

+22-40
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,15 @@
99
using Aspire.Hosting.Utils;
1010
using Microsoft.Extensions.DependencyInjection;
1111
using Microsoft.Extensions.Logging;
12-
using Polly;
13-
using Polly.Timeout;
1412
using Xunit;
1513
using Xunit.Abstractions;
1614

1715
namespace Aspire.Hosting.Containers.Tests;
1816

1917
public class WithDockerfileTests(ITestOutputHelper testOutputHelper)
2018
{
21-
// Currently we can only run this locally because the CI agents don't have buildkit support enabled.
2219
[Fact]
2320
[RequiresDocker]
24-
[ActiveIssue("https://github.com/dotnet/aspire/issues/4613")]
2521
public async Task WithBuildSecretPopulatesSecretFilesCorrectly()
2622
{
2723
using var builder = TestDistributedApplicationBuilder.Create();
@@ -44,20 +40,21 @@ public async Task WithBuildSecretPopulatesSecretFilesCorrectly()
4440
using var app = builder.Build();
4541
await app.StartAsync();
4642

43+
await WaitForResourceAsync(app, "testcontainer", "Running");
44+
4745
using var client = app.CreateHttpClient("testcontainer", "http");
4846

49-
var envSecretMessage = await client.GetStringWithRetryAsync("/ENV_SECRET.txt");
47+
var envSecretMessage = await client.GetStringAsync("/ENV_SECRET.txt");
5048
Assert.Equal("open sesame from env", envSecretMessage);
5149

52-
var fileSecretMessage = await client.GetStringWithRetryAsync("/FILE_SECRET.txt");
50+
var fileSecretMessage = await client.GetStringAsync("/FILE_SECRET.txt");
5351
Assert.Equal("open sesame from file", fileSecretMessage);
5452

5553
await app.StopAsync();
5654
}
5755

5856
[Fact]
5957
[RequiresDocker]
60-
[ActiveIssue("https://github.com/dotnet/aspire/issues/4613")]
6158
public async Task WithDockerfileLaunchesContainerSuccessfully()
6259
{
6360
using var builder = TestDistributedApplicationBuilder.Create();
@@ -72,9 +69,11 @@ public async Task WithDockerfileLaunchesContainerSuccessfully()
7269
using var app = builder.Build();
7370
await app.StartAsync();
7471

72+
await WaitForResourceAsync(app, "testcontainer", "Running");
73+
7574
using var client = app.CreateHttpClient("testcontainer", "http");
7675

77-
var message = await client.GetStringWithRetryAsync("/aspire.html"); // Proves the container built, ran, and contains customizations!
76+
var message = await client.GetStringAsync("/aspire.html");
7877

7978
Assert.Equal($"{DefaultMessage}\n", message);
8079

@@ -90,7 +89,6 @@ public async Task WithDockerfileLaunchesContainerSuccessfully()
9089

9190
[Fact]
9291
[RequiresDocker]
93-
[ActiveIssue("https://github.com/dotnet/aspire/issues/4613")]
9492
public async Task AddDockerfileLaunchesContainerSuccessfully()
9593
{
9694
using var builder = TestDistributedApplicationBuilder.Create();
@@ -104,9 +102,10 @@ public async Task AddDockerfileLaunchesContainerSuccessfully()
104102
using var app = builder.Build();
105103
await app.StartAsync();
106104

107-
using var client = app.CreateHttpClient("testcontainer", "http");
105+
await WaitForResourceAsync(app, "testcontainer", "Running");
108106

109-
var message = await client.GetStringWithRetryAsync("/aspire.html"); // Proves the container built, ran, and contains customizations!
107+
using var client = app.CreateHttpClient("testcontainer", "http");
108+
var message = await client.GetStringAsync("/aspire.html");
110109

111110
Assert.Equal($"{DefaultMessage}\n", message);
112111

@@ -401,7 +400,6 @@ public async Task AddDockerfileWithBuildSecretFilePathResultsInManifestReferenci
401400

402401
[Fact]
403402
[RequiresDocker]
404-
[ActiveIssue("https://github.com/dotnet/aspire/issues/4613")]
405403
public async Task WithDockerfileWithParameterLaunchesContainerSuccessfully()
406404
{
407405
using var builder = TestDistributedApplicationBuilder.Create();
@@ -424,9 +422,11 @@ public async Task WithDockerfileWithParameterLaunchesContainerSuccessfully()
424422
using var app = builder.Build();
425423
await app.StartAsync();
426424

425+
await WaitForResourceAsync(app, "testcontainer", "Running");
426+
427427
using var client = app.CreateHttpClient("testcontainer", "http");
428428

429-
var message = await client.GetStringWithRetryAsync("/aspire.html"); // Proves the container built, ran, and contains customizations!
429+
var message = await client.GetStringAsync("/aspire.html");
430430

431431
Assert.Equal($"hello\n", message);
432432

@@ -471,7 +471,6 @@ public async Task WithDockerfileWithParameterLaunchesContainerSuccessfully()
471471

472472
[Fact]
473473
[RequiresDocker]
474-
[ActiveIssue("https://github.com/dotnet/aspire/issues/4613")]
475474
public async Task AddDockerfileWithParameterLaunchesContainerSuccessfully()
476475
{
477476
using var builder = TestDistributedApplicationBuilder.Create();
@@ -493,9 +492,11 @@ public async Task AddDockerfileWithParameterLaunchesContainerSuccessfully()
493492
using var app = builder.Build();
494493
await app.StartAsync();
495494

495+
await WaitForResourceAsync(app, "testcontainer", "Running");
496+
496497
using var client = app.CreateHttpClient("testcontainer", "http");
497498

498-
var message = await client.GetStringWithRetryAsync("/aspire.html"); // Proves the container built, ran, and contains customizations!
499+
var message = await client.GetStringAsync("/aspire.html");
499500

500501
Assert.Equal($"hello\n", message);
501502

@@ -812,6 +813,12 @@ public async Task AddDockerfileWithValidContextPathValidDockerfileWithExplicitAb
812813
return (tempContextPath, tempDockerfilePath);
813814
}
814815

816+
private static async Task WaitForResourceAsync(DistributedApplication app, string resourceName, string resourceState, TimeSpan? timeout = null)
817+
{
818+
var rns = app.Services.GetRequiredService<ResourceNotificationService>();
819+
await rns.WaitForResourceAsync(resourceName, resourceState).WaitAsync(timeout ?? TimeSpan.FromMinutes(3));
820+
}
821+
815822
private const string DefaultMessage = "aspire!";
816823

817824
private const string HelloWorldDockerfile = $$"""
@@ -840,28 +847,3 @@ ARG MESSAGE
840847
RUN --mount=type=secret,id=ENV_SECRET cp /run/secrets/ENV_SECRET /app/static/ENV_SECRET.txt
841848
""";
842849
}
843-
844-
internal static class RetryExtensions
845-
{
846-
private static ResiliencePipeline? s_pipeline;
847-
848-
public static async Task<string> GetStringWithRetryAsync(this HttpClient client, string uri, CancellationToken cancellationToken = default)
849-
{
850-
if (s_pipeline is null)
851-
{
852-
s_pipeline = new ResiliencePipelineBuilder()
853-
.AddRetry(new Polly.Retry.RetryStrategyOptions()
854-
{
855-
ShouldHandle = new PredicateBuilder()
856-
.Handle<TimeoutRejectedException>()
857-
})
858-
.AddTimeout(TimeSpan.FromSeconds(120))
859-
.Build();
860-
}
861-
862-
return await s_pipeline.ExecuteAsync(
863-
async ct => await client.GetStringAsync(uri, ct),
864-
cancellationToken
865-
);
866-
}
867-
}

tests/Aspire.Hosting.Testing.Tests/TestingFactoryTests.cs

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public void CanGetResources()
4242
[RequiresDocker]
4343
public async Task HttpClientGetTest()
4444
{
45+
// Wait for resource to start.
46+
var rns = _app.Services.GetRequiredService<ResourceNotificationService>();
47+
await rns.WaitForResourceAsync("mywebapp1").WaitAsync(TimeSpan.FromSeconds(60));
48+
4549
var httpClient = _app.CreateHttpClientWithResilience("mywebapp1");
4650
var result1 = await httpClient.GetFromJsonAsync<WeatherForecast[]>("/weatherforecast");
4751
Assert.NotNull(result1);
@@ -64,6 +68,10 @@ public async Task SelectsFirstLaunchProfile()
6468
var profileName = config["AppHost:DefaultLaunchProfileName"];
6569
Assert.Equal("https", profileName);
6670

71+
// Wait for resource to start.
72+
var rns = _app.Services.GetRequiredService<ResourceNotificationService>();
73+
await rns.WaitForResourceAsync("mywebapp1").WaitAsync(TimeSpan.FromSeconds(60));
74+
6775
// Explicitly get the HTTPS endpoint - this is only available on the "https" launch profile.
6876
var httpClient = _app.CreateHttpClientWithResilience("mywebapp1", "https");
6977
var result = await httpClient.GetFromJsonAsync<WeatherForecast[]>("/weatherforecast");

tests/Aspire.Hosting.Tests/XunitAttributes.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
using Xunit;
55

6-
[assembly: CollectionBehavior(DisableTestParallelization = true)]
6+
[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)]

0 commit comments

Comments
 (0)