From b213f657960a8cb29512e7389eac3920445ab5ff Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 19 Oct 2026 00:34:01 -0600 Subject: [PATCH 1/4] refactor(ci): move podman MSI install into devsy-ci-podman install Replace the inline PowerShell Podman install in pr-ci.yml with a Go install subcommand that verifies the MSI SHA256, runs msiexec, tails the install log and searches for podman.exe on failure, and appends the install dir to GITHUB_PATH. --- .github/workflows/pr-ci.yml | 33 +-- hack/podman_ci/errors.go | 2 +- hack/podman_ci/errors_test.go | 4 +- hack/podman_ci/install.go | 208 +++++++++++++++++++ hack/podman_ci/install_test.go | 287 ++++++++++++++++++++++++++ hack/podman_ci/install_unsupported.go | 13 ++ hack/podman_ci/install_windows.go | 9 + hack/podman_ci/setup.go | 14 +- 8 files changed, 534 insertions(+), 36 deletions(-) create mode 100644 hack/podman_ci/install.go create mode 100644 hack/podman_ci/install_test.go create mode 100644 hack/podman_ci/install_unsupported.go create mode 100644 hack/podman_ci/install_windows.go diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index a3dfdac753..006cd2dd02 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -1056,33 +1056,12 @@ jobs: shell: pwsh timeout-minutes: 5 run: | - $msiPath = "$env:RUNNER_TEMP\podman.msi" - if ("${{ steps.podman-cache-windows.outputs.cache-hit }}" -ne "true") { - $msiUrl = "https://github.com/containers/podman/releases/download/v5.8.2/podman-installer-windows-amd64.msi" - Invoke-WebRequest -Uri $msiUrl -OutFile $msiPath - } - - $expectedHash = "eda54f26f9695d198d9a679fa45ae24ba35b78444f432b5fe0c122c5a3624c57" - $actualHash = (Get-FileHash -Path $msiPath -Algorithm SHA256).Hash.ToLower() - if ($actualHash -ne $expectedHash) { - throw "SHA256 mismatch for podman MSI! Expected: $expectedHash, Got: $actualHash" - } - Write-Host "Podman MSI checksum verified: $actualHash" - - $installDir = "C:\Program Files\RedHat\Podman" - $logFile = "$env:RUNNER_TEMP\podman-install.log" - $proc = Start-Process msiexec.exe -Wait -PassThru -ArgumentList "/i `"$msiPath`" /qn /norestart /l*v `"$logFile`" INSTALLDIR=`"$installDir`"" - Write-Host "MSI exit code: $($proc.ExitCode)" - - if (!(Test-Path "$installDir\podman.exe")) { - Write-Host "--- MSI install log (last 50 lines) ---" - Get-Content $logFile -Tail 50 - Write-Host "--- Searching for podman.exe ---" - Get-ChildItem "C:\" -Filter "podman.exe" -Recurse -Depth 4 -ErrorAction SilentlyContinue | ForEach-Object { Write-Host $_.FullName } - throw "podman.exe not found at $installDir" - } - - echo "$installDir" | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8 + & "$env:RUNNER_TEMP\devsy-ci-podman\devsy-ci-podman.exe" install ` + --url "https://github.com/containers/podman/releases/download/v5.8.2/podman-installer-windows-amd64.msi" ` + --sha256 "eda54f26f9695d198d9a679fa45ae24ba35b78444f432b5fe0c122c5a3624c57" ` + --msi-path "$env:RUNNER_TEMP\podman.msi" ` + --install-dir "C:\Program Files\RedHat\Podman" ` + --log-path "$env:RUNNER_TEMP\podman-install.log" - name: start podman (Windows) if: (matrix.install-kind == true || matrix.install-podman == true) && runner.os == 'Windows' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true') diff --git a/hack/podman_ci/errors.go b/hack/podman_ci/errors.go index 316f2d4b25..35865f6de5 100644 --- a/hack/podman_ci/errors.go +++ b/hack/podman_ci/errors.go @@ -17,7 +17,7 @@ func usageError(format string, args ...any) error { return &ExitError{Code: 2, Err: fmt.Errorf(format, args...)} } -func classifySetupError(err error) error { +func classifyOperationalError(err error) error { if err == nil { return nil } diff --git a/hack/podman_ci/errors_test.go b/hack/podman_ci/errors_test.go index 2f261cdee8..d4565ccec0 100644 --- a/hack/podman_ci/errors_test.go +++ b/hack/podman_ci/errors_test.go @@ -12,10 +12,10 @@ func TestExitCodeForError(t *testing.T) { want int }{ {name: "cobra usage error", err: errors.New("required flag not set"), want: 2}, - {name: "typed usage error", err: classifySetupError(usageError("invalid mode")), want: 2}, + {name: "typed usage error", err: classifyOperationalError(usageError("invalid mode")), want: 2}, { name: "operational error", - err: classifySetupError(errors.New("setup failed")), + err: classifyOperationalError(errors.New("setup failed")), want: 1, }, } { diff --git a/hack/podman_ci/install.go b/hack/podman_ci/install.go new file mode 100644 index 0000000000..db63bd1cab --- /dev/null +++ b/hack/podman_ci/install.go @@ -0,0 +1,208 @@ +package main + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "io" + "io/fs" + "net/http" + "os" + "path/filepath" + "strings" + "time" + + "github.com/spf13/cobra" +) + +const ( + msiexecPath = "msiexec.exe" + podmanExe = "podman.exe" + msiInstallTimeout = 4 * time.Minute + msiLogTailLines = 50 + podmanSearchDepth = 4 +) + +type Downloader func(ctx context.Context, url, dest string) error + +type InstallConfig struct { + URL string + SHA256 string + MSIPath string + InstallDir string + LogPath string + GitHubPath string +} + +func newInstallCommand() *cobra.Command { + var cfg InstallConfig + cmd := &cobra.Command{ + Use: "install", + Short: "Install Podman from a verified MSI", + RunE: func(*cobra.Command, []string) error { + logger := newLogger() + deps := Dependencies{ + Runner: &ExecRunner{Log: logger}, + Log: logger, + Files: osFileSystem{}, + Download: downloadFile, + } + return classifyOperationalError(platformInstall(context.Background(), deps, cfg)) + }, + } + cmd.Flags().StringVar(&cfg.URL, "url", "", "podman MSI download URL") + cmd.Flags().StringVar(&cfg.SHA256, "sha256", "", "expected SHA256 of the podman MSI") + cmd.Flags().StringVar(&cfg.MSIPath, "msi-path", "", "local path for the podman MSI") + cmd.Flags().StringVar(&cfg.InstallDir, "install-dir", "", "msiexec INSTALLDIR") + cmd.Flags().StringVar(&cfg.LogPath, "log-path", "", "msiexec verbose log path") + cmd.Flags().StringVar( + &cfg.GitHubPath, "github-path", os.Getenv("GITHUB_PATH"), + "file receiving the install dir for PATH", + ) + for _, name := range []string{"url", "sha256", "msi-path", "install-dir", "log-path"} { + _ = cmd.MarkFlagRequired(name) + } + return cmd +} + +func installPodmanMSI( + ctx context.Context, + deps Dependencies, + cfg InstallConfig, + searchRoot string, +) error { + // An MSI restored by actions/cache sits at MSIPath; the checksum below still guards it. + if _, statErr := deps.Files.Stat(cfg.MSIPath); statErr != nil { + if err := deps.Download(ctx, cfg.URL, cfg.MSIPath); err != nil { + return fmt.Errorf("download podman MSI: %w", err) + } + } + + msi, err := deps.Files.ReadFile(cfg.MSIPath) + if err != nil { + return fmt.Errorf("read podman MSI: %w", err) + } + if err := verifyMSIChecksum(msi, cfg.SHA256); err != nil { + return err + } + deps.Log.Printf("Podman MSI checksum verified: %s", installSum(msi)) + + res := deps.Runner.Run(ctx, CommandSpec{ + Path: msiexecPath, + Args: msiexecArgs(cfg), + Timeout: msiInstallTimeout, + }) + deps.Log.Printf("MSI exit code: %s", exitCodeString(res.ExitCode)) + + podman := filepath.Join(cfg.InstallDir, podmanExe) + if _, err := deps.Files.Stat(podman); err != nil { + logMissingPodman(deps, cfg, searchRoot) + return fmt.Errorf("podman.exe not found at %s", cfg.InstallDir) + } + + return appendGitHubPath(deps.Files, cfg.GitHubPath, cfg.InstallDir) +} + +func msiexecArgs(cfg InstallConfig) []string { + return []string{ + "/i", cfg.MSIPath, "/qn", "/norestart", "/l*v", cfg.LogPath, + fmt.Sprintf(`INSTALLDIR="%s"`, cfg.InstallDir), + } +} + +func installSum(data []byte) string { + sum := sha256.Sum256(data) + return hex.EncodeToString(sum[:]) +} + +func verifyMSIChecksum(data []byte, expected string) error { + actual := installSum(data) + if !strings.EqualFold(actual, expected) { + return fmt.Errorf("SHA256 mismatch for podman MSI! Expected: %s, Got: %s", expected, actual) + } + return nil +} + +func logMissingPodman(deps Dependencies, cfg InstallConfig, searchRoot string) { + deps.Log.Printf("podman.exe not found at %s", cfg.InstallDir) + deps.Log.Printf("MSI install log (last %d lines):", msiLogTailLines) + if data, err := deps.Files.ReadFile(cfg.LogPath); err != nil { + deps.Log.Printf("install log unavailable: %v", err) + } else { + deps.Log.Printf("%s", tailLines(data, msiLogTailLines)) + } + found := findFilesNamed(searchRoot, podmanExe, podmanSearchDepth) + deps.Log.Printf("podman.exe found under %s:", searchRoot) + for _, path := range found { + deps.Log.Printf("%s", path) + } +} + +func tailLines(data []byte, n int) string { + lines := strings.Split(strings.ReplaceAll(string(data), "\r\n", "\n"), "\n") + if len(lines) > 0 && lines[len(lines)-1] == "" { + lines = lines[:len(lines)-1] + } + if len(lines) > n { + lines = lines[len(lines)-n:] + } + return strings.Join(lines, "\n") +} + +// findFilesNamed mirrors PowerShell Get-ChildItem -Depth: items up to maxDepth+1 levels below root. +func findFilesNamed(root, name string, maxDepth int) []string { + var found []string + rootDepth := strings.Count(filepath.Clean(root), string(filepath.Separator)) + _ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return nil + } + depth := strings.Count(filepath.Clean(path), string(filepath.Separator)) - rootDepth + if d.IsDir() && depth > maxDepth { + return filepath.SkipDir + } + if !d.IsDir() && d.Name() == name { + found = append(found, path) + } + return nil + }) + return found +} + +func appendGitHubPath(files FileSystem, githubPath, dir string) error { + if githubPath == "" { + return nil + } + w, err := files.OpenAppend(githubPath) + if err != nil { + return fmt.Errorf("open GITHUB_PATH: %w", err) + } + _, writeErr := io.WriteString(w, dir+"\n") + if err := errors.Join(writeErr, w.Close()); err != nil { + return fmt.Errorf("write GITHUB_PATH: %w", err) + } + return nil +} + +func downloadFile(ctx context.Context, url, dest string) error { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return err + } + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + defer func() { _ = resp.Body.Close() }() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("GET %s: %s", url, resp.Status) + } + f, err := os.Create(dest) + if err != nil { + return err + } + _, copyErr := io.Copy(f, resp.Body) + return errors.Join(copyErr, f.Close()) +} diff --git a/hack/podman_ci/install_test.go b/hack/podman_ci/install_test.go new file mode 100644 index 0000000000..3a8c73be8f --- /dev/null +++ b/hack/podman_ci/install_test.go @@ -0,0 +1,287 @@ +package main + +import ( + "bytes" + "context" + "fmt" + "io" + "io/fs" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "slices" + "strings" + "testing" +) + +type installRunner struct { + calls []CommandSpec + exit int +} + +func (r *installRunner) Run(_ context.Context, spec CommandSpec) CommandResult { + r.calls = append(r.calls, spec) + return CommandResult{ExitCode: &r.exit} +} + +type installInfo struct{ fs.FileInfo } + +type installWriter struct{ bytes.Buffer } + +func (*installWriter) Close() error { return nil } + +type installFS struct { + files map[string][]byte + appends map[string]*installWriter +} + +func (f *installFS) ReadFile(path string) ([]byte, error) { + data, ok := f.files[path] + if !ok { + return nil, fs.ErrNotExist + } + return data, nil +} + +func (f *installFS) Stat(path string) (fs.FileInfo, error) { + if _, ok := f.files[path]; !ok { + return nil, fs.ErrNotExist + } + return installInfo{}, nil +} + +func (f *installFS) OpenAppend(path string) (io.WriteCloser, error) { + w := &installWriter{} + f.appends[path] = w + return w, nil +} + +type installFixture struct { + deps Dependencies + cfg InstallConfig + runner *installRunner + files *installFS + out *bytes.Buffer + downloads int + dir string + podman string +} + +func newInstallFixture(t *testing.T, msi []byte) *installFixture { + t.Helper() + dir := t.TempDir() + fx := &installFixture{ + runner: &installRunner{}, + files: &installFS{files: map[string][]byte{}, appends: map[string]*installWriter{}}, + out: &bytes.Buffer{}, + dir: dir, + } + fx.cfg = InstallConfig{ + URL: "https://example.test/podman.msi", + SHA256: installSum(msi), + MSIPath: filepath.Join(dir, "podman.msi"), + InstallDir: `C:\Program Files\RedHat\Podman`, + LogPath: filepath.Join(dir, "podman-install.log"), + GitHubPath: filepath.Join(dir, "github_path"), + } + fx.podman = filepath.Join(fx.cfg.InstallDir, "podman.exe") + fx.deps = Dependencies{ + Runner: fx.runner, + Log: &Logger{Out: fx.out, Err: fx.out}, + Files: fx.files, + Download: func(_ context.Context, _, dest string) error { + fx.downloads++ + fx.files.files[dest] = msi + return nil + }, + } + return fx +} + +func (fx *installFixture) install() error { + return installPodmanMSI(context.Background(), fx.deps, fx.cfg, fx.dir) +} + +func numberedLines(n int) []byte { + var lines []string + for i := 1; i <= n; i++ { + lines = append(lines, fmt.Sprintf("line %d", i)) + } + return []byte(strings.Join(lines, "\r\n") + "\r\n") +} + +func writeFiles(t *testing.T, paths ...string) { + t.Helper() + for _, path := range paths { + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, nil, 0o600); err != nil { + t.Fatal(err) + } + } +} + +func TestInstallPodmanMSISucceeds(t *testing.T) { + msi := []byte("msi-bytes") + fx := newInstallFixture(t, msi) + fx.cfg.SHA256 = strings.ToUpper(installSum(msi)) + fx.files.files[fx.podman] = []byte{} + + if err := fx.install(); err != nil { + t.Fatalf("installPodmanMSI() error = %v", err) + } + + if fx.downloads != 1 { + t.Fatalf("downloads = %d, want 1", fx.downloads) + } + if len(fx.runner.calls) != 1 { + t.Fatalf("msiexec calls = %d, want 1", len(fx.runner.calls)) + } + call := fx.runner.calls[0] + if call.Path != "msiexec.exe" { + t.Fatalf("msiexec path = %q, want msiexec.exe", call.Path) + } + wantArgs := []string{ + "/i", fx.cfg.MSIPath, "/qn", "/norestart", "/l*v", fx.cfg.LogPath, + `INSTALLDIR="C:\Program Files\RedHat\Podman"`, + } + if !slices.Equal(call.Args, wantArgs) { + t.Fatalf("msiexec args = %q, want %q", call.Args, wantArgs) + } + if got := fx.files.appends[fx.cfg.GitHubPath].String(); got != `C:\Program Files\RedHat\Podman`+"\n" { + t.Fatalf("GITHUB_PATH content = %q", got) + } + if !strings.Contains(fx.out.String(), "MSI exit code: 0") { + t.Fatalf("output %q missing msiexec exit code", fx.out.String()) + } +} + +func TestInstallPodmanMSIRejectsChecksumMismatch(t *testing.T) { + msi := []byte("msi-bytes") + fx := newInstallFixture(t, msi) + zeros := strings.Repeat("0", 64) + fx.cfg.SHA256 = zeros + + err := fx.install() + + want := fmt.Sprintf( + "SHA256 mismatch for podman MSI! Expected: %s, Got: %s", + zeros, + installSum(msi), + ) + if err == nil || err.Error() != want { + t.Fatalf("error = %v, want %q", err, want) + } + if len(fx.runner.calls) != 0 { + t.Fatalf("msiexec ran despite checksum mismatch") + } + if len(fx.files.appends) != 0 { + t.Fatalf("GITHUB_PATH written despite checksum mismatch") + } +} + +func TestInstallPodmanMSIMissingPodmanLogsLogTail(t *testing.T) { + fx := newInstallFixture(t, []byte("msi-bytes")) + fx.files.files[fx.cfg.LogPath] = numberedLines(60) + + err := fx.install() + + if err == nil || err.Error() != `podman.exe not found at C:\Program Files\RedHat\Podman` { + t.Fatalf("error = %v", err) + } + out := fx.out.String() + if !strings.Contains(out, "line 60\n") || !strings.Contains(out, "line 11\n") { + t.Fatalf("output missing last 50 log lines:\n%s", out) + } + if strings.Contains(out, "line 10\n") { + t.Fatalf("output includes more than the last 50 log lines:\n%s", out) + } + if len(fx.files.appends) != 0 { + t.Fatalf("GITHUB_PATH written when podman.exe is missing") + } +} + +func TestInstallPodmanMSIMissingPodmanSearchesByDepth(t *testing.T) { + fx := newInstallFixture(t, []byte("msi-bytes")) + shallow := filepath.Join(fx.dir, "shallow", "podman.exe") + deep := filepath.Join(fx.dir, "a", "b", "c", "d", "podman.exe") + tooDeep := filepath.Join(fx.dir, "a", "b", "c", "d", "e", "podman.exe") + writeFiles(t, shallow, deep, tooDeep) + + if err := fx.install(); err == nil { + t.Fatal("installPodmanMSI() succeeded without podman.exe") + } + + out := fx.out.String() + if !strings.Contains(out, shallow) || !strings.Contains(out, deep) { + t.Fatalf("output missing podman.exe search hits:\n%s", out) + } + if strings.Contains(out, tooDeep) { + t.Fatalf("output includes podman.exe beyond search depth:\n%s", out) + } +} + +func TestInstallPodmanMSIReusesCachedMSI(t *testing.T) { + msi := []byte("msi-bytes") + tests := []struct { + name string + data []byte + wantErr string + }{ + {name: "verified cached MSI", data: msi}, + {name: "tampered cached MSI", data: []byte("tampered"), wantErr: "SHA256 mismatch"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + fx := newInstallFixture(t, msi) + fx.files.files[fx.cfg.MSIPath] = test.data + fx.files.files[fx.podman] = []byte{} + + err := fx.install() + + if fx.downloads != 0 { + t.Fatalf("downloads = %d, want 0 when MSI is cached", fx.downloads) + } + switch { + case test.wantErr == "" && err != nil: + t.Fatalf("error = %v, want nil", err) + case test.wantErr != "" && (err == nil || !strings.Contains(err.Error(), test.wantErr)): + t.Fatalf("error = %v, want containing %q", err, test.wantErr) + } + }) + } +} + +func TestDownloadFile(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/podman.msi" { + _, _ = w.Write([]byte("msi-bytes")) + return + } + http.NotFound(w, r) + })) + t.Cleanup(server.Close) + dir := t.TempDir() + + dest := filepath.Join(dir, "podman.msi") + if err := downloadFile(context.Background(), server.URL+"/podman.msi", dest); err != nil { + t.Fatalf("downloadFile() error = %v", err) + } + got, err := os.ReadFile(dest) + if err != nil { + t.Fatal(err) + } + if string(got) != "msi-bytes" { + t.Fatalf("downloaded body = %q, want msi-bytes", got) + } + + if err := downloadFile( + context.Background(), + server.URL+"/missing", + filepath.Join(dir, "missing.msi"), + ); err == nil { + t.Fatal("downloadFile() succeeded for HTTP 404") + } +} diff --git a/hack/podman_ci/install_unsupported.go b/hack/podman_ci/install_unsupported.go new file mode 100644 index 0000000000..f153e03277 --- /dev/null +++ b/hack/podman_ci/install_unsupported.go @@ -0,0 +1,13 @@ +//go:build !windows + +package main + +import ( + "context" + "fmt" + "runtime" +) + +func platformInstall(_ context.Context, _ Dependencies, _ InstallConfig) error { + return fmt.Errorf("podman MSI install is only supported on Windows, not %s", runtime.GOOS) +} diff --git a/hack/podman_ci/install_windows.go b/hack/podman_ci/install_windows.go new file mode 100644 index 0000000000..2bcae846c1 --- /dev/null +++ b/hack/podman_ci/install_windows.go @@ -0,0 +1,9 @@ +package main + +import "context" + +const podmanSearchRoot = `C:\` + +func platformInstall(ctx context.Context, deps Dependencies, cfg InstallConfig) error { + return installPodmanMSI(ctx, deps, cfg, podmanSearchRoot) +} diff --git a/hack/podman_ci/setup.go b/hack/podman_ci/setup.go index ceaf75a5ef..a36cd9844f 100644 --- a/hack/podman_ci/setup.go +++ b/hack/podman_ci/setup.go @@ -25,11 +25,12 @@ func (osFileSystem) OpenAppend(path string) (io.WriteCloser, error) { } type Dependencies struct { - Runner Runner - Clock Clock - Log *Logger - Files FileSystem - Budget *Budget + Runner Runner + Clock Clock + Log *Logger + Files FileSystem + Budget *Budget + Download Downloader } func newRootCommand() *cobra.Command { @@ -40,6 +41,7 @@ func newRootCommand() *cobra.Command { SilenceErrors: true, } root.AddCommand(newSetupCommand()) + root.AddCommand(newInstallCommand()) return root } @@ -60,7 +62,7 @@ func newSetupCommand() *cobra.Command { Log: logger, Files: osFileSystem{}, } - return classifySetupError(setup(context.Background(), deps, cfg)) + return classifyOperationalError(setup(context.Background(), deps, cfg)) }, } cmd.Flags().StringVar(&cfg.PodmanPath, "podman-path", cfg.PodmanPath, "Podman executable path") From ddef2fb4844a74d6c4d2032384fe0319c61bc849 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 19 Oct 2026 00:39:02 -0600 Subject: [PATCH 2/4] fix(ci): remove partial podman MSI on failed download io.Copy or Close failures left a truncated MSI at MSIPath, which the cached-MSI path would then checksum-verify and fail on forever. Remove the destination on write or close errors and cover it with a test. --- hack/podman_ci/install.go | 7 ++++++- hack/podman_ci/install_test.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/hack/podman_ci/install.go b/hack/podman_ci/install.go index db63bd1cab..31432bf2c9 100644 --- a/hack/podman_ci/install.go +++ b/hack/podman_ci/install.go @@ -204,5 +204,10 @@ func downloadFile(ctx context.Context, url, dest string) error { return err } _, copyErr := io.Copy(f, resp.Body) - return errors.Join(copyErr, f.Close()) + closeErr := f.Close() + if err := errors.Join(copyErr, closeErr); err != nil { + _ = os.Remove(dest) + return err + } + return nil } diff --git a/hack/podman_ci/install_test.go b/hack/podman_ci/install_test.go index 3a8c73be8f..57dd4b0683 100644 --- a/hack/podman_ci/install_test.go +++ b/hack/podman_ci/install_test.go @@ -285,3 +285,19 @@ func TestDownloadFile(t *testing.T) { t.Fatal("downloadFile() succeeded for HTTP 404") } } + +func TestDownloadFileRemovesPartialFileOnTruncatedBody(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Length", "100") + _, _ = w.Write([]byte("short")) + })) + t.Cleanup(server.Close) + dest := filepath.Join(t.TempDir(), "podman.msi") + + if err := downloadFile(context.Background(), server.URL, dest); err == nil { + t.Fatal("downloadFile() succeeded for truncated body") + } + if _, err := os.Stat(dest); !os.IsNotExist(err) { + t.Fatalf("partial download left at %s: stat err = %v", dest, err) + } +} From 3ab090e2ee592c88ad87a348ef9cdb4f55beaabb Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 19 Oct 2026 01:26:00 -0600 Subject: [PATCH 3/4] refactor(ci): share install command name constant in podman_ci Replace repeated "install" literals with commandInstall to satisfy goconst. --- hack/podman_ci/install.go | 3 ++- hack/podman_ci/setup_linux.go | 4 ++-- hack/podman_ci/setup_linux_test.go | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/hack/podman_ci/install.go b/hack/podman_ci/install.go index 31432bf2c9..7f48808aad 100644 --- a/hack/podman_ci/install.go +++ b/hack/podman_ci/install.go @@ -21,6 +21,7 @@ const ( msiexecPath = "msiexec.exe" podmanExe = "podman.exe" msiInstallTimeout = 4 * time.Minute + commandInstall = "install" msiLogTailLines = 50 podmanSearchDepth = 4 ) @@ -39,7 +40,7 @@ type InstallConfig struct { func newInstallCommand() *cobra.Command { var cfg InstallConfig cmd := &cobra.Command{ - Use: "install", + Use: commandInstall, Short: "Install Podman from a verified MSI", RunE: func(*cobra.Command, []string) error { logger := newLogger() diff --git a/hack/podman_ci/setup_linux.go b/hack/podman_ci/setup_linux.go index 978ee4eef4..f6d1696471 100644 --- a/hack/podman_ci/setup_linux.go +++ b/hack/podman_ci/setup_linux.go @@ -325,7 +325,7 @@ func installPrivilegedFile(ctx context.Context, deps Dependencies, target, conte deps, CommandSpec{ Path: commandSudo, - Args: []string{"install", "-d", "-m", "0755", filepath.Dir(target)}, + Args: []string{commandInstall, "-d", "-m", "0755", filepath.Dir(target)}, Timeout: 15 * time.Second, }, ); err != nil { @@ -336,7 +336,7 @@ func installPrivilegedFile(ctx context.Context, deps Dependencies, target, conte deps, CommandSpec{ Path: commandSudo, - Args: []string{"install", "-m", "0644", name, target}, + Args: []string{commandInstall, "-m", "0644", name, target}, Timeout: 15 * time.Second, }, ) diff --git a/hack/podman_ci/setup_linux_test.go b/hack/podman_ci/setup_linux_test.go index c77a876f44..359feb36a8 100644 --- a/hack/podman_ci/setup_linux_test.go +++ b/hack/podman_ci/setup_linux_test.go @@ -210,7 +210,7 @@ func TestLinuxRootfulServiceContents(t *testing.T) { } var serviceContents string runner.fn = func(c recordedCommand) CommandResult { - if len(c.args) == 5 && slices.Equal(c.args[:3], []string{"install", "-m", "0644"}) { + if len(c.args) == 5 && slices.Equal(c.args[:3], []string{commandInstall, "-m", "0644"}) { contents, err := os.ReadFile(c.args[3]) if err != nil { t.Fatalf("read temporary service file: %v", err) From f553fd86b14e2c2524d43c6a362eaab53f1140b2 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 19 Oct 2026 03:47:06 -0600 Subject: [PATCH 4/4] fix(ci): pass msiexec command line verbatim on Windows --- .github/workflows/pr-ci.yml | 6 ++---- hack/podman_ci/install.go | 26 +++++++++----------------- hack/podman_ci/install_test.go | 22 +++++++++++++++------- hack/podman_ci/install_unsupported.go | 6 ++++++ hack/podman_ci/install_windows.go | 11 ++++++++++- hack/podman_ci/runner.go | 6 ++++++ 6 files changed, 48 insertions(+), 29 deletions(-) diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index 006cd2dd02..fea57428c8 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -1058,10 +1058,8 @@ jobs: run: | & "$env:RUNNER_TEMP\devsy-ci-podman\devsy-ci-podman.exe" install ` --url "https://github.com/containers/podman/releases/download/v5.8.2/podman-installer-windows-amd64.msi" ` - --sha256 "eda54f26f9695d198d9a679fa45ae24ba35b78444f432b5fe0c122c5a3624c57" ` - --msi-path "$env:RUNNER_TEMP\podman.msi" ` - --install-dir "C:\Program Files\RedHat\Podman" ` - --log-path "$env:RUNNER_TEMP\podman-install.log" + --sha256 "eda54f26f9695d198d9a679fa45ae24ba35b78444f432b5fe0c122c5a3624c57" --msi-path "$env:RUNNER_TEMP\podman.msi" ` + --install-dir "C:\Program Files\RedHat\Podman" --log-path "$env:RUNNER_TEMP\podman-install.log" - name: start podman (Windows) if: (matrix.install-kind == true || matrix.install-podman == true) && runner.os == 'Windows' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true') diff --git a/hack/podman_ci/install.go b/hack/podman_ci/install.go index 7f48808aad..98996f2e7c 100644 --- a/hack/podman_ci/install.go +++ b/hack/podman_ci/install.go @@ -58,10 +58,8 @@ func newInstallCommand() *cobra.Command { cmd.Flags().StringVar(&cfg.MSIPath, "msi-path", "", "local path for the podman MSI") cmd.Flags().StringVar(&cfg.InstallDir, "install-dir", "", "msiexec INSTALLDIR") cmd.Flags().StringVar(&cfg.LogPath, "log-path", "", "msiexec verbose log path") - cmd.Flags().StringVar( - &cfg.GitHubPath, "github-path", os.Getenv("GITHUB_PATH"), - "file receiving the install dir for PATH", - ) + cmd.Flags(). + StringVar(&cfg.GitHubPath, "github-path", os.Getenv("GITHUB_PATH"), "file to append the install dir to") for _, name := range []string{"url", "sha256", "msi-path", "install-dir", "log-path"} { _ = cmd.MarkFlagRequired(name) } @@ -92,7 +90,7 @@ func installPodmanMSI( res := deps.Runner.Run(ctx, CommandSpec{ Path: msiexecPath, - Args: msiexecArgs(cfg), + CmdLine: msiexecCommandLine(cfg), Timeout: msiInstallTimeout, }) deps.Log.Printf("MSI exit code: %s", exitCodeString(res.ExitCode)) @@ -106,11 +104,9 @@ func installPodmanMSI( return appendGitHubPath(deps.Files, cfg.GitHubPath, cfg.InstallDir) } -func msiexecArgs(cfg InstallConfig) []string { - return []string{ - "/i", cfg.MSIPath, "/qn", "/norestart", "/l*v", cfg.LogPath, - fmt.Sprintf(`INSTALLDIR="%s"`, cfg.InstallDir), - } +func msiexecCommandLine(cfg InstallConfig) string { + return fmt.Sprintf(`%s /i "%s" /qn /norestart /l*v "%s" INSTALLDIR="%s"`, + msiexecPath, cfg.MSIPath, cfg.LogPath, cfg.InstallDir) } func installSum(data []byte) string { @@ -142,13 +138,9 @@ func logMissingPodman(deps Dependencies, cfg InstallConfig, searchRoot string) { } func tailLines(data []byte, n int) string { - lines := strings.Split(strings.ReplaceAll(string(data), "\r\n", "\n"), "\n") - if len(lines) > 0 && lines[len(lines)-1] == "" { - lines = lines[:len(lines)-1] - } - if len(lines) > n { - lines = lines[len(lines)-n:] - } + text := strings.TrimRight(strings.ReplaceAll(string(data), "\r\n", "\n"), "\n") + lines := strings.Split(text, "\n") + lines = lines[max(0, len(lines)-n):] return strings.Join(lines, "\n") } diff --git a/hack/podman_ci/install_test.go b/hack/podman_ci/install_test.go index 57dd4b0683..a951436ea4 100644 --- a/hack/podman_ci/install_test.go +++ b/hack/podman_ci/install_test.go @@ -10,7 +10,6 @@ import ( "net/http/httptest" "os" "path/filepath" - "slices" "strings" "testing" ) @@ -143,12 +142,8 @@ func TestInstallPodmanMSISucceeds(t *testing.T) { if call.Path != "msiexec.exe" { t.Fatalf("msiexec path = %q, want msiexec.exe", call.Path) } - wantArgs := []string{ - "/i", fx.cfg.MSIPath, "/qn", "/norestart", "/l*v", fx.cfg.LogPath, - `INSTALLDIR="C:\Program Files\RedHat\Podman"`, - } - if !slices.Equal(call.Args, wantArgs) { - t.Fatalf("msiexec args = %q, want %q", call.Args, wantArgs) + if !strings.Contains(call.CmdLine, `INSTALLDIR="C:\Program Files\RedHat\Podman"`) { + t.Fatalf("msiexec command line = %q, missing quoted INSTALLDIR", call.CmdLine) } if got := fx.files.appends[fx.cfg.GitHubPath].String(); got != `C:\Program Files\RedHat\Podman`+"\n" { t.Fatalf("GITHUB_PATH content = %q", got) @@ -158,6 +153,19 @@ func TestInstallPodmanMSISucceeds(t *testing.T) { } } +func TestMSIExecCommandLine(t *testing.T) { + cfg := InstallConfig{ + MSIPath: `C:\tmp\podman.msi`, + LogPath: `C:\tmp\podman-install.log`, + InstallDir: `C:\Program Files\RedHat\Podman`, + } + want := `msiexec.exe /i "C:\tmp\podman.msi" /qn /norestart ` + + `/l*v "C:\tmp\podman-install.log" INSTALLDIR="C:\Program Files\RedHat\Podman"` + if got := msiexecCommandLine(cfg); got != want { + t.Fatalf("msiexecCommandLine() = %q, want %q", got, want) + } +} + func TestInstallPodmanMSIRejectsChecksumMismatch(t *testing.T) { msi := []byte("msi-bytes") fx := newInstallFixture(t, msi) diff --git a/hack/podman_ci/install_unsupported.go b/hack/podman_ci/install_unsupported.go index f153e03277..9cd02b7b7d 100644 --- a/hack/podman_ci/install_unsupported.go +++ b/hack/podman_ci/install_unsupported.go @@ -4,10 +4,16 @@ package main import ( "context" + "errors" "fmt" + "os/exec" "runtime" ) func platformInstall(_ context.Context, _ Dependencies, _ InstallConfig) error { return fmt.Errorf("podman MSI install is only supported on Windows, not %s", runtime.GOOS) } + +func setCmdLine(_ *exec.Cmd, _ string) error { + return errors.New("verbatim command lines are only supported on Windows") +} diff --git a/hack/podman_ci/install_windows.go b/hack/podman_ci/install_windows.go index 2bcae846c1..54012f1c93 100644 --- a/hack/podman_ci/install_windows.go +++ b/hack/podman_ci/install_windows.go @@ -1,9 +1,18 @@ package main -import "context" +import ( + "context" + "os/exec" + "syscall" +) const podmanSearchRoot = `C:\` func platformInstall(ctx context.Context, deps Dependencies, cfg InstallConfig) error { return installPodmanMSI(ctx, deps, cfg, podmanSearchRoot) } + +func setCmdLine(cmd *exec.Cmd, line string) error { + cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: line} + return nil +} diff --git a/hack/podman_ci/runner.go b/hack/podman_ci/runner.go index 890f6dbae9..a6fa699258 100644 --- a/hack/podman_ci/runner.go +++ b/hack/podman_ci/runner.go @@ -15,6 +15,7 @@ type CommandSpec struct { Args []string Timeout time.Duration Reserve time.Duration + CmdLine string // Windows-only verbatim command line; used instead of Args when set. } type CommandResult struct { @@ -53,6 +54,11 @@ func (r *ExecRunner) Run(ctx context.Context, spec CommandSpec) CommandResult { defer capture.Close() cmd := exec.Command(spec.Path, spec.Args...) + if spec.CmdLine != "" { + if err := setCmdLine(cmd, spec.CmdLine); err != nil { + return commandError(spec, err) + } + } cmd.Stdout, cmd.Stderr = capture.stdout, capture.stderr if err := cmd.Start(); err != nil { return commandError(spec, err)