From a9e392e33d10cd30f7426846ac83ae24ea77601e Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Thu, 22 Aug 2024 23:13:35 -0400 Subject: [PATCH 1/8] [tests] Add WithDockerfile playground app test --- .../Aspire.Playground.Tests.csproj | 1 + .../ProjectSpecificTests.cs | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/Aspire.Playground.Tests/ProjectSpecificTests.cs diff --git a/tests/Aspire.Playground.Tests/Aspire.Playground.Tests.csproj b/tests/Aspire.Playground.Tests/Aspire.Playground.Tests.csproj index 67930e243a9..95bc2116cfb 100644 --- a/tests/Aspire.Playground.Tests/Aspire.Playground.Tests.csproj +++ b/tests/Aspire.Playground.Tests/Aspire.Playground.Tests.csproj @@ -44,6 +44,7 @@ + diff --git a/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs b/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs new file mode 100644 index 00000000000..1d7536fd09f --- /dev/null +++ b/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Aspire.Hosting.Tests.Utils; +using SamplesIntegrationTests; +using SamplesIntegrationTests.Infrastructure; +using Xunit; +using Xunit.Abstractions; + +namespace Aspire.Playground.Tests; + +public class ProjectSpecificTests(ITestOutputHelper _testOutput) +{ + [Fact] + public async Task WithDockerfileTest() + { + var appHostPath = Directory.GetFiles(AppContext.BaseDirectory, "WithDockerfile.AppHost.dll").Single(); + var appHost = await DistributedApplicationTestFactory.CreateAsync(appHostPath, _testOutput); + await using var app = await appHost.BuildAsync(); + + await app.StartAsync(); + await app.WaitForResources().WaitAsync(TimeSpan.FromMinutes(2)); + + await app.WaitForTextAsync([$"I'm Batman. - Batman", "I am Iron Man. - Iron Man"]) + .WaitAsync(TimeSpan.FromMinutes(3)); + + app.EnsureNoErrorsLogged(); + await app.StopAsync(); + } +} From 0bdbfeb0d7d5b2f7e96728708e4ff3d5eb119e8a Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Fri, 23 Aug 2024 01:26:44 -0400 Subject: [PATCH 2/8] [tests] Add Kakfa playground app to tests --- playground/kafka/Consumer/ConsumerWorker.cs | 4 ++ .../Aspire.Playground.Tests.csproj | 1 + .../ProjectSpecificTests.cs | 54 ++++++++++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/playground/kafka/Consumer/ConsumerWorker.cs b/playground/kafka/Consumer/ConsumerWorker.cs index f2f587dbbab..c344914dd6d 100644 --- a/playground/kafka/Consumer/ConsumerWorker.cs +++ b/playground/kafka/Consumer/ConsumerWorker.cs @@ -19,6 +19,10 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken) try { result = consumer.Consume(TimeSpan.FromSeconds(1)); + if (result is not null) + { + logger.LogInformation($"Consumed message [{result.Message?.Key}] = {result.Message?.Value}"); + } } catch (ConsumeException ex) when (ex.Error.Code == ErrorCode.UnknownTopicOrPart) { diff --git a/tests/Aspire.Playground.Tests/Aspire.Playground.Tests.csproj b/tests/Aspire.Playground.Tests/Aspire.Playground.Tests.csproj index 95bc2116cfb..90de102a099 100644 --- a/tests/Aspire.Playground.Tests/Aspire.Playground.Tests.csproj +++ b/tests/Aspire.Playground.Tests/Aspire.Playground.Tests.csproj @@ -42,6 +42,7 @@ + diff --git a/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs b/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs index 1d7536fd09f..07d4f3ab7e1 100644 --- a/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs +++ b/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs @@ -1,10 +1,12 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using Aspire.Hosting; using Aspire.Hosting.Tests.Utils; using SamplesIntegrationTests; using SamplesIntegrationTests.Infrastructure; using Xunit; +using Xunit.Sdk; using Xunit.Abstractions; namespace Aspire.Playground.Tests; @@ -21,10 +23,60 @@ public async Task WithDockerfileTest() await app.StartAsync(); await app.WaitForResources().WaitAsync(TimeSpan.FromMinutes(2)); - await app.WaitForTextAsync([$"I'm Batman. - Batman", "I am Iron Man. - Iron Man"]) + await app.WaitForTextAsync($"I'm Batman. - Batman") .WaitAsync(TimeSpan.FromMinutes(3)); app.EnsureNoErrorsLogged(); await app.StopAsync(); } + + [Fact] + public async Task KafkaTest() + { + var appHostPath = Directory.GetFiles(AppContext.BaseDirectory, "KafkaBasic.AppHost.dll").Single(); + var appHost = await DistributedApplicationTestFactory.CreateAsync(appHostPath, _testOutput); + await using var app = await appHost.BuildAsync(); + + await app.StartAsync(); + await app.WaitForResources().WaitAsync(TimeSpan.FromMinutes(2)); + + await WaitForAllTextAsync(app, + [ + "Hello, World! 343", + "Received 1000 messages." + ], + timeoutSecs: 120); + + app.EnsureNoErrorsLogged(); + await app.StopAsync(); + } + + internal static async Task WaitForAllTextAsync(DistributedApplication app, IEnumerable logTexts, string? resourceName = null, int timeoutSecs = -1) + { + var table = logTexts.ToList(); + try + { + var task = app.WaitForTextAsync((log) => + { + foreach (var text in table) + { + if (log.Contains(text)) + { + table.Remove(text); + break; + } + } + + return table.Count == 0; + }, resourceName); + + await (timeoutSecs > 0 + ? task.WaitAsync(TimeSpan.FromSeconds(timeoutSecs)) + : task); + } + catch (TimeoutException te) + { + throw new XunitException($"The following messages were not found: '{string.Join("', '", table)}'", te); + } + } } From 736b871f1ef2ed966088a849f9504ba8c4c0fd3c Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Fri, 23 Aug 2024 17:35:11 -0400 Subject: [PATCH 3/8] WithDockerfile playground app: Set DOCKER_BUILDKIT=1 --- playground/withdockerfile/WithDockerfile.AppHost/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/playground/withdockerfile/WithDockerfile.AppHost/Program.cs b/playground/withdockerfile/WithDockerfile.AppHost/Program.cs index 05130224a90..240a77e541a 100644 --- a/playground/withdockerfile/WithDockerfile.AppHost/Program.cs +++ b/playground/withdockerfile/WithDockerfile.AppHost/Program.cs @@ -9,7 +9,8 @@ builder.AddDockerfile("mycontainer", "qots") .WithBuildArg("GO_VERSION", goVersion) - .WithBuildSecret("SECRET_ASENV", secret); + .WithBuildSecret("SECRET_ASENV", secret) + .WithEnvironment("DOCKER_BUILDKIT", "1"); #if !SKIP_DASHBOARD_REFERENCE // This project is only added in playground projects to support development/debugging From 5e9602e0ef570dccf7187133fc97b6d7f3f3ca77 Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Fri, 23 Aug 2024 17:36:22 -0400 Subject: [PATCH 4/8] WatchNotifications: Cancel, or set exception on tcs when WatchAsync fails --- .../Aspire.Hosting.Tests/Utils/LoggerNotificationExtensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Aspire.Hosting.Tests/Utils/LoggerNotificationExtensions.cs b/tests/Aspire.Hosting.Tests/Utils/LoggerNotificationExtensions.cs index d52bae4bb62..af3b7f37ff9 100644 --- a/tests/Aspire.Hosting.Tests/Utils/LoggerNotificationExtensions.cs +++ b/tests/Aspire.Hosting.Tests/Utils/LoggerNotificationExtensions.cs @@ -98,10 +98,12 @@ private static async Task WatchNotifications(DistributedApplication app, string? catch (OperationCanceledException) { // Expected if the application stops prematurely or the text was detected. + tcs.TrySetCanceled(); } catch (Exception ex) { logger.LogError(ex, "An error occurred while watching for resource notifications."); + tcs.TrySetException(ex); } } From a7cb8ced1466585f2014804ff5528ecd657f63ff Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Fri, 23 Aug 2024 17:36:42 -0400 Subject: [PATCH 5/8] LoggerNotificationExtensions: Add WaitForAllTestAsync --- .../Utils/LoggerNotificationExtensions.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/Aspire.Hosting.Tests/Utils/LoggerNotificationExtensions.cs b/tests/Aspire.Hosting.Tests/Utils/LoggerNotificationExtensions.cs index af3b7f37ff9..69dec7d2103 100644 --- a/tests/Aspire.Hosting.Tests/Utils/LoggerNotificationExtensions.cs +++ b/tests/Aspire.Hosting.Tests/Utils/LoggerNotificationExtensions.cs @@ -68,6 +68,39 @@ public static Task WaitForTextAsync(this DistributedApplication app, Predicate + /// Waits for all the specified texts to be logged. + /// + /// The instance to watch. + /// Any text to wait for. + /// An optional resource name to filter the logs for. + /// The cancellation token. + /// + public static async Task WaitForAllTextAsync(this DistributedApplication app, IEnumerable logTexts, string? resourceName = null, CancellationToken cancellationToken = default) + { + var table = logTexts.ToList(); + try + { + await app.WaitForTextAsync((log) => + { + foreach (var text in table) + { + if (log.Contains(text)) + { + table.Remove(text); + break; + } + } + + return table.Count == 0; + }, resourceName, cancellationToken).ConfigureAwait(false); + } + catch (TaskCanceledException te) when (cancellationToken.IsCancellationRequested) + { + throw new TaskCanceledException($"Task was canceled before these messages were found: '{string.Join("', '", table)}'", te); + } + } + private static async Task WatchNotifications(DistributedApplication app, string? resourceName, Predicate predicate, TaskCompletionSource tcs, CancellationTokenSource cancellationTokenSource) { var resourceNotificationService = app.Services.GetRequiredService(); From 71626177b87830ba1114a2c090b992806ca897e2 Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Fri, 23 Aug 2024 17:40:03 -0400 Subject: [PATCH 6/8] Use the new method from LoggerNotificationExtensions --- .../ProjectSpecificTests.cs | 30 ++++--------------- 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs b/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs index 07d4f3ab7e1..692bfd591b5 100644 --- a/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs +++ b/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs @@ -51,32 +51,14 @@ await WaitForAllTextAsync(app, await app.StopAsync(); } - internal static async Task WaitForAllTextAsync(DistributedApplication app, IEnumerable logTexts, string? resourceName = null, int timeoutSecs = -1) + internal static Task WaitForAllTextAsync(DistributedApplication app, IEnumerable logTexts, string? resourceName = null, int timeoutSecs = -1) { - var table = logTexts.ToList(); - try + CancellationTokenSource cts = new(); + if (timeoutSecs > 0) { - var task = app.WaitForTextAsync((log) => - { - foreach (var text in table) - { - if (log.Contains(text)) - { - table.Remove(text); - break; - } - } - - return table.Count == 0; - }, resourceName); - - await (timeoutSecs > 0 - ? task.WaitAsync(TimeSpan.FromSeconds(timeoutSecs)) - : task); - } - catch (TimeoutException te) - { - throw new XunitException($"The following messages were not found: '{string.Join("', '", table)}'", te); + cts.CancelAfter(TimeSpan.FromSeconds(timeoutSecs)); } + + return app.WaitForAllTextAsync(logTexts, resourceName, cts.Token); } } From f861204f2e241f1f11e38ac82feef894c2db80d0 Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Fri, 23 Aug 2024 17:54:29 -0400 Subject: [PATCH 7/8] fix build --- tests/Aspire.Playground.Tests/ProjectSpecificTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs b/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs index 692bfd591b5..f662e152479 100644 --- a/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs +++ b/tests/Aspire.Playground.Tests/ProjectSpecificTests.cs @@ -6,7 +6,6 @@ using SamplesIntegrationTests; using SamplesIntegrationTests.Infrastructure; using Xunit; -using Xunit.Sdk; using Xunit.Abstractions; namespace Aspire.Playground.Tests; From c1ce916eecfea5ee6861dc7cf6e071846212ad33 Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Fri, 23 Aug 2024 19:10:39 -0400 Subject: [PATCH 8/8] Fix test --- tests/helix/send-to-helix-buildonhelixtests.targets | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/helix/send-to-helix-buildonhelixtests.targets b/tests/helix/send-to-helix-buildonhelixtests.targets index 1488f77ecb9..9029c46e586 100644 --- a/tests/helix/send-to-helix-buildonhelixtests.targets +++ b/tests/helix/send-to-helix-buildonhelixtests.targets @@ -14,6 +14,8 @@ + +