From 4ebd12499761bb15bff026ccdbe04b33304119f5 Mon Sep 17 00:00:00 2001 From: radical <1472+radical@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:46:14 +0000 Subject: [PATCH 1/3] Stage CLI starter validation diagnostics before upload upload-artifact fails with ENOENT when a starter's node_modules changes after it enumerates the upload root. Upload a private copy without node_modules instead. Refs #22347 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .github/workflows/cli-starter-validation.yml | 12 +- eng/github-ci/test-trigger-map.yml | 3 +- eng/scripts/cli-starter-validation.ps1 | 5 +- ...age-cli-starter-validation-diagnostics.ps1 | 128 ++++++++++++++++++ ...ageCliStarterValidationDiagnosticsTests.cs | 114 ++++++++++++++++ 5 files changed, 258 insertions(+), 4 deletions(-) create mode 100644 eng/scripts/stage-cli-starter-validation-diagnostics.ps1 create mode 100644 tests/Infrastructure.Tests/PowerShellScripts/StageCliStarterValidationDiagnosticsTests.cs diff --git a/.github/workflows/cli-starter-validation.yml b/.github/workflows/cli-starter-validation.yml index 2a906eeeea9..782aa365e62 100644 --- a/.github/workflows/cli-starter-validation.yml +++ b/.github/workflows/cli-starter-validation.yml @@ -144,10 +144,20 @@ jobs: Copy-Item -Path $cliLogsDir -Destination $sharedLogsDir -Recurse -Force } + # upload-artifact fails if a file it found disappears before it reads it. Starter project + # dependency installers can still change node_modules after `aspire stop`, so upload a copy. + - name: Stage starter validation diagnostics + if: always() + shell: pwsh + run: | + & "$env:GITHUB_WORKSPACE/eng/scripts/stage-cli-starter-validation-diagnostics.ps1" ` + -SourceRoot (Join-Path $env:RUNNER_TEMP 'aspire-cli-starter-validation') ` + -DestinationRoot (Join-Path $env:RUNNER_TEMP 'aspire-cli-starter-validation-upload') + - name: Upload starter validation diagnostics if: always() uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with: name: cli-starter-validation-${{ inputs.artifactSuffix }} - path: ${{ runner.temp }}/aspire-cli-starter-validation + path: ${{ runner.temp }}/aspire-cli-starter-validation-upload if-no-files-found: ignore diff --git a/eng/github-ci/test-trigger-map.yml b/eng/github-ci/test-trigger-map.yml index 68f4c9274aa..5201c8ad44e 100644 --- a/eng/github-ci/test-trigger-map.yml +++ b/eng/github-ci/test-trigger-map.yml @@ -352,8 +352,9 @@ path_rules: - .github/workflows/cli-starter-validation.yml - eng/scripts/cli-starter-validation.ps1 - eng/scripts/get-aspire-cli-pr.ps1 + - eng/scripts/stage-cli-starter-validation-diagnostics.ps1 targets: [job:cli-starter-validation] - reason: the reusable workflow, PR installer, and scenario script implement starter validation + reason: the reusable workflow, PR installer, scenario script, and diagnostics staging script implement starter validation - paths: - .github/workflows/native-dashboard-validation.yml - eng/scripts/test-native-dashboard.ps1 diff --git a/eng/scripts/cli-starter-validation.ps1 b/eng/scripts/cli-starter-validation.ps1 index d7daf886c22..2fd76576baf 100644 --- a/eng/scripts/cli-starter-validation.ps1 +++ b/eng/scripts/cli-starter-validation.ps1 @@ -433,8 +433,9 @@ foreach ($template in $templates) } # The diagnostics artifact preserves the generated project but does not need installed - # dependencies. Remove them after all Aspire processes stop so upload-artifact cannot race - # a package installer cleanup and fail while stat-ing a disappearing node_modules directory. + # dependencies, so free their disk space here. This removal is best effort: a package + # installer can still change node_modules after `aspire stop` returns. The workflow + # uploads a staged copy without node_modules, so it does not depend on this removal. foreach ($relativeDependencyPath in @('node_modules', 'frontend/node_modules')) { $dependencyPath = Join-Path $projectRoot $relativeDependencyPath diff --git a/eng/scripts/stage-cli-starter-validation-diagnostics.ps1 b/eng/scripts/stage-cli-starter-validation-diagnostics.ps1 new file mode 100644 index 00000000000..7bff946b85a --- /dev/null +++ b/eng/scripts/stage-cli-starter-validation-diagnostics.ps1 @@ -0,0 +1,128 @@ +<# +.SYNOPSIS + Copies Aspire CLI starter validation diagnostics into a private directory for artifact upload. + +.DESCRIPTION + actions/upload-artifact first enumerates every path under the upload root and then calls + fs.stat on each result. It does not tolerate an entry that disappears in between and fails with: + + ENOENT: no such file or directory, stat '/aspire-go-starter/AspireCliGoStarterSmoke/frontend/node_modules' + + The generated starter projects contain node_modules trees. A JavaScript package installer, such + as an interrupted `frontend-installer` npm install, can still rewrite or delete them after + `aspire stop` returns. Upload a copy that no other process writes to instead. The copy skips + node_modules directories without enumerating them, skips directory symbolic links, and ignores + entries that are removed while they are being copied. + +.PARAMETER SourceRoot + The starter validation root that contains generated projects, diagnostics, and CLI logs. + +.PARAMETER DestinationRoot + The directory to create for upload. Any existing directory at this path is replaced. +#> +[CmdletBinding()] +param( + [Parameter(Mandatory = $true)] + [string]$SourceRoot, + + [Parameter(Mandatory = $true)] + [string]$DestinationRoot +) + +$ErrorActionPreference = 'Stop' + +# Test-Path cannot tell whether an entry vanished: it reports a dangling symbolic link as present. +# Classify the failure itself so only "not found" errors are tolerated. +function Test-IsNotFoundError +{ + param( + [Parameter(Mandatory = $true)] + [System.Management.Automation.ErrorRecord]$ErrorRecord + ) + + for ($exception = $ErrorRecord.Exception; $null -ne $exception; $exception = $exception.InnerException) + { + if ($exception -is [System.IO.FileNotFoundException] -or + $exception -is [System.IO.DirectoryNotFoundException] -or + $exception -is [System.Management.Automation.ItemNotFoundException]) + { + return $true + } + } + + return $false +} + +function Copy-DiagnosticsDirectory +{ + param( + [Parameter(Mandatory = $true)] + [string]$Source, + + [Parameter(Mandatory = $true)] + [string]$Destination + ) + + try + { + $children = @(Get-ChildItem -LiteralPath $Source -Force -ErrorAction Stop) + } + catch + { + if (-not (Test-IsNotFoundError -ErrorRecord $_)) + { + throw + } + + Write-Host "Skipping '$Source' because it was removed while staging diagnostics." + return + } + + New-Item -ItemType Directory -Path $Destination -Force | Out-Null + + foreach ($child in $children) + { + $target = Join-Path $Destination $child.Name + + if ($child.PSIsContainer) + { + # Installed dependencies are not diagnostics and are the entries other processes mutate. + # Symbolic links to directories can form cycles or point outside the validation root. + if ($child.Name -eq 'node_modules' -or $child.LinkType) + { + continue + } + + Copy-DiagnosticsDirectory -Source $child.FullName -Destination $target + continue + } + + try + { + Copy-Item -LiteralPath $child.FullName -Destination $target -Force -ErrorAction Stop + } + catch + { + if (-not (Test-IsNotFoundError -ErrorRecord $_)) + { + throw + } + + Write-Host "Skipping '$($child.FullName)' because it was removed while staging diagnostics." + } + } +} + +if (Test-Path -LiteralPath $DestinationRoot) +{ + Remove-Item -LiteralPath $DestinationRoot -Recurse -Force +} + +if (-not (Test-Path -LiteralPath $SourceRoot -PathType Container)) +{ + Write-Host "No starter validation diagnostics found at '$SourceRoot'." + return +} + +Copy-DiagnosticsDirectory -Source $SourceRoot -Destination $DestinationRoot +Write-Host "Staged starter validation diagnostics from '$SourceRoot' to '$DestinationRoot'." diff --git a/tests/Infrastructure.Tests/PowerShellScripts/StageCliStarterValidationDiagnosticsTests.cs b/tests/Infrastructure.Tests/PowerShellScripts/StageCliStarterValidationDiagnosticsTests.cs new file mode 100644 index 00000000000..54374879c0e --- /dev/null +++ b/tests/Infrastructure.Tests/PowerShellScripts/StageCliStarterValidationDiagnosticsTests.cs @@ -0,0 +1,114 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Aspire.TestUtilities; +using Xunit; + +namespace Infrastructure.Tests; + +/// +/// Tests for eng/scripts/stage-cli-starter-validation-diagnostics.ps1 +/// +public sealed class StageCliStarterValidationDiagnosticsTests : IDisposable +{ + private readonly TemporaryWorkspace _workspace; + private readonly string _scriptPath; + private readonly ITestOutputHelper _output; + + public StageCliStarterValidationDiagnosticsTests(ITestOutputHelper output) + { + _output = output; + _workspace = TemporaryWorkspace.Create(output); + _scriptPath = Path.Combine(RepoRoot.Path, "eng", "scripts", "stage-cli-starter-validation-diagnostics.ps1"); + } + + public void Dispose() => _workspace.Dispose(); + + [Fact] + [RequiresTools(["pwsh"])] + public async Task CopiesDiagnosticsWithoutNodeModules() + { + var sourceRoot = Path.Combine(_workspace.Path, "validation"); + var destinationRoot = Path.Combine(_workspace.Path, "upload"); + WriteFile(sourceRoot, "cli-logs/cli.log"); + WriteFile(sourceRoot, "aspire-go-starter/diagnostics/aspire-stop.log"); + WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/apphost.go"); + WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/frontend/package.json"); + WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/frontend/node_modules/@babel/core/package.json"); + WriteFile(sourceRoot, "aspire-ts-starter/AspireCliTsStarterSmoke/node_modules/vite/package.json"); + WriteFile(destinationRoot, "stale.txt"); + + var result = await RunScript(sourceRoot, destinationRoot); + + result.EnsureSuccessful(); + Assert.Equal( + [ + "aspire-go-starter/AspireCliGoStarterSmoke/apphost.go", + "aspire-go-starter/AspireCliGoStarterSmoke/frontend/package.json", + "aspire-go-starter/diagnostics/aspire-stop.log", + "cli-logs/cli.log" + ], + GetRelativeEntries(destinationRoot)); + Assert.Equal("cli-logs/cli.log", File.ReadAllText(Path.Combine(destinationRoot, "cli-logs", "cli.log"))); + } + + [Fact] + [RequiresTools(["pwsh"])] + public async Task SkipsEntriesThatNoLongerExistAndDirectoryLinks() + { + Assert.SkipWhen(OperatingSystem.IsWindows(), "Creating symbolic links requires extra privileges on Windows."); + + var sourceRoot = Path.Combine(_workspace.Path, "validation"); + var destinationRoot = Path.Combine(_workspace.Path, "upload"); + var outsideDirectory = Path.Combine(_workspace.Path, "outside"); + WriteFile(sourceRoot, "aspire-ts-starter/diagnostics/aspire-stop.log"); + WriteFile(outsideDirectory, "secret.txt"); + + // A dangling link reproduces an entry that is listed and then removed before it is copied. + File.CreateSymbolicLink( + Path.Combine(sourceRoot, "aspire-ts-starter", "removed.log"), + Path.Combine(_workspace.Path, "missing.log")); + Directory.CreateSymbolicLink(Path.Combine(sourceRoot, "aspire-ts-starter", "linked"), outsideDirectory); + + var result = await RunScript(sourceRoot, destinationRoot); + + result.EnsureSuccessful(); + Assert.Equal(["aspire-ts-starter/diagnostics/aspire-stop.log"], GetRelativeEntries(destinationRoot)); + } + + [Fact] + [RequiresTools(["pwsh"])] + public async Task MissingSourceLeavesNothingToUpload() + { + var destinationRoot = Path.Combine(_workspace.Path, "upload"); + WriteFile(destinationRoot, "stale.txt"); + + var result = await RunScript(Path.Combine(_workspace.Path, "missing"), destinationRoot); + + result.EnsureSuccessful(); + Assert.False(Directory.Exists(destinationRoot)); + } + + private async Task RunScript(string sourceRoot, string destinationRoot) + { + using var cmd = new PowerShellCommand(_scriptPath, _output) + .WithTimeout(TimeSpan.FromMinutes(2)); + + return await cmd.ExecuteAsync( + "-SourceRoot", $"\"{sourceRoot}\"", + "-DestinationRoot", $"\"{destinationRoot}\""); + } + + private static void WriteFile(string root, string relativePath) + { + var path = Path.Combine(root, relativePath); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + File.WriteAllText(path, relativePath); + } + + private static string[] GetRelativeEntries(string root) => + Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories) + .Select(path => Path.GetRelativePath(root, path).Replace(Path.DirectorySeparatorChar, '/')) + .Order(StringComparer.Ordinal) + .ToArray(); +} From f5fdb2d4db186ba739f0bf77d5366c86d5bffb7d Mon Sep 17 00:00:00 2001 From: radical <1472+radical@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:11:11 +0000 Subject: [PATCH 2/3] Route diagnostics staging script to Infrastructure.Tests StageCliStarterValidationDiagnosticsTests loads the script at runtime, so script-only changes must also select Infrastructure.Tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- eng/github-ci/test-trigger-map.yml | 1 + .../TestTriggerMap/TestTriggerMapTests.cs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/eng/github-ci/test-trigger-map.yml b/eng/github-ci/test-trigger-map.yml index 5201c8ad44e..919903db73e 100644 --- a/eng/github-ci/test-trigger-map.yml +++ b/eng/github-ci/test-trigger-map.yml @@ -564,6 +564,7 @@ path_rules: # Script behavior and packaging fixtures. - eng/scripts/download-native-archives.ps1 - eng/scripts/stage-native-cli-tool-packages.ps1 + - eng/scripts/stage-cli-starter-validation-diagnostics.ps1 - eng/scripts/stabilization-smoke-init-restore.sh - eng/scripts/validate-npm-package-signatures.ps1 - eng/scripts/validate-npm-release-aliases.ps1 diff --git a/tests/Infrastructure.Tests/TestTriggerMap/TestTriggerMapTests.cs b/tests/Infrastructure.Tests/TestTriggerMap/TestTriggerMapTests.cs index c6088e70b9c..80662d54e0f 100644 --- a/tests/Infrastructure.Tests/TestTriggerMap/TestTriggerMapTests.cs +++ b/tests/Infrastructure.Tests/TestTriggerMap/TestTriggerMapTests.cs @@ -357,6 +357,10 @@ private static IReadOnlySet LoadSolutionProjectPaths() "eng/scripts/cli-starter-validation.ps1", ["job:cli-starter-validation"] }, + { + "eng/scripts/stage-cli-starter-validation-diagnostics.ps1", + ["test:Infrastructure.Tests", "job:cli-starter-validation"] + }, { "eng/scripts/test-native-dashboard.ps1", ["job:native-dashboard-validation"] From 014fc591cafcd2f5fca46cdb80451db718482759 Mon Sep 17 00:00:00 2001 From: radical <1472+radical@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:55:32 +0000 Subject: [PATCH 3/3] Archive CLI starter validation diagnostics before upload Exclude dependencies and hidden files, retain generated sources, and publish only completed archives. Cover GNU/BSD tar behavior and archive failures. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .github/workflows/cli-starter-validation.yml | 6 +- eng/scripts/cli-starter-validation.ps1 | 2 +- ...age-cli-starter-validation-diagnostics.ps1 | 121 +++++------------- ...ageCliStarterValidationDiagnosticsTests.cs | 102 +++++++++++++-- 4 files changed, 128 insertions(+), 103 deletions(-) diff --git a/.github/workflows/cli-starter-validation.yml b/.github/workflows/cli-starter-validation.yml index 782aa365e62..41c98e39035 100644 --- a/.github/workflows/cli-starter-validation.yml +++ b/.github/workflows/cli-starter-validation.yml @@ -145,8 +145,8 @@ jobs: } # upload-artifact fails if a file it found disappears before it reads it. Starter project - # dependency installers can still change node_modules after `aspire stop`, so upload a copy. - - name: Stage starter validation diagnostics + # dependency installers can still change node_modules after `aspire stop`, so upload an archive. + - name: Archive starter validation diagnostics if: always() shell: pwsh run: | @@ -159,5 +159,5 @@ jobs: uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with: name: cli-starter-validation-${{ inputs.artifactSuffix }} - path: ${{ runner.temp }}/aspire-cli-starter-validation-upload + path: ${{ runner.temp }}/aspire-cli-starter-validation-upload/starter-validation.tar if-no-files-found: ignore diff --git a/eng/scripts/cli-starter-validation.ps1 b/eng/scripts/cli-starter-validation.ps1 index 2fd76576baf..a496d019ebe 100644 --- a/eng/scripts/cli-starter-validation.ps1 +++ b/eng/scripts/cli-starter-validation.ps1 @@ -435,7 +435,7 @@ foreach ($template in $templates) # The diagnostics artifact preserves the generated project but does not need installed # dependencies, so free their disk space here. This removal is best effort: a package # installer can still change node_modules after `aspire stop` returns. The workflow - # uploads a staged copy without node_modules, so it does not depend on this removal. + # uploads an archive without node_modules, so it does not depend on this removal. foreach ($relativeDependencyPath in @('node_modules', 'frontend/node_modules')) { $dependencyPath = Join-Path $projectRoot $relativeDependencyPath diff --git a/eng/scripts/stage-cli-starter-validation-diagnostics.ps1 b/eng/scripts/stage-cli-starter-validation-diagnostics.ps1 index 7bff946b85a..49c4b161e5d 100644 --- a/eng/scripts/stage-cli-starter-validation-diagnostics.ps1 +++ b/eng/scripts/stage-cli-starter-validation-diagnostics.ps1 @@ -1,6 +1,6 @@ <# .SYNOPSIS - Copies Aspire CLI starter validation diagnostics into a private directory for artifact upload. + Archives Aspire CLI starter validation diagnostics for artifact upload. .DESCRIPTION actions/upload-artifact first enumerates every path under the upload root and then calls @@ -10,15 +10,16 @@ The generated starter projects contain node_modules trees. A JavaScript package installer, such as an interrupted `frontend-installer` npm install, can still rewrite or delete them after - `aspire stop` returns. Upload a copy that no other process writes to instead. The copy skips - node_modules directories without enumerating them, skips directory symbolic links, and ignores - entries that are removed while they are being copied. + `aspire stop` returns. Upload a completed tar archive instead. tar excludes node_modules without + traversing them and stores symbolic links without following them. Hidden files and directories + remain excluded, matching upload-artifact's default. The archive contains the validation root + directory and its retained contents. .PARAMETER SourceRoot The starter validation root that contains generated projects, diagnostics, and CLI logs. .PARAMETER DestinationRoot - The directory to create for upload. Any existing directory at this path is replaced. + The directory in which to create starter-validation.tar for upload. #> [CmdletBinding()] param( @@ -30,99 +31,47 @@ param( ) $ErrorActionPreference = 'Stop' +$PSNativeCommandUseErrorActionPreference = $false -# Test-Path cannot tell whether an entry vanished: it reports a dangling symbolic link as present. -# Classify the failure itself so only "not found" errors are tolerated. -function Test-IsNotFoundError +$archivePath = Join-Path $DestinationRoot 'starter-validation.tar' +$temporaryArchivePath = "$archivePath.partial" +foreach ($path in @($archivePath, $temporaryArchivePath)) { - param( - [Parameter(Mandatory = $true)] - [System.Management.Automation.ErrorRecord]$ErrorRecord - ) - - for ($exception = $ErrorRecord.Exception; $null -ne $exception; $exception = $exception.InnerException) + if (Test-Path -LiteralPath $path) { - if ($exception -is [System.IO.FileNotFoundException] -or - $exception -is [System.IO.DirectoryNotFoundException] -or - $exception -is [System.Management.Automation.ItemNotFoundException]) - { - return $true - } + Remove-Item -LiteralPath $path -Force } - - return $false } -function Copy-DiagnosticsDirectory +if (-not (Test-Path -LiteralPath $SourceRoot -PathType Container)) { - param( - [Parameter(Mandatory = $true)] - [string]$Source, - - [Parameter(Mandatory = $true)] - [string]$Destination - ) - - try - { - $children = @(Get-ChildItem -LiteralPath $Source -Force -ErrorAction Stop) - } - catch - { - if (-not (Test-IsNotFoundError -ErrorRecord $_)) - { - throw - } - - Write-Host "Skipping '$Source' because it was removed while staging diagnostics." - return - } - - New-Item -ItemType Directory -Path $Destination -Force | Out-Null + Write-Host "No starter validation diagnostics found at '$SourceRoot'." + return +} - foreach ($child in $children) +New-Item -ItemType Directory -Path $DestinationRoot -Force | Out-Null +try +{ + # Both GNU tar and BSD/Windows tar exclude this directory name at any depth without entering it. + # Do not dereference links or ignore tar failures: errors in retained diagnostics must stay visible. + # Archive the named root from its parent: using "." lets BSD tar's hidden-file pattern exclude + # the entire "./" tree. ".?*" excludes hidden names at any depth, including "..name". + # https://github.com/actions/upload-artifact#uploading-hidden-files + $sourceDirectory = Get-Item -LiteralPath $SourceRoot + & tar --exclude=node_modules '--exclude=.?*' -cf $temporaryArchivePath -C $sourceDirectory.Parent.FullName $sourceDirectory.Name + if ($LASTEXITCODE -ne 0) { - $target = Join-Path $Destination $child.Name - - if ($child.PSIsContainer) - { - # Installed dependencies are not diagnostics and are the entries other processes mutate. - # Symbolic links to directories can form cycles or point outside the validation root. - if ($child.Name -eq 'node_modules' -or $child.LinkType) - { - continue - } - - Copy-DiagnosticsDirectory -Source $child.FullName -Destination $target - continue - } - - try - { - Copy-Item -LiteralPath $child.FullName -Destination $target -Force -ErrorAction Stop - } - catch - { - if (-not (Test-IsNotFoundError -ErrorRecord $_)) - { - throw - } - - Write-Host "Skipping '$($child.FullName)' because it was removed while staging diagnostics." - } + throw "Archiving starter validation diagnostics failed with tar exit code $LASTEXITCODE." } -} -if (Test-Path -LiteralPath $DestinationRoot) -{ - Remove-Item -LiteralPath $DestinationRoot -Recurse -Force + Move-Item -LiteralPath $temporaryArchivePath -Destination $archivePath -Force } - -if (-not (Test-Path -LiteralPath $SourceRoot -PathType Container)) +finally { - Write-Host "No starter validation diagnostics found at '$SourceRoot'." - return + if (Test-Path -LiteralPath $temporaryArchivePath) + { + Remove-Item -LiteralPath $temporaryArchivePath -Force + } } -Copy-DiagnosticsDirectory -Source $SourceRoot -Destination $DestinationRoot -Write-Host "Staged starter validation diagnostics from '$SourceRoot' to '$DestinationRoot'." +Write-Host "Archived starter validation diagnostics from '$SourceRoot' to '$archivePath'." diff --git a/tests/Infrastructure.Tests/PowerShellScripts/StageCliStarterValidationDiagnosticsTests.cs b/tests/Infrastructure.Tests/PowerShellScripts/StageCliStarterValidationDiagnosticsTests.cs index 54374879c0e..f9854f54552 100644 --- a/tests/Infrastructure.Tests/PowerShellScripts/StageCliStarterValidationDiagnosticsTests.cs +++ b/tests/Infrastructure.Tests/PowerShellScripts/StageCliStarterValidationDiagnosticsTests.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Formats.Tar; using Aspire.TestUtilities; using Xunit; @@ -25,18 +26,23 @@ public StageCliStarterValidationDiagnosticsTests(ITestOutputHelper output) public void Dispose() => _workspace.Dispose(); [Fact] - [RequiresTools(["pwsh"])] - public async Task CopiesDiagnosticsWithoutNodeModules() + [RequiresTools(["pwsh", "tar"])] + public async Task ArchivesDiagnosticsWithoutNodeModulesOrHiddenFiles() { - var sourceRoot = Path.Combine(_workspace.Path, "validation"); - var destinationRoot = Path.Combine(_workspace.Path, "upload"); + var sourceRoot = Path.Combine(_workspace.Path, "validation with spaces"); + var destinationRoot = Path.Combine(_workspace.Path, "upload with spaces"); WriteFile(sourceRoot, "cli-logs/cli.log"); + WriteFile(sourceRoot, ".aspire/settings.json"); + WriteFile(sourceRoot, ".env"); + WriteFile(sourceRoot, "..settings"); + WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/.aspire/settings.json"); + WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/frontend/.env"); WriteFile(sourceRoot, "aspire-go-starter/diagnostics/aspire-stop.log"); WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/apphost.go"); WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/frontend/package.json"); WriteFile(sourceRoot, "aspire-go-starter/AspireCliGoStarterSmoke/frontend/node_modules/@babel/core/package.json"); WriteFile(sourceRoot, "aspire-ts-starter/AspireCliTsStarterSmoke/node_modules/vite/package.json"); - WriteFile(destinationRoot, "stale.txt"); + WriteFile(destinationRoot, "starter-validation.tar"); var result = await RunScript(sourceRoot, destinationRoot); @@ -48,13 +54,13 @@ public async Task CopiesDiagnosticsWithoutNodeModules() "aspire-go-starter/diagnostics/aspire-stop.log", "cli-logs/cli.log" ], - GetRelativeEntries(destinationRoot)); - Assert.Equal("cli-logs/cli.log", File.ReadAllText(Path.Combine(destinationRoot, "cli-logs", "cli.log"))); + ReadArchiveFiles(destinationRoot, sourceRoot)); + Assert.Equal(["starter-validation.tar"], GetRelativeEntries(destinationRoot)); } [Fact] - [RequiresTools(["pwsh"])] - public async Task SkipsEntriesThatNoLongerExistAndDirectoryLinks() + [RequiresTools(["pwsh", "tar"])] + public async Task ArchivesLinksWithoutFollowingTargets() { Assert.SkipWhen(OperatingSystem.IsWindows(), "Creating symbolic links requires extra privileges on Windows."); @@ -64,7 +70,6 @@ public async Task SkipsEntriesThatNoLongerExistAndDirectoryLinks() WriteFile(sourceRoot, "aspire-ts-starter/diagnostics/aspire-stop.log"); WriteFile(outsideDirectory, "secret.txt"); - // A dangling link reproduces an entry that is listed and then removed before it is copied. File.CreateSymbolicLink( Path.Combine(sourceRoot, "aspire-ts-starter", "removed.log"), Path.Combine(_workspace.Path, "missing.log")); @@ -73,7 +78,18 @@ public async Task SkipsEntriesThatNoLongerExistAndDirectoryLinks() var result = await RunScript(sourceRoot, destinationRoot); result.EnsureSuccessful(); - Assert.Equal(["aspire-ts-starter/diagnostics/aspire-stop.log"], GetRelativeEntries(destinationRoot)); + Assert.Equal(["aspire-ts-starter/diagnostics/aspire-stop.log"], ReadArchiveFiles(destinationRoot, sourceRoot)); + using var archive = File.OpenRead(Path.Combine(destinationRoot, "starter-validation.tar")); + using var reader = new TarReader(archive); + List links = []; + while (reader.GetNextEntry() is { } entry) + { + if (entry.EntryType is TarEntryType.SymbolicLink) + { + links.Add(entry.Name); + } + } + Assert.Equal(["validation/aspire-ts-starter/linked", "validation/aspire-ts-starter/removed.log"], links.Order(StringComparer.Ordinal)); } [Fact] @@ -81,12 +97,48 @@ public async Task SkipsEntriesThatNoLongerExistAndDirectoryLinks() public async Task MissingSourceLeavesNothingToUpload() { var destinationRoot = Path.Combine(_workspace.Path, "upload"); - WriteFile(destinationRoot, "stale.txt"); + WriteFile(destinationRoot, "starter-validation.tar"); + WriteFile(destinationRoot, "starter-validation.tar.partial"); var result = await RunScript(Path.Combine(_workspace.Path, "missing"), destinationRoot); result.EnsureSuccessful(); - Assert.False(Directory.Exists(destinationRoot)); + Assert.Empty(Directory.EnumerateFileSystemEntries(destinationRoot)); + } + + [Theory] + [InlineData(1)] + [InlineData(2)] + [RequiresTools(["pwsh"])] + public async Task ArchiveFailureLeavesNothingToUpload(int tarExitCode) + { + var sourceRoot = Path.Combine(_workspace.Path, "validation"); + var destinationRoot = Path.Combine(_workspace.Path, "upload"); + WriteFile(sourceRoot, "diagnostics/aspire-stop.log"); + WriteFile(destinationRoot, "starter-validation.tar"); + + // A native command may leave partial output before failing. Exercise the production script + // with that failure without relying on permissions, disk exhaustion, or a timed filesystem race. + var wrapper = Path.Combine(_workspace.Path, "fail-tar.ps1"); + await File.WriteAllTextAsync(wrapper, """ + param($Script, $SourceRoot, $DestinationRoot, $TarExitCode) + function tar + { + [System.IO.File]::WriteAllText((Join-Path $DestinationRoot 'starter-validation.tar.partial'), 'incomplete') + $global:LASTEXITCODE = $TarExitCode + } + & $Script -SourceRoot $SourceRoot -DestinationRoot $DestinationRoot + """); + using var cmd = new PowerShellCommand(wrapper, _output).WithTimeout(TimeSpan.FromMinutes(2)); + var result = await cmd.ExecuteAsync( + "-Script", $"\"{_scriptPath}\"", + "-SourceRoot", $"\"{sourceRoot}\"", + "-DestinationRoot", $"\"{destinationRoot}\"", + "-TarExitCode", tarExitCode.ToString()); + + Assert.NotEqual(0, result.ExitCode); + Assert.Contains($"tar exit code {tarExitCode}", result.Output); + Assert.Empty(Directory.EnumerateFileSystemEntries(destinationRoot)); } private async Task RunScript(string sourceRoot, string destinationRoot) @@ -111,4 +163,28 @@ private static string[] GetRelativeEntries(string root) => .Select(path => Path.GetRelativePath(root, path).Replace(Path.DirectorySeparatorChar, '/')) .Order(StringComparer.Ordinal) .ToArray(); + + private static string[] ReadArchiveFiles(string root, string sourceRoot) + { + using var archive = File.OpenRead(Path.Combine(root, "starter-validation.tar")); + using var reader = new TarReader(archive); + List files = []; + while (reader.GetNextEntry() is { } entry) + { + if (entry.EntryType is not (TarEntryType.RegularFile or TarEntryType.V7RegularFile)) + { + continue; + } + + // Entries include the validation directory, for example "validation/cli-logs/cli.log". + var prefix = Path.GetFileName(sourceRoot) + "/"; + Assert.StartsWith(prefix, entry.Name, StringComparison.Ordinal); + var name = entry.Name[prefix.Length..]; + using var content = new StreamReader(entry.DataStream!); + Assert.Equal(name, content.ReadToEnd()); + files.Add(name); + } + + return files.Order(StringComparer.Ordinal).ToArray(); + } }