Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: occasional failure of operator build #1464

Merged
merged 1 commit into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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