|
// 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.Tests.Utils;
using Aspire.Deployment.EndToEnd.Tests.Helpers;
using Hex1b;
using Hex1b.Automation;
using Xunit;
namespace Aspire.Deployment.EndToEnd.Tests;
/// <summary>
/// End-to-end tests for deploying Aspire applications to Azure Container Apps.
/// </summary>
public sealed class AcaStarterDeploymentTests(ITestOutputHelper output)
{
// Timeout set to 15 minutes to allow for Azure provisioning.
// Full deployments can take 10-20+ minutes. Increase if needed.
private static readonly TimeSpan s_testTimeout = TimeSpan.FromMinutes(15);
[Fact]
public async Task DeployStarterTemplateToAzureContainerApps()
{
using var cts = new CancellationTokenSource(s_testTimeout);
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(
cts.Token, TestContext.Current.CancellationToken);
var cancellationToken = linkedCts.Token;
await DeployStarterTemplateToAzureContainerAppsCore(cancellationToken);
}
private async Task DeployStarterTemplateToAzureContainerAppsCore(CancellationToken cancellationToken)
{
// Validate prerequisites
var subscriptionId = AzureAuthenticationHelpers.TryGetSubscriptionId();
if (string.IsNullOrEmpty(subscriptionId))
{
Assert.Skip("Azure subscription not configured. Set ASPIRE_DEPLOYMENT_TEST_SUBSCRIPTION.");
}
if (!AzureAuthenticationHelpers.IsAzureAuthAvailable())
{
if (DeploymentE2ETestHelpers.IsRunningInCI)
{
Assert.Fail("Azure authentication not available in CI. Check OIDC configuration.");
}
else
{
Assert.Skip("Azure authentication not available. Run 'az login' to authenticate.");
}
}
var workspace = TemporaryWorkspace.Create(output);
var recordingPath = DeploymentE2ETestHelpers.GetTestResultsRecordingPath(nameof(DeployStarterTemplateToAzureContainerApps));
var startTime = DateTime.UtcNow;
var deploymentUrls = new Dictionary<string, string>();
// Note: aspire deploy creates its own resource group with pattern rg-aspire-{appname}
// We add a unique suffix per run to avoid collisions with concurrent runs or cleanup in progress
var runSuffix = DateTime.UtcNow.ToString("HHmmss");
var projectName = $"AcaTest{runSuffix}";
output.WriteLine($"Test: {nameof(DeployStarterTemplateToAzureContainerApps)}");
output.WriteLine($"Project Name: {projectName}");
output.WriteLine($"Expected Resource Group: rg-aspire-{projectName.ToLowerInvariant()}apphost");
output.WriteLine($"Subscription: {subscriptionId[..8]}...");
output.WriteLine($"Workspace: {workspace.WorkspaceRoot.FullName}");
try
{
var builder = Hex1bTerminal.CreateBuilder()
.WithHeadless()
.WithAsciinemaRecording(recordingPath)
.WithPtyProcess("/bin/bash", ["--norc"]);
using var terminal = builder.Build();
var pendingRun = terminal.RunAsync(cancellationToken);
// Pattern searchers for aspire new interactive prompts
var waitingForTemplateSelectionPrompt = new CellPatternSearcher()
.FindPattern("> Starter App");
var waitingForProjectNamePrompt = new CellPatternSearcher()
.Find($"Enter the project name ({workspace.WorkspaceRoot.Name}): ");
var waitingForOutputPathPrompt = new CellPatternSearcher()
.Find("Enter the output path:");
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 aspire add prompts
var waitingForAddVersionSelectionPrompt = new CellPatternSearcher()
.Find("(based on NuGet.config)");
// Pattern searcher for deployment success
var waitingForPipelineSucceeded = new CellPatternSearcher()
.Find("PIPELINE SUCCEEDED");
var counter = new SequenceCounter();
var sequenceBuilder = new Hex1bTerminalInputSequenceBuilder();
// Step 1: Prepare environment
output.WriteLine("Step 1: Preparing environment...");
sequenceBuilder.PrepareEnvironment(workspace, counter);
// Step 2: Set up CLI environment (in CI)
// The workflow builds and installs the CLI to ~/.aspire/bin before running tests
// We just need to source it in the bash session
if (DeploymentE2ETestHelpers.IsRunningInCI)
{
output.WriteLine("Step 2: Using pre-installed Aspire CLI from local build...");
// Source the CLI environment (sets PATH and other env vars)
sequenceBuilder.SourceAspireCliEnvironment(counter);
}
// Step 3: Create starter project using aspire new with interactive prompts
output.WriteLine("Step 3: Creating starter project...");
sequenceBuilder.Type("aspire new")
.Enter()
.WaitUntil(s => waitingForTemplateSelectionPrompt.Search(s).Count > 0, TimeSpan.FromSeconds(60))
.Enter() // Select first template (Starter App ASP.NET Core/Blazor)
.WaitUntil(s => waitingForProjectNamePrompt.Search(s).Count > 0, TimeSpan.FromSeconds(30))
.Type(projectName)
.Enter()
.WaitUntil(s => waitingForOutputPathPrompt.Search(s).Count > 0, TimeSpan.FromSeconds(10))
.Enter() // Accept default output path
.WaitUntil(s => waitingForUrlsPrompt.Search(s).Count > 0, TimeSpan.FromSeconds(10))
.Enter() // Select "No" for localhost URLs (default)
.WaitUntil(s => waitingForRedisPrompt.Search(s).Count > 0, TimeSpan.FromSeconds(10))
// For Redis prompt, default is "Yes" so we need to select "No" by pressing Down
.Key(Hex1b.Input.Hex1bKey.DownArrow)
.Enter() // Select "No" for Redis Cache
.WaitUntil(s => waitingForTestPrompt.Search(s).Count > 0, TimeSpan.FromSeconds(10))
.Enter() // Select "No" for test project (default)
.WaitForSuccessPrompt(counter, TimeSpan.FromMinutes(5));
// Step 4: Navigate to project directory
output.WriteLine("Step 4: Navigating to project directory...");
sequenceBuilder
.Type($"cd {projectName}")
.Enter()
.WaitForSuccessPrompt(counter);
// Step 5: Add Aspire.Hosting.Azure.AppContainers package
output.WriteLine("Step 5: Adding Azure Container Apps hosting package...");
sequenceBuilder.Type("aspire add Aspire.Hosting.Azure.AppContainers")
.Enter();
// In CI, aspire add shows a version selection prompt
if (DeploymentE2ETestHelpers.IsRunningInCI)
{
sequenceBuilder
.WaitUntil(s => waitingForAddVersionSelectionPrompt.Search(s).Count > 0, TimeSpan.FromSeconds(60))
.Enter(); // select first version (PR build)
}
sequenceBuilder.WaitForSuccessPrompt(counter, TimeSpan.FromSeconds(180));
// Step 6: Modify AppHost.cs to add Azure Container App Environment
sequenceBuilder.ExecuteCallback(() =>
{
var projectDir = Path.Combine(workspace.WorkspaceRoot.FullName, projectName);
var appHostDir = Path.Combine(projectDir, $"{projectName}.AppHost");
var appHostFilePath = Path.Combine(appHostDir, "AppHost.cs");
output.WriteLine($"Looking for AppHost.cs at: {appHostFilePath}");
var content = File.ReadAllText(appHostFilePath);
// Insert the Azure Container App Environment before builder.Build().Run();
var buildRunPattern = "builder.Build().Run();";
var replacement = """
// Add Azure Container App Environment for deployment
builder.AddAzureContainerAppEnvironment("infra");
builder.Build().Run();
""";
content = content.Replace(buildRunPattern, replacement);
File.WriteAllText(appHostFilePath, content);
output.WriteLine($"Modified AppHost.cs at: {appHostFilePath}");
});
// Step 7: Navigate to AppHost project directory
output.WriteLine("Step 6: Navigating to AppHost directory...");
sequenceBuilder
.Type($"cd {projectName}.AppHost")
.Enter()
.WaitForSuccessPrompt(counter);
// Step 8: Unset ASPIRE_PLAYGROUND before deploy and set Azure location
sequenceBuilder.Type("unset ASPIRE_PLAYGROUND && export Azure__Location=westus3")
.Enter()
.WaitForSuccessPrompt(counter);
// Step 9: Deploy to Azure Container Apps using aspire deploy
// Use --clear-cache to ensure fresh deployment without cached location from previous runs
output.WriteLine("Step 7: Starting Azure Container Apps deployment...");
sequenceBuilder
.Type("aspire deploy --clear-cache")
.Enter()
// Wait for pipeline to complete successfully
.WaitUntil(s => waitingForPipelineSucceeded.Search(s).Count > 0, TimeSpan.FromMinutes(10))
.WaitForSuccessPrompt(counter, TimeSpan.FromMinutes(2));
// Step 10: Extract deployment URLs and verify endpoints
output.WriteLine("Step 8: Verifying deployed endpoints...");
var expectedResourceGroup = $"rg-aspire-{projectName.ToLowerInvariant()}apphost";
sequenceBuilder
.Type($"RG_NAME=\"{expectedResourceGroup}\" && " +
"echo \"Resource group: $RG_NAME\" && " +
"if ! az group show -n \"$RG_NAME\" &>/dev/null; then echo \"❌ Resource group not found\"; exit 1; fi && " +
// Get external endpoints only (exclude .internal. which are not publicly accessible)
"urls=$(az containerapp list -g \"$RG_NAME\" --query \"[].properties.configuration.ingress.fqdn\" -o tsv 2>/dev/null | grep -v '\\.internal\\.') && " +
"if [ -z \"$urls\" ]; then echo \"❌ No external container app endpoints found\"; exit 1; fi && " +
"failed=0 && " +
"for url in $urls; do " +
"echo -n \"Checking https://$url... \"; " +
"STATUS=$(curl -s -o /dev/null -w \"%{http_code}\" \"https://$url\" --max-time 10 2>/dev/null); " +
"if [ \"$STATUS\" = \"200\" ] || [ \"$STATUS\" = \"302\" ]; then echo \"✅ $STATUS\"; else echo \"❌ $STATUS\"; failed=1; fi; " +
"done && " +
"if [ \"$failed\" -ne 0 ]; then echo \"❌ One or more endpoint checks failed\"; exit 1; fi")
.Enter()
.WaitForSuccessPrompt(counter, TimeSpan.FromMinutes(2));
// Step 11: Exit terminal
sequenceBuilder
.Type("exit")
.Enter();
var sequence = sequenceBuilder.Build();
await sequence.ApplyAsync(terminal, cancellationToken);
await pendingRun;
var duration = DateTime.UtcNow - startTime;
output.WriteLine($"Deployment completed in {duration}");
// Report success
DeploymentReporter.ReportDeploymentSuccess(
nameof(DeployStarterTemplateToAzureContainerApps),
$"rg-aspire-{projectName.ToLowerInvariant()}apphost",
deploymentUrls,
duration);
output.WriteLine("✅ Test passed!");
}
catch (Exception ex)
{
var duration = DateTime.UtcNow - startTime;
output.WriteLine($"❌ Test failed after {duration}: {ex.Message}");
DeploymentReporter.ReportDeploymentFailure(
nameof(DeployStarterTemplateToAzureContainerApps),
$"rg-aspire-{projectName.ToLowerInvariant()}apphost",
ex.Message,
ex.StackTrace);
throw;
}
finally
{
// Note: aspire deploy creates its own resource group (rg-aspire-{appname})
// The cleanup workflow runs hourly and removes resource groups older than 3 hours.
// We trigger cleanup here as a best-effort, but rely on the cleanup workflow for reliability.
var resourceGroupToCleanup = $"rg-aspire-{projectName.ToLowerInvariant()}apphost";
output.WriteLine($"Triggering cleanup of resource group: {resourceGroupToCleanup}");
TriggerCleanupResourceGroup(resourceGroupToCleanup, output);
DeploymentReporter.ReportCleanupStatus(resourceGroupToCleanup, success: true, "Cleanup triggered (fire-and-forget)");
}
}
/// <summary>
/// Triggers cleanup of a specific resource group.
/// This is fire-and-forget - the hourly cleanup workflow handles any missed resources.
/// </summary>
private static void TriggerCleanupResourceGroup(string resourceGroupName, ITestOutputHelper output)
{
// Fire and forget - trigger deletion of the specific resource group created by this test
// The cleanup workflow will handle any that don't get deleted
var process = new System.Diagnostics.Process
{
StartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "az",
Arguments = $"group delete --name {resourceGroupName} --yes --no-wait",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
try
{
process.Start();
output.WriteLine($"Cleanup triggered for resource group: {resourceGroupName}");
}
catch (Exception ex)
{
output.WriteLine($"Failed to trigger cleanup: {ex.Message}");
}
}
}
|