Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions hack/podman_ci/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ const (
)

type SetupConfig struct {
Mode Mode
PodmanPath string
CrunPath string
BootstrapTimeout time.Duration
GitHubEnvPath string
Mode Mode
PodmanPath string
CrunPath string
BootstrapTimeout time.Duration
GitHubEnvPath string
GitHubStepSummaryPath string
}

func validateConfig(cfg SetupConfig, goos string) error {
Expand Down
58 changes: 58 additions & 0 deletions hack/podman_ci/hang_report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"strings"
)

const maxStepSummaryDetailBytes = 32 << 10

var (
workflowDataEscaper = strings.NewReplacer("%", "%25", "\r", "%0D", "\n", "%0A")
workflowPropertyEscaper = strings.NewReplacer(
"%", "%25", "\r", "%0D", "\n", "%0A", ":", "%3A", ",", "%2C",
)
)

// hangReporter leaves evidence of a setup hang in the step output (a GitHub
// warning annotation) and in the job step summary.
type hangReporter struct {
log *Logger
files FileSystem
summaryPath string
}

func (r hangReporter) Report(title, message, details string) {
r.log.Printf(
"::warning title=%s::%s",
workflowPropertyEscaper.Replace(title),
workflowDataEscaper.Replace(message),
)
r.appendSummary(title, message, details)
}

func (r hangReporter) appendSummary(title, message, details string) {
if r.summaryPath == "" || r.files == nil {
return
}
out, err := r.files.OpenAppend(r.summaryPath)
if err != nil {
r.log.Printf("[podman-windows] step summary unavailable: %v", err)
return
}
defer func() { _ = out.Close() }()
_, _ = fmt.Fprintf(
out,
"### %s\n\n%s\n\n```\n%s\n```\n",
title,
message,
tailBytes(details, maxStepSummaryDetailBytes),
)
}

func tailBytes(text string, limit int) string {
if len(text) <= limit {
return text
}
return strings.ToValidUTF8(text[len(text)-limit:], "")
}
95 changes: 95 additions & 0 deletions hack/podman_ci/hang_report_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"bytes"
"errors"
"io"
"strings"
"testing"
)

const testSummaryPath = "summary.md"

type summaryFS struct {
FileSystem
appended map[string]*summaryFile
openErr error
}

type summaryFile struct{ bytes.Buffer }

func (*summaryFile) Close() error { return nil }

func (f *summaryFS) OpenAppend(path string) (io.WriteCloser, error) {
if f.openErr != nil {
return nil, f.openErr
}
if f.appended == nil {
f.appended = map[string]*summaryFile{}
}
file := &summaryFile{}
f.appended[path] = file
return file, nil
}

func TestHangReporterWritesEscapedAnnotationAndSummary(t *testing.T) {
var out bytes.Buffer
files := &summaryFS{}
reporter := hangReporter{
log: &Logger{Out: &out},
files: files,
summaryPath: testSummaryPath,
}

reporter.Report("title: a,b", "100%\nsecond line", "diagnostic output")

wantAnnotation := "::warning title=title%3A a%2Cb::100%25%0Asecond line\n"
if out.String() != wantAnnotation {
t.Fatalf("annotation = %q, want %q", out.String(), wantAnnotation)
}
summary := files.appended[testSummaryPath].String()
for _, want := range []string{"### title: a,b", "100%\nsecond line", "diagnostic output"} {
if !strings.Contains(summary, want) {
t.Fatalf("summary missing %q: %s", want, summary)
}
}
}

func TestHangReporterTruncatesSummaryDetailsKeepingTail(t *testing.T) {
files := &summaryFS{}
reporter := hangReporter{log: &Logger{Out: io.Discard}, files: files, summaryPath: "s.md"}
details := "HEAD" + strings.Repeat("x", maxStepSummaryDetailBytes) + "TAIL"

reporter.Report("title", "message", details)

summary := files.appended["s.md"].String()
if strings.Contains(summary, "HEAD") || !strings.Contains(summary, "TAIL") {
t.Fatalf("summary did not keep the tail of the details: %.80s", summary)
}
}

func TestHangReporterSummaryFailureDoesNotHideAnnotation(t *testing.T) {
var out bytes.Buffer
reporter := hangReporter{
log: &Logger{Out: &out},
files: &summaryFS{openErr: errors.New("read-only")},
summaryPath: testSummaryPath,
}

reporter.Report("title", "message", "details")

if !strings.Contains(out.String(), "::warning title=title::message") ||
!strings.Contains(out.String(), "step summary unavailable: read-only") {
t.Fatalf("output = %q", out.String())
}
}

func TestHangReporterWithoutSummaryPathOnlyAnnotates(t *testing.T) {
files := &summaryFS{}
var out bytes.Buffer
hangReporter{log: &Logger{Out: &out}, files: files}.Report("title", "message", "details")

if len(files.appended) != 0 || !strings.Contains(out.String(), "::warning") {
t.Fatalf("appended=%v output=%q", files.appended, out.String())
}
}
15 changes: 11 additions & 4 deletions hack/podman_ci/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ func newRootCommand() *cobra.Command {

func newSetupCommand() *cobra.Command {
cfg := SetupConfig{
PodmanPath: platformDefaultPodmanPath(),
CrunPath: "/usr/local/bin/crun",
BootstrapTimeout: 270 * time.Second,
GitHubEnvPath: os.Getenv("GITHUB_ENV"),
PodmanPath: platformDefaultPodmanPath(),
CrunPath: "/usr/local/bin/crun",
BootstrapTimeout: 270 * time.Second,
GitHubEnvPath: os.Getenv("GITHUB_ENV"),
GitHubStepSummaryPath: os.Getenv("GITHUB_STEP_SUMMARY"),
}
cmd := &cobra.Command{
Use: "setup", Short: "Configure and validate a Podman CI runtime", Args: cobra.NoArgs,
Expand All @@ -75,6 +76,12 @@ func newSetupCommand() *cobra.Command {
DurationVar(&cfg.BootstrapTimeout, "bootstrap-timeout", cfg.BootstrapTimeout, "maximum bootstrap duration")
cmd.Flags().
StringVar(&cfg.GitHubEnvPath, "github-env", cfg.GitHubEnvPath, "GitHub Actions environment file")
cmd.Flags().StringVar(
&cfg.GitHubStepSummaryPath,
"github-step-summary",
cfg.GitHubStepSummaryPath,
"GitHub Actions step summary file",
)
cmd.Flags().Var(newModeValue(&cfg.Mode), "mode", "Podman mode: rootless or rootful")
_ = cmd.MarkFlagRequired("mode")
return cmd
Expand Down
Loading
Loading