// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text.Json;
using Aspire.TestUtilities;
using Xunit;
namespace Infrastructure.Tests;
public sealed class CliStarterValidationTests(ITestOutputHelper output)
{
[Theory]
[InlineData(false, "none")]
[InlineData(true, "none")]
[InlineData(false, "run")]
[InlineData(true, "run")]
[InlineData(false, "start")]
[InlineData(true, "start")]
[InlineData(false, "http")]
[InlineData(false, "http-status")]
[InlineData(false, "http-start")]
[InlineData(false, "http-retry")]
[RequiresTools(["pwsh"])]
public async Task ValidatesGoFrontendAndCleansUpDependencies(bool linux, string failedPhase)
{
using var workspace = TemporaryWorkspace.Create(output);
var wrapperPath = Path.Combine(workspace.Path, "validate.ps1");
var validationRoot = Path.Combine(workspace.Path, "validation");
var eventsPath = Path.Combine(workspace.Path, "events.jsonl");
await File.WriteAllTextAsync(wrapperPath, """
$ErrorActionPreference = 'Stop'
Set-Variable -Name IsLinux -Value ($env:TEST_LINUX -eq 'true') -Force
function Write-Invocation($Command, $Arguments, $Directory = (Get-Location).Path)
{
@{
template = Split-Path (Split-Path $Directory) -Leaf
command = $Command
arguments = @($Arguments)
} | ConvertTo-Json -Compress | Add-Content $env:TEST_EVENTS
}
function aspire
{
if ($args[0] -eq 'new')
{
$projectRoot = $args[[Array]::IndexOf($args, '--output') + 1]
foreach ($relativePath in @('node_modules', 'frontend/node_modules'))
{
$dependencies = Join-Path $projectRoot $relativePath
New-Item -ItemType Directory -Path $dependencies -Force | Out-Null
Set-Content (Join-Path $dependencies 'preserved.txt') 'installed dependency'
}
}
else
{
Write-Invocation $args[0] @($args | Select-Object -Skip 1)
if ($args[0] -eq 'logs')
{
'installer diagnostic'
}
}
}
# Replace only external process execution; run the complete production script,
# including its resource checks, failure aggregation, and finally cleanup.
function Start-Process
{
param($FilePath, $ArgumentList, $WorkingDirectory,
$RedirectStandardOutput, $RedirectStandardError, [switch]$Wait, [switch]$PassThru)
if ($FilePath -ne 'aspire')
{
throw "Unexpected executable: $FilePath"
}
Write-Invocation $ArgumentList[0] @($ArgumentList | Select-Object -Skip 1) $WorkingDirectory
if ($ArgumentList[0] -in @('run', 'start'))
{
$global:TestPhase = $ArgumentList[0]
}
$exitCode = 0
if ($env:TEST_FAIL_PHASE -eq $global:TestPhase -and
$WorkingDirectory -like '*AspireCliGoStarterSmoke' -and
$ArgumentList[0] -eq 'wait' -and $ArgumentList[1] -eq 'frontend')
{
$exitCode = 1
}
$stdout = if ($ArgumentList[0] -in @('run', 'start'))
{
@{ appHostPid = 123; appHostPath = $WorkingDirectory; logFile = 'apphost.log' } |
ConvertTo-Json -Compress
}
elseif ($ArgumentList[0] -eq 'describe')
{
'{"resources":[{"urls":[{"url":"http://localhost:12345"}]}]}'
}
else
{
"Resource command exited with code $exitCode"
}
Set-Content $RedirectStandardOutput $stdout
Set-Content $RedirectStandardError ''
$process = [pscustomobject]@{ Id = 123; ExitCode = $exitCode }
$process | Add-Member -MemberType ScriptMethod -Name WaitForExit -Value { param($Timeout) return $true }
return $process
}
$global:TestHttpAttempts = 0
function Invoke-WebRequest
{
param($Uri, $TimeoutSec, [switch]$SkipHttpErrorCheck)
Write-Invocation 'http' @($Uri)
$global:TestHttpAttempts++
if ($env:TEST_FAIL_PHASE -eq 'http' -or
($env:TEST_FAIL_PHASE -eq 'http-start' -and $global:TestPhase -eq 'start') -or
($env:TEST_FAIL_PHASE -eq 'http-retry' -and $global:TestHttpAttempts -eq 1))
{
throw 'connection refused'
}
if ($env:TEST_FAIL_PHASE -eq 'http-status')
{
return [pscustomobject]@{ StatusCode = 503 }
}
return [pscustomobject]@{ StatusCode = 200 }
}
& $env:TEST_SCRIPT -PRNumber 1 -MaxStartupSeconds 1 `
-ResourceReadyTimeoutSeconds 3 -ValidationRoot $env:TEST_ROOT
""");
using var command = new PowerShellCommand(wrapperPath, output)
.WithTimeout(TimeSpan.FromMinutes(2))
.WithEnvironmentVariable("TEST_SCRIPT", Path.Combine(RepoRoot.Path, "eng", "scripts", "cli-starter-validation.ps1"))
.WithEnvironmentVariable("TEST_ROOT", validationRoot)
.WithEnvironmentVariable("TEST_EVENTS", eventsPath)
.WithEnvironmentVariable("TEST_LINUX", linux ? "true" : "false")
.WithEnvironmentVariable("TEST_FAIL_PHASE", failedPhase);
var result = await command.ExecuteAsync();
if (failedPhase is "http" or "http-status" or "http-start")
{
Assert.NotEqual(0, result.ExitCode);
Assert.Contains("frontend at 'http://localhost:12345' did not respond", result.Output);
Assert.Contains(failedPhase == "http-status" ? "HTTP 503" : "connection refused", result.Output);
}
else if (failedPhase is "run" or "start")
{
Assert.NotEqual(0, result.ExitCode);
Assert.Contains("aspire-go-starter: aspire wait for resource frontend failed with exit code 1.", result.Output);
}
else
{
result.EnsureSuccessful();
}
var invocations = File.ReadAllLines(eventsPath)
.Select(line => JsonSerializer.Deserialize<JsonElement>(line))
.Where(item => item.GetProperty("template").GetString() == "aspire-go-starter")
.ToArray();
var waitedResources = invocations
.Where(item => item.GetProperty("command").GetString() == "wait")
.Select(item => item.GetProperty("arguments")[0].GetString())
.ToArray();
var commands = invocations.Select(item => item.GetProperty("command").GetString()).ToArray();
if (failedPhase == "run")
{
Assert.Equal(["frontend"], waitedResources);
Assert.Equal(["add", "run", "wait", "logs", "stop"], commands);
}
else if (failedPhase is "http" or "http-status")
{
Assert.Equal(["frontend"], waitedResources);
Assert.Equal(["add", "run", "wait", "describe"], commands[..4]);
Assert.All(commands[4..^2], invocation => Assert.Equal("http", invocation));
Assert.Equal(["logs", "stop"], commands[^2..]);
}
else
{
Assert.Equal(linux && failedPhase != "start" ? ["frontend", "app", "frontend", "cache"] : ["frontend", "app", "frontend"], waitedResources);
var requests = failedPhase == "http-retry" ? new[] { "http", "http" } : ["http"];
string[] startCommands = ["add", "run", "wait", "describe", .. requests, "stop", "start", "resources", .. waitedResources.Skip(1).Select(_ => "wait")];
if (failedPhase == "start")
{
Assert.Equal([.. startCommands, "logs", "stop"], commands);
}
else if (failedPhase == "http-start")
{
Assert.Equal([.. startCommands, "describe"], commands[..(startCommands.Length + 1)]);
Assert.All(commands[(startCommands.Length + 1)..^2], invocation => Assert.Equal("http", invocation));
Assert.Equal(["logs", "stop"], commands[^2..]);
}
else
{
Assert.Equal([.. startCommands, "describe", "http", "stop"], commands);
}
}
var goProject = Path.Combine(validationRoot, "aspire-go-starter", "AspireCliGoStarterSmoke");
if (failedPhase is "run" or "start" or "http" or "http-status" or "http-start")
{
var logInvocation = Assert.Single(invocations, item => item.GetProperty("command").GetString() == "logs");
Assert.Contains("--include-hidden", logInvocation.GetProperty("arguments").EnumerateArray().Select(arg => arg.GetString()));
var logFile = failedPhase is "run" or "http" or "http-status" ? "aspire-run-resource-logs.log" : "aspire-resource-logs.log";
var logPath = Path.Combine(validationRoot, "aspire-go-starter", "diagnostics", logFile);
Assert.Equal("installer diagnostic", File.ReadAllText(logPath).Trim());
}
foreach (var relativePath in new[] { "node_modules", Path.Combine("frontend", "node_modules") })
{
Assert.False(Directory.Exists(Path.Combine(goProject, relativePath)));
}
}
}