Skip to content

Commit

Permalink
ci: wrong way to use exec method (OpenAtomFoundation#1464)
Browse files Browse the repository at this point in the history
  • Loading branch information
machinly authored May 9, 2023
1 parent a7e4bc2 commit 615c5c6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
40 changes: 18 additions & 22 deletions operator/test/e2e/utils/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,37 @@ package utils
import (
"io"
"os/exec"
"sync"
)

// ExecCmdWithOutput executes a command and returns the output.
// return stdoutHas,stderrHas,err
func ExecCmdWithOutput(cmd string, args ...string) (string, string, error) {
cmdSt := exec.Command(cmd, args...)

stdoutReader, err := cmdSt.StdoutPipe()
if err != nil {
panic(err)
return "", "", err
}
stderrReader, err := cmdSt.StderrPipe()
if err != nil {
panic(err)
return "", "", err
}
wg := &sync.WaitGroup{}
wg.Add(2)

var stdout, stderr []byte
go func() {
defer wg.Done()
stdout, err = io.ReadAll(stdoutReader)
if err != nil {
return
}
}()
go func() {
defer wg.Done()
stderr, err = io.ReadAll(stderrReader)
if err != nil {
return
}
}()
err = cmdSt.Start()
if err != nil {
return "", "", err
}

stdout, err := io.ReadAll(stdoutReader)
if err != nil {
return "", "", err
}

stderr, err := io.ReadAll(stderrReader)
if err != nil {
return "", "", err
}

err = cmdSt.Run()
wg.Wait()
err = cmdSt.Wait()
return string(stdout), string(stderr), err
}
23 changes: 23 additions & 0 deletions operator/test/e2e/utils/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ func TestExecCmdWithOutput(t *testing.T) {
stderrHas: "No such file or directory",
wantErr: true,
},
{
name: "test sleep 3",
args: args{
cmd: "bash",
args: []string{
"-c",
"for i in {1..3}; do echo $i; sleep 1; done",
},
},
stdoutHas: "1\n2\n3",
stderrHas: "",
wantErr: false,
},
{
name: "test unknow command",
args: args{
cmd: "no_this_command",
args: []string{},
},
stdoutHas: "",
stderrHas: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 615c5c6

Please sign in to comment.