9
9
using Aspire . Hosting . Utils ;
10
10
using Microsoft . Extensions . DependencyInjection ;
11
11
using Microsoft . Extensions . Logging ;
12
- using Polly ;
13
- using Polly . Timeout ;
14
12
using Xunit ;
15
13
using Xunit . Abstractions ;
16
14
17
15
namespace Aspire . Hosting . Containers . Tests ;
18
16
19
17
public class WithDockerfileTests ( ITestOutputHelper testOutputHelper )
20
18
{
21
- // Currently we can only run this locally because the CI agents don't have buildkit support enabled.
22
19
[ Fact ]
23
20
[ RequiresDocker ]
24
- [ ActiveIssue ( "https://github.com/dotnet/aspire/issues/4613" ) ]
25
21
public async Task WithBuildSecretPopulatesSecretFilesCorrectly ( )
26
22
{
27
23
using var builder = TestDistributedApplicationBuilder . Create ( ) ;
@@ -44,20 +40,21 @@ public async Task WithBuildSecretPopulatesSecretFilesCorrectly()
44
40
using var app = builder . Build ( ) ;
45
41
await app . StartAsync ( ) ;
46
42
43
+ await WaitForResourceAsync ( app , "testcontainer" , "Running" ) ;
44
+
47
45
using var client = app . CreateHttpClient ( "testcontainer" , "http" ) ;
48
46
49
- var envSecretMessage = await client . GetStringWithRetryAsync ( "/ENV_SECRET.txt" ) ;
47
+ var envSecretMessage = await client . GetStringAsync ( "/ENV_SECRET.txt" ) ;
50
48
Assert . Equal ( "open sesame from env" , envSecretMessage ) ;
51
49
52
- var fileSecretMessage = await client . GetStringWithRetryAsync ( "/FILE_SECRET.txt" ) ;
50
+ var fileSecretMessage = await client . GetStringAsync ( "/FILE_SECRET.txt" ) ;
53
51
Assert . Equal ( "open sesame from file" , fileSecretMessage ) ;
54
52
55
53
await app . StopAsync ( ) ;
56
54
}
57
55
58
56
[ Fact ]
59
57
[ RequiresDocker ]
60
- [ ActiveIssue ( "https://github.com/dotnet/aspire/issues/4613" ) ]
61
58
public async Task WithDockerfileLaunchesContainerSuccessfully ( )
62
59
{
63
60
using var builder = TestDistributedApplicationBuilder . Create ( ) ;
@@ -72,9 +69,11 @@ public async Task WithDockerfileLaunchesContainerSuccessfully()
72
69
using var app = builder . Build ( ) ;
73
70
await app . StartAsync ( ) ;
74
71
72
+ await WaitForResourceAsync ( app , "testcontainer" , "Running" ) ;
73
+
75
74
using var client = app . CreateHttpClient ( "testcontainer" , "http" ) ;
76
75
77
- var message = await client . GetStringWithRetryAsync ( "/aspire.html" ) ; // Proves the container built, ran, and contains customizations!
76
+ var message = await client . GetStringAsync ( "/aspire.html" ) ;
78
77
79
78
Assert . Equal ( $ "{ DefaultMessage } \n ", message ) ;
80
79
@@ -90,7 +89,6 @@ public async Task WithDockerfileLaunchesContainerSuccessfully()
90
89
91
90
[ Fact ]
92
91
[ RequiresDocker ]
93
- [ ActiveIssue ( "https://github.com/dotnet/aspire/issues/4613" ) ]
94
92
public async Task AddDockerfileLaunchesContainerSuccessfully ( )
95
93
{
96
94
using var builder = TestDistributedApplicationBuilder . Create ( ) ;
@@ -104,9 +102,10 @@ public async Task AddDockerfileLaunchesContainerSuccessfully()
104
102
using var app = builder . Build ( ) ;
105
103
await app . StartAsync ( ) ;
106
104
107
- using var client = app . CreateHttpClient ( "testcontainer" , "http " ) ;
105
+ await WaitForResourceAsync ( app , "testcontainer" , "Running " ) ;
108
106
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" ) ;
110
109
111
110
Assert . Equal ( $ "{ DefaultMessage } \n ", message ) ;
112
111
@@ -401,7 +400,6 @@ public async Task AddDockerfileWithBuildSecretFilePathResultsInManifestReferenci
401
400
402
401
[ Fact ]
403
402
[ RequiresDocker ]
404
- [ ActiveIssue ( "https://github.com/dotnet/aspire/issues/4613" ) ]
405
403
public async Task WithDockerfileWithParameterLaunchesContainerSuccessfully ( )
406
404
{
407
405
using var builder = TestDistributedApplicationBuilder . Create ( ) ;
@@ -424,9 +422,11 @@ public async Task WithDockerfileWithParameterLaunchesContainerSuccessfully()
424
422
using var app = builder . Build ( ) ;
425
423
await app . StartAsync ( ) ;
426
424
425
+ await WaitForResourceAsync ( app , "testcontainer" , "Running" ) ;
426
+
427
427
using var client = app . CreateHttpClient ( "testcontainer" , "http" ) ;
428
428
429
- var message = await client . GetStringWithRetryAsync ( "/aspire.html" ) ; // Proves the container built, ran, and contains customizations!
429
+ var message = await client . GetStringAsync ( "/aspire.html" ) ;
430
430
431
431
Assert . Equal ( $ "hello\n ", message ) ;
432
432
@@ -471,7 +471,6 @@ public async Task WithDockerfileWithParameterLaunchesContainerSuccessfully()
471
471
472
472
[ Fact ]
473
473
[ RequiresDocker ]
474
- [ ActiveIssue ( "https://github.com/dotnet/aspire/issues/4613" ) ]
475
474
public async Task AddDockerfileWithParameterLaunchesContainerSuccessfully ( )
476
475
{
477
476
using var builder = TestDistributedApplicationBuilder . Create ( ) ;
@@ -493,9 +492,11 @@ public async Task AddDockerfileWithParameterLaunchesContainerSuccessfully()
493
492
using var app = builder . Build ( ) ;
494
493
await app . StartAsync ( ) ;
495
494
495
+ await WaitForResourceAsync ( app , "testcontainer" , "Running" ) ;
496
+
496
497
using var client = app . CreateHttpClient ( "testcontainer" , "http" ) ;
497
498
498
- var message = await client . GetStringWithRetryAsync ( "/aspire.html" ) ; // Proves the container built, ran, and contains customizations!
499
+ var message = await client . GetStringAsync ( "/aspire.html" ) ;
499
500
500
501
Assert . Equal ( $ "hello\n ", message ) ;
501
502
@@ -812,6 +813,12 @@ public async Task AddDockerfileWithValidContextPathValidDockerfileWithExplicitAb
812
813
return ( tempContextPath , tempDockerfilePath ) ;
813
814
}
814
815
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
+
815
822
private const string DefaultMessage = "aspire!" ;
816
823
817
824
private const string HelloWorldDockerfile = $$ """
@@ -840,28 +847,3 @@ ARG MESSAGE
840
847
RUN --mount=type=secret,id=ENV_SECRET cp /run/secrets/ENV_SECRET /app/static/ENV_SECRET.txt
841
848
""" ;
842
849
}
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
- }
0 commit comments