add powershell run helper

pull/8265/head
Medya Gh 2020-05-24 04:46:35 -07:00
parent ad437c2c9c
commit 7b81e8068f
No known key found for this signature in database
GPG Key ID: 7CF7792C6DF3245C
2 changed files with 41 additions and 7 deletions

View File

@ -170,8 +170,14 @@ func validateDockerEnv(ctx context.Context, t *testing.T, profile string) {
mctx, cancel = context.WithTimeout(ctx, Seconds(13))
defer cancel()
// do a eval $(minikube -p profile docker-env) and check if we are point to docker inside minikube
c = exec.CommandContext(mctx, "/bin/bash", "-c", "eval $("+Target()+" -p "+profile+" docker-env) && docker images")
rr, err = Run(t, c)
if runtime.GOOS == "windows" { // golang exec powershell needs some tricks !
c = exec.CommandContext(mctx, Target(), "-p "+profile+" docker-env | Invoke-Expression ; docker images")
rr, err = Run(t, c, true)
} else {
c = exec.CommandContext(mctx, "/bin/bash", "-c", "eval $("+Target()+" -p "+profile+" docker-env) && docker images")
rr, err = Run(t, c)
}
if err != nil {
t.Fatalf("failed to run minikube docker-env. args %q : %v ", rr.Command(), err)
}

View File

@ -86,16 +86,33 @@ func (rr RunResult) Output() string {
}
// Run is a test helper to log a command being executed \_(ツ)_/¯
func Run(t *testing.T, cmd *exec.Cmd) (*RunResult, error) {
func Run(t *testing.T, cmd *exec.Cmd, powershell ...bool) (*RunResult, error) {
t.Helper()
rr := &RunResult{Args: cmd.Args}
isPowershell := false
if len(powershell) > 0 {
isPowershell = powershell[0]
}
newCmd := cmd
if isPowershell {
psBin, err := exec.LookPath("powershell.exe")
if err != nil {
return &RunResult{}, err
}
args := append([]string{"-NoProfile", "-NonInteractive"}, cmd.Args...)
newCmd = exec.Command(psBin, args...)
}
rr := &RunResult{Args: newCmd.Args}
t.Logf("(dbg) Run: %v", rr.Command())
var outb, errb bytes.Buffer
cmd.Stdout, rr.Stdout = &outb, &outb
cmd.Stderr, rr.Stderr = &errb, &errb
newCmd.Stdout, rr.Stdout = &outb, &outb
newCmd.Stderr, rr.Stderr = &errb, &errb
start := time.Now()
err := cmd.Run()
err := newCmd.Run()
elapsed := time.Since(start)
if err == nil {
// Reduce log spam
@ -447,3 +464,14 @@ func killProcessFamily(t *testing.T, pid int) {
}
}
}
func RunPowershellCmd(args ...string) (string, error) {
psBin, err := exec.LookPath("powershell.exe")
if err != nil {
return "", err
}
args = append([]string{"-NoProfile", "-NonInteractive"}, args...)
cmd := exec.Command(psBin, args...)
out, err := cmd.CombinedOutput()
return string(out), err
}