Skip to content

Commit 00696f3

Browse files
croissannesupakeen
authored andcommitted
osbuild/osbuild-exec: make osbuild command a variable
This is useful for testing, as osbuild can be overwritten in unit tests.
1 parent 6340fa0 commit 00696f3

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

pkg/osbuild/osbuild-exec.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const (
2424
MonitorLog = "LogMonitor"
2525
)
2626

27+
var OSBuildCmd = "osbuild"
28+
2729
type OSBuildOptions struct {
2830
StoreDir string
2931
OutputDir string
@@ -47,7 +49,7 @@ func NewOSBuildCmd(manifest []byte, exports, checkpoints []string, optsPtr *OSBu
4749

4850
// nolint: gosec
4951
cmd := exec.Command(
50-
"osbuild",
52+
OSBuildCmd,
5153
"--store", opts.StoreDir,
5254
"--output-directory", opts.OutputDir,
5355
fmt.Sprintf("--cache-max-size=%v", cacheMaxSize),
@@ -82,14 +84,16 @@ func NewOSBuildCmd(manifest []byte, exports, checkpoints []string, optsPtr *OSBu
8284
// Note that osbuild returns non-zero when the pipeline fails. This function
8385
// does not return an error in this case. Instead, the failure is communicated
8486
// with its corresponding logs through osbuild.Result.
85-
func RunOSBuild(manifest []byte, exports, checkpoints []string, errorWriter io.Writer, opts *OSBuildOptions) (*Result, error) {
87+
func RunOSBuild(manifest []byte, exports, checkpoints []string, errorWriter io.Writer, optsPtr *OSBuildOptions) (*Result, error) {
88+
opts := common.ValueOrEmpty(optsPtr)
89+
8690
if err := CheckMinimumOSBuildVersion(); err != nil {
8791
return nil, err
8892
}
8993

9094
var stdoutBuffer bytes.Buffer
9195
var res Result
92-
cmd := NewOSBuildCmd(manifest, exports, checkpoints, opts)
96+
cmd := NewOSBuildCmd(manifest, exports, checkpoints, &opts)
9397

9498
if opts.JSONOutput {
9599
cmd.Stdout = &stdoutBuffer
@@ -152,7 +156,7 @@ func CheckMinimumOSBuildVersion() error {
152156
// OSBuildVersion returns the version of osbuild.
153157
func OSBuildVersion() (string, error) {
154158
var stdoutBuffer bytes.Buffer
155-
cmd := exec.Command("osbuild", "--version")
159+
cmd := exec.Command(OSBuildCmd, "--version")
156160
cmd.Stdout = &stdoutBuffer
157161

158162
err := cmd.Run()
@@ -169,7 +173,7 @@ func OSBuildVersion() (string, error) {
169173

170174
// OSBuildInspect converts a manifest to an inspected manifest.
171175
func OSBuildInspect(manifest []byte) ([]byte, error) {
172-
cmd := exec.Command("osbuild", "--inspect")
176+
cmd := exec.Command(OSBuildCmd, "--inspect")
173177
cmd.Stdin = bytes.NewBuffer(manifest)
174178

175179
out, err := cmd.Output()

pkg/osbuild/osbuild-exec_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package osbuild_test
33
import (
44
"fmt"
55
"io"
6+
"os"
7+
"path/filepath"
68
"testing"
79

810
"github.com/stretchr/testify/assert"
@@ -11,6 +13,21 @@ import (
1113
"github.com/osbuild/images/pkg/osbuild"
1214
)
1315

16+
func mockOsbuildCmd(s string) (restore func()) {
17+
saved := osbuild.OSBuildCmd
18+
osbuild.OSBuildCmd = s
19+
return func() {
20+
osbuild.OSBuildCmd = saved
21+
}
22+
}
23+
24+
func makeFakeOsbuild(t *testing.T, content string) string {
25+
p := filepath.Join(t.TempDir(), "fake-osbuild")
26+
err := os.WriteFile(p, []byte("#!/bin/sh\n"+content), 0755)
27+
assert.NoError(t, err)
28+
return p
29+
}
30+
1431
func TestNewOSBuildCmdNilOptions(t *testing.T) {
1532
mf := []byte(`{"real": "manifest"}`)
1633
cmd := osbuild.NewOSBuildCmd(mf, nil, nil, nil)
@@ -91,3 +108,23 @@ func TestNewOSBuildCmdFullOptions(t *testing.T) {
91108
assert.NoError(t, err)
92109
assert.Equal(t, mf, stdin)
93110
}
111+
112+
func TestRunOSBuild(t *testing.T) {
113+
fakeOsbuildBinary := makeFakeOsbuild(t, `
114+
if [ "$1" = "--version" ]; then
115+
echo '90000.0'
116+
else
117+
echo '{"success": true}'
118+
fi
119+
`)
120+
restore := mockOsbuildCmd(fakeOsbuildBinary)
121+
defer restore()
122+
123+
opts := &osbuild.OSBuildOptions{
124+
JSONOutput: true,
125+
}
126+
result, err := osbuild.RunOSBuild([]byte(`{"fake":"manifest"}`), nil, nil, nil, opts)
127+
assert.NoError(t, err)
128+
assert.NotNil(t, result)
129+
assert.True(t, result.Success)
130+
}

0 commit comments

Comments
 (0)