-
Notifications
You must be signed in to change notification settings - Fork 991
Handle non-interactive interaction service availability for resource commands #16973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
James Newton-King (JamesNK)
merged 6 commits into
microsoft:main
from
JamesNK:fix/interaction-service-noninteractive-resource-command
May 13, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eae1502
Handle non-interactive interaction service availability
JamesNK 14d3a7e
Fixes
JamesNK 1c21ba0
Handle non-interactive resource commands and tighten logger nullability
JamesNK f6c6b38
Add timeout guard to interaction prompt E2E command test
JamesNK 6d8be9f
Simplify E2E test to use empty C# app host template
JamesNK 50f4f17
Add ASPIREINTERACTION001 suppression to E2E test app host
JamesNK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 44 additions & 37 deletions
81
src/Aspire.Cli/Backchannel/AppHostAuxiliaryBackchannel.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
tests/Aspire.Cli.EndToEnd.Tests/ResourceCommandTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Cli.EndToEnd.Tests.Helpers; | ||
| using Aspire.Cli.Resources; | ||
| using Aspire.Cli.Tests.Utils; | ||
| using Hex1b.Automation; | ||
| using Xunit; | ||
|
|
||
| namespace Aspire.Cli.EndToEnd.Tests; | ||
|
|
||
| /// <summary> | ||
| /// End-to-end tests for aspire resource command execution. | ||
| /// </summary> | ||
| public sealed class ResourceCommandTests(ITestOutputHelper output) | ||
| { | ||
| [Fact] | ||
| [CaptureWorkspaceOnFailure] | ||
| public async Task ResourceCommand_FailsWhenInteractionServiceIsRequired() | ||
| { | ||
| var repoRoot = CliE2ETestHelpers.GetRepoRoot(); | ||
| var strategy = CliInstallStrategy.Detect(output.WriteLine); | ||
| var projectSuffix = Guid.NewGuid().ToString("N")[..6]; | ||
| var projectName = $"ResourceCmdApp_{projectSuffix}"; | ||
|
|
||
| using var workspace = TemporaryWorkspace.Create(output); | ||
|
|
||
| using var terminal = CliE2ETestHelpers.CreateDockerTestTerminal(repoRoot, strategy, output, mountDockerSocket: true, workspace: workspace); | ||
|
|
||
| var pendingRun = terminal.RunAsync(TestContext.Current.CancellationToken); | ||
|
|
||
| var counter = new SequenceCounter(); | ||
| var auto = new Hex1bTerminalAutomator(terminal, defaultTimeout: TimeSpan.FromSeconds(500)); | ||
| var testBodyFailed = false; | ||
|
|
||
| try | ||
| { | ||
| await auto.PrepareDockerEnvironmentAsync(counter, workspace, enableDcpDiagnostics: true); | ||
| await auto.InstallAspireCliAsync(strategy, counter); | ||
| await auto.AspireNewAsync(projectName, counter, template: AspireTemplate.EmptyAppHost); | ||
|
|
||
| await auto.TypeAsync($"cd {projectName}"); | ||
| await auto.EnterAsync(); | ||
| await auto.WaitForSuccessPromptAsync(counter); | ||
|
|
||
| // Read the generated apphost.cs so we can extract the #:sdk line with the | ||
| // resolved version, then replace the entire file with a minimal app host | ||
| // that has a placeholder resource and a command that uses IInteractionService. | ||
| var appHostFilePath = Path.Combine(workspace.WorkspaceRoot.FullName, projectName, "apphost.cs"); | ||
| var content = File.ReadAllText(appHostFilePath); | ||
|
|
||
| // Extract the first line (#:sdk directive) so the replacement uses the same SDK version. | ||
| var sdkLine = content.Split('\n', 2)[0].TrimEnd('\r'); | ||
|
|
||
| var newContent = $$""" | ||
| {{sdkLine}} | ||
|
|
||
| #pragma warning disable ASPIREINTERACTION001 | ||
|
|
||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var cache = builder.AddContainer("cache", "redis"); | ||
|
|
||
| cache.WithCommand( | ||
| name: "needs-interaction", | ||
| displayName: "Needs interaction", | ||
| executeCommand: async context => | ||
| { | ||
| var interactionService = (IInteractionService)context.ServiceProvider.GetService(typeof(IInteractionService))!; | ||
|
|
||
| try | ||
| { | ||
| // This should throw because InteractionService is not available in non-interactive mode. | ||
| // Bound the wait to avoid hanging the E2E run if behavior regresses. | ||
| _ = await interactionService.PromptInputAsync( | ||
| title: "Prompt title", | ||
| message: "Prompt message", | ||
| inputLabel: "Name", | ||
| placeHolder: "placeholder").WaitAsync(TimeSpan.FromSeconds(5)); | ||
|
|
||
| return CommandResults.Failure("Prompt unexpectedly completed without throwing."); | ||
| } | ||
| catch (TimeoutException) | ||
| { | ||
| return CommandResults.Failure("Prompt timed out after 5 seconds."); | ||
| } | ||
| }); | ||
|
|
||
| builder.Build().Run(); | ||
| """; | ||
|
|
||
| File.WriteAllText(appHostFilePath, newContent); | ||
|
|
||
| await auto.TypeAsync("aspire start"); | ||
| await auto.EnterAsync(); | ||
| await auto.WaitUntilTextAsync(RunCommandStrings.AppHostStartedSuccessfully, timeout: TimeSpan.FromMinutes(3)); | ||
| await auto.WaitForSuccessPromptAsync(counter); | ||
|
|
||
| await auto.TypeAsync("aspire resource cache needs-interaction"); | ||
| await auto.EnterAsync(); | ||
| await auto.WaitUntilTextAsync("Failed to execute command 'needs-interaction' on resource 'cache'", timeout: TimeSpan.FromSeconds(30)); | ||
| await auto.WaitUntilTextAsync("InteractionService is not available", timeout: TimeSpan.FromSeconds(30)); | ||
| await auto.WaitForAnyPromptAsync(counter, timeout: TimeSpan.FromSeconds(30)); | ||
|
|
||
| await auto.TypeAsync("if [ $? -eq 0 ]; then echo RESOURCE_CMD_SUCCEEDED_UNEXPECTEDLY; else echo RESOURCE_CMD_FAILED_AS_EXPECTED; fi"); | ||
| await auto.EnterAsync(); | ||
| await auto.WaitUntilTextAsync("RESOURCE_CMD_FAILED_AS_EXPECTED", timeout: TimeSpan.FromSeconds(10)); | ||
| await auto.WaitForSuccessPromptAsync(counter); | ||
|
|
||
| await auto.TypeAsync("aspire stop"); | ||
| await auto.EnterAsync(); | ||
| await auto.WaitUntilAppHostStoppedSuccessfullyAsync(timeout: TimeSpan.FromMinutes(1)); | ||
| await auto.WaitForSuccessPromptAsync(counter); | ||
| } | ||
| catch | ||
| { | ||
| testBodyFailed = true; | ||
| throw; | ||
| } | ||
| finally | ||
| { | ||
| try | ||
| { | ||
| await auto.CaptureAspireDiagnosticsAsync(counter, workspace); | ||
| } | ||
| catch | ||
| { | ||
| // Best effort diagnostics capture. | ||
| } | ||
|
|
||
| try | ||
| { | ||
| await auto.TypeAsync("exit"); | ||
| await auto.EnterAsync(); | ||
| await pendingRun; | ||
| } | ||
| catch | ||
| { | ||
| if (!testBodyFailed) | ||
| { | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.