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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<PackageVersion Include="Serilog.Sinks.Seq" Version="9.1.0" />
<PackageVersion Include="AWSSDK.SQS" Version="4.1.0.2" />
<PackageVersion Include="Aspire.Hosting.AppHost" Version="13.6.5" />
<PackageVersion Include="Aspire.Hosting.Dotnet" Version="13.6.5-preview.1.26559.7" />
<PackageVersion Include="Aspire.Hosting.Testing" Version="13.6.5" />
<PackageVersion Include="Azure.Data.Tables" Version="12.13.0" />
<PackageVersion Include="Azure.Storage.Blobs" Version="12.30.1" />
Expand Down
21 changes: 16 additions & 5 deletions src/TUnit.Aspire.Core/AspireFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,11 +1037,11 @@ private void ConfigureOtlpEndpoints(IDistributedApplicationTestingBuilder builde

foreach (var resource in builder.Resources)
{
if (resource is ProjectResource projectResource)
if (IsDotnetProgramResource(resource))
{
var resourceName = projectResource.Name;
var resourceName = resource.Name;

projectResource.Annotations.Add(
resource.Annotations.Add(
new EnvironmentCallbackAnnotation(context =>
{
// Forward to user's dashboard if they set OTEL_EXPORTER_OTLP_ENDPOINT
Expand Down Expand Up @@ -1070,6 +1070,17 @@ private void ConfigureOtlpEndpoints(IDistributedApplicationTestingBuilder builde
}
}

// IDotnetProgramResource is experimental in Aspire 13.x and stable from Aspire 17.
#pragma warning disable ASPIREPROJECTS001
/// <summary>
/// Matches .NET project resources from both <c>AddProject</c> (<see cref="ProjectResource"/>) and
/// <c>AddDotnetProject</c> (<c>DotnetProjectResource</c>, an executable resource), which share
/// <see cref="IDotnetProgramResource"/>.
/// </summary>
private static bool IsDotnetProgramResource(IResource resource)
=> resource is IDotnetProgramResource and IResourceWithEnvironment;
#pragma warning restore ASPIREPROJECTS001

/// <summary>
/// Emits a one-time hint for any project resource that produced console output but never sent
/// correlated OpenTelemetry logs to TUnit — the typical symptom of an SUT missing OTLP log
Expand All @@ -1087,8 +1098,8 @@ private async Task EmitTelemetryWiringHintsAsync(DistributedApplication app, Otl
// The fixture injects OTEL_SERVICE_NAME = resource name (see ConfigureOtlpEndpoints), so
// an SUT using the default service name matches by resource name here; one that overrides
// its service name may produce a false hint — hence the soft wording below.
var candidates = model.Resources.OfType<ProjectResource>()
.Where(p => ShouldWaitForResource(p) && !receiver.HasSeenLogsFrom(p.Name))
var candidates = model.Resources
.Where(p => IsDotnetProgramResource(p) && ShouldWaitForResource(p) && !receiver.HasSeenLogsFrom(p.Name))
.ToList();
if (candidates.Count == 0)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net10.0</TargetFrameworks>
<!-- Single-target (clear the Directory.Build.props default): AddDotnetProject resolves the run
command with the ComputeRunArguments target, which a multi-targeting outer build lacks. -->
<TargetFramework>net10.0</TargetFramework>
<TargetFrameworks></TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
74 changes: 74 additions & 0 deletions tests/TUnit.Aspire.Tests/DotnetProjectResourceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Diagnostics;
using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Testing;
using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using TUnit.Core;

namespace TUnit.Aspire.Tests;

/// <summary>
/// Runs the API service through <c>AddDotnetProject</c>, which creates a <c>DotnetProjectResource</c>
/// (an executable resource) rather than a <see cref="ProjectResource"/>.
/// </summary>
public class DotnetProjectResourceFixture : AspireFixture<Projects.TUnit_Aspire_Tests_AppHost>
{
public const string ServiceName = "dotnet-api-service";

protected override TimeSpan ResourceTimeout => TimeSpan.FromSeconds(120);

protected override ResourceWaitBehavior WaitBehavior => ResourceWaitBehavior.Named;

protected override IEnumerable<string> ResourcesToWaitFor() => [ServiceName];

protected override IEnumerable<string> ResourcesToRemove() => ["nginx-no-healthcheck", "api-service", "my-secret"];

protected override void ConfigureBuilder(IDistributedApplicationTestingBuilder builder)
{
// Relative paths resolve against the AppHost directory.
builder.AddDotnetProject(ServiceName, "../TUnit.Aspire.Tests.ApiService/TUnit.Aspire.Tests.ApiService.csproj")
.WithHttpEndpoint(env: "ASPNETCORE_HTTP_PORTS")
.WithCertificateTrustScope(CertificateTrustScope.None);
}
}

[ClassDataSource<DotnetProjectResourceFixture>(Shared = SharedType.PerClass)]
[Category("Docker")]
[Category("Integration")]
public class DotnetProjectResourceTests(DotnetProjectResourceFixture fixture)
{
[Test]
public async Task Logs_FromDotnetProjectResource_AppearInTestOutput()
{
var marker = $"dotnet-project-marker-{Guid.NewGuid():N}";

var client = fixture.CreateHttpClient(DotnetProjectResourceFixture.ServiceName);
using var response = await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");

await Assert.That((int)response.StatusCode).IsEqualTo(200);

var output = await PollForOutput(marker);
await Assert.That(output).Contains($"[{DotnetProjectResourceFixture.ServiceName}]");
await Assert.That(output).Contains(marker);
}

private static async Task<string> PollForOutput(string expectedMarker)
{
var testContext = TestContext.Current!;
var sw = Stopwatch.StartNew();

while (sw.Elapsed < TimeSpan.FromSeconds(30))
{
var output = testContext.GetStandardOutput();
if (output.Contains(expectedMarker))
{
return output;
}

await Task.Delay(250);
}

return testContext.GetStandardOutput();
}
}
2 changes: 2 additions & 0 deletions tests/TUnit.Aspire.Tests/TUnit.Aspire.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
</ItemGroup>

<ItemGroup>
<!-- AddDotnetProject; preview in Aspire 13.x, used only to test DotnetProjectResource support. -->
<PackageReference Include="Aspire.Hosting.Dotnet" />
<PackageReference Include="Aspire.Hosting.Testing" />
<PackageReference Include="Microsoft.Testing.Extensions.HangDump" />
</ItemGroup>
Expand Down
Loading