Skip to content

Commit

Permalink
Ensure docker build log are streamed back to apphost. (#5125)
Browse files Browse the repository at this point in the history
* Ensure docker build log are streamed back to apphost.

* Update tests/Aspire.Hosting.Containers.Tests/Aspire.Hosting.Containers.Tests.csproj

---------

Co-authored-by: Eric Erhardt <[email protected]>
  • Loading branch information
mitchdenny and eerhardt authored Aug 5, 2024
1 parent 0a3e89d commit 3cdcaa2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
<ItemGroup>
<PackageReference Include="Polly.Core" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)src\Aspire.Hosting.Testing\ResourceLoggerForwarderService.cs" Link="Utils\ResourceLoggerForwarderService.cs" />
</ItemGroup>
</Project>
38 changes: 38 additions & 0 deletions tests/Aspire.Hosting.Containers.Tests/WithDockerfileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,44 @@ public async Task WithBuildSecretPopulatesSecretFilesCorrectly()
await app.StopAsync();
}

[Fact]
[RequiresDocker]
public async Task ContainerBuildLogsAreStreamedToAppHost()
{
using var builder = TestDistributedApplicationBuilder.Create();
builder.Services.AddHostedService<ResourceLoggerForwarderService>();
builder.Services.AddLogging(logging =>
{
logging.AddFakeLogging();
logging.AddXunit(testOutputHelper);
});

var (tempContextPath, tempDockerfilePath) = await CreateTemporaryDockerfileAsync();

builder.AddContainer("testcontainer", "testimage")
.WithHttpEndpoint(targetPort: 80)
.WithDockerfile(tempContextPath, tempDockerfilePath);

using var app = builder.Build();

await app.StartAsync();

// Wait for the resource to come online.
await WaitForResourceAsync(app, "testcontainer", "Running");
using var client = app.CreateHttpClient("testcontainer", "http");
var message = await client.GetStringAsync("/aspire.html");

// By the time we can make a request to the service the logs
// should be streamed back to the app host.
var collector = app.Services.GetFakeLogCollector();
var logs = collector.GetSnapshot();

// Just looking for a common message in Docker build output.
Assert.Contains(logs, log => log.Message.Contains("load build definition from Dockerfile"));

await app.StopAsync();
}

[Fact]
[RequiresDocker]
public async Task WithDockerfileLaunchesContainerSuccessfully()
Expand Down

0 comments on commit 3cdcaa2

Please sign in to comment.