Skip to content

Commit 41914f0

Browse files
committed
osbuild/osbuild-exec: add file for monitor to options
If opts.MonitorFile is set, the monitor output is written to a separate FD which corresponds with the specified file.
1 parent 40f31c8 commit 41914f0

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

pkg/osbuild/osbuild-exec.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ type OSBuildOptions struct {
4343
Stdout io.Writer
4444
Stderr io.Writer
4545

46-
Monitor MonitorType
47-
MonitorFD uintptr
46+
// If MonitorFD is set, a file (MonitorW) needs to be inherited by the osbuild process. The
47+
// caller should make sure to close it afterwards.
48+
Monitor MonitorType
49+
MonitorFile *os.File
4850

4951
JSONOutput bool
5052

@@ -79,8 +81,10 @@ func NewOSBuildCmd(manifest []byte, optsPtr *OSBuildOptions) *exec.Cmd {
7981
if opts.Monitor != "" {
8082
cmd.Args = append(cmd.Args, fmt.Sprintf("--monitor=%s", opts.Monitor))
8183
}
82-
if opts.MonitorFD != 0 {
83-
cmd.Args = append(cmd.Args, fmt.Sprintf("--monitor-fd=%d", opts.MonitorFD))
84+
85+
if opts.MonitorFile != nil {
86+
cmd.Args = append(cmd.Args, "--monitor-fd=3")
87+
cmd.ExtraFiles = []*os.File{opts.MonitorFile}
8488
}
8589

8690
if opts.JSONOutput {

pkg/osbuild/osbuild-exec_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func TestNewOSBuildCmdNilOptions(t *testing.T) {
5151

5252
func TestNewOSBuildCmdFullOptions(t *testing.T) {
5353
mf := []byte(`{"real": "manifest"}`)
54+
_, wp, err := os.Pipe()
55+
assert.NoError(t, err)
5456
cmd := osbuild.NewOSBuildCmd(
5557
mf,
5658
&osbuild.OSBuildOptions{
@@ -63,7 +65,7 @@ func TestNewOSBuildCmdFullOptions(t *testing.T) {
6365
Checkpoints: []string{"checkpoint-1", "checkpoint-2"},
6466
ExtraEnv: []string{"EXTRA_ENV_1=1", "EXTRA_ENV_2=2"},
6567
Monitor: osbuild.MonitorLog,
66-
MonitorFD: 10,
68+
MonitorFile: wp,
6769
JSONOutput: true,
6870
CacheMaxSize: 10 * datasizes.GiB,
6971
},
@@ -89,7 +91,7 @@ func TestNewOSBuildCmdFullOptions(t *testing.T) {
8991
"--checkpoint",
9092
"checkpoint-2",
9193
"--monitor=LogMonitor",
92-
"--monitor-fd=10",
94+
"--monitor-fd=3",
9395
"--json",
9496
},
9597
cmd.Args,
@@ -101,6 +103,8 @@ func TestNewOSBuildCmdFullOptions(t *testing.T) {
101103
stdin, err := io.ReadAll(cmd.Stdin)
102104
assert.NoError(t, err)
103105
assert.Equal(t, mf, stdin)
106+
107+
assert.Equal(t, wp, cmd.ExtraFiles[0])
104108
}
105109

106110
func TestRunOSBuildJSONOutput(t *testing.T) {
@@ -157,6 +161,44 @@ osbuild-stderr-output
157161
assert.Empty(t, stderr.Bytes())
158162
}
159163

164+
func TestRunOSBuildMonitor(t *testing.T) {
165+
fakeOSBuildBinary := makeFakeOSBuild(t, `
166+
>&3 echo -n '{"some": "monitor"}'
167+
168+
if [ "$1" = "--version" ]; then
169+
echo '90000.0'
170+
else
171+
echo -n osbuild-stdout-output
172+
fi
173+
>&2 echo -n osbuild-stderr-output
174+
`)
175+
restore := osbuild.MockOSBuildCmd(fakeOSBuildBinary)
176+
defer restore()
177+
178+
rp, wp, err := os.Pipe()
179+
assert.NoError(t, err)
180+
defer wp.Close()
181+
182+
var stdout, stderr bytes.Buffer
183+
opts := &osbuild.OSBuildOptions{
184+
Stdout: &stdout,
185+
Stderr: &stderr,
186+
Monitor: osbuild.MonitorJSONSeq,
187+
MonitorFile: wp,
188+
}
189+
190+
// without json output set the result will be empty
191+
_, err = osbuild.RunOSBuild(nil, opts)
192+
assert.NoError(t, err)
193+
assert.NoError(t, wp.Close())
194+
195+
monitorOutput, err := io.ReadAll(rp)
196+
assert.NoError(t, err)
197+
assert.Equal(t, `{"some": "monitor"}`, string(monitorOutput))
198+
assert.Equal(t, "osbuild-stdout-output", stdout.String())
199+
assert.Equal(t, "osbuild-stderr-output", stderr.String())
200+
}
201+
160202
func TestSyncWriter(t *testing.T) {
161203
var mu sync.Mutex
162204
var buf bytes.Buffer

0 commit comments

Comments
 (0)