| 1 | |
| 2 | |
| 3 | package guestos |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/sylvinus/agent-vm/internal/diagnostics" |
| 16 | ) |
| 17 | |
| 18 | func TestWindowsHostRepairPreservesProcessOutput(t *testing.T) { |
| 19 | profile := t.TempDir() |
| 20 | if err := BindWindowsProfile(profile, windowsBindingFixture(profile)); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | kit := nativeManifestKit(t, Windows11) |
| 24 | script := `param([string]$Profile,[string]$ManifestPath,[string]$ManifestSHA256,[switch]$HostOnly) |
| 25 | $ErrorActionPreference='Stop' |
| 26 | [Console]::OutputEncoding=[Text.UTF8Encoding]::new($false) |
| 27 | if (!$HostOnly -or (Get-FileHash -LiteralPath $ManifestPath -Algorithm SHA256).Hash -ne $ManifestSHA256) { exit 38 } |
| 28 | Write-Output 'repair-fixture-stdout' |
| 29 | [Console]::Error.WriteLine('repair-fixture-service-registration-failed token=repair-fixture-secret') |
| 30 | exit 37 |
| 31 | ` |
| 32 | if err := os.WriteFile(filepath.Join(kit, "maintain-windows-workbench.ps1"), []byte(script), 0600); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | var stdout, stderr strings.Builder |
| 36 | out := diagnostics.NewStream(nil, "storage-recovery", "info", func(line string) { stdout.WriteString(line) }) |
| 37 | errOut := diagnostics.NewStream(nil, "storage-recovery", "warn", func(line string) { stderr.WriteString(line) }) |
| 38 | ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| 39 | defer cancel() |
| 40 | err := RepairWindowsHostManager(ctx, profile, kit, out, errOut) |
| 41 | _ = out.Close() |
| 42 | _ = errOut.Close() |
| 43 | var exit *exec.ExitError |
| 44 | if !errors.As(err, &exit) || exit.ExitCode() != 37 { |
| 45 | t.Fatalf("repair lost the original process result: %v", err) |
| 46 | } |
| 47 | if !strings.Contains(stdout.String(), "repair-fixture-stdout") || !strings.Contains(stderr.String(), "repair-fixture-service-registration-failed") { |
| 48 | t.Fatalf("repair output did not reach the shared diagnostic streams: stdout=%q stderr=%q", stdout.String(), stderr.String()) |
| 49 | } |
| 50 | if strings.Contains(stderr.String(), "repair-fixture-secret") || !strings.Contains(stderr.String(), "[redacted]") { |
| 51 | t.Fatal("repair output bypassed diagnostic redaction") |
| 52 | } |
| 53 | remaining, err := filepath.Glob(filepath.Join(profile, "native-host-repair-*.json")) |
| 54 | if err != nil || len(remaining) != 0 { |
| 55 | t.Fatalf("repair manifest was retained after failure: %v %v", remaining, err) |
| 56 | } |
| 57 | } |