From 7a0e262daf1c52e8e6174d72eb7f7b919b0a302c Mon Sep 17 00:00:00 2001 From: David Pine Date: Thu, 5 Nov 2026 22:44:02 -0600 Subject: [PATCH] Add Go AppHost initialization and stable restore E2E coverage (#21806) ## Description Adds focused Go AppHost E2E coverage for the initialization and package-resolution gaps identified while reviewing the Preview flag-removal PR. - Extends SDK-refresh coverage to interactive init, non-interactive `init --language go` in an empty directory, and non-interactive init alongside an existing Go application. The brownfield case verifies source preservation and wires the existing application through the default Go integration. - Builds and vets the consumer and explicitly vets the generated `.aspire/modules` module, which Go's root `./...` traversal skips. - Adds a separate stable-identity starter restore/build test with an isolated NuGet cache, exact Go/Redis package pins, SDK regeneration, and NuGet metadata assertions proving the packages came from the local hive. - Retains normal-channel starter startup coverage and adds guaranteed stop cleanup after resource assertions. Contributes to #21108 and #21110; companion coverage for #21183. This does not close either readiness issue or include the flag-removal changes. Existing flag-setup calls are unchanged so #21183 can remove them as planned. ### Execution lanes Initialization, code generation, build/vet, and normal-channel startup run in the existing regular CLI E2E lane. The stable restore/build test requires a stable-shaped local-hive archive and explicitly skips ordinary prerelease PR archives, matching the existing stable-emulation tests. The stabilization job in `ci.yml` is currently hard-disabled; this PR does not enable it or claim automated stable-release gating. The existing runtime-package routes already cover Go, its generator, Redis and SQL Server. The selector attributes the changed test files to `Aspire.Cli.EndToEnd.Tests`; no map changes are required. ### Validation | Command | Result | | --- | --- | | `.\restore.cmd` | Passed; reported existing dependency-advisory warnings. | | `dotnet build tests\Aspire.Cli.EndToEnd.Tests\Aspire.Cli.EndToEnd.Tests.csproj --no-restore -m:2 -v:quiet` | Passed, 0 warnings, 0 errors. | | `dotnet test --project tests\Aspire.Cli.EndToEnd.Tests\Aspire.Cli.EndToEnd.Tests.csproj --no-launch-profile -- --filter-class "*.GoCodegenValidationTests" --filter-class "*.GoStarterTemplateTests" --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true"` | Initial run: all three init/codegen cases and normal-channel startup passed. An initially broader stable scenario failed at startup, after restore/build/vet passed, because the PR-stamped binary requested its original prerelease SDK from nuget.org. Stable restore/build is now a separate test, scoped to the requested package-resolution coverage. | | `dotnet test --project tests\Aspire.Cli.EndToEnd.Tests\Aspire.Cli.EndToEnd.Tests.csproj --no-launch-profile -- --filter-method "*.RestoreAndBuildGoStarterFromStablePackages" --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true"` | Passed, 1 test, 0 skipped, against the final stable restore/build scenario. | | `.\localhive.ps1 -Version 17.0.99 -Output .\artifacts\go-stable-e2e -Rid linux-x64 -Archive` | Blocked by NU5104 for stable component packages depending on preview .NET 11 packages. The pack excludes the changed test project. A consumer-only retry with `$env:NoWarn='NU5104'` packed the packages, then hit Windows-to-Linux NativeAOT dashboard publishing restrictions. No warning suppression or build-policy changes are committed. | | `git diff --check` | Passed. | For the Docker executions, `ASPIRE_E2E_POLYGLOT_GO_IMAGE=aspire-go-e2e-6972:latest` selected an existing Go 1.26.10 image. `ASPIRE_E2E_ARCHIVE` pointed to a consumer archive assembled from the approved #21183 native CLI (`ecee5b208e`) and real `17.0.99` packages built from this worktree. This validates the consumer scenarios, not a fully rebuilt stable native bundle or stable-identity startup. Linux stable-hive reproduction commands are documented in `docs/contributing.md`. ## Checklist - Is this feature complete? - [x] Yes. Ready to ship. - [ ] No. Follow-up changes expected. - Are you including unit tests for the changes and scenario tests if relevant? - [x] Yes - [ ] No - Did you add public API? - [ ] Yes - If yes, did you have an API Review for it? - [ ] Yes - [ ] No - Did you add `` and `` elements on your triple slash comments? - [ ] Yes - [ ] No - [x] No - Does the change make any security assumptions or guarantees? - [ ] Yes - If yes, have you done a threat model and had a security review? - [ ] Yes - [ ] No - [x] No --------- Co-authored-by: IEvangelist <7679720+IEvangelist@users.noreply.github.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> (cherry picked from commit 1b946a70114eaf152778109bd0b86df19ae47006) --- docs/contributing.md | 26 +++++ .../GoCodegenValidationTests.cs | 71 +++++++++++++- .../GoStarterTemplateTests.cs | 94 +++++++++++++++++-- .../Helpers/CliE2ETestHelpers.cs | 31 ++++++ .../Helpers/CliInstallStrategyTests.cs | 24 ++++- 5 files changed, 236 insertions(+), 10 deletions(-) diff --git a/docs/contributing.md b/docs/contributing.md index b780ce9dadc..83be8642400 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -114,6 +114,32 @@ To test changes from a specific pull request locally, see [dogfooding-pull-reque To validate how the CLI resolves `Aspire.*` packages for **staging** and **stable** release-branch builds (including making an installed PR build behave like a staging build), see [cli-staging-validation.md](/docs/cli-staging-validation.md). +The Go CLI E2E scenarios in `GoCodegenValidationTests` cover interactive initialization, +non-interactive initialization in an empty directory and alongside an existing Go application, +SDK refresh, and Go build/vet checks. `GoStarterTemplateTests.RestoreAndBuildGoStarterFromStablePackages` +requires a stable-shaped local hive; normal prerelease PR archives skip that test. + +**Prerequisite:** use an already-built stable-shaped local-hive archive containing the CLI +and matching packages at an unpublished stable version. Building one with +`localhive --version 17.0.99 --archive` currently fails during packing with `NU5104` +because stable component packages depend on prerelease .NET 11 packages. This also +affects Linux; changing the RID does not avoid the package-version restriction. +The test command below does not prepare that archive or validate a stable native bundle. + +Given such an archive, exercise both classes on Linux with Docker: + +```bash +ASPIRE_E2E_ARCHIVE="/path/to/stable-shaped-localhive.tar.gz" \ + dotnet test --project tests/Aspire.Cli.EndToEnd.Tests/Aspire.Cli.EndToEnd.Tests.csproj --no-launch-profile -- \ + --filter-class "*.GoCodegenValidationTests" --filter-class "*.GoStarterTemplateTests" \ + --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true" +``` + +Use an unpublished stable version for the local hive. The stable starter test runs the CLI +with a stable identity, an isolated NuGet cache and the hive as its package source. It checks +the exact Go and Redis package pins, regenerates the SDK and compiles/vets it. The separate +`CreateAndRunGoStarterProject` scenario verifies startup using the archive's normal channel. + ### NuGet configuration in CLI unit tests Use `NuGetTestHelper.CreateClient()` or `CreateService()` for tests that compose real NuGet configuration, and `NuGetTestHelper.LoadSettings()` for native settings assertions. These helpers preserve directory-level configuration inheritance while replacing the runner's user-wide settings with a controlled baseline and excluding machine-wide settings by default. Supply `TestMachineWideSettings` explicitly when testing machine-wide policy. diff --git a/tests/Aspire.Cli.EndToEnd.Tests/GoCodegenValidationTests.cs b/tests/Aspire.Cli.EndToEnd.Tests/GoCodegenValidationTests.cs index e15f24a0b1f..cc48a68c1e9 100644 --- a/tests/Aspire.Cli.EndToEnd.Tests/GoCodegenValidationTests.cs +++ b/tests/Aspire.Cli.EndToEnd.Tests/GoCodegenValidationTests.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.Text.Json.Nodes; using Aspire.Cli.EndToEnd.Tests.Helpers; using Hex1b.Automation; using Xunit; @@ -13,9 +14,12 @@ namespace Aspire.Cli.EndToEnd.Tests; /// public sealed class GoCodegenValidationTests(ITestOutputHelper output) { - [Fact] + [Theory] + [InlineData(false, false)] + [InlineData(true, false)] + [InlineData(true, true)] [CaptureWorkspaceOnFailure] - public async Task RestoreGeneratesSdkFiles() + public async Task RestoreGeneratesSdkFiles(bool nonInteractive, bool existingProject) { var repoRoot = CliE2ETestHelpers.GetRepoRoot(); var strategy = CliInstallStrategy.Detect(output.WriteLine); @@ -29,7 +33,60 @@ public async Task RestoreGeneratesSdkFiles() await auto.PrepareDockerEnvironmentAsync(counter, workspace); await auto.InstallAspireCliAsync(strategy, counter); - await auto.AspireInitGoAppHostAsync(counter); + var existingFiles = new Dictionary(); + if (existingProject) + { + var apiDirectory = workspace.CreateDirectory("api"); + existingFiles[Path.Combine(apiDirectory.FullName, "go.mod")] = """ + module example.com/existing-api + + go 1.26 + """; + existingFiles[Path.Combine(apiDirectory.FullName, "main.go")] = """ + package main + + import "fmt" + + func main() { + fmt.Println("existing Go application") + } + """; + foreach (var (path, content) in existingFiles) + { + await File.WriteAllTextAsync(path, content); + } + } + + if (nonInteractive) + { + await auto.RunCommandAsync( + "aspire init --language go --non-interactive --suppress-agent-init < /dev/null", + counter, + TimeSpan.FromMinutes(3)); + } + else + { + await auto.AspireInitGoAppHostAsync(counter); + } + + var configPath = Path.Combine(workspace.WorkspaceRoot.FullName, "aspire.config.json"); + var config = JsonNode.Parse(await File.ReadAllTextAsync(configPath)); + Assert.NotNull(config); + Assert.Equal("go", config["appHost"]?["language"]?.GetValue()); + Assert.Equal("apphost.go", config["appHost"]?["path"]?.GetValue()); + Assert.False(string.IsNullOrEmpty(config["packages"]?["Aspire.Hosting.Go"]?.GetValue())); + + foreach (var (path, content) in existingFiles) + { + Assert.Equal(content, await File.ReadAllTextAsync(path)); + } + + if (existingProject) + { + GoAppHostTestHelpers.InsertBeforeBuild( + Path.Combine(workspace.WorkspaceRoot.FullName, "apphost.go"), + """builder.AddGoApp("existing-api", "./api")"""); + } await auto.AspireAddPackageAsync("Aspire.Hosting.Redis", counter); await auto.RunCommandAsync("aspire restore --non-interactive", counter, TimeSpan.FromMinutes(3)); @@ -79,5 +136,13 @@ public async Task RestoreGeneratesSdkFiles() "aspire.go does not contain AddSqlServer from Aspire.Hosting.SqlServer; restore did not refresh an already-populated .aspire/modules"); Assert.NotEqual(initialCodegenHash, File.ReadAllText(codegenHashPath)); + + // Build the consumer and vet the generated module explicitly: ./... skips the hidden .aspire directory. + await auto.RunCommandAsync("go build -buildvcs=false ./... && go vet ./...", counter, TimeSpan.FromMinutes(3)); + await auto.RunCommandAsync("(cd .aspire/modules && go vet ./...)", counter, TimeSpan.FromMinutes(3)); + if (existingProject) + { + await auto.RunCommandAsync("(cd api && go build -buildvcs=false ./... && go vet ./...)", counter, TimeSpan.FromMinutes(3)); + } } } diff --git a/tests/Aspire.Cli.EndToEnd.Tests/GoStarterTemplateTests.cs b/tests/Aspire.Cli.EndToEnd.Tests/GoStarterTemplateTests.cs index 37c68bf16e0..f0a3eed1ffa 100644 --- a/tests/Aspire.Cli.EndToEnd.Tests/GoStarterTemplateTests.cs +++ b/tests/Aspire.Cli.EndToEnd.Tests/GoStarterTemplateTests.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.Text.Json.Nodes; using Aspire.Cli.EndToEnd.Tests.Helpers; using Hex1b.Automation; using Xunit; @@ -16,10 +17,27 @@ public sealed class GoStarterTemplateTests(ITestOutputHelper output) { [CaptureWorkspaceOnFailure] [Fact] - public async Task CreateAndRunGoStarterProject() + public Task CreateAndRunGoStarterProject() => CreateRestoreAndBuildGoStarterAsync(emulateStableRelease: false); + + [CaptureWorkspaceOnFailure] + [Fact] + public Task RestoreAndBuildGoStarterFromStablePackages() => CreateRestoreAndBuildGoStarterAsync(emulateStableRelease: true); + + private async Task CreateRestoreAndBuildGoStarterAsync(bool emulateStableRelease) { var repoRoot = CliE2ETestHelpers.GetRepoRoot(); var strategy = CliInstallStrategy.Detect(output.WriteLine); + string? stableVersion = null; + if (emulateStableRelease) + { + Assert.SkipUnless(strategy.Mode == CliInstallMode.LocalHive, + "Stable Go starter coverage requires a localhive --version X.Y.Z --archive build."); + Assert.NotNull(strategy.ArchivePath); + stableVersion = CliE2ETestHelpers.TryGetStableAspireVersionFromArchive(strategy.ArchivePath); + Assert.SkipWhen(stableVersion is null, + "Build a stable-shaped local hive and set ASPIRE_E2E_ARCHIVE to its archive."); + } + using var workspace = TemporaryWorkspace.Create(output); using var terminal = CliE2ETestHelpers.CreateDockerTestTerminal( @@ -34,7 +52,29 @@ public async Task CreateAndRunGoStarterProject() await using var terminalRun = CliE2ETestHelpers.StartRun(terminal, workspace, auto, counter, output, TestContext.Current.CancellationToken); await auto.PrepareDockerEnvironmentAsync(counter, workspace); - await auto.InstallAspireCliAsync(strategy, counter); + if (emulateStableRelease) + { + // Do not configure the local channel: package resolution must honor the stable identity + // and its invocation-scoped hive, rather than a persisted channel or warm NuGet cache. + await auto.ExtractLocalHiveArchiveAsync("/tmp/aspire-localhive.tar.gz", counter); + await auto.SourceAspireCliEnvironmentAsync(counter); + await auto.RunCommandAsync( + $"export ASPIRE_CLI_CHANNEL=stable ASPIRE_CLI_VERSION={stableVersion} " + + "ASPIRE_CLI_PACKAGES=\"$HOME/.aspire/hives/local/packages\" " + + "NUGET_PACKAGES=\"$PWD/.stable-nuget-packages\"", + counter); + await auto.TypeAsync("aspire --version"); + await auto.EnterAsync(); + await auto.WaitUntilAsync( + snapshot => snapshot.ContainsText(stableVersion!) && snapshot.ContainsText("emulating identity"), + timeout: TimeSpan.FromSeconds(30), + description: "stable CLI identity before creating the Go starter"); + await auto.WaitForSuccessPromptAsync(counter); + } + else + { + await auto.InstallAspireCliAsync(strategy, counter); + } await auto.AspireNewAsync("GoStarterApp", counter, template: AspireTemplate.GoStarter); @@ -43,16 +83,58 @@ public async Task CreateAndRunGoStarterProject() var aspireModulePath = Path.Combine(projectRoot, ".aspire", "modules", "aspire.go"); Assert.True(File.Exists(aspireModulePath), $"Expected generated Go SDK file not found: {aspireModulePath}"); + if (emulateStableRelease) + { + var config = JsonNode.Parse(await File.ReadAllTextAsync(Path.Combine(projectRoot, "aspire.config.json"))); + Assert.NotNull(config); + Assert.Equal(stableVersion, config["packages"]?["Aspire.Hosting.Go"]?.GetValue()); + Assert.Equal(stableVersion, config["packages"]?["Aspire.Hosting.Redis"]?.GetValue()); + Assert.Null(config["channel"]); + } + await auto.TypeAsync("cd GoStarterApp"); await auto.EnterAsync(); await auto.WaitForSuccessPromptAsync(counter); + if (emulateStableRelease) + { + await auto.RunCommandAsync("rm -rf ../.stable-nuget-packages", counter); + } + + // A second restore must regenerate the SDK, not succeed by reusing the template's output. + await auto.RunCommandAsync("rm -rf .aspire/modules && aspire restore --non-interactive < /dev/null", counter, TimeSpan.FromMinutes(3)); + Assert.True(File.Exists(aspireModulePath), $"Restore did not regenerate {aspireModulePath}"); + + if (emulateStableRelease) + { + foreach (var packageId in new[] { "aspire.hosting.go", "aspire.hosting.redis" }) + { + var metadata = $"../.stable-nuget-packages/{packageId}/{stableVersion}/.nupkg.metadata"; + await auto.RunCommandAsync( + $"node -e 'const fs = require(\"node:fs\"), assert = require(\"node:assert/strict\"); " + + $"const metadata = JSON.parse(fs.readFileSync(\"{metadata}\", \"utf8\")); " + + "const expected = fs.realpathSync(process.env.HOME + \"/.aspire/hives/local/packages\"); " + + "assert.equal(fs.realpathSync(metadata.source), expected);'", + counter); + } + } + // The workspace is not a git repository, so VCS stamping would fail the build. await auto.RunCommandAsync("go build -buildvcs=false ./...", counter, TimeSpan.FromMinutes(3)); + await auto.RunCommandAsync("go vet ./... && (cd .aspire/modules && go vet ./...)", counter, TimeSpan.FromMinutes(3)); - // The Go API runs `go mod tidy` before it starts, which downloads the OpenTelemetry modules. - await auto.AspireStartAsync(counter, startTimeout: TimeSpan.FromMinutes(4)); - await auto.AssertResourcesExistAsync(counter, "cache", "api"); - await auto.AspireStopAsync(counter); + if (!emulateStableRelease) + { + // The Go API runs `go mod tidy` before it starts, which downloads the OpenTelemetry modules. + await auto.AspireStartAsync(counter, startTimeout: TimeSpan.FromMinutes(4)); + try + { + await auto.AssertResourcesExistAsync(counter, "cache", "api"); + } + finally + { + await auto.AspireStopAsync(counter); + } + } } } diff --git a/tests/Aspire.Cli.EndToEnd.Tests/Helpers/CliE2ETestHelpers.cs b/tests/Aspire.Cli.EndToEnd.Tests/Helpers/CliE2ETestHelpers.cs index f902a0acf94..d3ed6545e97 100644 --- a/tests/Aspire.Cli.EndToEnd.Tests/Helpers/CliE2ETestHelpers.cs +++ b/tests/Aspire.Cli.EndToEnd.Tests/Helpers/CliE2ETestHelpers.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using System.Formats.Tar; +using System.IO.Compression; using System.Runtime.CompilerServices; using System.Text.Json; using System.Text.Json.Nodes; @@ -1157,6 +1159,35 @@ private static LocalChannelInfo PrepareLocalChannelCore( return bestRaw; } + internal static string? TryGetStableAspireVersionFromArchive(string archivePath) + { + using var archive = File.OpenRead(archivePath); + using var gzip = new GZipStream(archive, CompressionMode.Decompress); + using var tar = new TarReader(gzip); + + // Local-hive entries look like "hives/local/packages/Aspire.Hosting.17.0.99.nupkg". + // Some tar writers prefix entries with "./"; prerelease packages must not enable this lane. + var stablePackageRegex = new Regex(@"^(?:\./)?hives/local/packages/Aspire\.Hosting\.(?\d+\.\d+\.\d+)\.nupkg$", RegexOptions.IgnoreCase); + Version? bestVersion = null; + while (tar.GetNextEntry() is { } entry) + { + if (entry.EntryType is not (TarEntryType.RegularFile or TarEntryType.V7RegularFile)) + { + continue; + } + + var match = stablePackageRegex.Match(entry.Name); + if (match.Success && + Version.TryParse(match.Groups["version"].Value, out var version) && + (bestVersion is null || version > bestVersion)) + { + bestVersion = version; + } + } + + return bestVersion?.ToString(); + } + internal static void WriteLocalChannelSettings(string projectRoot, string sdkVersion) { var configPath = Path.Combine(projectRoot, "aspire.config.json"); diff --git a/tests/Aspire.Cli.EndToEnd.Tests/Helpers/CliInstallStrategyTests.cs b/tests/Aspire.Cli.EndToEnd.Tests/Helpers/CliInstallStrategyTests.cs index cd5a15ee623..6caffb13d24 100644 --- a/tests/Aspire.Cli.EndToEnd.Tests/Helpers/CliInstallStrategyTests.cs +++ b/tests/Aspire.Cli.EndToEnd.Tests/Helpers/CliInstallStrategyTests.cs @@ -1,14 +1,36 @@ // 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 System.IO.Compression; using Hex1b; using Xunit; namespace Aspire.Cli.EndToEnd.Tests.Helpers; [Collection(CliInstallEnvironmentCollection.Name)] -public class CliInstallStrategyTests +public class CliInstallStrategyTests(ITestOutputHelper output) { + [Theory] + [InlineData("hives/local/packages/Aspire.Hosting.17.0.99.nupkg", "17.0.99")] + [InlineData("./hives/local/packages/Aspire.Hosting.17.0.99.nupkg", "17.0.99")] + [InlineData("hives/local/packages/Aspire.Hosting.17.0.0-dev.nupkg", null)] + [InlineData("hives/local/packages/Aspire.Hosting.Go.17.0.99.nupkg", null)] + [InlineData("other/Aspire.Hosting.17.0.99.nupkg", null)] + public void TryGetStableAspireVersionFromArchive_UsesSelectedHive(string packageEntry, string? expectedVersion) + { + using var workspace = TemporaryWorkspace.Create(output); + var archivePath = Path.Combine(workspace.WorkspaceRoot.FullName, "selected.tar.gz"); + using (var file = File.Create(archivePath)) + using (var gzip = new GZipStream(file, CompressionLevel.Fastest)) + using (var tar = new TarWriter(gzip)) + { + tar.WriteEntry(new PaxTarEntry(TarEntryType.RegularFile, packageEntry)); + } + + Assert.Equal(expectedVersion, CliE2ETestHelpers.TryGetStableAspireVersionFromArchive(archivePath)); + } + [Fact] public void GetPullRequestInstallArgs_ReturnsPrNumber() {