image-builder: update TestBuildIntegrationErrors tests

This commit updates the tests for the error reporting. Since
we are now using the new "progress" module we can no longer
directly mock osStderr. So this now uses the new testutil
helper `CaptureStdio`.

Note also that the behavior of `verbose` and `term` is slightly
different - for backward compatibility in `verbose` mode the
stderr from osbuild is directly connected to the caller stderr.

For term mode this is not done to prevent spurious message from
leaking into the terminal progress and the full error is available
from the actual go error as captured output.
This commit is contained in:
Michael Vogt 2025-01-29 12:56:22 +01:00
parent e41cf0e429
commit f46ca14f85
3 changed files with 53 additions and 18 deletions

View file

@ -433,7 +433,14 @@ func TestBuildIntegrationArgs(t *testing.T) {
}
}
func TestBuildIntegrationErrors(t *testing.T) {
var failingOsbuild = `
cat - > "$0".stdin
echo "error on stdout"
>&2 echo "error on stderr"
exit 1
`
func TestBuildIntegrationErrorsProgressVerbose(t *testing.T) {
if testing.Short() {
t.Skip("manifest generation takes a while")
}
@ -444,34 +451,60 @@ func TestBuildIntegrationErrors(t *testing.T) {
restore := main.MockNewRepoRegistry(testrepos.New)
defer restore()
var fakeStdout, fakeStderr bytes.Buffer
restore = main.MockOsStdout(&fakeStdout)
restore = main.MockOsArgs([]string{
"build",
"qcow2",
"--distro", "centos-9",
"--progress=verbose",
})
defer restore()
restore = main.MockOsStderr(&fakeStderr)
fakeOsbuildCmd := testutil.MockCommand(t, "osbuild", failingOsbuild)
defer fakeOsbuildCmd.Restore()
var err error
stdout, stderr := testutil.CaptureStdio(t, func() {
err = main.Run()
})
assert.EqualError(t, err, "running osbuild failed: exit status 1")
assert.Contains(t, stdout, "error on stdout\n")
assert.Contains(t, stderr, "error on stderr\n")
}
func TestBuildIntegrationErrorsProgressTerm(t *testing.T) {
if testing.Short() {
t.Skip("manifest generation takes a while")
}
if !hasDepsolveDnf() {
t.Skip("no osbuild-depsolve-dnf binary found")
}
restore := main.MockNewRepoRegistry(testrepos.New)
defer restore()
restore = main.MockOsArgs([]string{
"build",
"qcow2",
"--distro", "centos-9",
"--progress=term",
})
defer restore()
script := `
cat - > "$0".stdin
>&2 echo "error on stderr"
exit 1
`
fakeOsbuildCmd := testutil.MockCommand(t, "osbuild", script)
fakeOsbuildCmd := testutil.MockCommand(t, "osbuild", failingOsbuild)
defer fakeOsbuildCmd.Restore()
err := main.Run()
assert.EqualError(t, err, "running osbuild failed: exit status 1")
// ensure errors from osbuild are passed to the user
// XXX: once the osbuild.Status is used, also check that stdout
// is available (but that cannot be done with the existing
// osbuild-exec.go)
assert.Equal(t, "error on stderr\n", fakeStderr.String())
var err error
stdout, stderr := testutil.CaptureStdio(t, func() {
err = main.Run()
})
assert.EqualError(t, err, `error running osbuild: exit status 1
Output:
error on stdout
error on stderr
`)
assert.NotContains(t, stdout, "error on stdout")
assert.NotContains(t, stderr, "error on stderr")
}
func TestManifestIntegrationWithSBOMWithOutputDir(t *testing.T) {