Skip to content

Commit

Permalink
fix: prevent stdout from disappearing in script templates. Fixes #11330
Browse files Browse the repository at this point in the history
… (#11368)

Signed-off-by: boiledfroginthewell <boiledfroginthewell@users.noreply.github.com>
  • Loading branch information
boiledfroginthewell authored and terrytangyuan committed Aug 11, 2023
1 parent 1984c1a commit 1513e22
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
9 changes: 9 additions & 0 deletions cmd/argoexec/commands/emissary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,21 @@ func TestEmissary(t *testing.T) {
assert.Equal(t, "1", string(data))
})
t.Run("Stdout", func(t *testing.T) {
_ = os.Remove(varRunArgo + "/ctr/main/stdout")
err := run("echo hello")
assert.NoError(t, err)
data, err := ioutil.ReadFile(varRunArgo + "/ctr/main/stdout")
assert.NoError(t, err)
assert.Contains(t, string(data), "hello")
})
t.Run("Sub-process", func(t *testing.T) {
_ = os.Remove(varRunArgo + "/ctr/main/stdout")
err := run("(sleep 60; echo 'should not wait for sub-process')& echo -n hello")
assert.NoError(t, err)
data, err := os.ReadFile(varRunArgo + "/ctr/main/stdout")
assert.NoError(t, err)
assert.Equal(t, "hello", string(data))
})
t.Run("Combined", func(t *testing.T) {
err := run("echo hello > /dev/stderr")
assert.NoError(t, err)
Expand Down
8 changes: 7 additions & 1 deletion workflow/executor/os-specific/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"os"
"os/exec"
"time"

log "github.com/sirupsen/logrus"
"golang.org/x/term"
Expand All @@ -16,7 +17,12 @@ func simpleStart(cmd *exec.Cmd) (func(), error) {
return nil, err
}

return func() {}, nil
closer := func() {
cmd.WaitDelay = 100 * time.Millisecond
_ = cmd.Wait()
}

return closer, nil
}

func isTerminal(stdin io.Reader) bool {
Expand Down
47 changes: 47 additions & 0 deletions workflow/executor/os-specific/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package os_specific

import (
"bytes"
"io"
"os/exec"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestSimpleStartCloser(t *testing.T) {
cmd := exec.Command("sh", "-c", "echo -n A123456789B123456789C123456789D123456789E123456789")
var stdoutWriter bytes.Buffer
slowWriter := SlowWriter{
&stdoutWriter,
}
// Command outputs are asynchronously written to cmd.Stdout.
// Using SlowWriter causes the situation where the invoked command has exited but its outputs have not been written yet.
cmd.Stdout = slowWriter

closer, err := StartCommand(cmd)
assert.NoError(t, err)
err = cmd.Process.Release()
assert.NoError(t, err)
// Wait for echo command to exit before calling closer
time.Sleep(100 * time.Millisecond)
closer()

assert.Equal(t, "A123456789B123456789C123456789D123456789E123456789", stdoutWriter.String())
}

type SlowWriter struct {
Writer io.Writer
}

func (s SlowWriter) Write(data []byte) (n int, err error) {
for i := range data {
_, err := s.Writer.Write(data[i : i+1])
if err != nil {
return i, err
}
time.Sleep(7 * time.Millisecond)
}
return len(data), nil
}

0 comments on commit 1513e22

Please sign in to comment.