|
// 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.Tests.Utils;
using Hex1b;
using Hex1b.Automation;
using Xunit;
namespace Aspire.Cli.EndToEnd.Tests;
/// <summary>
/// End-to-end tests for the aspire resources command.
/// Each test class runs as a separate CI job for parallelization.
/// </summary>
public sealed class ResourcesCommandTests(ITestOutputHelper output)
{
[Fact]
public async Task ResourcesCommandShowsRunningResources()
{
var workspace = TemporaryWorkspace.Create(output);
var prNumber = CliE2ETestHelpers.GetRequiredPrNumber();
var commitSha = CliE2ETestHelpers.GetRequiredCommitSha();
var isCI = CliE2ETestHelpers.IsRunningInCI;
var recordingPath = CliE2ETestHelpers.GetTestResultsRecordingPath(nameof(ResourcesCommandShowsRunningResources));
var builder = Hex1bTerminal.CreateBuilder()
.WithHeadless()
.WithDimensions(160, 48)
.WithAsciinemaRecording(recordingPath)
.WithPtyProcess("/bin/bash", ["--norc"]);
using var terminal = builder.Build();
var pendingRun = terminal.RunAsync(TestContext.Current.CancellationToken);
// Pattern searchers for aspire new prompts
var waitingForTemplateSelectionPrompt = new CellPatternSearcher()
.Find("> Starter App");
var waitingForProjectNamePrompt = new CellPatternSearcher()
.Find($"Enter the project name ({workspace.WorkspaceRoot.Name}): ");
var waitingForOutputPathPrompt = new CellPatternSearcher()
.Find($"Enter the output path: (./AspireResourcesTestApp): ");
var waitingForUrlsPrompt = new CellPatternSearcher()
.Find($"Use *.dev.localhost URLs");
var waitingForRedisPrompt = new CellPatternSearcher()
.Find($"Use Redis Cache");
var waitingForTestPrompt = new CellPatternSearcher()
.Find($"Do you want to create a test project?");
// Pattern searchers for start/stop/resources commands
var waitForAppHostStartedSuccessfully = new CellPatternSearcher()
.Find("AppHost started successfully.");
var waitForAppHostStoppedSuccessfully = new CellPatternSearcher()
.Find("AppHost stopped successfully.");
// Pattern for aspire resources output - table header
var waitForResourcesTableHeader = new CellPatternSearcher()
.Find("Name");
// Pattern for resources - should show the webfrontend and apiservice
var waitForWebfrontendResource = new CellPatternSearcher()
.Find("webfrontend");
var waitForApiserviceResource = new CellPatternSearcher()
.Find("apiservice");
// Pattern for verifying JSON output was written to file
var waitForJsonFileWritten = new CellPatternSearcher()
.Find("webfrontend");
var counter = new SequenceCounter();
var sequenceBuilder = new Hex1bTerminalInputSequenceBuilder();
sequenceBuilder.PrepareEnvironment(workspace, counter);
if (isCI)
{
sequenceBuilder.InstallAspireCliFromPullRequest(prNumber, counter);
sequenceBuilder.SourceAspireCliEnvironment(counter);
sequenceBuilder.VerifyAspireCliVersion(commitSha, counter);
}
// Create a new project using aspire new
sequenceBuilder.Type("aspire new")
.Enter()
.WaitUntil(s => waitingForTemplateSelectionPrompt.Search(s).Count > 0, TimeSpan.FromSeconds(30))
.Enter() // select first template (Starter App)
.WaitUntil(s => waitingForProjectNamePrompt.Search(s).Count > 0, TimeSpan.FromSeconds(10))
.Type("AspireResourcesTestApp")
.Enter()
.WaitUntil(s => waitingForOutputPathPrompt.Search(s).Count > 0, TimeSpan.FromSeconds(10))
.Enter()
.WaitUntil(s => waitingForUrlsPrompt.Search(s).Count > 0, TimeSpan.FromSeconds(10))
.Enter()
.WaitUntil(s => waitingForRedisPrompt.Search(s).Count > 0, TimeSpan.FromSeconds(10))
.Enter()
.WaitUntil(s => waitingForTestPrompt.Search(s).Count > 0, TimeSpan.FromSeconds(10))
.Enter()
.WaitForSuccessPrompt(counter);
// Navigate to the AppHost directory
sequenceBuilder.Type("cd AspireResourcesTestApp/AspireResourcesTestApp.AppHost")
.Enter()
.WaitForSuccessPrompt(counter);
// Start the AppHost in the background using aspire run --detach
sequenceBuilder.Type("aspire run --detach")
.Enter()
.WaitUntil(s => waitForAppHostStartedSuccessfully.Search(s).Count > 0, TimeSpan.FromMinutes(3))
.WaitForSuccessPrompt(counter);
// Wait a bit for resources to stabilize
sequenceBuilder.Type("sleep 5")
.Enter()
.WaitForSuccessPrompt(counter);
// Now verify aspire resources shows the running resources (human-readable table)
sequenceBuilder.Type("aspire resources")
.Enter()
.WaitUntil(s => waitForResourcesTableHeader.Search(s).Count > 0, TimeSpan.FromSeconds(30))
.WaitUntil(s => waitForWebfrontendResource.Search(s).Count > 0, TimeSpan.FromSeconds(5))
.WaitUntil(s => waitForApiserviceResource.Search(s).Count > 0, TimeSpan.FromSeconds(5))
.WaitForSuccessPrompt(counter);
// Test aspire resources --format json output - pipe to file to avoid terminal buffer issues
sequenceBuilder.Type("aspire resources --format json > resources.json")
.Enter()
.WaitForSuccessPrompt(counter);
// Verify the JSON file contains expected resources
sequenceBuilder.Type("cat resources.json | grep webfrontend")
.Enter()
.WaitUntil(s => waitForJsonFileWritten.Search(s).Count > 0, TimeSpan.FromSeconds(10))
.WaitForSuccessPrompt(counter);
// Stop the AppHost using aspire stop
sequenceBuilder.Type("aspire stop")
.Enter()
.WaitUntil(s => waitForAppHostStoppedSuccessfully.Search(s).Count > 0, TimeSpan.FromMinutes(1))
.WaitForSuccessPrompt(counter);
// Exit the shell
sequenceBuilder.Type("exit")
.Enter();
var sequence = sequenceBuilder.Build();
await sequence.ApplyAsync(terminal, TestContext.Current.CancellationToken);
await pendingRun;
}
}
|