test/image: improve logging of failed osbuild execution

Previously, the test merged stderr and stderr and treated it as json. This was
obviously wrong. osbuild's stderr only returns python exception that are
of course not json.

The behaviour is now different:

1) stderr and stdout are treated separately
2) json from stdout is indented to prevent long lines. If it fails the test
   just prints the stdout as returned from osbuild.
3) stderr is printed as returned from osbuild

Signed-off-by: Ondřej Budai <ondrej@budai.cz>
This commit is contained in:
Ondřej Budai 2021-03-02 09:37:56 +01:00 committed by Alexander Todorov
parent f091af55d8
commit b28f32312d

View file

@ -54,16 +54,25 @@ func runOsbuild(manifest []byte, store, outputDirectory string) error {
cmd := constants.GetOsbuildCommand(store, outputDirectory)
cmd.Stdin = bytes.NewReader(manifest)
var outBuffer bytes.Buffer
var outBuffer, errBuffer bytes.Buffer
cmd.Stdout = &outBuffer
cmd.Stderr = &outBuffer
cmd.Stderr = &errBuffer
err := cmd.Run()
if err != nil {
// Pretty print the osbuild error output.
buf := new(bytes.Buffer)
_ = json.Indent(buf, outBuffer.Bytes(), "", " ")
fmt.Println(buf)
fmt.Println("stdout:")
// stdout is json, indent it, otherwise we get a huge one-liner
var formattedStdout bytes.Buffer
indentErr := json.Indent(&formattedStdout, outBuffer.Bytes(), "", " ")
if indentErr == nil {
fmt.Println(formattedStdout.String())
} else {
// fallback to raw output if json indent failed
fmt.Println(outBuffer.String())
}
// stderr isn't structured, print it as is
fmt.Printf("stderr:\n%s", errBuffer.String())
return fmt.Errorf("running osbuild failed: %v", err)
}