Skip to content

Commit 7802ebe

Browse files
authored
[tests] Add more playground apps (#5395)
* [tests] Add WithDockerfile playground app test * [tests] Add Kakfa playground app to tests * WithDockerfile playground app: Set DOCKER_BUILDKIT=1 * WatchNotifications: Cancel, or set exception on tcs when WatchAsync fails * LoggerNotificationExtensions: Add WaitForAllTestAsync
1 parent 13d1770 commit 7802ebe

File tree

6 files changed

+108
-1
lines changed

6 files changed

+108
-1
lines changed

playground/kafka/Consumer/ConsumerWorker.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)
1919
try
2020
{
2121
result = consumer.Consume(TimeSpan.FromSeconds(1));
22+
if (result is not null)
23+
{
24+
logger.LogInformation($"Consumed message [{result.Message?.Key}] = {result.Message?.Value}");
25+
}
2226
}
2327
catch (ConsumeException ex) when (ex.Error.Code == ErrorCode.UnknownTopicOrPart)
2428
{

playground/withdockerfile/WithDockerfile.AppHost/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
builder.AddDockerfile("mycontainer", "qots")
1111
.WithBuildArg("GO_VERSION", goVersion)
12-
.WithBuildSecret("SECRET_ASENV", secret);
12+
.WithBuildSecret("SECRET_ASENV", secret)
13+
.WithEnvironment("DOCKER_BUILDKIT", "1");
1314

1415
#if !SKIP_DASHBOARD_REFERENCE
1516
// This project is only added in playground projects to support development/debugging

tests/Aspire.Hosting.Tests/Utils/LoggerNotificationExtensions.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,39 @@ public static Task WaitForTextAsync(this DistributedApplication app, Predicate<s
6868
return tcs.Task;
6969
}
7070

71+
/// <summary>
72+
/// Waits for all the specified texts to be logged.
73+
/// </summary>
74+
/// <param name="app">The <see cref="DistributedApplication" /> instance to watch.</param>
75+
/// <param name="logTexts">Any text to wait for.</param>
76+
/// <param name="resourceName">An optional resource name to filter the logs for.</param>
77+
/// <param name="cancellationToken">The cancellation token.</param>
78+
/// <returns></returns>
79+
public static async Task WaitForAllTextAsync(this DistributedApplication app, IEnumerable<string> logTexts, string? resourceName = null, CancellationToken cancellationToken = default)
80+
{
81+
var table = logTexts.ToList();
82+
try
83+
{
84+
await app.WaitForTextAsync((log) =>
85+
{
86+
foreach (var text in table)
87+
{
88+
if (log.Contains(text))
89+
{
90+
table.Remove(text);
91+
break;
92+
}
93+
}
94+
95+
return table.Count == 0;
96+
}, resourceName, cancellationToken).ConfigureAwait(false);
97+
}
98+
catch (TaskCanceledException te) when (cancellationToken.IsCancellationRequested)
99+
{
100+
throw new TaskCanceledException($"Task was canceled before these messages were found: '{string.Join("', '", table)}'", te);
101+
}
102+
}
103+
71104
private static async Task WatchNotifications(DistributedApplication app, string? resourceName, Predicate<string> predicate, TaskCompletionSource tcs, CancellationTokenSource cancellationTokenSource)
72105
{
73106
var resourceNotificationService = app.Services.GetRequiredService<ResourceNotificationService>();
@@ -98,10 +131,12 @@ private static async Task WatchNotifications(DistributedApplication app, string?
98131
catch (OperationCanceledException)
99132
{
100133
// Expected if the application stops prematurely or the text was detected.
134+
tcs.TrySetCanceled();
101135
}
102136
catch (Exception ex)
103137
{
104138
logger.LogError(ex, "An error occurred while watching for resource notifications.");
139+
tcs.TrySetException(ex);
105140
}
106141
}
107142

tests/Aspire.Playground.Tests/Aspire.Playground.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
<ProjectReference Include="$(PlaygroundSourceDir)Qdrant/Qdrant.AppHost/Qdrant.AppHost.csproj" />
4343
<ProjectReference Include="$(PlaygroundSourceDir)SqlServerEndToEnd/SqlServerEndToEnd.AppHost/SqlServerEndToEnd.AppHost.csproj" />
4444
<ProjectReference Include="$(PlaygroundSourceDir)TestShop/TestShop.AppHost/TestShop.AppHost.csproj" />
45+
<ProjectReference Include="$(PlaygroundSourceDir)kafka/KafkaBasic.AppHost/KafkaBasic.AppHost.csproj" />
4546
<ProjectReference Include="$(PlaygroundSourceDir)keycloak/Keycloak.AppHost/Keycloak.AppHost.csproj" />
4647
<ProjectReference Include="$(PlaygroundSourceDir)milvus/MilvusPlayground.AppHost/MilvusPlayground.AppHost.csproj" />
48+
<ProjectReference Include="$(PlaygroundSourceDir)withdockerfile/WithDockerfile.AppHost/WithDockerfile.AppHost.csproj" />
4749

4850
<!-- Issue: https://github.com/dotnet/aspire/issues/5274 -->
4951
<!-- Only `dotnet run` startup checked -->
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Aspire.Hosting;
5+
using Aspire.Hosting.Tests.Utils;
6+
using SamplesIntegrationTests;
7+
using SamplesIntegrationTests.Infrastructure;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
11+
namespace Aspire.Playground.Tests;
12+
13+
public class ProjectSpecificTests(ITestOutputHelper _testOutput)
14+
{
15+
[Fact]
16+
public async Task WithDockerfileTest()
17+
{
18+
var appHostPath = Directory.GetFiles(AppContext.BaseDirectory, "WithDockerfile.AppHost.dll").Single();
19+
var appHost = await DistributedApplicationTestFactory.CreateAsync(appHostPath, _testOutput);
20+
await using var app = await appHost.BuildAsync();
21+
22+
await app.StartAsync();
23+
await app.WaitForResources().WaitAsync(TimeSpan.FromMinutes(2));
24+
25+
await app.WaitForTextAsync($"I'm Batman. - Batman")
26+
.WaitAsync(TimeSpan.FromMinutes(3));
27+
28+
app.EnsureNoErrorsLogged();
29+
await app.StopAsync();
30+
}
31+
32+
[Fact]
33+
public async Task KafkaTest()
34+
{
35+
var appHostPath = Directory.GetFiles(AppContext.BaseDirectory, "KafkaBasic.AppHost.dll").Single();
36+
var appHost = await DistributedApplicationTestFactory.CreateAsync(appHostPath, _testOutput);
37+
await using var app = await appHost.BuildAsync();
38+
39+
await app.StartAsync();
40+
await app.WaitForResources().WaitAsync(TimeSpan.FromMinutes(2));
41+
42+
await WaitForAllTextAsync(app,
43+
[
44+
"Hello, World! 343",
45+
"Received 1000 messages."
46+
],
47+
timeoutSecs: 120);
48+
49+
app.EnsureNoErrorsLogged();
50+
await app.StopAsync();
51+
}
52+
53+
internal static Task WaitForAllTextAsync(DistributedApplication app, IEnumerable<string> logTexts, string? resourceName = null, int timeoutSecs = -1)
54+
{
55+
CancellationTokenSource cts = new();
56+
if (timeoutSecs > 0)
57+
{
58+
cts.CancelAfter(TimeSpan.FromSeconds(timeoutSecs));
59+
}
60+
61+
return app.WaitForAllTextAsync(logTexts, resourceName, cts.Token);
62+
}
63+
}

tests/helix/send-to-helix-buildonhelixtests.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
<ItemGroup>
1515
<HelixPreCommand Include="$(_EnvVarSetKeyword) SkipDashboardProjectReference=true" />
1616
<HelixPreCommand Include="$(_EnvVarSetKeyword) TestsRunningOutsideOfRepo=true" />
17+
<!-- required for tests that build docker images, like Aspire.Playground.Tests -->
18+
<HelixPreCommand Include="$(_EnvVarSetKeyword) DOCKER_BUILDKIT=1" />
1719

1820
<HelixPreCommand Condition="'$(OS)' == 'Windows_NT'" Include="set NUGET_PACKAGES=%HELIX_WORKITEM_ROOT%\nuget-cache\" />
1921
<HelixPreCommand Condition="'$(OS)' != 'Windows_NT'" Include="export NUGET_PACKAGES=$HELIX_WORKITEM_ROOT/nuget-cache/" />

0 commit comments

Comments
 (0)