Skip to content

Commit 23339ac

Browse files
authored
Merge pull request #85 from oasisprotocol/ptrus/feature/rofl-cli-split-stdout
rofl: Split stdout and stderr for CLI commands
2 parents 0a5b3bc + 5452fdc commit 23339ac

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

e2e/e2e_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ func TestE2E(t *testing.T) {
135135
if result.Err != "" {
136136
// XXX: CI seems to slow to push the image to registry, so the build timeouts, don't fail the test in that case.
137137
// Remove this in future, if the timeotus get adjusted.
138-
if strings.Contains(result.Logs, "response status code 524") {
138+
if strings.Contains(result.Stdout, "response status code 524") {
139139
t.Log("build timed out, not failing the test")
140140
return
141141
}
142142
require.FailNow(result.Err, "build failed", result.Err)
143143
}
144-
require.NotEmpty(result.Logs, "no logs in response body")
144+
require.NotEmpty(result.Stdout, "no logs in response body")
145145
require.NotEmpty(result.Manifest, "no manifest in response body")
146146
require.NotEmpty(result.ManifestHash, "no manifest hash in response body")
147147
require.NotEmpty(result.OciReference, "no OCI reference in response body")
@@ -280,7 +280,7 @@ func TestE2E(t *testing.T) {
280280
require.NoError(json.Unmarshal(body, &validateRes), "failed to unmarshal response body")
281281
require.False(validateRes.Valid, "validation should fail for %s", tc.description)
282282
require.NotEmpty(validateRes.Err, "error should be present for %s", tc.description)
283-
require.NotEmpty(validateRes.Logs, "logs should be present for %s", tc.description)
283+
require.NotEmpty(validateRes.Stderr, "stderr should be present for %s", tc.description)
284284

285285
t.Logf("Test case '%s' failed as expected with error: %s", tc.description, validateRes.Err)
286286
})

tasks/roflbuild.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ type RoflBuildResult struct {
5555
OciReference string `json:"oci_reference"`
5656
ManifestHash string `json:"manifest_hash"`
5757

58-
Logs string `json:"logs"`
58+
Stdout string `json:"stdout"`
59+
Stderr string `json:"stderr"`
5960

6061
Err string `json:"err"`
6162
}

tasks/roflvalidate.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ type RoflValidatePayload struct {
4646

4747
// RoflValidateResult are the results of the rofl validate task.
4848
type RoflValidateResult struct {
49-
Valid bool `json:"valid"`
50-
Logs string `json:"logs"`
51-
Err string `json:"err"`
49+
Valid bool `json:"valid"`
50+
Stdout string `json:"stdout"`
51+
Stderr string `json:"stderr"`
52+
Err string `json:"err"`
5253
}
5354

5455
// RoflValidateResultsKey is the redis key for the results of the rofl validate task.

worker/oasiscli/oasiscli.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"context"
77
"encoding/json"
88
"fmt"
9-
"io"
109
"log/slog"
1110
"math/rand"
1211
"os"
@@ -45,8 +44,10 @@ type CommandResult struct {
4544
// Validate is the result of the validate command.
4645
Validate *CommandValidateResult `json:"validate,omitempty"`
4746

48-
// Logs are the outputted logs by the command.
49-
Logs []byte `json:"logs"`
47+
// Stdout is the standard output from the command.
48+
Stdout []byte `json:"stdout"`
49+
// Stderr is the error output from the command.
50+
Stderr []byte `json:"stderr"`
5051
// Err contains the error during execution of the command.
5152
Err error `json:"err,omitempty"`
5253
}
@@ -122,10 +123,10 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (*CommandResult, error
122123
}
123124
c.SysProcAttr = getSysProcAttr()
124125

125-
var outputBuf bytes.Buffer
126126
var stdoutBuf bytes.Buffer
127-
c.Stdout = io.MultiWriter(&outputBuf, &stdoutBuf)
128-
c.Stderr = &outputBuf
127+
var stderrBuf bytes.Buffer
128+
c.Stdout = &stdoutBuf
129+
c.Stderr = &stderrBuf
129130

130131
results := &CommandResult{}
131132
if err := c.Start(); err != nil {
@@ -142,10 +143,11 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (*CommandResult, error
142143
_ = syscall.Kill(-c.Process.Pid, syscall.SIGKILL)
143144
}
144145
<-done
145-
slog.Error("command timed out", "command", c.String(), "logs", outputBuf.String(), "error", ctx.Err())
146+
slog.Error("command timed out", "command", c.String(), "stdout", stdoutBuf.String(), "stderr", stderrBuf.String(), "error", ctx.Err())
146147
return nil, ctx.Err()
147148
case err := <-done:
148-
results.Logs = outputBuf.Bytes()
149+
results.Stdout = stdoutBuf.Bytes()
150+
results.Stderr = stderrBuf.Bytes()
149151
if err != nil {
150152
results.Err = fmt.Errorf("command '%s' failed: %w", c.String(), err)
151153
return results, nil
@@ -166,8 +168,8 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (*CommandResult, error
166168
case CommandPush:
167169
// The output should be a JSON object with the OCI digest and manifest hash.
168170
var parsed map[string]string
169-
if err := json.Unmarshal(stdoutBuf.Bytes(), &parsed); err != nil {
170-
slog.Error("failed to parse push output", "error", err, "stdout", stdoutBuf.Bytes(), "logs", results.Logs)
171+
if err := json.Unmarshal(results.Stdout, &parsed); err != nil {
172+
slog.Error("failed to parse push output", "error", err, "stdout", results.Stdout, "stderr", results.Stderr)
171173
return nil, fmt.Errorf("failed to parse push output: %w", err)
172174
}
173175
if parsed["oci_reference"] == "" {

worker/worker.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,18 @@ func (p *roflProcessor) processBuildTask(ctx context.Context, taskID string, pay
227227
result.Err = ErrInternalError.Error()
228228
return result
229229
}
230-
result.Logs = string(buildResult.Logs)
230+
result.Stdout = string(buildResult.Stdout)
231+
result.Stderr = string(buildResult.Stderr)
231232

232233
// Propagate the build command error if it failed.
233234
if buildResult.Err != nil {
234-
p.logger.Error("build command failed", "error", buildResult.Err, "logs", buildResult.Logs)
235+
p.logger.Error("build command failed", "error", buildResult.Err, "stdout", buildResult.Stdout, "stderr", buildResult.Stderr)
235236
result.Err = buildResult.Err.Error()
236237
return result
237238
}
238239

239240
if buildResult.Build == nil {
240-
p.logger.Error("build result is nil, but no error was returned", "logs", buildResult.Logs)
241+
p.logger.Error("build result is nil, but no error was returned", "stdout", buildResult.Stdout, "stderr", buildResult.Stderr)
241242
result.Err = ErrInternalError.Error()
242243
return result
243244
}
@@ -252,17 +253,18 @@ func (p *roflProcessor) processBuildTask(ctx context.Context, taskID string, pay
252253
result.Err = ErrInternalError.Error()
253254
return result
254255
}
255-
result.Logs += "\n" + string(pushResult.Logs)
256+
result.Stdout += "\n" + string(pushResult.Stdout)
257+
result.Stderr += "\n" + string(pushResult.Stderr)
256258

257259
// Propagate the push command error if it failed.
258260
if pushResult.Err != nil {
259-
p.logger.Error("push command failed", "error", pushResult.Err, "logs", pushResult.Logs)
261+
p.logger.Error("push command failed", "error", pushResult.Err, "stdout", pushResult.Stdout, "stderr", pushResult.Stderr)
260262
result.Err = pushResult.Err.Error()
261263
return result
262264
}
263265

264266
if pushResult.Push == nil {
265-
p.logger.Error("push result is nil, but no error was returned", "logs", pushResult.Logs)
267+
p.logger.Error("push result is nil, but no error was returned", "stdout", pushResult.Stdout, "stderr", pushResult.Stderr)
266268
result.Err = ErrInternalError.Error()
267269
return result
268270
}
@@ -325,11 +327,12 @@ func (p *roflProcessor) processValidateTask(ctx context.Context, taskID string,
325327
result.Err = ErrInternalError.Error()
326328
return result
327329
}
328-
result.Logs = string(validateResult.Logs)
330+
result.Stdout = string(validateResult.Stdout)
331+
result.Stderr = string(validateResult.Stderr)
329332

330333
// Check if validation succeeded.
331334
if validateResult.Err != nil {
332-
p.logger.Debug("validate command failed", "error", validateResult.Err, "logs", validateResult.Logs)
335+
p.logger.Debug("validate command failed", "error", validateResult.Err, "stdout", validateResult.Stdout, "stderr", validateResult.Stderr)
333336
result.Valid = false
334337
result.Err = validateResult.Err.Error()
335338
} else {

0 commit comments

Comments
 (0)