Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pkg/osbuild/osbuild-exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
MonitorLog = "LogMonitor"
)

var OSBuildCmd = "osbuild"

type OSBuildOptions struct {
StoreDir string
OutputDir string
Expand All @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
37 changes: 37 additions & 0 deletions pkg/osbuild/osbuild-exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package osbuild_test
import (
"fmt"
"io"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Loading