Skip to content

Commit

Permalink
progress: tweak progress error reporting
Browse files Browse the repository at this point in the history
This commit adds catpure of os.Std{out,err} when running osbuild so
that we capture the error log and can display it as part of
an error from osbuild, e.g. when osbuild itself crashes.

This gives us more accurate error reporting if anything fails
during the osbuild building.
  • Loading branch information
mvo5 committed Jan 27, 2025
1 parent c5d6741 commit eb97668
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
8 changes: 8 additions & 0 deletions bib/pkg/progress/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ func MockIsattyIsTerminal(fn func(uintptr) bool) (restore func()) {
isattyIsTerminal = saved
}
}

func MockOsbuildCmd(s string) (restore func()) {
saved := osbuildCmd
osbuildCmd = s
return func() {
osbuildCmd = saved
}
}
13 changes: 7 additions & 6 deletions bib/pkg/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ func runOSBuildNoProgress(pb ProgressBar, manifest []byte, store, outputDirector
return err
}

var osbuildCmd = "osbuild"

func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirectory string, exports, extraEnv []string) error {
rp, wp, err := os.Pipe()
if err != nil {
Expand All @@ -342,7 +344,7 @@ func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirect
defer wp.Close()

cmd := exec.Command(
"osbuild",
osbuildCmd,
"--store", store,
"--output-directory", outputDirectory,
"--monitor=JSONSeqMonitor",
Expand All @@ -353,12 +355,11 @@ func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirect
cmd.Args = append(cmd.Args, "--export", export)
}

var stdio bytes.Buffer
cmd.Env = append(os.Environ(), extraEnv...)
cmd.Stdin = bytes.NewBuffer(manifest)
cmd.Stderr = os.Stderr
// we could use "--json" here and would get the build-result
// exported here
cmd.Stdout = nil
cmd.Stdout = &stdio
cmd.Stderr = &stdio
cmd.ExtraFiles = []*os.File{wp}

osbuildStatus := osbuild.NewStatusScanner(rp)
Expand Down Expand Up @@ -397,7 +398,7 @@ func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirect
}

if err := cmd.Wait(); err != nil {
return fmt.Errorf("error running osbuild: %w\nLog:\n%s", err, strings.Join(tracesMsgs, "\n"))
return fmt.Errorf("error running osbuild: %w\nOutput:\n%s", err, stdio.String())
}

return nil
Expand Down
26 changes: 26 additions & 0 deletions bib/pkg/progress/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package progress_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"reflect"
"testing"

Expand Down Expand Up @@ -130,3 +132,27 @@ func TestProgressNewAutoselect(t *testing.T) {
assert.Equal(t, reflect.TypeOf(pb), reflect.TypeOf(tc.expected), fmt.Sprintf("[%v] %T not the expected %T", tc.onTerm, pb, tc.expected))
}
}

func makeFakeOsbuild(t *testing.T, content string) string {
p := filepath.Join(t.TempDir(), "fake-osbuild")
err := os.WriteFile(p, []byte("#!/bin/sh\n"+content), 0755)
assert.NoError(t, err)
return p
}

func TestRunOSBuildWithProgress(t *testing.T) {
restore := progress.MockOsbuildCmd(makeFakeOsbuild(t, `echo osbuild-stdout-output
>&2 echo osbuild-stderr-output
exit 112
`))
defer restore()

pbar, err := progress.New("debug")
assert.NoError(t, err)
err = progress.RunOSBuild(pbar, []byte(`{"fake":"manifest"}`), "", "", nil, nil)
assert.EqualError(t, err, `error running osbuild: exit status 112
Output:
osbuild-stdout-output
osbuild-stderr-output
`)
}

0 comments on commit eb97668

Please sign in to comment.