progress: fix missing build log output on errors in progress

This commit fixes a silly mistake from PR#810. The issue is that
in #810 we started to collect the osbuild stdout/stderr so that
we can show crashes from osbuild or other unexpected output.

However when a stage fails this is reported via the json progress
and not directly on stdout/stderr - this was missed when #810
was done.

This commit does a short term fix by collecting the buildlog again
and showing it in the error and also updates the test to be more
realistic. However we really need a test that actually tests
the real behavior, ideally a real osbuild run with a stage error
so that we can be sure we capture this (criticial!) functionality.
This commit is contained in:
Michael Vogt 2025-02-05 11:10:52 +01:00 committed by Simon de Vlieger
parent 0e1a0f8ace
commit 3e7ebe81c4
2 changed files with 20 additions and 2 deletions

View file

@ -368,6 +368,7 @@ func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirect
}
wp.Close()
var tracesMsgs []string
var statusErrs []error
for {
st, err := osbuildStatus.Status()
@ -389,10 +390,18 @@ func runOSBuildWithProgress(pb ProgressBar, manifest []byte, store, outputDirect
if st.Message != "" {
pb.SetMessagef(st.Message)
}
// keep all messages/traces for better error reporting
if st.Message != "" {
tracesMsgs = append(tracesMsgs, st.Message)
}
if st.Trace != "" {
tracesMsgs = append(tracesMsgs, st.Trace)
}
}
if err := cmd.Wait(); err != nil {
return fmt.Errorf("error running osbuild: %w\nOutput:\n%s", err, stdio.String())
return fmt.Errorf("error running osbuild: %w\nBuildLog:\n%s\nOutput:\n%s", err, strings.Join(tracesMsgs, "\n"), stdio.String())
}
if len(statusErrs) > 0 {
return fmt.Errorf("errors parsing osbuild status:\n%w", errors.Join(statusErrs...))

View file

@ -3,6 +3,7 @@ package progress_test
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
@ -141,7 +142,13 @@ func makeFakeOsbuild(t *testing.T, content string) string {
}
func TestRunOSBuildWithProgressErrorReporting(t *testing.T) {
restore := progress.MockOsbuildCmd(makeFakeOsbuild(t, `echo osbuild-stdout-output
restore := progress.MockOsStderr(io.Discard)
defer restore()
restore = progress.MockOsbuildCmd(makeFakeOsbuild(t, `
>&3 echo '{"message": "osbuild-stage-message"}'
echo osbuild-stdout-output
>&2 echo osbuild-stderr-output
exit 112
`))
@ -151,6 +158,8 @@ exit 112
assert.NoError(t, err)
err = progress.RunOSBuild(pbar, []byte(`{"fake":"manifest"}`), "", "", nil, nil)
assert.EqualError(t, err, `error running osbuild: exit status 112
BuildLog:
osbuild-stage-message
Output:
osbuild-stdout-output
osbuild-stderr-output