From bfb3fdc7e479e4a7611569582cf5bd5fe2acdc1a Mon Sep 17 00:00:00 2001 From: Sanne Raymaekers Date: Wed, 22 Oct 2025 16:16:56 +0200 Subject: [PATCH] osbuild/osbuild-exec: make osbuild command a variable This is useful for testing, as osbuild can be overwritten in unit tests. --- pkg/osbuild/osbuild-exec.go | 14 +++++++----- pkg/osbuild/osbuild-exec_test.go | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/pkg/osbuild/osbuild-exec.go b/pkg/osbuild/osbuild-exec.go index 15f044f6a5..83a7702c91 100644 --- a/pkg/osbuild/osbuild-exec.go +++ b/pkg/osbuild/osbuild-exec.go @@ -24,6 +24,8 @@ const ( MonitorLog = "LogMonitor" ) +var OSBuildCmd = "osbuild" + type OSBuildOptions struct { StoreDir string OutputDir string @@ -47,7 +49,7 @@ func NewOSBuildCmd(manifest []byte, exports, checkpoints []string, optsPtr *OSBu // nolint: gosec cmd := exec.Command( - "osbuild", + OSBuildCmd, "--store", opts.StoreDir, "--output-directory", opts.OutputDir, fmt.Sprintf("--cache-max-size=%v", cacheMaxSize), @@ -82,14 +84,16 @@ func NewOSBuildCmd(manifest []byte, exports, checkpoints []string, optsPtr *OSBu // Note that osbuild returns non-zero when the pipeline fails. This function // does not return an error in this case. Instead, the failure is communicated // with its corresponding logs through osbuild.Result. -func RunOSBuild(manifest []byte, exports, checkpoints []string, errorWriter io.Writer, opts *OSBuildOptions) (*Result, error) { +func RunOSBuild(manifest []byte, exports, checkpoints []string, errorWriter io.Writer, optsPtr *OSBuildOptions) (*Result, error) { + opts := common.ValueOrEmpty(optsPtr) + if err := CheckMinimumOSBuildVersion(); err != nil { return nil, err } var stdoutBuffer bytes.Buffer var res Result - cmd := NewOSBuildCmd(manifest, exports, checkpoints, opts) + cmd := NewOSBuildCmd(manifest, exports, checkpoints, &opts) if opts.JSONOutput { cmd.Stdout = &stdoutBuffer @@ -152,7 +156,7 @@ func CheckMinimumOSBuildVersion() error { // OSBuildVersion returns the version of osbuild. func OSBuildVersion() (string, error) { var stdoutBuffer bytes.Buffer - cmd := exec.Command("osbuild", "--version") + cmd := exec.Command(OSBuildCmd, "--version") cmd.Stdout = &stdoutBuffer err := cmd.Run() @@ -169,7 +173,7 @@ func OSBuildVersion() (string, error) { // OSBuildInspect converts a manifest to an inspected manifest. func OSBuildInspect(manifest []byte) ([]byte, error) { - cmd := exec.Command("osbuild", "--inspect") + cmd := exec.Command(OSBuildCmd, "--inspect") cmd.Stdin = bytes.NewBuffer(manifest) out, err := cmd.Output() diff --git a/pkg/osbuild/osbuild-exec_test.go b/pkg/osbuild/osbuild-exec_test.go index 8ddf1fd8c4..074df1576a 100644 --- a/pkg/osbuild/osbuild-exec_test.go +++ b/pkg/osbuild/osbuild-exec_test.go @@ -3,6 +3,8 @@ package osbuild_test import ( "fmt" "io" + "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -11,6 +13,21 @@ import ( "github.com/osbuild/images/pkg/osbuild" ) +func mockOsbuildCmd(s string) (restore func()) { + saved := osbuild.OSBuildCmd + osbuild.OSBuildCmd = s + return func() { + osbuild.OSBuildCmd = saved + } +} + +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 TestNewOSBuildCmdNilOptions(t *testing.T) { mf := []byte(`{"real": "manifest"}`) cmd := osbuild.NewOSBuildCmd(mf, nil, nil, nil) @@ -91,3 +108,23 @@ func TestNewOSBuildCmdFullOptions(t *testing.T) { assert.NoError(t, err) assert.Equal(t, mf, stdin) } + +func TestRunOSBuild(t *testing.T) { + fakeOsbuildBinary := makeFakeOsbuild(t, ` +if [ "$1" = "--version" ]; then + echo '90000.0' +else + echo '{"success": true}' +fi +`) + restore := mockOsbuildCmd(fakeOsbuildBinary) + defer restore() + + opts := &osbuild.OSBuildOptions{ + JSONOutput: true, + } + result, err := osbuild.RunOSBuild([]byte(`{"fake":"manifest"}`), nil, nil, nil, opts) + assert.NoError(t, err) + assert.NotNil(t, result) + assert.True(t, result.Success) +}