From f46ca14f85c5a77f40875b55504df975d07b5dbc Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 29 Jan 2025 12:56:22 +0100 Subject: [PATCH] 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. --- cmd/image-builder/main_test.go | 67 +++++++++++++++++++++++++--------- go.mod | 2 +- go.sum | 2 + 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/cmd/image-builder/main_test.go b/cmd/image-builder/main_test.go index dc7af6a..7106361 100644 --- a/cmd/image-builder/main_test.go +++ b/cmd/image-builder/main_test.go @@ -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) { diff --git a/go.mod b/go.mod index 2db9021..8e4cd42 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ toolchain go1.22.2 require ( github.com/BurntSushi/toml v1.4.0 github.com/gobwas/glob v0.2.3 - github.com/osbuild/bootc-image-builder/bib v0.0.0-20250125114430-c5d674173ba0 + github.com/osbuild/bootc-image-builder/bib v0.0.0-20250128074618-f6403409fc97 github.com/osbuild/images v0.112.0 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.1 diff --git a/go.sum b/go.sum index 8469538..6ddb347 100644 --- a/go.sum +++ b/go.sum @@ -220,6 +220,8 @@ github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaL github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec= github.com/osbuild/bootc-image-builder/bib v0.0.0-20250125114430-c5d674173ba0 h1:aLxeLEO/C6/mAc4O8fgmDE7uaf6EwAiW79IP2GI/bvo= github.com/osbuild/bootc-image-builder/bib v0.0.0-20250125114430-c5d674173ba0/go.mod h1:22erXOiwfznPLNE5Y8hp35fnE0vh5ZjWp/iIQsVnYTg= +github.com/osbuild/bootc-image-builder/bib v0.0.0-20250128074618-f6403409fc97 h1:Xyv+vDEGCcG1qkHkdQFPBXhvM5WiMiAJm/AjBkzW4Sw= +github.com/osbuild/bootc-image-builder/bib v0.0.0-20250128074618-f6403409fc97/go.mod h1:22erXOiwfznPLNE5Y8hp35fnE0vh5ZjWp/iIQsVnYTg= github.com/osbuild/images v0.112.0 h1:+pKwPniwYTRRgist6V+7DQfZEg7osddl1z4pASecq4M= github.com/osbuild/images v0.112.0/go.mod h1:58tzp7jV50rjaH9gMpvmQdVati0c4TaC5Op7wmSD/tY= github.com/ostreedev/ostree-go v0.0.0-20210805093236-719684c64e4f h1:/UDgs8FGMqwnHagNDPGOlts35QkhAZ8by3DR7nMih7M=